Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Faster generation of QPolygonF based on a vector of QPointF
Forum Update on Monday, May 27th 2025

Faster generation of QPolygonF based on a vector of QPointF

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 310 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • CraxC Offline
    CraxC Offline
    Crax
    wrote on last edited by Crax
    #1

    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?

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved