74

I'm trying to draw some simple 3D objects in TikZ. I can do spheres and cuboids okay, but I'm struggling with circular cylinders: When rotating the scene (I use tikz-3dplot for this), the end faces have to be transformed from circles into ellipses. Using \draw ... circle; doesn't work, because a path defined like that will always stay circular. If I use \draw ... to ... to ... constructs with the in and out set to adequate values, I can "simulate" circles that transform slightly better: The "corner" points of the circle end up in the right place, but the in and out angles are not transformed appropriately.

What can I do to get "3D circles", i.e. circles that turn into ellipses when the 3D space is rotated?

Here's an example showing the problem:

\documentclass{article}
\usepackage{tikz}
\usepackage{tikz-3dplot}

\begin{document}

\tdplotsetmaincoords{40}{20}
\noindent The vertical cylinder walls\\
are not aligned properly \ldots

\begin{tikzpicture}[tdplot_main_coords]
\draw (0,0) circle [radius=1cm];
\end{tikzpicture}
\begin{tikzpicture}[tdplot_main_coords]
\draw (1,0,2) -- (0,1,2) -- (-1,0,2) -- (0,-1,2) -- cycle
    (-1,0,0) -- (0,-1,0) -- (0,-1,2) -- (-1,0,2) -- cycle
    (1,0,2) -- (0,-1,2) -- (0,-1,0) -- (1,0,0) -- cycle;
\end{tikzpicture}
\begin{tikzpicture}[tdplot_main_coords]
\draw [in=225, out=-45, relative] (0.5,0.5,2) to (-0.5,0.5,2) to (-0.5,-0.5,2) to (0.5,-0.5,2) to (0.5,0.5,2)
    (0.5,0.5,0) to (-0.5,0.5,0) to (-0.5,-0.5,0) to (0.5,-0.5,0) to (0.5,0.5,0)
    (0.5,0.5,2) -- (0.5,0.5,0)
    (-0.5,-0.5,2) -- (-0.5,-0.5,0);
\end{tikzpicture}

\tdplotsetrotatedcoords{40}{50}{-30}\vspace{0.5cm}
\noindent\ldots and the support points\\don't get transformed correctly

\begin{tikzpicture}[tdplot_rotated_coords]
\draw (0,0) circle [radius=1cm];
\end{tikzpicture}
\begin{tikzpicture}[tdplot_rotated_coords]
\draw  (1,0,2) -- (0,1,2) -- (-1,0,2) -- (0,-1,2) -- cycle
    (-1,0,0) -- (0,-1,0) -- (0,-1,2) -- (-1,0,2) -- cycle
    (1,0,2) -- (0,-1,2) -- (0,-1,0) -- (1,0,0) -- cycle;
\end{tikzpicture}
\begin{tikzpicture}[tdplot_rotated_coords]
\draw [in=225, out=-45, relative] (0.5,0.5,2) to (-0.5,0.5,2) to (-0.5,-0.5,2) to (0.5,-0.5,2) to (0.5,0.5,2)
    (0.5,0.5,0) to (-0.5,0.5,0) to (-0.5,-0.5,0) to (0.5,-0.5,0) to (0.5,0.5,0)
    (0.5,-0.5,2) -- (0.5,-0.5,0)
    (-0.5,0.5,2) -- (-0.5,0.5,0);
\end{tikzpicture}

\end{document}
Stefan Pinnow
  • 29,535
Jake
  • 232,450

3 Answers3

100

What a good question! I'm surprised that no-one's asked this before ...

There are two pieces to the puzzle here: getting the end-circles right, and deciding where to draw the edges. The first turns out to actually be easy - if you know what to look for in the TikZ manual. The second takes a little bit of maths, but not too much.

Let's deal with the first. When you draw a "circle" in TikZ, you are allowed to specify the radii. So you can say \draw (0,0) circle[x radius=2cm, y radius=3cm];. This will produce an ellipse with major axis straight up and minor axis in the horizontal. Now, in TikZ, you are allowed to specify dimensionless distances. Let's imagine the conversation:

You: Draw me a circle with x-radius 2cm and y-radius 3cm.

TikZ: Yes, Sir! Right away, Sir!

