C#中如何判断直线是否与平面相交?
我有两个点(一条线段)和一个矩形。 我想知道如何计算线段是否与矩形相交。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
使用类:
方法:
Use class:
Method:
如果是 2d,则所有线都在唯一的平面上。
所以,这是基本的 3-D 几何。 您应该能够通过一个简单的方程式来完成此操作。
查看此页面:
第二种解决方案应该很容易实现,只要将矩形的坐标转换为平面方程即可。
此外,检查分母是否不为零(直线不相交或包含在平面内)。
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).
我讨厌浏览 MSDN 文档(它们非常慢且奇怪:-s),但我认为它们应该有类似于 这个 Java 方法...如果他们没有,对他们不利! XD(顺便说一句,它适用于线段,不适用于线)。
无论如何,你可以看看开源的Java SDK,看看它是如何实现的,也许你会学到一些新的技巧(当我看别人的代码时,我总是感到惊讶)
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)
是否可以使用简单的线段公式检查矩形每条边的线。
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
执行http://mathworld.wolfram.com/Line-LineIntersection.html直线和矩形的每条边。
或者:http://mathworld.wolfram.com/Line-PlaneIntersection.html
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
来自我的“几何”课:
From my "Geometry" class: