gpt4 book ai didi

math - 查找 3d 点到线段的距离

转载 作者:行者123 更新时间:2023-12-03 18:16:56 25 4
gpt4 key购买 nike

我有一个 3d 点 P 和由 A 和 B 定义的线段(A 是线段的起点,B 是终点)。

我想计算 P 和线 AB 之间的最短距离。

计算点到无限长线的距离很容易,因为它们是 Wolfram Mathworld 上的解决方案。 ,并且我已经实现了,但是我需要为有限长度的线执行此操作。

经过多次环顾四周,我还没有设法在 3d 中找到可靠的解决方案。

我已经使用包含浮点数 x、y 和 z 的结构在 C++ 中实现了计算点积、叉积、幅度等的算法。

几乎任何语言的伪代码、链接或代码都很棒。

最佳答案

Java函数

/**
* Calculates the euclidean distance from a point to a line segment.
*
* @param v the point
* @param a start of line segment
* @param b end of line segment
* @return distance from v to line segment [a,b]
*
* @author Afonso Santos
*/
public static
double
distanceToSegment( final R3 v, final R3 a, final R3 b )
{
final R3 ab = b.sub( a ) ;
final R3 av = v.sub( a ) ;

if (av.dot(ab) <= 0.0) // Point is lagging behind start of the segment, so perpendicular distance is not viable.
return av.modulus( ) ; // Use distance to start of segment instead.

final R3 bv = v.sub( b ) ;

if (bv.dot(ab) >= 0.0) // Point is advanced past the end of the segment, so perpendicular distance is not viable.
return bv.modulus( ) ; // Use distance to end of the segment instead.

return (ab.cross( av )).modulus() / ab.modulus() ; // Perpendicular distance of point to segment.
}

整个(自包含)R3 3D 代数包的要点: https://gist.github.com/reciprocum/4e3599a9563ec83ba2a63f5a6cdd39eb

部分开源库 https://sourceforge.net/projects/geokarambola/

关于math - 查找 3d 点到线段的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4858264/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com