You: Draw me a circle with x-radius 2 and y-radius 3.

TikZ: Yes, Sir! Righta ... hang on! "x-radius 2"? 2 what? 2 apples? 2 oranges?

You: 2 in the x direction.

TikZ: (getting a bit querulous) Yes, but where's that?

You: The x direction. Over that way.

TikZ: Could you be more specific?

You: Okay, let's say that the x direction is 1cm along and 0.5cm up. Will that do?

TikZ: Yes. pause Now, what about y?

The point of that silliness is that when you specify a dimenionless length, TikZ interprets it as that number times the appropriate vector. So x radius=2 means "set the x-axis of this ellipse to be twice the current x vector". But we have complete control over what that x vector is! So if we tell TikZ that x means something else, it will use it.

The great thing about this is that if you say that x={(1cm,1cm)}, y={(0cm,1cm)} then TikZ will draw something approximating (cos θ, cos θ + sin θ), which is exactly what you want if you happen to be looking at a circle in such a way that the x-axis is lying actually along the diagonal.

Compare and contrast:

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[ultra thick] (0,0) circle[x radius=2,y radius=2];
\draw[ultra thick] (4,0) circle[x={(1cm,1cm)},y={(0cm,1cm)},x radius=2,y radius=2];
\draw[ultra thick] (8,0) circle[x={(1cm,1cm)},y={(0cm,1cm)},x radius=2cm,y radius=2cm];
\end{tikzpicture}
\end{document}

circles

The middle one is the right one here.

So for the ends of the cylinder, the trick is simple: draw your ends as honest circles using dimensionless lengths. Then adjust the x and y values to suit. (There is one warning here: if you use dimensionless lengths to set the x and y values then they are set in terms of the current x and y. Although this might be what you want in theory, if you write x={(1,1)}, y={(1,-1)} then the x that is used to set the y is the new one, not the old one.) With dimensionless radii, it is also possible to use transformations correctly.

Now to the edges of the cylinder. The difficulty here is that the place to draw the edges is determined by their eventual location on the page. So to decide where to draw the edges we need to find out exactly where the cylinder ends will be drawn and find their extreme points. What we mean by "extreme" here is that we want to draw a tangent line along the direction of the cylinder. So we take the orthogonal direction to the direction of the cylinder as rendered on the page and look for the extreme points of the deformed circles as rendered on the page in that direction.

Fortunately, this is quite easy. Our deformed circle has the equation cos θ x + sin θ y whether in 3D space or on the page. So we just need to work out what x and y are on the page. We can do that by "drawing" a vector of one unit in the x direction and recording the actual coordinates. To get the direction orthogonal to the direction of the cylinder, we "draw" a vector in the direction of the cylinder and record its coordinates as well. Let's call that one z. Then we take its orthogonal direction, say w. So now we examine the function cos θ w·x + sin θ w·y and look for its extreme values. Differentiating and setting equal to zero, we get -sin θ w·x + cos θ w·y = 0. Rearranging, we get tan θ = w·y/w·x. Since we only want sin θ and cos θ we don't actually need to work out θ itself (though we can do so if we need it for something else). Note that we use these in the original coordinate system.

Putting all that together, if we assume that our cylinder should go along the z-direction (whatever that happens to be) and the cross-section be in the x-y plane (again, whatever that happens to be) then working out the mathematics is the following code.

\path (1,0,0);
\pgfgetlastxy{\cylxx}{\cylxy}
\path (0,1,0);
\pgfgetlastxy{\cylyx}{\cylyy}
\path (0,0,1);
\pgfgetlastxy{\cylzx}{\cylzy}
\pgfmathsetmacro{\cylt}{(\cylzy * \cylyx - \cylzx * \cylyy)/ (\cylzy * \cylxx - \cylzx * \cylxy)}
\pgfmathsetmacro{\ang}{atan(\cylt)}
\pgfmathsetmacro{\ct}{1/sqrt(1 + (\cylt)^2)}
\pgfmathsetmacro{\st}{\cylt * \ct}

If this were in a macro, we could be a bit more efficient and use \tikz@scan@one@point instead of drawing a point and getting the last x-y values.

