Angles & Perpendicular Projections of Vectors

by
K. D.  Stroyan
University of Iowa

copyright 1997 by Academic Press, Inc. - All rights reserved.

Special Mathematica Functions used in this NoteBook

We told you not to look.  This horrible stuff sets various defaults and defines some functions needed to draw the geometric side of the lexicon.  Ignore the details here and go on to the lesson.

Off[General::spell];
Off[General::spell1];
Off[ParametricPlot3D::ppcom]
Needs["Graphics`Colors`"]

SetOptions[Graphics,Axes->False,AxesOrigin->{0,0},AspectRatio->Automatic];
SetOptions[Graphics3D,ViewPoint->{5,2,1},Boxed->False,AspectRatio->Automatic];
SetOptions[ParametricPlot3D,ViewPoint->{5,2,1},AspectRatio->Automatic];
SetOptions[Plot3D,ViewPoint->{5,2,1},AspectRatio->Automatic];
SetOptions[ParametricPlot,AspectRatio->Automatic];
SetOptions[Plot,AspectRatio->Automatic];

colorQ[a_]:=(Head[a]===RGBColor);
grayQ[a_]:=(Head[a]===GrayLevel);
thickQ[a_]:=(Head[a]===Thickness);
absthickQ[a_]:=(Head[a]===AbsoluteThickness);
notruleQ[u_] := !(Head[u]===Rule);
ruleQ[u_] := (Head[u]===Rule);

Norm::usage = "Norm[expr] returns the Euclidean length of the
		vector, 'expr'.";

Norm[a_?VectorQ] := Sqrt[a.a];

Angle[a_?VectorQ,b_?VectorQ] := ArcCos[(a.b)/(Norm[a] Norm[b])]

Proj[a_?VectorQ,b_?VectorQ] := ((a.b)/(b.b))b

tol = .5*10^-6;

OrthUp[a_?VectorQ] :=
	Module[{b,c,d,f},
		d = N[a/Norm[a]];
		If[Chop[d[[3]]^2 - 1,tol] == 0, b={1,0,0} ,
		b = {0,0,1} - d[[3]] d; b = b/Norm[b];];
	Return[b]];
	
OrthSet[a_?VectorQ] :=
	Module[{b,c,d},
		d = N[a/Norm[a]];
		If[Chop[d[[1]],tol]==0,b={1,0,0},
			 If[Chop[d[[2]],tol]==0,b={0,1,0},
			  b={-d[[2]],d[[1]],0}]];
			b=b/Norm[b];c = Cross[d,b];
	Return[{b,c/Norm[c],d}]];

				
Rotate[a_?VectorQ,theta_] :=
	Module[{u,v,r},
		u = OrthSet[a];
		v = Transpose[u];
		r = N[{{Cos[theta],-Sin[theta],0},{Sin[theta],Cos[theta],0},{0,0,1}}];
	Return[v.r.u]];

Vector2D[tail_,tip_,opts___]:=
	Module[{ntail,ntip,sopts,Punit,up,arrow},
	ntail = N[tail];
	ntip = N[tip];
	
	sopts = Join[Select[{opts},colorQ],Select[{opts},
				thickQ],Select[{opts},absthickQ],Select[{opts},grayQ]];

If[Chop[ntail.ntail+ntip.ntip,tol] == 0,
			(*too small to plot or not number*)
			Return[Graphics[Point[{0,0}]]];
	,						
If[Chop[(ntip-ntail).(ntip-ntail),tol] == 0,
		Return[Graphics[{Join[sopts,{Line[{ntail,ntip}]}]}]];
	,
	Punit = (.075 Norm[ntip-ntail]/Norm[ntip-ntail]) (ntip-ntail) ;
	up = 0.15 {-Punit[[2]],Punit[[1]]};
	arrow = Line[{ntail,ntip,ntip-Punit+up,ntip,ntip-Punit-up}];
	]];
Return[Graphics[{Join[{AbsoluteThickness[2]},Join[sopts,{arrow}]]}]];
];

