% Chapter 4 from Asymptote tutorial Jim Hefferon
\def\asydir{chapter4/asy/}
\chapter{3D}

A strength of \Asy{} is its ability in three dimensions.
It can easily draw what you want to draw, including
% that it extends  to three dimensions constructs for good-looking paths
% that come from two-dimensional \MF{} and \MP.
doing projections, which can be tricky to get right by eye.

In addition, you can choose to make these graphics manipulable, so that you can 
use your mouse to turn them around or peek under them, and in 
general have an explore.
This is great for a Calculus lecture so its what I'm showing here.

Note that only some PDF viewers let you manipulate.
For instance, Adobe's Reader works but the readers embedded in web
browsers do not.
To test your reader just click on the graphic below.
(You may be asked to let the reader 
run the code that does the manipulation.)

We start with a picture showing the vector of displacements $(2,1,1)$ at a 
number of initial points.
% \begin{center}
%   \asyinclude{chapter4/asy/vectors}
% \end{center}
\begin{center}
  \input chapter4/asy/vectors.tex
\end{center}  

The input code has two different things going on.
First, so that the vectors are scattered about,
in lines 34--36 they get a randomly-chosen initial point.
The randomization uses the seed from line~24.
To find that number I uncommented lines 21--23 and commented out line~24,
compiled the \path{.asy} file a couple of times 
until I got a scatter that I liked, and then I froze the seed.
With that, the loop in
lines~33--44 creates a randomly placed vector and
draws it if the entire vector will show, 
until there are such eight vectors. 
\begin{center}
  \inputminted{Asymptote}{chapter4/asy/vectors.asy}
\end{center}
The other thing in the code is the 3D stuff.
It is surprisingly like the 2D constructs that we have seen.
Line~10's 
\mintinline{Asymptote}{import graph3} gives access to 
\Asy's 3D routines, and extends them to axes and graph plotting.
Some things are new, such as that instead of 
\mintinline{Asymptote}{pair}'s
you want
\mintinline{Asymptote}{triple}'s, 
and instead of 
\mintinline{Asymptote}{xaxis(..)}
you say 
\mintinline{Asymptote}{xaxis3(..)}.
But much of it is at least similar.

The next graphic shows surfaces, 
which are genuinely different than what we saw in 2D.
It illustrates that the angle between two 
intersecting planes is the same as the angle between their normal
vectors. 
% \begin{center}
%   \asyinclude{chapter4/asy/planes.asy}
% \end{center}
\begin{center}
  \vcenteredhbox{\input chapter4/asy/planes.tex }%
\end{center}  
We can imagine that lines~19 and~20 set the location of the camera
that is taking this picture.
Even if we use a reader where the image is manipulable,
we need a starting view.
\begin{center}
  \inputminted{Asymptote}{chapter4/asy/planes.asy}
\end{center}

This graphic's code 
spotlights the power of transforms.
We don't have to give the equations of the planes or specify their normals.
Instead, in line~17 we define the edge of 
the horizontal plane region and then in line~18 we create that
as a surface. 
To get the other plane, the one at an angle, 
we make a transform \mintinline{Asymptote}{p_t} in line~19 
that basically rotates by \mintinline{Asymptote}{rotation_degs}
about the $x$~axis.
The new plane with its edge and its normal vector 
then comes from applying that transform
to the horizontal plane, its edge, and its normal.

To color the surfaces, in lines~24 and~26 we use 
\mintinline{Asymptote}{figure material}.
We will reuse these later so the definition is in \path{jh.sty};
see lines~28--35 in the Appendix.
This coloring involves \mintinline{Asymptote}{opacity(..)} and 
note that you can indeed see through the planes to some extent. 

Finally, in lines~40--46 we take advantage of one of \Asy's many helper functions
to find and draw the arc of the angle between the planes and the normals.
 
Our last graphic is from the Calculus~I lecture on the volume of
a surface of revolution, specifically using slices that are
washers.
We start with the $xy$~plane area between $y=x^2$ and $y=x$,
here defined as line~33's \mintinline{Asymptote}{pth}.
\begin{center}
  \inputminted{Asymptote}{chapter4/asy/washer.asy}
\end{center}
In line~39 \Asy{} rotates that area about the $y$~axis, giving the 3D figure.
% \begin{center}
%   \asyinclude{chapter4/asy/washer.asy}
% \end{center}
\begin{center}
  \vcenteredhbox{\input chapter4/asy/washer.tex }%
\end{center}  





\section{Include the graphic in a \LaTeX{} file}
To illustrate including 3D output in your document we use
\path{asy/vectors.asy}.
Create the \LaTeX{} file \path{main_3d.tex}. 
\begin{center}
  \inputminted{TeX}{chapter4/main_3d.tex}
\end{center} 
Run \LaTeX{}. 
\begin{minted}{Bash}
jim@millstone:~/Documents/asy_tut/src/chapter4$ pdflatex main_3d
\end{minted}
(As outlined in the document,
another option that many people prefer
is to instead use \mintinline{TeX}{\asyinclude{..}}.
For that you first run \LaTeX{}, then go to the \path{asy/} subdirectory and
run \mintinline{Bash}{asy -inlineimage main_3d-1} and then return
to the starting to directory to run \LaTeX{} on the above document a
second time.)
Here is a shot of the output.
\begin{center}
  \framebox{\includegraphics[page=1,scale=0.325,trim=0.25in 5in 0.25in 0.25in]{chapter4/main_3d.pdf}}
\end{center}
In that output PDF, 
you should be able to click on the graphic and move it around
(of course, your reader must supports this). 



