[英]How to check if 3d float point lies on a line within error?
这条线不是线段,但应该延伸到无穷大。 我尝试使用这个家伙的答案,但我只是得到了nan
,因为我除以 0。最好我想要使用点积和叉积等快速简单的东西。
x = np.array([-1,-1,-1])
y = np.array([4,4,4])
a = np.array([2,2,2])
epsilon = 0.0001
#algorithm
print((x[2] - x[0]) / (x[1] - x[0]))
print((y[2] - y[0]) / (y[1] - y[0]))
print((a[2] - a[0]) / (a[1] - a[0]))
输出:
nan
nan
nan
这是一种可能性,根据应用程序可以简化代码,但我更愿意给出一个具有清晰解释而不是清晰代码的解决方案。
def colinear(p1, p2, p3, rtol=1e-5):
'''
Test colinearity making sure that sin(angle) < rtol
'''
# twice the area of the triangle (p1, p2, p3)
A = np.linalg.norm(np.cross(p2 - p1, p3 - p1))
# length of two sides meeting the vertice p1
a = np.linalg.norm(p2 - p1)
b = np.linalg.norm(p3 - p1)
c = np.linalg.norm(p2 - p3)
# pick the two largest sides
_, a, b = sorted([a,b,c])
# the angle at the vertice where those points meet
# will be A / (a * b)
return A < rtol * a * b
x = np.array([-1,-1,-1])
y = np.array([4,4,4])
a = np.array([2,2,2])
assert colinear(x, y, a)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.