Vector3D[tail_,tip_,opts___]:=
	Module[{ntail,ntip,sopts,Punit,up,u2,u3,u4,rotmat,arrow},
	ntail = N[tail];
	ntip = N[tip];
	
	sopts = Join[Select[{opts},colorQ],Select[{opts},
				thickQ],Select[{opts},absthickQ],Select[{opts},grayQ]];

If[Chop[ntail.ntail+ntip.ntip,tol] == 0,
			(*too small to plot or not number*)
			Return[Graphics3D[Point[{0,0,0}]]];					
	,		
If[Chop[(ntip-ntail).(ntip-ntail),tol] == 0,
		Return[Graphics3D[{Join[sopts,{Line[{ntail,ntip}]}]}]];
	,
	Punit = (ntip-ntail);
	rotmat = Rotate[Punit,N[Pi/2]];
	up = OrthUp[Punit];
	Punit = .075 Punit;
	up = .15 Norm[Punit] up;
	u2 = rotmat.up;
	u3 = rotmat.u2;
	u4 = rotmat.u3;
	arrow = Line[{ntail,ntip,
				ntip - Punit + up,ntip,
				ntip - Punit + u2,ntip,
				ntip - Punit + u3,ntip,
				ntip - Punit + u4}];
	]];
Return[Graphics3D[{Join[{AbsoluteThickness[2]},Join[sopts,{arrow}]]}]];
];

aRCtAN[y_,x_] := Module[{v},
	If[ Chop[N[x],tol] == 0 ,If[ N[y] < 0, v = -N[Pi/2], v = N[Pi/2]],
		v = N[ArcTan[y/x]];
		If[ x < 0 && y < 0, v = N[Pi -  v],v]];
	Return[v]];

Arc2D[X_,Y_,opts___] := Graphics[{opts,
		Disk[{0,0},1,{Min[
							aRCtAN[
						N[X[[2]]],N[X[[1]]]
								]
							,
							aRCtAN[
							N[Y[[2]]],N[Y[[1]]]
								]
						]
						,
					Max[
							aRCtAN[
						N[X[[2]]],N[X[[1]]]
								]
							,
							aRCtAN[
							N[Y[[2]]],N[Y[[1]]]
								]
						]}
				]}
				];
Arc3D[X_,Y_] := 
	Module[{t,U,V},
		U[V_]:= N[(1/Sqrt[V.V]) V];
		Return[Graphics3D[
		Polygon[Append[Table[U[t X + (1-t) Y],{t,0,1,0.1}],{0,0,0}]]
				]
		]
	];

(*Zero Arrow causes trouble. Put in Vector?*)

Vector[tail_,tip_,opts___]:=
	If[Length[N[tail]]==2,Return[Vector2D[tail,tip,opts]],
		Return[Vector3D[tail,tip,opts]]];
		
Arrow[vec_,opts___]:= 
	Module[{a,b},
				If[Length[N[vec]]==2,
				a=ToExpression["Tail"]/.Select[{opts},ruleQ]/.
					{ToExpression["Tail"]->{0,0}};
				Return[Vector2D[a,a+vec,opts]],
				a=ToExpression["Tail"]/.Select[{opts},ruleQ]/.
					{ToExpression["Tail"]->{0,0,0}};
				Return[Vector3D[a,a+vec,opts]]]
				];

Frame2D[delta_] := 
      Graphics[{
            {Green,Line[{{-delta/4,0},{delta,0}}]},
                    Text["x",{1.01 delta,0}],
            {Green,Line[{{0,-delta/4},{0,delta}}]},
                    Text["y",{0, 1.01 delta}]}];

Frame3D[delta_] := 
      Graphics3D[{
            {Green,Line[{{-delta/4,0,0},{delta,0,0}}]},
                    Text["x",{1.01 delta,0,0}],
            {Green,Line[{{0,-delta/4,0},{0,delta,0}}]},
                    Text["y",{0, 1.01 delta,0}],
            {Green,Line[{{0,0,-delta/4},{0,0,delta}}]},
                    Text["z",{0,0,1.01 delta}]}];

