如果我有三个点 P1、P2、P3 及其坐标(x,y)
P1(x,y)和P3(x,y)是线(起点,终点)的坐标,P3是需要投影的点。
how can i find the coordinate of point r(x,y) which is projection of P3 over P1 and P2
该解决方案扩展到具有任何几何尺寸(2D、3D、4D...)的点。它假设所有点都是一维 numpy 数组(或一维形状为 1 的二维数组)。我不确定您是否需要投影落在线段上或线段的延伸上,所以我将两者都包括在内。您可以选择最适合您的问题的一个:
#distance between p1 and p2
l2 = np.sum((p1-p2)**2)
if l2 == 0:
print('p1 and p2 are the same points')
#The line extending the segment is parameterized as p1 + t (p2 - p1).
#The projection falls where t = [(p3-p1) . (p2-p1)] / |p2-p1|^2
#if you need the point to project on line extention connecting p1 and p2
t = np.sum((p3 - p1) * (p2 - p1)) / l2
#if you need to ignore if p3 does not project onto line segment
if t > 1 or t < 0:
print('p3 does not project onto p1-p2 line segment')
#if you need the point to project on line segment between p1 and p2 or closest point of the line segment
t = max(0, min(1, np.sum((p3 - p1) * (p2 - p1)) / l2))
projection = p1 + t * (p2 - p1)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)