您需要解方程 (h(t)-p0).n = 0,其中 h(t) 是您的螺旋。
这个方程没有简单的解析解,但你可以用数值求解,例如 scipy:
import numpy as np
from scipy import optimize
n = np.array([nx, ny, nz])
p0 = np.array([p0x, p0y, p0z])
def h(t):
return np.array([R*np.cos(t), R*np.sin(t), a*t])
res = optimize.minimize_scalar(lambda t: np.dot(h(t) - p0, n))
print(res.x)
如果你没有scipy/numpy,在这种特定情况下实现Newton method相对容易(我们可以解析计算h(t)的导数)。纯python版本:
from math import cos, sin
n = [nx, ny, nz]
p0 = [p0x, p0y, p0z]
def dot(a, b):
return sum([x*y for x, y in zip(a, b)])
def h(t):
return [R*cos(t), R*sin(t), a*t]
def hp(t): # the derivative of h
return [-R*sin(t), R*cos(t), a]
def find_root_newton(x, f, fp, epsilon=1e-5):
xn = x + 2*epsilon
while(abs(xn - x) > epsilon):
x = xn
xn = x - f(x)/fp(x)
return xn
t = find_root_newton(0., lambda t: dot(h(t), n) - dot(p0, n),
lambda t: dot(hp(t), n))
print(h(t))
如果螺旋的轴在平面上,它可能会失败(在这种情况下,无论如何你的问题定义不好),而且效率不高。