Once we have those values, drawing the cylinder itself is quite easy. Here's an example.

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\begin{scope}[x={(.7cm,-.3cm)}]
\path (1,0,0);
\pgfgetlastxy{\cylxx}{\cylxy}
\path (0,1,0);
\pgfgetlastxy{\cylyx}{\cylyy}
\path (0,0,1);
\pgfgetlastxy{\cylzx}{\cylzy}
\pgfmathsetmacro{\cylt}{(\cylzy * \cylyx - \cylzx * \cylyy)/ (\cylzy * \cylxx - \cylzx * \cylxy)}
\pgfmathsetmacro{\ang}{atan(\cylt)}
\pgfmathsetmacro{\ct}{1/sqrt(1 + (\cylt)^2)}
\pgfmathsetmacro{\st}{\cylt * \ct}
\fill[red] (\ct,\st,0) -- ++(0,0,-8) arc[start angle=\ang,delta angle=180,radius=1] -- ++(0,0,8) arc[start angle=\ang+180,delta angle=-180,radius=1];
\begin{scope}[every path/.style={ultra thick}]
\draw (0,0,0) circle[radius=1];
\draw[->] (0,0,0) -- (1,0,0);
\draw[->] (0,0,0) -- (0,1,0);
\draw (\ct,\st,0) -- ++(0,0,-8);
\draw (-\ct,-\st,0) -- ++(0,0,-8);
\draw (\ct,\st,-8) arc[start angle=\ang,delta angle=180,radius=1];
\draw[dashed] (\ct,\st,-8) arc[start angle=\ang,delta angle=-180,radius=1];
\end{scope}
\end{scope}
\end{tikzpicture}
\end{document}

This produces the following:

cylinder

If we want to get a bit fancy, we can put some shading in the inside of the cylinder by filling it with successively darker colours. Simply put

\foreach \ztint in {0,.1,...,1} {
\pgfmathsetmacro{\tint}{(\ztint/2 + .5)*100}
\fill[blue!\tint] (\ct,\st,-\ztint) -- ++(0,0,-4) arc[start %% angle=\ang,delta angle=-180,radius=1] -- ++(0,0,4) arc[start %% angle=\ang+180,delta angle=180,radius=1];
}

before the line that starts \fill[red]. This produces:

cylinder with shading

If we change the definition of the vectors, say to [x={(.7cm,.3cm)},z={(.5cm,-.5cm)}], we find that it transforms as it should:

another cylinder

The usual caveats about using TikZ to draw 3D drawings apply here: it's up to you, the user, to decide what gets drawn on top of what and to arrange your drawing appropriately.

Andrew Stacey
  • 153,724
  • 43
  • 389
  • 751
  • In your first example ("the middle one was is the right one"), I had to add \begin{center}\begin{tikzpicture} and then \end{tikzpicture}\end{center} around. Shouldn't (a, b) do the job? – Peter Jones Oct 17 '11 at 14:32
  • @PeterJones: I don't understand your comment. My first example compiles as-is, doesn't it? It's a bit wide for the page, is that what you mean? I also don't understand your last sentence, (a,b) refers to what? I'm probably being dense here, but you've gotten me confused! – Andrew Stacey Oct 17 '11 at 18:05
  • As your answer potentially is used as a tutorial by viewers, and I was one of them, I thought perhaps my comment could be useful. I tested your codes and noticed it was in the bad box on the right side of the page. I appreciate that answers here are dense and directed towards main points, rather than just a ready made solution. Viewers should supplement it with their own efforts too. Comments also are towards such efforts. I did not understand what are (0, 0) and (4, 0) and (8, 0) in each line [by (a, b) I mean them]. Do they position the circles in the page? – Peter Jones Oct 18 '11 at 11:12
  • 1
    @PeterJones: Your expanded comment is definitely useful! As that particular example was intended just to illustrate the previous paragraph, I didn't consider such matters. Perhaps with that example it would actually have been better not to have a "complete document". So, yes: to get a nice big picture I ignored overfull boxes, and the (a,b)s are just to place the circles next to each other. – Andrew Stacey Oct 18 '11 at 11:22
  • 2
    This reads very fine. Can anyone explain, why \draw (.75,1,.5) circle[x={(1,0,0)},y={(0,0,1)},radius=.125] is not used for a 3d-circle in the x/z-plane (for example)?
    This is also nice readable, isn't it?
    – Bastian Ebeling Aug 20 '12 at 13:05
  • @BastianEbeling I'm not sure what you mean. If you mean that that's an alternative way to get a circle in the x-z plane, then all I'd say is that there are many ways to get the same output. If you mean that you'd expect that to draw a circle in the x-z plane but it doesn't, then I suggest that you ask a fresh question about it. – Andrew Stacey Aug 20 '12 at 14:08
  • @AndrewStacey - I only wanted to present that alternative - without criticizing you. I further thaught "my alternative" is just nice readable and rememberable thus worth being mentioned here. – Bastian Ebeling Aug 21 '12 at 05:53
  • 1
    @BastianEbeling No criticism taken. It's always good to know that there are many ways to accomplish a given task. – Andrew Stacey Aug 21 '12 at 08:05
  • @AndrewStacey Well done. Can you elaborate on how you came up with the different equations used to define the macros cylt, ct, st, and ang? Can you refer me to a book that explains this approach? – tush Nov 02 '22 at 13:17
  • @tush the explanation is in the paragraph starting "Fortunately...", and I don't know of a relevant book - it's an application of vector geometry so a general introduction to vectors would give you the pieces but I don't know of one that would exactly cover ghis application. – Andrew Stacey Nov 03 '22 at 00:35
