Reputation: 3896
I want to check if a line (or any point of a line) is within a rectangle or intersects a rectangle.
I have (x0, y0) and (x1, y1) as starting and ending points of a line. Also, (ax,ay) and (bx,by) as the top-left and bottom-right points of a rectangle
For example,
____________
| |
---|----- | Result: true
| |
|____________|
/
_/__________
|/ |
/ | Result: true
/| |
|____________|
____________
| |
| -------- | Result: true
| |
|____________| ---------- Result: false
Can anyone suggest how to do this? I dont want to know which point is that, i just want to know if its there or not.
Thanks a lot for help
Upvotes: 10
Views: 2918
Reputation: 16780
The first and third cases are trivial - simply return true if either endpoint of the line is within the box (i.e. > ax and ay, < bx and by).
The second presents a problem - we can't rely on the endpoints of our line anymore. In this case, we will have to test the line with each edge of the rectangle.
The equation for our line will be (x1 - x0)*x + (y1 - y0)*y + x0*y0 - x1*y1 = 0
, and we can construct a similar equation for each side of the rectangle using the corners. Following that, substituting the equation for the sides of the rectangle into our line will give us the intersection.
Finally, we check to ensure that the point is within the bounds of the side of the rectangle, and likewise within the line segment we are considering.
There is a more detailed account of this in this discussion.
Upvotes: 5