\(\(\( (*\ \(:\)\(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`";\)

    \)\)\)

\(MATH\ 257\ Calculus\ III				Week\ of\ May\ 12, \ 2003\)
Lab 7 :  Cross Products and Planes
Name 1
Name 2
Section:

I.  Laboratory Objectives
	Use Mathematica, vectors, and matrices to explore
		Cross Products

II.  Formatting and Syntax Information

v×u				Two ways to do the Cross Product of two 
	Cross[v,u]		vectors v and u.  The × is the smallest "X" on the 
					Basic Inputs pallette. (Under the i.)
					
Before you begin execute the following command.  Click YES when asked to execute any initialization cells:

<< Graphics`Arrow`

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

Cross Products

If you have two vectors u and v, there is a way you can multiply them together to get a new vector, u×v.  This is called the cross product of u and v.  Computing the cross product by hand is a real pain, as you will see in class (if you haven't seen this already).  Luckily, Mathematica will do all the yucky computations for us. You can use either of the commands Cross[u,v] or u×x to get the cross product of vectors u and v.

Try the following example.  The third vector shown will be the cross product of the first two.

u = {1, 1, 1}

    v = {-3,  -2,  -1}

    Cross[u, 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.

So, if you want to draw a 3D vector and label it, all you need to do is put a Text[StyleForm[]] command immediately following the Arrow3D[] command.  Like this:

u = {1, 2, 3};
Show[
      Graphics3D[{
Arrow3D[{0, 0, 0},  
            u],  
Text[
            StyleForm["this is vector u",  FontSize -> 16],  
            u]
}, 
        Axes -> 
          True,   PlotRange -> {{0, 3},  {0, 3},  {0, 
              3}}]]

Question 1: 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.

You should notice in the previous problem the following fact about cross products:
       u×v  is a new vector that is perpendicular (or normal) to both vectors u and v

Question 2: What happens when you take the cross product of two parallel vectors?  Remember: two vectors are parallel if they have either the same direction or completely opposite directions.  Experiment with vectors of your choosing to find the cross product of two parallel vectors in Mathematica.  (Make sure your vectors are parallel!)  Why do you think this happens?

Why do we need cross products?  There are many reasons.  Here are two:
	(1) They give a good way of producing normal vectors.
	(2) If u and v are  vectors, then |u×v| (the magnitude of u×v) will equal the 
	     area of the parellelogram determined by u and v.  (See the below picture.)
This last one is proven in the textbook.  (You'll probably see it in class.)

\(Show[
          Graphics[{Thickness[ .01], 
              Arrow[{0, 0}, {3, 1}], 
Arrow[{0, 0}, {2, 
                  4}], 
Text[
                FontForm["u", {"Courier-Bold", 
                    14}], {1.5,  .73}], 
Text[
                FontForm["v", {"Courier-Bold", 14}], { .7, 
                  2.2}], 
Dashing[{ .02,  .02}], 
Arrow[{2, 4}, \ {5, 5}], \ 
              Arrow[{3, 1}, {5, 5}], 
Text[
                FontForm["Area=|u\[Cross]v|", \ {"Courier-Bold", \ 
                    14}], \ {2.5, 2.5}]
}], \ 
          AspectRatio -> Automatic]\)

Question 3: 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.)

Graphing Planes in 3D

Graphing 3D surfaces in Mathematica is easy if you have a function of the form z=f(x,y).  Just use the Plot3D[] command, which works just like Plot[] except you have to specify the x and y ranges.

For example, let's plot the plane  z = -x +.25 y +1.   (Here we use the ViewPoint command to get a better view of it.)

plane1 = 
      Plot3D[-x  +   .25*y  +  1,  {x,  -2, 2},  {y, -2, 
          2},  PlotRange -> {-2, 2},  AspectRatio -> 1,  
        ViewPoint -> {2.318,  -2.360,  0.713}]

(Mathematica chops off parts of the plane that don't fit.  That's what you see at the top.)
Let's draw some vectors on this plane.  Plugging in x=0, y=0, we get z=1, so the point (0,0,1) is on this plane. We can also plug in x=1, y=0 and plug in x=0, y=1 to get other points.  Then we can draw some arrows connecting these points:

p = {0, 0, 1};
p1 = {1, 0, 0};
p2 = {0, 1, 1.25};
Show[{plane1,  
        Graphics3D[{Thickness[ .01], Arrow3D[p, p1],  
            Arrow3D[p, p2]}]}]

Notice how we used the Show[] command to combine the plane1 plot with the Graphics3D arrow commands.  

So, let's find the cross product of these two vectors!  We cannot just use p, p1, and p2 as they are, since they would be vectors going from the origin to these points.  Instead, we gotta subtract them:  one vector will be p1-p, and the other will be p2-p.  So call their cross product Nvec (since it will be a normal vector), and let's graph them all together!

Nvec = (p1 - p)\[Cross](p2 - p)
Show[{plane1,  
        Graphics3D[{Thickness[ .01], Arrow3D[p, p1],  
            Arrow3D[p, p2], 
Arrow3D[p, 
              Nvec + p]}]}]

Notice how the perspective is a little weird, but it does look like Nvec is normal to the plane!

Question 4: Use the same procedure we used above to make a picture of the plane z=3x+y together with a normal vector at the point (0,0,0).

Here's how you can make Mathematica plot these two planes at the same time:

plane1 = 
        Plot3D[-x  +   .25*y  +  1,  {x,  -2, 2},  {y, -2, 
            2},  PlotRange -> {-2, 2},  AspectRatio -> 1,  
          ViewPoint -> {2.318,  -2.360,  0.713}];

    plane2 = 
        Plot3D[3  x  +  y ,  {x,  -3, 3},  {y, -3, 3},  
          PlotRange -> {-3, 3},  AspectRatio -> 1,  
          ViewPoint -> {-1, 1, 1}];

    Show[{plane1,  plane2}]

Question 5: Use Mathematica to figure out the angle between these two planes.  Yes, there are two different angles you could get as your answer; either will be accepted.  Hint: use normal vectors and the dot product!

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!