Results 1 to 11 of 11

Thread: Why isn't QPainterPath::intersects giving me the expected result?

  1. #1
    Join Date
    Nov 2010
    Posts
    77
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Why isn't QPainterPath::intersects giving me the expected result?

    I made the following test code for the QPainterPath::intersects test function. I wanted a way to detect if a point is in the vicinity (let's say 2 pixels) of a line stored in a QPainterPath.

    Qt Code:
    1. path.moveTo(0, 0);
    2. path.lineTo(0, -20);
    3. path.lineTo(40, -20);
    4. path.lineTo(40, -95);
    5. int testX = 4;
    6. int testY = -17;
    7. QRectF rect(testX - 2, testY - 2, 4, 4);
    8. bool intersects = path.intersects(rect);
    9. qDebug() << "Intersects?" << ((intersects) ? "true" : "false");
    10. qDebug() << "Rect: x =" << rect.x() << "| y =" << rect.y() << "| w =" << rect.width() << "| h =" << rect.height();
    To copy to clipboard, switch view to plain text mode 

    This produces the following output:
    Intersects? true
    Rect: x = 2 | y = -19 | w = 4 | h = 4
    I'd expect the intersects function to return false in this case because no point in the rectangle overlaps with the drawn line. Am I using the function incorrectly?
    Last edited by blooglet; 12th May 2011 at 13:27. Reason: spelling corrections

  2. #2
    Join Date
    Jun 2010
    Posts
    26
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Why isn't QPainterPath::intersects giving me the expected result?

    I think that your rectangle is top left -> bottom right = (2,-19) -> (6, -23) and therefore intersects at (2,-20) and (6,-20). I think you are assuming that from the start point of the rectangle it is drawn in a positive direction but Qt is drawing from a top down point of view.

  3. #3
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Why isn't QPainterPath::intersects giving me the expected result?

    But you point is inside! Take look on a shape! last sector intersects with second sector of path it looks like you mistyped a shape data.
    Last edited by MarekR22; 12th May 2011 at 14:21.

  4. #4
    Join Date
    Nov 2010
    Posts
    77
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Why isn't QPainterPath::intersects giving me the expected result?

    Quote Originally Posted by koan View Post
    I think you are assuming that from the start point of the rectangle it is drawn in a positive direction but Qt is drawing from a top down point of view.
    Wait... so a QRectF with x = 2, y = -19, w = 4 and h = 4 doesn't stretch to (6, -15) but rather (6, -23)? So the height is counted towards lower Y values? I'm confused.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Why isn't QPainterPath::intersects giving me the expected result?

    Why guess? Draw both the path and the rect using QPainter and you'll see what gets drawn where.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Nov 2010
    Posts
    77
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Why isn't QPainterPath::intersects giving me the expected result?

    Here's the reference drawing I obtained.



    Qt Code:
    1. path.moveTo(0, 0);
    2. path.lineTo(0, -20);
    3. path.lineTo(40, -20);
    4. path.lineTo(40, -95);
    5. int testX = 4;
    6. int testY = -17;
    7. QRectF rect(testX - 2, testY - 2, 4, 4);
    8. bool intersects = path.intersects(rect);
    9. qDebug() << "Intersects?" << ((intersects) ? "true" : "false");
    10. qDebug() << "Rect: x =" << rect.x() << "| y =" << rect.y() << "| w =" << rect.width() << "| h =" << rect.height();
    11.  
    12. scene->addPath(path);
    13. scene->addRect(rect);
    To copy to clipboard, switch view to plain text mode 

    Intersects? true
    Rect: x = 2 | y = -19 | w = 4 | h = 4
    They don't intersect in the drawing, do they?

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Why isn't QPainterPath::intersects giving me the expected result?

    "It depends". If the path is treated as a closed path then they intersect. Try moving the rectangle to Y>0 (e.g. x=2, y=1, w=4, h=4) and see if it is still considered intersecting with the path.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Nov 2010
    Posts
    77
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Why isn't QPainterPath::intersects giving me the expected result?

    It appears that the rectangle is considered "not intersecting" when it is outside the bounding rectangle of the painter path. This is not the behavior I want. I want to check if the rectangle overlaps with the line. Am I using the wrong method?

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Why isn't QPainterPath::intersects giving me the expected result?

    The docs say:
    Set operations on paths will treat the paths as areas. Non-closed paths will be treated as implicitly closed.
    I don't think there is a method in QPainterPath that does directly what you want.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Nov 2010
    Posts
    77
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Why isn't QPainterPath::intersects giving me the expected result?

    Does Qt provide a way to, given a path consisting of lines that isn't necessarily closed and a mouse coordinate, calculate if I clicked on the path? I then want to extend this concept to "in the vicinity of the path" by using a QRectF instead of a QPointF.

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Why isn't QPainterPath::intersects giving me the expected result?

    This is a mathematical problem that can easily be solved without using any dedicated frameworks. Each line conforms to the ax+by+c = 0 equation. Using coordinates of ends of the lines you can calculate a, b and c and then it is easy to compute the solution by substituting x and y with coordinates of the point you wish to check for intersection. It is similar when talking about intersection of lines. And of course there is QLineF::intersect() to help you.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. QPainterPath::quadTo(...) calls QPainterPath::cubicTo(...) ?
    By brucelamond in forum Qt Programming
    Replies: 0
    Last Post: 28th April 2011, 23:30
  2. QRegExp - get only last result
    By kabanek in forum Newbie
    Replies: 2
    Last Post: 3rd November 2010, 22:17
  3. How to display result
    By sksingh73 in forum Newbie
    Replies: 1
    Last Post: 7th June 2010, 08:39
  4. QSqlQuery return result
    By arpspatel in forum Qt Programming
    Replies: 2
    Last Post: 9th April 2010, 07:55
  5. Error in QRectF::intersects
    By lni in forum Qt Programming
    Replies: 3
    Last Post: 11th February 2009, 17:24

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.