View2D[stuff_] := Show[stuff,Frame2D[1]];
View3D[stuff_] := Show[stuff,Frame3D[1]];

Notebook Overview

Given a vector measured in Cartesian coordinates, such as  X = {3,4} in 2D  or  X = {1,-2,2} in 3D we want formulas to compute the geometric quantities of "magnitude and direction."  This NoteBook explains these formulas.

References to the Text

Section 15.6

Section 15.6 in the text explains the geometric formulas of this NoteBook in detail.

Dot Product & Length

Dot Product

The dot product of two vectors of the same length is defined to be the sum of the products of like coordinates.  Un-equal length vectors do not have a dot product.  Mathematica uses the period . symbol for dot product.  <Enter> the next computation to see Mathematica's dot product.

The dot product does not have a direct geometric interpretation, but plays an important role in several geometric formulas.

Clear[A, a, b, c, X, x, y, z]; A = {a, b, c}; B = {a, b}; X = {x, y, z}; Y = {x, y}; A . XB . YA . Y

Length: The Norm[X_] function.

The length of a vector is the square root of the sum of the squares of the vector's coordinates.  This is shown to be a version of the Pythagorean theorem in Section 15.2 of the text.  The calculation can be written concisely as the square root of the dot product as follows:  <Enter> it to define a Mathematica  "Norm[.]" function for later use.

\(Norm[X_] := \@\(X . X\); 
Clear[X, x, y, z, Y]; 
X = {x, y, z}\)Norm[X]Y = {3, 4}Norm[Y]

One important connection between geometry and the dot product is the formula:

|X| * |Y| * Cos[ Angle[X,Y] ] = X . Y

This is illustrated in 2D and 3D in the next two computations. <Enter> them now.

2-D

X = {5, 12}; Y = {4, 3}; d = X . Y; x = Norm[X]; y = Norm[Y]; View2D[{Arrow[X, Blue], Graphics[Text[x "=Norm", 0.5 X]], 
        Arrow[Y, Red], Graphics[Text[y "=Norm", 0.8 Y]]}]; Print["|X|*|Y|*Cos[Angle[X,Y]] = ", x, "*", y, "*", 
      Cos[Angle[X, Y]], " = ", x y Cos[Angle[X, Y]]]; Print[X, ".", Y, " = ", d];

Practice Exercise

Copy the computation above and modify it to draw the vectors {-3,-2}, {-8,12}, find their dot product and lengths.

3-D

X = {3, 4, 2}; Y = {1, -2, 3}; x = Norm[X]; y = Norm[Y]; d = X . Y; View3D[{Arrow[X, Blue], 
        Graphics3D[Text[x " = Norm[X]", 0.7 X]], Arrow[Y, Red], 
        Graphics3D[Text[y " = Norm[Y]", 0.8 Y]]}]; Print["|X|*|Y|*Cos[Angle[X,Y]] = ", x, "*", y, "*", 
      Cos[Angle[X, Y]], " = ", x y Cos[Angle[X, Y]]]; Print[X, ".", Y, " = ", d];

Practice Exercise

Copy the computation above and modify it to draw the vectors {5,2,-3}, {2,-2,2}, find their dot product and lengths.

Angle and Direction

When we know cartesian coordinates, the dot product formula:

|X| * |Y| * Cos[ Angle[X,Y] ] = X . Y

may be solved for the angle,

Cos[ Angle[X,Y] ] =(( X . Y)/(( | X |  *  | Y |  )))
so
Angle[X,Y] = ArcCos[(( X . Y)/(( | X |  *  | Y |  )))]

<Enter> the next cell to define a Mathematica "Angle[.,.]" function and see two computations using it.

Angle

Norm[X_] := (Sqrt[X . X]); Angle[X_, Y_] := ArcCos[X . (Y/(Norm[X] Norm[Y]))]Angle[{1, 1}, {1, 0}]N[Angle[{1, 1}, {1, 0}]]

Clear[x, y, z, a, b, c, X, Y]; X = {x, y, z}; Y = {a, b, c}; Angle[X, Y]

The next two cells show the geometric angle.

