| Drawing with the tikz-3dplot Package |
|
|
|
| LaTeX - Graphics, Figures & Tables |
| Written by Jeff Hein |
| Thursday, 21 June 2012 09:33 |
|
When I started working on my thesis dissertation using LaTeX, I discovered the TikZ package for drawing vector-based figures. I needed a way to easily draw three-dimensional figures, and so I put together a few handy tools in the Let's explore some examples of what - A contribution to the LaTeX and Graphics contest - This article is available for reading and for download in pdf format - The BasicsLet's draw a cube, with side length of 2. To help illustrate the comparison, we'll draw a grid in the x-y plane, and also show the x, y, and z axes. \begin{tikzpicture}
[cube/.style={very thick,black},
grid/.style={very thin,gray},
axis/.style={->,blue,thick}]
%draw a grid in the x-y plane
\foreach \x in {-0.5,0,...,2.5}
\foreach \y in {-0.5,0,...,2.5}
{
\draw[grid] (\x,-0.5) -- (\x,2.5);
\draw[grid] (-0.5,\y) -- (2.5,\y);
}
%draw the axes
\draw[axis] (0,0,0) -- (3,0,0) node[anchor=west]{$x$};
\draw[axis] (0,0,0) -- (0,3,0) node[anchor=west]{$y$};
\draw[axis] (0,0,0) -- (0,0,3) node[anchor=west]{$z$};
%draw the top and bottom of the cube
\draw[cube] (0,0,0) -- (0,2,0) -- (2,2,0) -- (2,0,0) -- cycle;
\draw[cube] (0,0,2) -- (0,2,2) -- (2,2,2) -- (2,0,2) -- cycle;
%draw the edges of the cube
\draw[cube] (0,0,0) -- (0,0,2);
\draw[cube] (0,2,0) -- (0,2,2);
\draw[cube] (2,0,0) -- (2,0,2);
\draw[cube] (2,2,0) -- (2,2,2);
\end{tikzpicture}
\tdplotsetmaincoords{60}{125}
\begin{tikzpicture}
[tdplot_main_coords,
cube/.style={very thick,black},
grid/.style={very thin,gray},
axis/.style={->,blue,thick}]
%draw a grid in the x-y plane
\foreach \x in {-0.5,0,...,2.5}
\foreach \y in {-0.5,0,...,2.5}
{
\draw[grid] (\x,-0.5) -- (\x,2.5);
\draw[grid] (-0.5,\y) -- (2.5,\y);
}
%draw the axes
\draw[axis] (0,0,0) -- (3,0,0) node[anchor=west]{$x$};
\draw[axis] (0,0,0) -- (0,3,0) node[anchor=west]{$y$};
\draw[axis] (0,0,0) -- (0,0,3) node[anchor=west]{$z$};
%draw the top and bottom of the cube
\draw[cube] (0,0,0) -- (0,2,0) -- (2,2,0) -- (2,0,0) -- cycle;
\draw[cube] (0,0,2) -- (0,2,2) -- (2,2,2) -- (2,0,2) -- cycle;
%draw the edges of the cube
\draw[cube] (0,0,0) -- (0,0,2);
\draw[cube] (0,2,0) -- (0,2,2);
\draw[cube] (2,0,0) -- (2,0,2);
\draw[cube] (2,2,0) -- (2,2,2);
\end{tikzpicture}
In the default configuration, shown on the left, the xy plane lies in the same plane as the page. The z-axis extends down and to the left, giving the illusion of depth. This is a useful representation of a three-dimensional space as is, but gives little in the range of customization. The To specify the display orientation, use the command As suggested by the name, \tdplotsetmaincoords{60}{120}%
\tdplotsetrotatedcoords{0}{20}{0}%
\begin{tikzpicture}
[tdplot_rotated_coords,
cube/.style={very thick,black},
grid/.style={very thin,gray},
axis/.style={->,blue,thick},
rotated axis/.style={->,purple,thick}]
%draw a grid in the x-y plane
\foreach \x in {-0.5,0,...,2.5}
\foreach \y in {-0.5,0,...,2.5}
{
\draw[grid] (\x,-0.5) -- (\x,2.5);
\draw[grid] (-0.5,\y) -- (2.5,\y);
}
%draw the main coordinate frame axes
\draw[axis,tdplot_main_coords] (0,0,0) -- (3,0,0) node[anchor=west]{$x$};
\draw[axis,tdplot_main_coords] (0,0,0) -- (0,3,0) node[anchor=north west]{$y$};
\draw[axis,tdplot_main_coords] (0,0,0) -- (0,0,3) node[anchor=west]{$z$};
%draw the rotated coordinate frame axes
\draw[rotated axis] (0,0,0) -- (3,0,0) node[anchor=west]{$x'$};
\draw[rotated axis] (0,0,0) -- (0,3,0) node[anchor=south west]{$y'$};
\draw[rotated axis] (0,0,0) -- (0,0,3) node[anchor=west]{$z'$};
%draw the top and bottom of the cube
\draw[cube] (0,0,0) -- (0,2,0) -- (2,2,0) -- (2,0,0) -- cycle;
\draw[cube] (0,0,2) -- (0,2,2) -- (2,2,2) -- (2,0,2) -- cycle;
%draw the edges of the cube
\draw[cube] (0,0,0) -- (0,0,2);
\draw[cube] (0,2,0) -- (0,2,2);
\draw[cube] (2,0,0) -- (2,0,2);
\draw[cube] (2,2,0) -- (2,2,2);
\end{tikzpicture}
Here, I show the axes for both the main coordinate frame (xyz) and rotated coordinate frame (x'y'z'). Using the command ExamplesDefining Defining CoordinatesFor example, the command \tdplotsetmaincoords{60}{120}
\begin{tikzpicture}
[scale=3,
tdplot_main_coords,
axis/.style={->,blue,thick},
vector/.style={-stealth,red,very thick}]
%standard tikz coordinate definition using x, y, z coords
\coordinate (O) at (0,0,0);
%tikz-3dplot coordinate definition using r, theta, phi coords
\tdplotsetcoord{P}{.8}{55}{60}
%draw axes
\draw[axis] (0,0,0) -- (1,0,0) node[anchor=north east]{$x$};
\draw[axis] (0,0,0) -- (0,1,0) node[anchor=north west]{$y$};
\draw[axis] (0,0,0) -- (0,0,1) node[anchor=south]{$z$};
%draw a vector from O to P
\draw[vector] (O) -- (P);
\end{tikzpicture}
\tdplotsetmaincoords{60}{120}
\begin{tikzpicture}
[scale=3,
tdplot_main_coords,
axis/.style={->,blue,thick},
vector/.style={-stealth,red,very thick},
vector guide/.style={dashed,red,thick}]
%standard tikz coordinate definition using x, y, z coords
\coordinate (O) at (0,0,0);
%tikz-3dplot coordinate definition using r, theta, phi coords
\tdplotsetcoord{P}{.8}{55}{60}
%draw axes
\draw[axis] (0,0,0) -- (1,0,0) node[anchor=north east]{$x$};
\draw[axis] (0,0,0) -- (0,1,0) node[anchor=north west]{$y$};
\draw[axis] (0,0,0) -- (0,0,1) node[anchor=south]{$z$};
%draw a vector from O to P
\draw[vector] (O) -- (P);
%draw guide lines to components
\draw[vector guide] (O) -- (Pxy);
\draw[vector guide] (Pxy) -- (P);
\end{tikzpicture}
In the left diagram, it is hard to identify where the vector lies in the three-dimensional space. In the right diagram, I make use of the (automatically generated) projection coordinate Drawing Circles and ArcsTo draw a circle or arc, the \tdplotsetmaincoords{60}{120}
\begin{tikzpicture}
[scale=3,
tdplot_main_coords,
axis/.style={->,blue,thick},
vector/.style={-stealth,red,very thick},
vector guide/.style={dashed,red,thick},
angle/.style={red,thick}]
%standard tikz coordinate definition using x, y, z coords
\coordinate (O) at (0,0,0);
%tikz-3dplot coordinate definition using r, theta, phi coords
\tdplotsetcoord{P}{.8}{55}{60}
%draw axes
\draw[axis] (0,0,0) -- (1,0,0) node[anchor=north east]{$x$};
\draw[axis] (0,0,0) -- (0,1,0) node[anchor=north west]{$y$};
\draw[axis] (0,0,0) -- (0,0,1) node[anchor=south]{$z$};
%draw a vector from O to P
\draw[vector] (O) -- (P);
%draw guide lines to components
\draw[vector guide] (O) -- (Pxy);
\draw[vector guide] (Pxy) -- (P);
%draw an arc illustrating the angle defining the orientation
\tdplotdrawarc[angle]{(O)}{.35}{0}{60}{anchor=north}{$\phi$}
%define the rotated coordinate frame to lie in the "theta plane"
\tdplotsetthetaplanecoords{55}
\tdplotdrawarc[tdplot_rotated_coords,angle]{(O)}{.35}{0}{55}
{anchor=south west}{$\theta$}
\end{tikzpicture}
\tdplotsetmaincoords{55}{5}
\begin{tikzpicture}
[scale=3,
tdplot_main_coords,
curve/.style={red,densely dotted,thick}]
\coordinate (O) at (0,0,0);
\foreach \angle in {-90,-75,...,90}
{
%calculate the sine and cosine of the angle
\tdplotsinandcos{\sintheta}{\costheta}{\angle}%
%define a point along the z-axis through which to draw
%a circle in the xy-plane
\coordinate (P) at (0,0,\sintheta);
%draw the circle in the main frame
\tdplotdrawarc[curve]{(P)}{\costheta}{0}{360}{}{}
%define the rotated coordinate frame based on the angle
\tdplotsetthetaplanecoords{\angle}
%draw the circle in the rotated frame
\tdplotdrawarc[curve,tdplot_rotated_coords]{(O)}{1}{0}{360}{}{}
}
\end{tikzpicture}
The left example is an extension of the vector example shown earlier. An arc was added in the xy plane to illustrate the azimuthal angle, φ, and another arc was added to illustrate the polar angle, θ. In the right example, a series of circles are drawn to represent longitudinal and latitudinal lines on a sphere. To make these examples, a few useful helper functions are used. First, the\tdplotsetthetaplanecoords{φ} function gives a convenient way of placing rotated coordinate frame such that the x'y' plane is lined up to draw the polar angle, θ. Second, the \tdplotsinandcos{\sintheta}{\costheta}{θ} command takes the specified angle, θ (in degrees), and stores the sine and cosine value of that angle in the macros \sintheta and \costheta. Note that you can use any name in place of \sintheta and \costheta for your convenience.
Drawing SurfacesDrawing lines and curves can be great fun, but what if you wanted to represent a solid object with opaque surfaces? With proper planning and careful consideration of which surfaces are drawn first, you can render simple shapes. \tdplotsetmaincoords{60}{125}
\begin{tikzpicture}
[tdplot_main_coords,
grid/.style={very thin,gray},
axis/.style={->,blue,thick},
cube/.style={opacity=.5,very thick,fill=red}]
%draw a grid in the x-y plane
\foreach \x in {-0.5,0,...,2.5}
\foreach \y in {-0.5,0,...,2.5}
{
\draw[grid] (\x,-0.5) -- (\x,2.5);
\draw[grid] (-0.5,\y) -- (2.5,\y);
}
%draw the axes
\draw[axis] (0,0,0) -- (3,0,0) node[anchor=west]{$x$};
\draw[axis] (0,0,0) -- (0,3,0) node[anchor=west]{$y$};
\draw[axis] (0,0,0) -- (0,0,3) node[anchor=west]{$z$};
%draw the bottom of the cube
\draw[cube] (0,0,0) -- (0,2,0) -- (2,2,0) -- (2,0,0) -- cycle;
%draw the back-right of the cube
\draw[cube] (0,0,0) -- (0,2,0) -- (0,2,2) -- (0,0,2) -- cycle;
%draw the back-left of the cube
\draw[cube] (0,0,0) -- (2,0,0) -- (2,0,2) -- (0,0,2) -- cycle;
%draw the front-right of the cube
\draw[cube] (2,0,0) -- (2,2,0) -- (2,2,2) -- (2,0,2) -- cycle;
%draw the front-left of the cube
\draw[cube] (0,2,0) -- (2,2,0) -- (2,2,2) -- (0,2,2) -- cycle;
%draw the top of the cube
\draw[cube] (0,0,2) -- (0,2,2) -- (2,2,2) -- (2,0,2) -- cycle;
\end{tikzpicture}
\tdplotsetmaincoords{60}{125}
\begin{tikzpicture}[
tdplot_main_coords,
grid/.style={very thin,gray},
axis/.style={->,blue,thick},
cube/.style={very thick,fill=red},
cube hidden/.style={very thick,dashed}]
%draw a grid in the x-y plane
\foreach \x in {-0.5,0,...,2.5}
\foreach \y in {-0.5,0,...,2.5}
{
\draw[grid] (\x,-0.5) -- (\x,2.5);
\draw[grid] (-0.5,\y) -- (2.5,\y);
}
%draw the axes
\draw[axis] (0,0,0) -- (3,0,0) node[anchor=west]{$x$};
\draw[axis] (0,0,0) -- (0,3,0) node[anchor=west]{$y$};
\draw[axis] (0,0,0) -- (0,0,3) node[anchor=west]{$z$};
%draw the front-right of the cube
\draw[cube] (2,0,0) -- (2,2,0) -- (2,2,2) -- (2,0,2) -- cycle;
%draw the front-left of the cube
\draw[cube] (0,2,0) -- (2,2,0) -- (2,2,2) -- (0,2,2) -- cycle;
%draw the top of the cube
\draw[cube] (0,0,2) -- (0,2,2) -- (2,2,2) -- (2,0,2) -- cycle;
%draw dashed lines to represent hidden edges
\draw[cube hidden] (0,0,0) -- (2,0,0);
\draw[cube hidden] (0,0,0) -- (0,2,0);
\draw[cube hidden] (0,0,0) -- (0,0,2);
\end{tikzpicture}
The left example uses fill color transparency to illustrate surfaces hidden behind others. Here, the covered and back edges of the cube are distinguished by having the fill for the front surfaces drawn over top. The right example has opaque front surfaces of the cube drawn first, and then the back edges are illustrated using dashed lines. In both situations, the order of drawing depends on the perspective. If you change the orientation of the coordinate frame, you will need to be careful about whether the drawing order needs to change. In the case with dashed lines, you may even need to reassign roles to the components of the object. Another example of surface plots is the \tdplotsetmaincoords{70}{135}
\begin{tikzpicture}[scale=4,line join=bevel,tdplot_main_coords, fill opacity=.5]
\pgfsetlinewidth{.2pt}
\tdplotsphericalsurfaceplot[parametricfill]{72}{36}{sin(\tdplottheta)*cos(\tdplottheta)}{black}{\tdplotphi}%
{\draw[color=black,thick,->] (0,0,0) -- (1,0,0) node[anchor=north east]{$x$};}%
{\draw[color=black,thick,->] (0,0,0) -- (0,1,0) node[anchor=north west]{$y$};}%
{\draw[color=black,thick,->] (0,0,0) -- (0,0,1) node[anchor=south]{$z$};}%
\end{tikzpicture}
Here, I am plotting the function r = sin(θ)cos(θ), and using φ as the parameter to choose the surface fill hue. The instructions for LimitationsWhen drawing shapes in \begin{tikzpicture}
%draw a grid in the x-y plane
\foreach \x in {-0.5,0,...,2.5}
\foreach \y in {-0.5,0,...,2.5}
{
\draw[gray,very thin] (\x,-0.5) -- (\x,2.5);
\draw[gray,very thin] (-0.5,\y) -- (2.5,\y);
}
%draw the axes
\draw[->] (0,0,0) -- (3,0,0) node[anchor=west]{$x$};
\draw[->] (0,0,0) -- (0,3,0) node[anchor=west]{$y$};
\draw[->] (0,0,0) -- (0,0,3) node[anchor=west]{$z$};
%draw the top and bottom of the cube
\draw[very thick] (0,0,0) rectangle (2,2,0);
\draw[very thick] (0,0,2) rectangle (2,2,2);
%draw the edges of the cube
\draw[very thick] (0,0,0) -- (0,0,2);
\draw[very thick] (0,2,0) -- (0,2,2);
\draw[very thick] (2,0,0) -- (2,0,2);
\draw[very thick] (2,2,0) -- (2,2,2);
\end{tikzpicture}
\tdplotsetmaincoords{60}{125}
\begin{tikzpicture}[tdplot_main_coords]
%draw a grid in the x-y plane
\foreach \x in {-0.5,0,...,2.5}
\foreach \y in {-0.5,0,...,2.5}
{
\draw[gray,very thin] (\x,-0.5) -- (\x,2.5);
\draw[gray,very thin] (-0.5,\y) -- (2.5,\y);
}
%draw the axes
\draw[->] (0,0,0) -- (3,0,0) node[anchor=west]{$x$};
\draw[->] (0,0,0) -- (0,3,0) node[anchor=west]{$y$};
\draw[->] (0,0,0) -- (0,0,3) node[anchor=west]{$z$};
%draw the top and bottom of the cube
\draw[very thick] (0,0,0) rectangle (2,2,0);
\draw[very thick] (0,0,2) rectangle (2,2,2);
%draw the edges of the cube
\draw[very thick] (0,0,0) -- (0,0,2);
\draw[very thick] (0,2,0) -- (0,2,2);
\draw[very thick] (2,0,0) -- (2,0,2);
\draw[very thick] (2,2,0) -- (2,2,2);
\end{tikzpicture}
If you look carefully at the rotated diagram, you'll notice that the rectangles are drawn with the correct beginning and end points, but the overall shape conforms to the original coordinate system of the page. For More InformationFor more information, I recommend you have a look at the package, located at www.ctan.org/pkg/tikz-3dplot.About the Author:Jeff Hein is the author of the |
Latest Forum Posts
Re: Bibliography Items numbered in Order of Citations
23/05/2013 01:50, mayankmi
Spacing between LOF captions
23/05/2013 01:12, gilmour
Re: Equivalent of bounding box when compiling with pdflatex
22/05/2013 23:52, cgnieder
Re: how to don't reset the cite counter between chapters?
22/05/2013 23:38, Xire
Equivalent of bounding box when compiling with pdflatex
22/05/2013 23:18, samshort25
Re: Line Breaks in Local Layout Text Styles (XeTeX & CJK)
22/05/2013 19:47, dreamon
Re: Bibliography Items numbered in Order of Citations
22/05/2013 18:33, localghost
Bibliography Items numbered in Order of Citations
22/05/2013 18:22, mayankmi
Re: Section Numbers and Names in Header
22/05/2013 12:59, localghost
Re: Section Numbers and Names in Header
22/05/2013 12:51, TheTobinator










Comments