31

Here is some code I made some time ago (uses gnuplot). You probably adapt it to draw simpler cylinders...

\tikzset{math3d/.style= {x={(1cm,0cm)}, y={(0.353cm,0.353cm)}, z={(0cm,1cm)}}}
\begin{tikzpicture}[math3d]
    \newcommand{\n}{11}
    \newcommand{\h}{2}
    \newcommand{\rl}{2}
    \newcommand{\rh}{1}
    \path[draw,fill=white] plot[domain=0:2*pi,samples=4*\n] ({\rl*cos(\x r)}, {\rl*sin(\x r)}, 0);
    \foreach \t in {1,...,\n} {
        \filldraw[fill=gray] ({\rl*cos(2*\t*pi/\n r)},{\rl*sin(2*\t*pi/\n r)},0) -- ({\rl*cos((2*\t+1)*pi/\n r)},{\rl*sin((2*\t+1)*pi/\n r)},0) -- ({\rh*cos((2*\t+1)*pi/\n r)},{\rh*sin((2*\t+1)*pi/\n r)},\h) -- ({\rh*cos(2*\t*pi/\n r)},{\rh*sin(2*\t*pi/\n r)},\h) -- cycle;
    }
    \path[draw,fill=white] plot[domain=0:2*pi,samples=4*\n] ({\rh*cos(\x r)}, {\rh*sin(\x r)}, \h);
    \begin{scope}[dashed,->,opacity=0.7]
        \draw (0,0,0) -- (\rl,0,0) node[below] {$x$};
        \draw (0,0,0) -- (0,\rl,0) node[right] {$y$};
        \draw (0,0,0) -- (0,0,\h) node[above] {$z$};
    \end{scope}
\end{tikzpicture}

enter image description here

For example, start by setting \rl and \rh to the same value (low radius and high radius). \n is the number of "panes", and \h is the height of the cylinder.

Note that it isn't that easy to know where to draw the apparent vertical lines of the cylinders. One can surely compute the angle at which they occur, but it is not straightforward. The solution above enables to bypass this problem.

percusse
  • 157,807
FelixCQ
  • 1,443
  • 1
  • 13
  • 11
4

Using sketch you can get:

cylinders

the code (take from the documentation) is

def n_cyl_segs 20 def n_views 10 def I [1,0,0]
def endopts [fill=blue]
repeat { n_views, rotate(180/n_views, [I]) then translate([I] * 2.1) }
sweep[endopts]{ n_cyl_segs<>, rotate(360/n_cyl_segs, [0,1,0]) }
line[fill=red](1,-1)(1,1)
global { language tikz }

IMHO a very useful reference for drawing objects in the correct perspective is: Norling - Perspective made easy

vi pa
  • 3,394