Results 1 to 4 of 4

Thread: Draw only a portion of Qt Curved Path

  1. #1
    Join Date
    Aug 2014
    Posts
    4
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Question Draw only a portion of Qt Curved Path

    Hello,

    I want to draw only the portion of the QPainter Curved Path. I have the following code:

    Qt Code:
    1. QPointF points[5];
    2. points[0] = QPoint(100, 200);
    3. points[1] = QPoint(200, 60);
    4. points[2] = QPoint(500, 180);
    5. points[3] = QPoint(600, 100);
    6.  
    7. QPainter painter(this);
    8. painter.scale(1,-1); painter.translate(0, -250);
    9. QPen pen;
    10. pen.setWidth(2);
    11.  
    12. painter.setPen(pen);
    13. float a = 1.0 / 6.0;
    14. cPath.cubicTo(p[1], -a*p[0] + p[1] + a*p[2], a*p[1] + p[2] -a*p[3], p[2]);
    15.  
    16. pen.setColor(Qt::darkRed);
    17. painter.strokePath(cPath, pen);
    To copy to clipboard, switch view to plain text mode 

    This draws the bezier curve just fine. But now I want to draw only to draw the portion of this whole curve, for example only the portion between points[1] and points[2].

    How can I achieve this?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Draw only a portion of Qt Curved Path

    You could try QPainterPath::toSubpathPolygons(), which might get you back the polyline. Otherwise, you could either find some bezier curve generating code online or look into the QPainterPath source code and extract the algorithm from there. Then you can create an array of the points for as much of the curve as you want.
    Last edited by d_stranz; 12th August 2015 at 18:20.

  3. The following user says thank you to d_stranz for this useful post:

    Gurjot (13th August 2015)

  4. #3
    Join Date
    Oct 2014
    Posts
    81
    Thanks
    20
    Thanked 9 Times in 9 Posts
    Qt products
    Qt5
    Platforms
    Windows
    Wiki edits
    7

    Default Re: Draw only a portion of Qt Curved Path

    Interestingly, a cubic Bézier curve has its two middle control points (1 and 2, from 0, 1, 2, 3) exactly in thirds (0.33333...) in terms of parameter value.

    So if you want to draw just the piece of the curve from section 1 to section 2, you need to break that curve apart (with math) at points t1=0.333 and t2=0.666.

    To find the new control points of this piece of curve it's (relatively) easy: you run the De Casteljau's algorithm in reverse, like the following illustrates.
    http://pomax.github.io/bezierinfo/#moulding
    The De Casteljau algorithm is simple, it uses proportional lengths of connected lines:
    http://jeremykun.com/tag/de-casteljau/

  5. The following user says thank you to Kryzon for this useful post:

    d_stranz (13th August 2015)

  6. #4
    Join Date
    Aug 2014
    Posts
    4
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Draw only a portion of Qt Curved Path

    Quote Originally Posted by d_stranz View Post
    You could try QPainterPath::toSubpathPolygons(), which might get you back the polyline.
    Thank you, your suggestion helped.

    I got the list of QPolygonF from which I got a single QPolygonF. And then I created a new QPolygonF in which I only inserted those QPointF which were needed. And then I drew that.

Similar Threads

  1. Replies: 5
    Last Post: 3rd March 2015, 05:31
  2. Draw smooth path in QGraphicsItem
    By karankumar1609 in forum Qt Programming
    Replies: 0
    Last Post: 10th July 2013, 04:35
  3. Show portion of an image
    By ucntcme in forum Qt Programming
    Replies: 1
    Last Post: 22nd May 2010, 08:10
  4. (portion of...) QByteArray to UInt
    By pdoria in forum Qt Programming
    Replies: 3
    Last Post: 25th July 2009, 15:23
  5. Displaying a portion of an image
    By tas in forum Qt Programming
    Replies: 4
    Last Post: 28th November 2008, 03:09

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.