I have a QGraphicsScene with a QGraphicsObject that is animated and is traveling in a specific direction. Since I want it to remain within the scene, I’m attempting to continuously check the distance to each of the borders to determine which border the path intersects and where the path will intersect it. The intention of course, is to change the direction of travel prior to intersecting the border. To accomplish this, the current path is used to define a QLineF using the current position as p1, and a point several pixels along the path of travel as p2. Then the intersect function of that line is used to check for an UnboundedIntersection with each of the four borders.

The documentation seems to imply that the direction of the line, or which end is point 1 or point 2, is irrelevant. I take that to mean that no matter the location of the line, or which direction the line is pointing, if I check for an unbounded intersection against every border, it will always return a BoundedIntersection or an UnboundedIntersection intersect type unless the line is parallel with the border I’m checking it against. I had a diagram illustrating my point but couldn't upload it. So this is the code I'm attempting to use for checking for a border...

Qt Code:
  1. QPointF Target::checkEdge(QPointF crntPos)
  2. {
  3. QPointF intrsctPoint; // New point for intersection
  5. const QLineF nEdge(0, 0, 1000, 0); // Initialize N edge boundry
  6. const QLineF sEdge(0, 1000, 1000, 1000); // Initialize S edge boundry
  7. const QLineF eEdge(1000, 0, 1000, 1000); // Initialize E edge boundry
  8. const QLineF wEdge(0, 0, 0, 1000); // Initialize W edge boundry
  10. QLineF track = QLineF::fromPolar(1, heading); // Track w/angle
  11. track.translate(crntPos); // Start line at current pos
  12. if(track.intersect(eEdge, &intrsctPoint) == QLineF::UnboundedIntersection)
  13. track.setP2(intrsctPoint);
  14. if(track.intersect(nEdge, &intrsctPoint) == QLineF::UnboundedIntersection)
  15. track.setP2(intrsctPoint);
  16. if(track.intersect(wEdge, &intrsctPoint) == QLineF::UnboundedIntersection)
  17. track.setP2(intrsctPoint);
  18. if(track.intersect(sEdge, &intrsctPoint) == QLineF::UnboundedIntersection)
  19. track.setP2(intrsctPoint);
  21. return intrsctPoint;
  22. }
To copy to clipboard, switch view to plain text mode 

Assuming that my interpretation of the documentation is correct, it there something that I have missed? Is there a way to check for an intersection between two lines only in the direction they are created, from p1 to p2?

Thank you in advance for your response