固定 z 轴上的简单线平面相交?
如果我知道平面始终位于同一 z 轴(因此无法旋转),并且其宽度/高度是无限的,那么有什么快速方法可以检查我的线在平面中的何处相交?另外,我的“线”实际上不是一条线,而是一个 3d 矢量,因此“线”可以延伸到无限远的距离。
下面是依赖两点的代码: (p1 和 p2 是直线的起点和终点。plane_z = 平面所在的位置)
k1 = -p2.z/(p1.z-p2.z-plane_z);
k2 = 1.0f-k1;
ix = k1*p1.x + k2*p2.x;
iy = k1*p1.y + k2*p2.y;
iz = plane_z; // where my plane lays
另一个使用向量的解决方案(我也像第一个示例一样使用两个点,“p2.x-p1.x”等是向量计算):
a = (plane_z-p1.z)/(p2.z-p1.z);
ix = p1.x + a*(p2.x-p1.x);
iy = p1.y + a*(p2.y-p1.y);
iz = plane_z;
Edit3:添加了Orbling的解决方案,该解决方案稍快一些,并且不一定依赖于两个点。
What is, and is there, a fast way to check where in the plane my line will intersect, if i know the plane is always in the same z-axis (so it cannot be rotated), and its width/height is infinite? Also, my "line" isn't actually a line, but a 3d vector, so the "line" can go to infinite distance.
Here is the code that relies on two points:
(p1 and p2 are start and end points of the line. plane_z = where the plane is)
k1 = -p2.z/(p1.z-p2.z-plane_z);
k2 = 1.0f-k1;
ix = k1*p1.x + k2*p2.x;
iy = k1*p1.y + k2*p2.y;
iz = plane_z; // where my plane lays
Another solution which works with a vector (i made it use two points as the first example did too, "p2.x-p1.x" etc. is the vector calculation):
a = (plane_z-p1.z)/(p2.z-p1.z);
ix = p1.x + a*(p2.x-p1.x);
iy = p1.y + a*(p2.y-p1.y);
iz = plane_z;
Edit3: added Orbling's solution which is slightly faster, and doesnt rely on two points necessarily.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以实现像 http://paulbourke.net/geometry/planeline/ 这样的直接解决方案,然后应用您的简化。在代数解(#2)中,A和B在你的情况下为零(如果我正确理解了这个陈述)
注意:您的线应该是一个点和一个方向,或者是两个点,对吗?
You can implement a strait-forward solution like there http://paulbourke.net/geometry/planeline/, then apply your simplifications. In the algebraic solution (#2) A and B are zeros in your case (if i understand correctly this statement)
Note: your line should be a point and a direction, or two points right?