我有一个平面 ,plane A由它的正交向量定义,比如说(a, b, c)。
(即向量(a, b, c)与 正交plane A)
我希望将向量(d, e, f)投影到plane A.
我怎样才能在 Python 中做到这一点?我认为必须有一些简单的方法。
我有一个平面 ,plane A由它的正交向量定义,比如说(a, b, c)。
(即向量(a, b, c)与 正交plane A)
我希望将向量(d, e, f)投影到plane A.
我怎样才能在 Python 中做到这一点?我认为必须有一些简单的方法。
取出(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). 因此,在 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])