\(MATH\ 257\ Calculus\ III				Week\ of\ April\ 28, \ 
        2003\)
Lab 4 :  3  D Animation and Cross Products
Name 1
Name 2
Section:

\(\(\( (*\ \(:\)\(Title : Arrow\ 3  D\)*) \)\( (*\ \(:\)\(Context : 
          Graphics`Arrow3D`\)*) \)\( (*\ \(:\)\(Author : 
          John\ M . Novak\)*) \)\( (*\ \(:\)\(Summary : 
          crude\ 3  D\ arrow\ primitive\)*) \)\( (*\ \(\(:\)\(Copyright : 
          Copyright\ 1996\ Wolfram\ Research\)\), \(\(Inc\)\(.\)\)*) \)\( (*\ \(:\)\(Package\ \(Version : 
            1.0\)\)*) \)\( (*\ \(:\)\(Mathematica\ \(Version : 
            3.0\)\)*) \)\( (*\ \(:\)\(History : 
          V1 \( .0--\)\ March\ 1996\ by\ John\ M . 
              Novak\)*) \)\( (*\ \(\(:\)\(Keywords : arrow\)\), 
      Graphics3D*) \)\( (*\ \(\(:\)\(Sources : 
          Tom\ Wickham - 
            Jones\)\), "Mathematica Graphics"*) \)\( (*\ \(\(:\)\(Discussion : 
          This\ is\ a\ package\ to\ add\ crude\ 3  D\ arrows\ by\ using\ 3  
            D\ graphics\ primitives . 
              The\ main\ problem\ with\ this\ approach\ is\ that\ arrows\ don' t\ have\ an\ optimal\ appearance\)\), 
      and\ distinction\ between\ \((e . g . )\)\ arrows\ point\ toward\ vs . 
          away\ from\ the\ user\ is\ difficult\ to\ make . 
          On\ the\ other\ hand, it\ is\ readily\ implementable, 
      which\ is\ far\ superior\ to\ any\ other\ approach\ available\ at\ this\ time . \((The\ PostScript\ used\ in\ 2  D\ arrows\ won' 
              t\ layer\ correctly\ in\ 3  D, nor\ would\ 2  D\ primitives; 
            placing\ 3  
              D\ planar\ primitives\ with\ the\ correct\ orientation\ has\ problems\ with\ perspective\ transforms\ as\ well\ as\ design\ problems\ with\ things\ like\ arrows\ with\ a\ small\ angle\ w . r . t . 
                the\ \(\(user\)\(.\)\))\)\ Because\ of\ the\ essentially\ different\ approach\ from\ 2  D\ arrows, 
      the\ package\ is\ being\ made\ independent\ of\ the\ 2  
        D\ package\ for\ the\ time\ \(\(being\)\(.\)\)*) \)\(BeginPackage["Graphics`Arrow3D`"]


    \(Arrow3D::usage = "Arrow3D[start, end] is a 3D graphics primitive representing an arrow in
space. The start point and the end point are given as {x, y, z}
triplets.
Options controlling the length and width of the arrowhead are
available.";\)


    Begin["`Private`"]

 (*The\ following\ routine\ is\ based\ on\ OrthogonalVectors\ from\ Tom\ Wickham - 
        Jones'\ Mathematica\ Graphics\ \(\(book\)\(.\)\)*) 


    anOrthogonalVector[norm : {_, _, _}] := 
      Block[{pos, a, b, v1}, 
        pos = If[VectorQ[norm, NumberQ], Abs[N[norm]], 
            norm]; 
pos = 
          Sort[Transpose[{pos, Range[3]}]]; 
{pos, a, b} = 
          Map[Last, pos]; 
v1 = 
          ReplacePart[{0, 0, 0}, \(-Part[norm, a]\), 
            b]; 
ReplacePart[v1, Part[norm, b], 
          a]]


    normalize[vec_] := 
      vec/Sqrt[vec . 
            vec]

 (*The\ following\ routine' rotationmatrix'\ was\ borrowed\ from\ the\ standard\ package\ \(\(Graphics`SurfaceOfRevolution`\)\(.\)\)*) 


    rotationmatrix[axis_, theta_] := 
      Module[{n1, n2, n3}, {n1, n2, n3} = 
          normalize[axis] // 
            N; 
{{n1^2 + \((1 - n1^2)\)\ Cos[theta], 
              n1\ n2\ \((1 - Cos[theta])\) + n3\ Sin[theta], 
              n1\ n3\ \((1 - Cos[theta])\) - 
                n2\ Sin[theta]}, {n1\ n2\ \((1 - Cos[theta])\) - 
                n3\ Sin[theta], n2^2 + \((1 - n2^2)\)\ Cos[theta], 
              n2\ n3\ \((1 - Cos[theta])\) + 
                n1\ Sin[theta]}, {n1\ n3\ \((1 - Cos[theta])\) + 
                n2\ Sin[theta], 
              n2\ n3\ \((1 - Cos[theta])\) - n1\ Sin[theta], 
              n3^2 + \((1 - n3^2)\)\ Cos[theta]}} // N]


    Arrow3D[base : {_, _, _}, tip : {_, _, _}, polys_:  10, 
        len_:  0.8] := {Line[{base, tip}], 
        mycone[tip - base, base, tip, polys, len]}


    Arrow3D[base : {_, _, _}, tip : {_, _, _}, ___] := 
      Point[base] /; base == tip


    mycone[vec_, base_, tip_, polys_, len_] := 
      Block[{norm = anOrthogonalVector[vec], theta}, 
        Map[Polygon[Append[#, tip]] &, 
          Partition[
            Table[base + 
                  len\ vec + \((1 - len)\)/2\ norm . 
                      rotationmatrix[vec, theta], {theta, 0, 2\ Pi, 
                  2\ Pi/polys}] // N, 2, 1]]]


    End[]


    EndPackage[]



    \("Graphics`Arrow3D`";\)

    \("Arrow3D[start, end] is a 3D graphics primitive representing an arrow in space. The start point and the end point are given as {x, y, z} triplets. Options controlling the length and width of the arrowhead are available.";\)

    \("Graphics`Arrow3D`Private`";\)

    \("Graphics`Arrow3D`Private`";\)

    \)\)\)

I.  Laboratory Objectives
	Use Mathematica, vectors, and matrices to explore
		3D Computer Animation.

II.  Formatting and Syntax Information

Polygon[{P1,P2,P3}]	Makes a polygon object with corners P1, P2, P3.  This can be drawn 
				using the Show[Graphics3D[ ]] command.
M.v	 				If M is a matrix and v is a vector, this will multiply
				M by v.
MatrixForm[M]		This displays the matrix M in matrix form.
Table[blah, {k,0,2π,.2}		This makes a table of <blah> where we increase k from
					0 to 2π in increments of .2.
					
	v×u				Two ways to do the Cross Product of two vectors v and u.  
	Cross[v,u]		The × is the smallest "X" on the Basic Inputs pallette. (Under the i.)
					
When you  execute the first command,  YOU WILL BE ASKED if you want to execute any initialization cells.  CLICK YES.  This will load the Arrow3D package.

Be sure to read everything and execute all the commands as you go along.

Polygons in 3D
Suppose you want Mathematica to draw a little pyramid for you.  The easiest way to do this would be to define some points in space, turn them into polygons and drawing them.  First, the points:

P1 = {0, 0, 0}

    P2 = {1, 0, 0}

    P3 = {0, 1, 0}

    P4 = {0, 0, 1}

To draw the pyramid that these points form, we gotta use the Polygon[ ] command:

Show[
      Graphics3D[{Polygon[{P1, P2, P3}],  Polygon[{P1, P2, P4}],  
          Polygon[{P1, P3, P4}],  Polygon[{P2, P3, P4}]}]]

Now, our GOAL is to make an animation that shows this pyramid spinning about some axis.  To do this we'll use the 3D rotation matrices:

rotate about z-axis: (({{({{Cos[θ], -Sin[θ]}}), 0}, {({{Sin[θ], Cos[θ]}, {0, 0}}), ({{0}, {1}})}})), about the y-axis:  (({{({{Cos[θ], 0}}), -Sin[θ]}, {({{0, 1}, {Sin[θ], 0}}), ({{0}, {Cos[θ]}})}})) , 

or about the x-axis: (({{1, ({{0,             0}})}, {({{0}, {0}}), ({{Cos[θ], -Sin[θ]}, {Sin[θ], Cos[θ]}})}})).  
We define matrices in Mathematica just like vectors:  as a list.  For example, here's the matrix that rotates about the z-axis:

Rz[θ_] = {{Cos[θ],  -Sin[θ],  
          0},  {Sin[θ],  Cos[θ],  0},  {0, 0, 
          1}}

To see it in matrix form (to make sure we didn't screw up), use the MatrixForm[] command:

MatrixForm[Rz[θ]]

Here are the other two 3D rotation matrices:

Ry[θ_] = {{Cos[θ],  0,  -Sin[θ]},  {0,  
          1,  0},  {Sin[θ],  0,  
          Cos[θ]}}
Rx[θ_] = {{1, 0, 0},  {0,  
          Cos[θ],  -Sin[θ]},  {0,  Sin[θ],  
          Cos[θ]}}

Now let's make an animation of our pyramid rotating about the x-axis!!  

All we have to do is make a Table[ ] of plots where in each plot we multiply all the points P1, P2, P3, and P4 by one of out matrices.  Below I've done this in a way that will be easy for you to modify later.  I defined a new matrix M[t] to be the matrix we care about, Rx,  and then inserted it into the Polygon commands.  (So M[t].P1 will rotate point P1 by angle t.)  I've included some Arrow3D commands that draw the positive x,y,z axes for reference.

Clear[M, t];
M[t_] = Rx[t];
Table[
Show[
        Graphics3D[{Arrow3D[{0, 0, 0},  {2, 0, 
                0}], 
Arrow3D[{0, 0, 0},  {0, 2, 
                0}], 
Arrow3D[{0, 0, 0},  {0, 0, 
                2}], 
Polygon[{M[t] . P1, M[t] . P2, 
                M[t] . P3}],  Polygon[{M[t] . P1, M[t] . P2, M[t] . P4}],  
            Polygon[{M[t] . P1, M[t] . P3, M[t] . P4}],  
            Polygon[{M[t] . P2, M[t] . P3, 
                M[t] . P4}]},  
PlotRange -> {{-2, 2}, {-2, 2}, {-2, 2}}]]
, {t, 0,  
        2  π,  .2}]

Whoa!  Mathematica just made a TON of plots!  Before you do anything else double-click on the second-to-the-left-most handle to the right of your plots.  This will shrink the table of plots into a single-sized cell. Always remember to minimize your animations like this to keep your Lab at a managable size.  

Then you should double-click on the graphic, and you'll see it animate!  Your pyramid should rotate about the x-axis.

Question 1a: Make an animation that rotates the pyramid about the z axis. (You should only have to change the M[t] matrix in the above commands to do this.)

Now, what if you want to rotate about, say, the x-axis and then the y-axis?  How do you do that?  Simple!  Just multiply the matrices you want, and let this new matrix be M[t].  It's that simple!  But NOTE: to multiply matrices in Mathematica you need to use the dot "." command, like we did for dot products.

Question 1b: Make an animation that rotates the pyramid about the x and y axes simultaneously.

Question 1c: Make an animation that rotates the pyramid about the line y=x in the x-y plane.  Hint: you gotta figure out what product of matrices would do this.  Not all of your rotations have to be by t!

Question 2: Use the methods above to make an animation of a cube rotating about the y axis.  (It's up to you to decide how you want to position the cube in space.)

Cross Products

Let's mess around with some cross products as well.  You'll use either of the commands Cross[u,v] or u×x to get the cross product of vectors u and v.

One useful thing is to be able to label vectors when you draw them in Mathematica.  If you're drawing a vector a starting at the origin, use the Arrow3D command to draw it:
		Arrow3D[{0,0,0}, a]
and use the Text[StyleForm[ "text", FontSize-> whatever  ], {coordinates of the text} ] command to give it a label:
		Text[StyleForm["a", FontSize->16], a]
That will put an "a" at the coordinates of the vector a.  The FontSize command makes sure it'll be big enough to see.  (You can experiment with different font sizes.)  See the Help browser for more info.

Question 3: Make up two nonzero, nonparallel vectors and call them a and b.  Make a 3D drawing of your two vectors, starting at the origin, together with their cross product, and label which vector is which.

Question 4: What happens when you take the cross product of two parallel vectors?  Experiment with vectors of your choosing to demonstrate this in Mathematica.  Why do you think this happens?

Question 5: Suppose you're given three points of a triangle: (0,0,0), (x1, y1, 0), and (x2, y2, 0). Use the cross product to generate a formula (in terms of the x1, x2, y1, and y2) of the area of this triangle.  (Hint: think half of a parallelogram.)

Now that you're done, 
	(1) 	Clean up your work by deleting everything that's not needed.  
		There should only be the title, your names, section number, and the questions and your answers
		(with any needed explanations, graphs, etc.).
	(2) 	Save this to your disk.
	(3)	Go back to Blackboard and upload your final lab report to the Digital Drop Box, located
		in the User Tools area.  Remember that you have to first ADD your file to your digital drop box and 
		then SEND it to me!