Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

How can I do it in Python? I think there must be some easy ways.

like image 862
Sibbs Gambling Avatar asked Jul 29 '13 03:07

Sibbs Gambling


People also ask

How do you find the orthogonal projection of a vector on a plane?

The projection of onto a plane can be calculated by subtracting the component of that is orthogonal to the plane from . If you think of the plane as being horizontal, this means computing minus the vertical component of , leaving the horizontal component.

How do you find the projection of a vector in Python?

Alternatively, if you want to compute the projection of y onto x , then replace y with x in the denominator ( norm ) of the above equation. for the vector projection of x onto y . This is scalar projection. To get vector projection, you need to multiply this (Scalar) with a unit vector in direction of y.

What is the projection of orthogonal vectors?

The projection of a vector on a plane is its orthogonal projection on that plane. The rejection of a vector from a plane is its orthogonal projection on a straight line which is orthogonal to that plane. Both are vectors. The first is parallel to the plane, the second is orthogonal.


1 Answers

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

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))]

Then you can say:

p = project_onto_plane([3, 4, 5], [1, 2, 3])
like image 182
jason Avatar answered Oct 21 '22 06:10

jason