Problems with Open-Source Downloads read https://www.qt.io/blog/problem-with-open-source-downloads and https://forum.qt.io/post/638946

Faster generation of QPolygonF based on a vector of QPointF



  • I'm developing a simple handwriting program, and i ran into a performance issue when the user creates long strokes.
    The strokes are drawn using a QPolygonF, whose points gets generated dinamically while the user draws on screen. When the user draws on screen, some checks are done on the current position of the stylus/mouse, in order to decide if a new point should be added, in that case the point gets added to a QVector of QPointF, and the following method gets invoked to update the Stroke's strokePoly:

    //points[].first is the pressure of the point, while points[].second is the position of the point)
    void Stroke::updateStrokePolygon() {
        if(points.length() == 1) {
            strokePoly.append(points[0].second);
        } else {
            int i = points.count() - 1;
            QVector2D normal = QVector2D(points[i].second.y() - points[i - 1].second.y(), -(points[i].second.x() - points[i - 1].second.x())).normalized();
            QPointF top = points[i].second + (normal.toPointF() * strokeWidth * points[i].first);
            QPointF bottom = points[i].second - (normal.toPointF() * strokeWidth* points[i].first);
    
            strokePoly.append(top);
            strokePoly.prepend(bottom);
        }
    }
    

    which simply calculates the points on the edges of the stroke using the normal of the line that connects the previous point and the new one.
    Now, since QPolygonF is based on QVector, the strokePoly.prepend(bottom) instruction gets slower every time i add a new point, since i need to shift all the other points by one position before prepending the new one.
    Is there a way to generate the polygon faster?


Log in to reply