2-D Angle with Figure

Y = {5, 5 (Sqrt[3])}; X = {4, 4}; theta = Angle[X, Y]; Print["Exact angle = ", theta]; a = N[theta, 2]; View2D[{Arrow[X, Blue], Arrow[Y, Red], Arc2D[X, Y], 
        Graphics[Text[a "=a",  .3 Y +  .4 X]]}];

Practice Exercise

Copy the computation above and modify it to draw the vectors {-3,-2}, {-8,12} and find the angle between them.

3-D Angle with Figure

X = {-3, 1, 2}; Y = {1, -2, 1}; theta = Angle[X, Y]; Print["Exact angle = ", theta]; a = N[theta, 2]; View3D[{Arrow[X, Blue], Arrow[Y, Red], Arc3D[X, Y], 
        Graphics3D[Text[a "=a",  .3 Y +  .4 X]]}];

Practice Exercise

Copy the computation above and modify it to draw the vectors {5,2,-3}, {2,-2,2}, and find the angle between them.

Direction Cosines & Direction Angles

The "direction" of a vector could be measured as the angles between the vector and the axes.  To do this in 2D, we use the unit vestors  E1 = {1,0} and E2 = {0,1}.  If  X = {x1,x2}, the formula

Cos[ Angle[X,Y] ] = (X.Y)/( |X| * |Y| )
becomes simply
Cos[ Angle[X,E1] ] = (x1)/( |X| )   and   Cos[ Angle[X,E2] ] = (x2)/( |X| )

These numbers are called direction cosines.  They are a useful measure of direction that does not require computation of an ArcCosine (although that additional step is not difficult with Mathematica..)

Practice Exercise

If  X = (x1,x2,x3),  E1 - {1,0,0},  E2 = {0,1,0}  and  E3 = {0,0,1} in 3D, show that

Cos[ Angle[X,E1] ] = (x1)/( |X| )
Cos[ Angle[X,E2] ] = (x2)/( |X| )
Cos[ Angle[X,E3] ] = (x3)/( |X| )

You don't need a computer.

2-D  Direction Angles and Direction Cosines

\(X = {1, \@3}; 
E1 = {1, 0}; 
α\_1 = Angle[X, E1]; 

    a1 = N[α\_1]; 
E2 = {0, 1}; 
α\_2 = Angle[X, E2]; 

    a2 = N[α\_2]; 

    Print["Exact angle to x-axis = ", α\_1, " approx. = ", 
      a1]; 
Print["Direction Cosines = (", 
      X[[1]]\/Norm[X], " , ", 
      X[[2]]\/Norm[X], ")"]; 

    \(View2D[{Arrow[X, Blue], Arrow[E1, Red], Arrow[E2, GreenDark], 
        Arc2D[X, E1, Yellow], Arc2D[X, E2, GreenPale], 
        Graphics[Text[a1\ "=a1",  .5\ E1 +  .2\ X]], 
        Graphics[Text[a2\ "=a2",  .5\ E2 +  .2\ X]]}]; \)\)

Practice Exercise

(1)  Copy the computation above and modify it to draw and compute the angles and direction cosines of the vector  X = {1,1}.

(2)  Copy the computation above and modify it to draw and compute the angles and direction cosines of the vector  X = {-2,2}.

(3)  Copy the computation above and modify it to draw and compute the angles and direction cosines of the vector  X = {3,-3}.

(4)  Copy the computation above and modify it to draw and compute the angles and direction cosines of the vector  X = {-4,-4}.

3-D

