Results 1 to 6 of 6

Thread: find the lines cross while draw free path using QPainterPath

  1. #1
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default find the lines cross while draw free path using QPainterPath

    Hi friends,
    im implementing the freeform style drawing for drawing a free path using the mouse press and move with Qpainter QPainterpath

    now i have to detect when exactly the drawn path crossed or intersects with the other. how i can identify when the lines crossed each other at some point and give warning to the user.

    is there a Qt API to tell that if a path composed of different elements intersect in a point or not.?
    because QPainterPath::​intersects function can be used for two different painterpath . how to check for the current painterpath

    IntersectionPainterpath.png
    "Behind every great fortune lies a crime" - Balzac

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: find the lines cross while draw free path using QPainterPath

    A workaround: Can you capture the current mouse path to a temp path, check if it intersects the master path it yes inform user, else just append the current mouse path to master path.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: find the lines cross while draw free path using QPainterPath

    hi Santosh,

    This is what i tried earlier

    inside mouseMoveEvent

    Qt Code:
    1. for(int i =1; i < theResvPath.elementCount(); i++)
    2. {
    3. QPointF repPath = theResvPath.elementAt(i);
    4. QRectF sampRect = QRectF(e->pos().x() , e->pos().y() ,1, 1 );
    5.  
    6.  
    7. bool intersects = theResvPath.intersects(sampRect);
    8. qDebug()<<"The painter path:"<<repPath<<"and the rect:"<<sampRect<<"and the intersects:"<<intersects;
    9.  
    10.  
    11. if(intersects){
    12. }
    13.  
    14. theResvPath.lineTo(e->pos());
    15.  
    16. }
    To copy to clipboard, switch view to plain text mode 

    but the intersects return true even one x,y is inside the path

    The painter path: QPointF(982.268, 337.732) and the rect: QRectF(917.938,609.897 1x1) and the intersects: true .
    "Behind every great fortune lies a crime" - Balzac

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: find the lines cross while draw free path using QPainterPath

    Try playing with code
    Qt Code:
    1. #include <QtWidgets>
    2.  
    3. class MousePaintWidget : public QWidget
    4. {
    5. public:
    6. explicit MousePaintWidget(QWidget * parent = 0)
    7. : QWidget(parent)
    8. , mousePressed(false)
    9. {
    10. ;
    11. }
    12.  
    13. protected:
    14. void paintEvent(QPaintEvent *)
    15. {
    16. QPainter p(this);
    17. p.drawPath(masterPath);
    18.  
    19. if(mousePressed)
    20. {
    21. p.setPen(Qt::red);
    22. p.drawLine(firstPoint, lastPoint);
    23. }
    24. }
    25.  
    26. void mouseMoveEvent(QMouseEvent * mouseEvent)
    27. {
    28. if(mousePressed)
    29. {
    30. QPainterPath newPath;
    31.  
    32. newPath.moveTo(lastPoint);
    33. newPath.lineTo(mouseEvent->pos());
    34.  
    35. if(masterPath.elementCount() == 0)
    36. {
    37. masterPath.moveTo(lastPoint);
    38. }
    39. else
    40. {
    41. if(newPath.intersects(masterPath))
    42. {
    43. mousePressed = false;
    44. }
    45. else
    46. {
    47. masterPath.lineTo(lastPoint);
    48. }
    49. }
    50.  
    51. lastPoint = mouseEvent->pos();
    52.  
    53. update();
    54. }
    55. }
    56.  
    57. void mousePressEvent(QMouseEvent * mouseEvent)
    58. {
    59. firstPoint = lastPoint = mouseEvent->pos();
    60. masterPath = QPainterPath();
    61. mousePressed = true;
    62. }
    63.  
    64. void mouseReleaseEvent(QMouseEvent *)
    65. {
    66. mousePressed = false;
    67. }
    68.  
    69. private:
    70. bool mousePressed;
    71. QPainterPath masterPath;
    72. QPoint lastPoint;
    73. QPoint firstPoint;
    74. };
    75.  
    76. int main(int argc, char *argv[])
    77. {
    78. QApplication app(argc, argv);
    79.  
    80. MousePaintWidget w;
    81. w.show();
    82.  
    83. return app.exec();
    84. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

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

    wagmare (23rd February 2015)

  6. #5
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: find the lines cross while draw free path using QPainterPath

    Hi ,
    I tried your logic with the QpainterPath intersect and it is coming fine in the required but
    QPainterPath::intersects() return true even if the endpoint is inside the unclosed area we have drawn .

    i know im not clear with my answer so i added the image with this reply .

    you can see the blue path is been drawn when the painterpath intersects .

    Qt Code:
    1. void paintEvent(QPaintEvent *)
    2. {
    3. QPainter p(this);
    4. p.drawPath(masterPath);
    5.  
    6. if(mousePressed)
    7. {
    8. p.setPen(Qt::red);
    9. p.drawLine(firstPoint, lastPoint);
    10. }
    11.  
    12. p.setPen(QPen(Qt::blue,4));
    13. p.drawPath(newerPath);
    14. }
    15.  
    16. void mouseMoveEvent(QMouseEvent * mouseEvent)
    17. {
    18. if(mousePressed)
    19. {
    20. QPainterPath newPath;
    21.  
    22. newPath.moveTo(lastPoint);
    23. newPath.lineTo(mouseEvent->pos());
    24.  
    25. QList<QPolygonF> polyList = masterPath.toSubpathPolygons();
    26.  
    27.  
    28. if(masterPath.elementCount() == 0)
    29. {
    30. masterPath.moveTo(lastPoint);
    31. }
    32. else
    33. {
    34.  
    35.  
    36. if(newPath.intersects(masterPath))
    37. {
    38.  
    39. newerPath.moveTo(lastPoint);
    40. newerPath.lineTo(mouseEvent->pos());
    41.  
    42.  
    43.  
    44.  
    45. // mousePressed = false;
    46.  
    47. masterPath.lineTo(lastPoint);
    48. }
    49. else
    50. {
    51. masterPath.lineTo(lastPoint);
    52. }
    53. }
    54.  
    55. lastPoint = mouseEvent->pos();
    56.  
    57. update();
    58. }
    59. }
    To copy to clipboard, switch view to plain text mode 

    i have attached both the normal and the time when i move across the start point of the path

    NormalCase.jpg
    MovingTowardsStartPoint.jpg


    event the doc says
    Set operations on paths will treat the paths as areas. Non-closed paths will be treated as implicitly closed.
    please help me

    thanks in advance
    Last edited by wagmare; 24th February 2015 at 08:51.
    "Behind every great fortune lies a crime" - Balzac

  7. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: find the lines cross while draw free path using QPainterPath

    I think that is the way QPainterPath works....

    Well you could try writing an intersection detection function for yourself, it is not trivial yet but not complex either, it involves a little geometry.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

Similar Threads

  1. free hand draw in qgraphicsview
    By wagmare in forum Newbie
    Replies: 0
    Last Post: 22nd November 2013, 07:02
  2. How to find the nearest point on a QPainterPath
    By Markus in forum Qt Programming
    Replies: 1
    Last Post: 9th June 2010, 00:18
  3. Edyuk : fully-featured, highly flexible and free cross-platform IDE
    By fullmetalcoder in forum Qt-based Software
    Replies: 169
    Last Post: 20th July 2009, 12:42
  4. How draw a rotated text in a QPainterPath?
    By iw2nhl in forum Qt Programming
    Replies: 6
    Last Post: 17th August 2007, 19:55
  5. how to find the free space on a drive
    By klaus1111 in forum Qt Programming
    Replies: 2
    Last Post: 14th July 2006, 11:25

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.