我正在寻找可以计算 3D 中的点 (x_0,y_0,z_0) 到直线的距离的 Python 函数segment由其端点 (x_1,y_1,z_1) 和 (x_2,y_2,z_2) 定义。
我只找到了这个问题的 2D 解决方案。
There are solutions to finding a distance from a point to a line in 3d, but not to a line segment, like here:
(图片取自计算特殊情况下点到线段的距离 https://stackoverflow.com/questions/46079554/calculate-distance-point-to-line-segment-with-special-cases)
这个答案改编自这里:在Python中计算点数组到线段之间的欧几里德距离,无需for循环 https://stackoverflow.com/questions/54442057/calculate-the-euclidian-distance-between-an-array-of-points-to-a-line-segment-in/54442561#54442561.
功能lineseg_dist
返回点 p 到线段 [a,b] 的距离。p
, a
and b
是 np.arrays。
import numpy as np
def lineseg_dist(p, a, b):
# normalized tangent vector
d = np.divide(b - a, np.linalg.norm(b - a))
# signed parallel distance components
s = np.dot(a - p, d)
t = np.dot(p - b, d)
# clamped parallel distance
h = np.maximum.reduce([s, t, 0])
# perpendicular distance component
c = np.cross(p - a, d)
return np.hypot(h, np.linalg.norm(c))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)