I have two points (a line segment) and a rectangle. I would like to know how to calculate if the line segment intersects the rectangle.
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
I hate browsing the MSDN docs (they're awfully slow and weird :-s) but I think they should have something similar to this Java method... and if they haven't, bad for them! XD (btw, it works for segments, not lines).
In any case, you can peek the open source Java SDK to see how is it implemented, maybe you'll learn some new trick (I'm always surprised when I look other people's code)
From my "Geometry" class:
If it is 2d, then all lines are on the only plane.
So, this is basic 3-D geometry. You should be able to do this with a straightforward equation.
Check out this page:
The second solution should be easy to implement, as long as you translate the coordinates of your rectangle into the equation of a plane.
Furthermore, check that your denominator isn't zero (line doesn't intersect or is contained in the plane).
Do http://mathworld.wolfram.com/Line-LineIntersection.html for the line and each side of the rectangle.
Or: http://mathworld.wolfram.com/Line-PlaneIntersection.html
Isn't it possible to check the line against each side of the rectangle using simple line segment formula.
since it is missing i'll just add it for completeness
note that the method rotates the line segments so as to avoid direction-related problems