PDA

View Full Version : Understanding QLineF::intersectType



Corny
20th September 2019, 01:57
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...



QPointF Target::checkEdge(QPointF crntPos)
{
QPointF intrsctPoint; // New point for intersection

const QLineF nEdge(0, 0, 1000, 0); // Initialize N edge boundry
const QLineF sEdge(0, 1000, 1000, 1000); // Initialize S edge boundry
const QLineF eEdge(1000, 0, 1000, 1000); // Initialize E edge boundry
const QLineF wEdge(0, 0, 0, 1000); // Initialize W edge boundry

QLineF track = QLineF::fromPolar(1, heading); // Track w/angle
track.translate(crntPos); // Start line at current pos
if(track.intersect(eEdge, &intrsctPoint) == QLineF::UnboundedIntersection)
track.setP2(intrsctPoint);
if(track.intersect(nEdge, &intrsctPoint) == QLineF::UnboundedIntersection)
track.setP2(intrsctPoint);
if(track.intersect(wEdge, &intrsctPoint) == QLineF::UnboundedIntersection)
track.setP2(intrsctPoint);
if(track.intersect(sEdge, &intrsctPoint) == QLineF::UnboundedIntersection)
track.setP2(intrsctPoint);

return intrsctPoint;
}


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

d_stranz
20th September 2019, 16:27
This seems like an overly complicated solution. Can't you simply use the boundingRect or sceneBoundingRect of your graphics object and look for the proximity of each edge to the scene bounds?