# Orthogonal Projection of vector onto plane

I'm currently trying to learn Mathematica, and I've got some linear algebra tasks to solve with it. I've gotten quite far but now I'm stuck on this one exercise. The instructions are:

With the help of Mathematica-commands, draw a new picture, where you can see the orthogonal projection of the vector onto the plane. It should look something like this: Now, I started out by drawing the vector in the 3D plane with this code:

Graphics3D [ { Thick , Arrow [ { { 0 , 0 , 0} , { 1 , −1 , 2 } } ] ,
InfinitePlane [ { { 1 , 0 , 0} , { 1 , 1 , 1} , { 0 , 0 , 1 } } ] } ,
Axes -> True , AxesLabel -> { "X" , "Y" , "Z" } ]


This gave me the 3D image in the picture above, without the projection (the dashed line) obviously. But now I'm stuck, and my question is, how would I get the orthogonal projection of the vector?

• Maybe it will help you to know the normal of the plane? You can obtain it by plane = InfinitePlane[{{1, 0, 0}, {1, 1, 1}, {0, 0, 1}}]; Normalize[ Cross[plane[[1, 2]] - plane[[1, 1]], plane[[1, 3]] - plane[[1, 1]]]]. Jan 1 '19 at 16:17
• Sadly enough, that doesn't work for me. The result comes back saying "Part specification plane is longer than depth of the object."
– wznd
Jan 1 '19 at 17:03
• Hm. Weird. Did you really execute all code I posted? Jan 1 '19 at 17:07
• Ah, nevermind. I got it to work now. This gave me three normals, which all had the value of 1/Sqrt. How do I proceed from here?
– wznd
Jan 1 '19 at 17:15
• No, it gave you one normal vector. Let's call it v. The projector onto the place described by the plane is IdentityMatrix - KroneckerProduct[v,v]. It is now only a matter of a suitable translation... Jan 1 '19 at 17:22

Try this version

p0 = {0, 0, 0};
p1 = {1, -1, 2};
p2 = {1, 0, 0};
p3 = {1, 1, 1};
p4 = {0, 0, 1};
gr1 = Graphics3D[{Thick, Arrow[{p0, p1}],InfinitePlane[{p2,p3,p4}]},Axes -> True, AxesLabel-> {"X", "Y","Z"}];
v = p1 - p0;
n1 = p2 - p3;
n2 = p3 - p4;
n = Cross[n1, n2];
pl = p0;
pp = p2;
equs = Thread[pl + lambda v == pp + mu n1 + nu n2];
sol = Solve[equs, {lambda, mu, nu}][];
pb = p0 + lambda v /. sol;
vern = n/Norm[n];
prjn = (v.vern) vern;
prjP = v - prjn;
gr2 = Graphics3D[{Thick, Dashed, Red, Arrow[{pb, prjP + pb}]}];
gr3 = Graphics3D[{Thick, Green, Arrow[{pb, prjn}]}];
Show[gr1, gr2, gr3] In green the component along the normal to the plane and in dashed red the projection onto the plane.

NOTE

The line segment $$\mu p_0+(1-\mu)p_1$$ for $$0 \le \mu \le 1$$ is supported by the line $$L\to p_l + \lambda (p_1-p_0) = p_l + \lambda \vec v$$. The plane containing the three points $$p_2,p_3,p_4$$ can be defined as $$\Pi\to p_p+\mu(p_2-p_3)+\nu(p_3-p_4) = p_p + \mu\vec n_1+\nu\vec n_2$$ The intersection point $$p_b = L\cap\Pi$$ is obtained by solving for $$(\lambda^*,\mu^*,\nu^*)$$ the linear system $$p_l + \lambda \vec v = p_p + \mu\vec n_1+\nu\vec n_2$$ and then $$p_b = p_l+\lambda^* \vec v$$ The plane normal is obtained as $$\vec n = \vec n_1\times\vec n_2$$ and the $$\vec v$$ component regarding $$\vec n$$ is obtained as $$\vec v_{\vec n} = \left(\vec v\cdot\frac{\vec n}{|\vec n|}\right)\frac{\vec n}{|\vec n|}$$ and finally $$\vec v_{\Pi} = \vec v-\vec v_{\vec n}$$

• Thank you for the answer! Let's see if I get the steps right. First you get the normal to the plane by taking the cross-product between n1 and n2. But here's where I kind of get lost. What does the following functions mean? Do you think you could explain the next couple of lines for me? I could just post this, but I want to understand aswell. Thank you very much.
– wznd
Jan 2 '19 at 14:58
• @wznd Attached an explanation. Jan 2 '19 at 15:54
• Thank you for that. Now all I'm wondering is three lines of code, that I can't seem to understand, even with your great explanation. I'll post a picture with the lines of code that I don't understand. I don't know if it's to much to ask, but if it would be possible for you to explain the functions in words, or a bit simpler to a math novice like me, I'd greatly appreciate it. Here's the section that I don't really understand: gyazo.com/707fd1863efd810e6ea58f295651d5ef
– wznd
Jan 2 '19 at 16:03
• @wznd Removing the semicolon at the end of the command facilitates the understanding. Jan 2 '19 at 16:57
• Yeah, I did that initially and it help somewhat. But I'm still unsure about what the equs, solve and prjn variables specifically mean/do.
– wznd
Jan 2 '19 at 17:20

The code in Cesareo's answer can be shortened slightly.

Using the same set of initial points as in the other answer:

p0 = {0, 0, 0}; p1 = {1, -1, 2}; p2 = {1, 0, 0}; p3 = {1, 1, 1}; p4 = {0, 0, 1};


Some intermediate vectors:

d = p1 - p0;
nrm = Cross[p2 - p3, p3 - p4];


Use RegionIntersection[] to find the point of intersection:

pin = First[RegionIntersection[InfinitePlane[{p2, p3, p4}], InfiniteLine[{p0, p1}]]]
{1/4, -1/4, 1/2}


From there:

Graphics3D[{Arrow[Tube[{p0, p1}]], {Opacity[2/3], InfinitePlane[{p2, p3, p4}]},
{Green, Arrow[Tube[{pin, pin + Normalize[nrm]}]]},
{Red, Arrow[Tube[{pin, pin + d - Projection[d, nrm]}]]},
{Blue, Sphere[pin, 0.03]}}, Axes -> True, AxesLabel -> {"X", "Y", "Z"}] Note the use of the Projection[] function.