Solved QLineF::intersectType
-
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 on all four borders unless the line is parallel with the border I’m checking it against.
This diagram illustrates my point.
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? -
That can be easily checked when solving the system to determine the intersection. Here's a fully functional code [1] you can adapt to your case, but yes, you need to do it on your side.
[1]: https://codereview.qt-project.org/c/qt/qtbase/+/261770/1/src/corelib/tools/qline.cpp
-
@Corny said in QLineF::intersectType:
Is there a way to check for an intersection between two lines only in the direction they are created, from p1 to p2?
Yes, but you have to write it on your own. But since you get the interesectionPoint from QLineF::intersect() it shouldn't be too hard to determine the direction of the line to this point by yourself.
-
That can be easily checked when solving the system to determine the intersection. Here's a fully functional code [1] you can adapt to your case, but yes, you need to do it on your side.
[1]: https://codereview.qt-project.org/c/qt/qtbase/+/261770/1/src/corelib/tools/qline.cpp
-
@Christian-Ehrlicher
I believe I'll still get two unbounded intersections. But your right, it can be figured out from there. However, at this point though, the only solution I can come up with seems clumsy and unwieldy. I was kind of hopping someone might have a more elegant solution than the one I've awkwardly flung together.
Thank you for your reply. -
@kshegunov
Thanks for your response. I'll be going through the code you provided.