将 (d, e, f)
减去其在垂直于平面的标准化法向量(在您的情况下为 (a, b, c)
)上的投影。因此:
v = (d, e, f)
- sum((d, e, f) *. (a, b, c)) * (a, b, c) / sum((a, b, c) *. (a, b, c))
在这里,我所说的*.
是指逐个元素相乘。因此,这意味着:
sum([x * y for x, y in zip([d, e, f], [a, b, c])])
或者
d * a + e * b + f * c
如果你只是想表达清晰但过于学究,可以这样做:
对于(a, b, c) + (a, b, c)
和(a, b, c) *. (a, b, c)
也同理。因此,在Python中:
from math import sqrt
def dot_product(x, y):
return sum([x[i] * y[i] for i in range(len(x))])
def norm(x):
return sqrt(dot_product(x, x))
def normalize(x):
return [x[i] / norm(x) for i in range(len(x))]
def project_onto_plane(x, n):
d = dot_product(x, n) / norm(n)
p = [d * normalize(n)[i] for i in range(len(n))]
return [x[i] - p[i] for i in range(len(x))]
那么你可以说:
p = project_onto_plane([3, 4, 5], [1, 2, 3])