X = {1, 2, 3}; E1 = {1, 0, 0}; E2 = {0, 1, 0}; E3 = {0, 0, 1}; \(View3D[{Arrow[X, Blue], Arc3D[X, E1], Arc3D[X, E2], Arc3D[X, E3]}]; 

    α\_1 = Angle[X, E1]; 
α\_2 = Angle[X, E2]; 

    α\_3 = Angle[X, E3]; 
a1 = N[α\_1, 2]; 
    a2 = N[α\_2, 2]; a3 = N[α\_3, 2]; 

    Print["Angles=(", α\_1, " , ", α\_2, " , ", 
      α\_3, ")"]; 

    \(Print["Approximate Angles = (", a1, " , ", a2, " , ", a3, 
      ")"]; \)\)Print["Direction Cosines = (", 
      X[[1]]/Norm[X], " , ", 
      X[[2]]/Norm[X], " , ", 
      X[[3]]/Norm[X], ")"]; Print["Approximate Cosines = (", 
      N[X[[1]]/Norm[X]], " , ", 
      N[X[[2]]/Norm[X]], " , ", 
      N[X[[3]]/Norm[X]], ")"];

Practice Exercise

(1)  Copy the computation above and modify it to draw and compute the angles and direction cosines of the vector  X = {1,1,1}.

(2)  Copy the computation above and modify it to draw and compute the angles and direction cosines of the vector  X = {-2,2,2}.

(3)  Copy the computation above and modify it to draw and compute the angles and direction cosines of the vector  X = {3,-3,3}.

(4)  Copy the computation above and modify it to draw and compute the angles and direction cosines of the vector  X = {-4,-4,4}.

X is perpendicular to Y <->  X.Y = 0

A very important special case of the angle formula  Cos[ Angle[X,Y] ] = (X.Y)/( |X| * |Y| )  is when the angle is a right angle.  Since Cosine[ Pi/2 ] = 0, if the vectors are nonzero, the angle being Pi/2 is equivalent to

X.Y = 0

Practice Exercise

Which signs on the vector components Y = {?2,?1,?2} make it perpendicular to  X = {1,2,2}?  Verify your computations and draw the vectors by copying and modifying the appropriate computation above.

Perpendicular Projection

The projection of a vector  X  in the direction of a vector  Y  is given by a simple formula.  The simple fact that the formula requires no square roots or arccosines makes it memorable.  Enter the next 3 computations to see the formula at work.

Proj[X_, Y_] := ((X . Y Y)/Y) . YX = {3, 4}; Y = {1, 1}; Proj[X, Y]

2-D

X = {2, 2 (Sqrt[3])}; Y = {4 (Sqrt[3]), 4}; Z = Proj[X, Y]View2D[{Arrow[X, Blue], Arrow[Y, Red], Arrow[Z, Yellow], 
        Graphics[Line[{Z, X}]]}];

3-D

X = {3, 4, 5}; Y = 4 {1, 2, 1}; Z = Proj[X, Y]View3D[{Arrow[X, Blue], Arrow[Y, Red], Arrow[Z, Yellow], 
        Graphics3D[Line[{Z, X}]]}];

Practice Exercise: Try this computation and explain.

X = {3, 4, 5}; Y = {1, -2, 1}; Z = Proj[X, Y]

Exercises

Exercise 15.6.2

For each pair of vectors below:

Draw  X, Y and show the angle between them.

Compute  |X|, |Y|, X.Y and the radian and degree measures of the angle between them:

X = {-3,-2} & Y = {-8,12}
X = {2,1} & Y = {-4,5}
X = {7,-3} & Y = {-2,-8}
X = {6,-9} & Y = {3,2}
X = {9,1} & Y = {2,-18}

X = {3,2,1} & Y = {1,2,3}
X = {1,-2,2} & Y = {2,-1,-2}
X = {1,-2,2} & Y = {2,2,1}
X = {2,2,1} & Y = {1,-2,2}
X = {-1,4,2Sqrt[2]} & Y = {-1, 2Sqrt[2],4}

Exercise 15.6.3

Draw the vectors  X  and  Y and Z = the vector projection of  X  in the direction of  Y.  Also find the norm or geometric length | Z |.

X = {-3,-2} & Y = {-8,12}
X = {2,1} & Y = {-4,5}
X = {3,4} & Y = {4,3}
X = {3,4} & Y = {-4,3}

X = {1,2,3} & Y = {3,2,1}
X = {1,0,0} & Y = {3,2,1}
X = {0,1,0} & Y = {3,2,1}
X = {1,-2,2} & Y = {2,-1,-2}