确定点是否在线段上

32

我有由这两个点定义的线段:A(x1,y1,z1)B(x2,y2,z2)。我还有一个点p(x,y,z)。如何检查该点是否在线段上?


2
因为我需要C#的任何示例代码。 - AMH
1
是的,对我来说听起来很明显 :) - Leggy7
我尝试回复MetaMapper的帖子,但是我没有50的声望。 MetaMapper的解决方案是错误的。我个人花了很多时间调试,我不希望其他人也经历同样的事情。Andy的解决方案是正确的,只需要将其转换为C#即可。我希望这能为某人节省一些时间。 - Ruell Black
上面的问题只处理了2D情况。 - user202729
12个回答

0

向量 (B - A) × (p - A) 的叉积应该比向量 B - A 短得多。理想情况下,叉积为零,但在有限精度的浮点数硬件上不太可能实现。


-1

您可以检查点是否位于由point1和point2以及线方向定义的两个平面之间:

///  Returns the closest point from @a point to this line on this line.
vector3 <Type>
line3d <Type>::closest_point (const vector3 <Type> & point) const
{
    return this -> point () + direction () * dot (point - this -> point (), direction ());
}

///  Returns true if @a point lies between point1 and point2.
template <class Type>
bool
line_segment3 <Type>::is_between (const vector3 <Type> & point) const
{
    const auto closest = line () .closest_point (point);
    return abs ((closest - point0 ()) + (closest - point1 ())) <= abs (point0 () - point1 ());
}

这根本不是C#代码,所以对于这个问题没有用 - 对于相同问题的C/C++版本可能很好...而且解释对于普通人来说并不友好。 - Alexei Levenkov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接