Search code examples
algorithmqtmathpolygonpolyline

How to get vertexes of polyline from a list of its points?


I have a list of points (pixels) QList<QPoint> that represent a curve looking like a orthogonal polyline.

enter image description here

My task is to split this one to small straight lines (an instance of QList<QLineF>). In order to know the end of a previous subline and beginning of a next subline, I got to know which points are vertexes of polyline, that is, the points where sublines are intersected.

What would be the best way to figure out that some point is a vertex?


Solution

  • I've found the answer by myself. It doesn't seem to be convenient and clear, nevertheless it works out.

    QList<QPoint> vertexes;
    
    for (int i = 2; i < points.size(); i++)
    {
        bool xChanged = points[i-2].x() != points[i].x();
        bool yChanged = points[i-2].y() != points[i].y();
    
        if (xChanged && yChanged)
            vertexes.append(points[i-1]);
    }
    
    vertexes.prepend(points.first());
    vertexes.append(points.last());
    

    We check two points every loop iteration - the current point and the point two points ago. If their X and Y aren't equal, it means that curve change its direction and the point between them is a vertex.