How May I Project Vectors Onto A Plane Defined By Its Orthogonal Vector In Python?
I have a plane, plane A, defined by its orthogonal vector, say (a, b, c). (i.e. the vector (a, b, c) is orthogonal to plane A) I wish to project a vector (d, e, f) onto plane A. Ho
Solution 1:
Take (d, e, f)
and subtract off the projection of it onto the normalized normal to the plane (in your case (a, b, c)
). So:
v =(d, e, f)-sum((d, e, f)*. (a, b,c))*(a, b,c)/sum((a, b,c)*. (a, b,c))
Here, by *.
I mean the component-wise product. So this would mean:
sum([x * y for x, y in zip([d, e, f], [a, b, c])])
or
d * a + e * b + f * c
if you just want to be clear but pedantic
and similarly for (a, b, c) *. (a, b, c)
. Thus, in Python:
from math import sqrt
defdot_product(x, y):
returnsum([x[i] * y[i] for i inrange(len(x))])
defnorm(x):
return sqrt(dot_product(x, x))
defnormalize(x):
return [x[i] / norm(x) for i inrange(len(x))]
defproject_onto_plane(x, n):
d = dot_product(x, n) / norm(n)
p = [d * normalize(n)[i] for i inrange(len(n))]
return [x[i] - p[i] for i inrange(len(x))]
Then you can say:
p = project_onto_plane([3, 4, 5], [1, 2, 3])
Post a Comment for "How May I Project Vectors Onto A Plane Defined By Its Orthogonal Vector In Python?"