3D中点和线段与线段上的交点之间的最短距离 [英] Python shortest distance between a point and a line segment in 3D and intersection point on the segment
本文介绍了3D中点和线段与线段上的交点之间的最短距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找3D中点和线段之间的最短距离以及线段上的交点之间的最短距离的Python实现
推荐答案
这里有一个使用numpy
:
def min_distance(r: np.ndarray, a: np.ndarray):
""" Compute the minimal distance between a point and a segment.
Given a segment of points xa and xb and a point p
Parameters
----------
r
xb - xa
a
xa - p
Returns
-------
d
The minimal distance spanning from p to the segment
"""
min_t = np.clip(-a.dot(r) / (r.dot(r)), 0, 1)
d = a + min_t * r
return np.sqrt(d.dot(d))
说明
给定由两个点
xa
和xb
标识的段和通用点p
。我们定义以下数量
线段上的通用点,具有坐标:
因此,点p
到线段上的普通点的距离为:
我们希望最小化平方距离(我们使用平方距离使计算更容易)
即
这为我们提供了最小化距离的t
值(实际上使距离保持不变)。
现在我们需要记住,我们被约束为Keep,这样我们就可以夹住t
。
计算d(t_min)
将提供最小距离。
距离将为返回的d
:np.linalg.norm(d)
或np.sqrt(d.dot(d))
的范数。
这篇关于3D中点和线段与线段上的交点之间的最短距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文