求平面和直线的交点

2024-05-17 11:36:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图找到一个等价物,但我正在努力找到解决问题的办法。 我有两条在python中定义为2D列表的曲线。我想找到曲线1到曲线2的正交投影。 我开始这样做:

import numpy as np
import matplotlib.pyplot as plt
def unit_tangent_vector(curve):
    tangent_vectors = np.diff(curve,axis=0)
    unit_tangent_vectors = tangent_vectors / np.linalg.norm(tangent_vectors,axis=1)[:,None]
    return unit_tangent_vectors

curve_1 = np.array([[   0. ,1050.45881  ]
 [  54.4012659,1050.45924  ]
 [ 108.772202  ,1050.46115  ]
 [ 163.063568  ,1050.46476  ]
 [ 217.210682  ,1050.47023  ]
 [ 271.123361  ,1050.47781  ]
 [ 324.675052  ,1050.48779  ]
 [ 377.692745  ,1050.50048  ]
 [ 427.845685  ,1053.07373  ]
 [ 461.323846  ,1068.71938  ]
 [ 494.723661  ,1084.33569  ]])
curve_2 = np.array([[   0.,        1050.56801  ]
 [  51.8993183 ,1050.56801  ]
 [ 103.798637  ,1050.56801  ]
 [ 155.697955  ,1050.56801  ]
 [ 207.597273  ,1050.56801  ]
 [ 259.496592  ,1050.56801  ]
 [ 311.39591   ,1050.56801  ]
 [ 363.295228  ,1050.56801  ]
 [ 415.191677  ,1050.56813  ]
 [ 455.961611  ,1065.59296  ]
 [ 495.02527   ,1083.69965  ]])

curve_1_tangents = unit_tangent_vector(curve_1)
curve_1_tangents = np.vstack([curve_1_tangents,curve_1_tangents[-1]]) # Need to have the same number of values as the curve
curve_2_tangents = unit_tangent_vector(curve_2)
curve_2_tangents = np.vstack([curve_2_tangents,curve_2_tangents[-1]])

ndots = np.einsum('ij,ij->i', curve_1_tangents, curve_2_tangents)

w_vector = curve_2 - curve_1


si = -np.array([t_le.dot(w) for t_le, w in zip(curve_1, w_vector)]) / ndots

psi = w_vector + si[:,None]*curve_2_tangents+curve_1 # Intersections


ax = plt.figure().gca()

ax.plot(curve_1.T[0],curve_1.T[1],marker="o",color="blue")
ax.plot(curve_2.T[0], curve_2.T[1], color="red")
ax.scatter(psi.T[0], psi.T[1], marker="x", color="green")
n = 8
ax.quiver(curve_1[n][0], curve_1[n][1], curve_1_tangents[n][0], curve_1_tangents[n][1],color="grey")
ax.quiver(curve_2[n][0],curve_2[n][1],curve_2_tangents[n][0],curve_2_tangents[n][1])
plt.show()

然而,切线计算似乎不正确,并导致错误的结果。例如,如果我运行此代码,它将为我提供: enter image description here

我应该有青色的交叉点。 比如说,灰色切线向量是平面的法线。我想找到每个平面(蓝色曲线的所有点)和红色曲线之间的交点。我不是在寻找两条曲线之间的交点

基本上,此函数仅适用于一个平面和一条直线:

def find_intersection(plane_normal,plane_point,ray_direction,ray_point):
epsilon=1e-6
ndotu = plane_normal.dot(ray_direction) 

if abs(ndotu) < epsilon:
    print ("no intersection or line is within plane")

    w = ray_point - plane_point
    si = -plane_normal.dot(w) / ndotu
    Psi = w + si * ray_direction + plane_point #intersections

我只想把它扩展到一个包含几个点的numpy数组

谢谢你的帮助


Tags: asnpunittangentax曲线colorpoint