# Drawing a circle on a non xy-plane with TikZ

Say I'm drawing a 3D picture. Now I would like to draw a circle, perspectively correct, on the yz-plane, but TikZ does not seem to be capable of this. All one can do is drawing circles on the xy-plane. Also ellipses, which take two radii, are drawn on the xy-plane only. I know I can use rotate to rotate ellipses, but this is not really straightforward, as it involves calculating rotation angles and radii.

So is there anyway to tell TikZ to draw on a certain plane? Or is there any other fancy package fo it?

Just for illustration:

\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\draw (0,0,0) -- (0,4,0) -- (0,4,4) -- (0,0,4) -- cycle;
\draw (0,2,2) circle (2);
\end{tikzpicture}

\end{document}


which produces

Obviousely, I would like to draw the circle in the yz-plane. This would look like an ellipse, fitting perfectly in the yz-plane square (which, due to perspective looks like a parallelogram).

-
You might find my answer to this question of some use: tex.stackexchange.com/questions/31548/… –  Loop Space Oct 19 '11 at 20:19
@Andrew Stacey: Wow, worked perfectly. Care to write an answer so I can accept it? –  Tom Bombadil Oct 19 '11 at 20:44
I'm not sure that there's anything more that I would say than is already in that answer and it's the first part of that answer too so there's not a load of extra stuff to wade through to find it (except for my ... curious ... style). So it feels a bit more like a candidate for closing as duplicate (there are already two votes for such). Would you be deeply offended if I suggested that instead? –  Loop Space Oct 20 '11 at 8:34
Of course not. I have the answer I needed, and that's what counts. I think I'll accept Altermundus answer then. –  Tom Bombadil Oct 20 '11 at 11:26

Two examples of what you can draw with the 3d library. The first on has been modified because something was wrong with shade colour.

\documentclass[]{article}
\usepackage{tikz}
\usetikzlibrary{3d}

\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5pt}%

\begin{document}

\begin{tikzpicture}
[x={(-0.2cm,-0.4cm)}, y={(1cm,0cm)}, z={(0cm,1cm)},
scale=3,
fill opacity=0.80,
color={gray},bottom color=white,top color=black]

\tikzset{zxplane/.style={canvas is zx plane at y=#1,very thin}}
\tikzset{yxplane/.style={canvas is yx plane at z=#1,very thin}}

\begin{scope}[yxplane=-1]
\end{scope}

\begin{scope}[zxplane=-1]
\end{scope}

\begin{scope}[zxplane=1]
\end{scope}

\begin{scope}[yxplane=1]
\end{scope}
\end{tikzpicture}

\begin{tikzpicture}[scale=4]
\begin{scope}[canvas is zy plane at x=0]
\draw (0,0) circle (1cm);
\draw (-1,0) -- (1,0) (0,-1) -- (0,1);
\end{scope}

\begin{scope}[canvas is zx plane at y=0]
\draw (0,0) circle (1cm);
\draw (-1,0) -- (1,0) (0,-1) -- (0,1);
\end{scope}

\begin{scope}[canvas is xy plane at z=0]
\draw (0,0) circle (1cm);
\draw (-1,0) -- (1,0) (0,-1) -- (0,1);
\end{scope}
\end{tikzpicture}
\end{document}


-
Thanks for this terrific example. However, I am not able to figure where the gray color shading in the first picture is coming from? I changed all the colors to various other colors so I can correlate the code with the lines, and nothing I seem produces a color other than gray for the shading. Let me know if you think I should post a question, or did I miss something obvious. –  Peter Grill Oct 20 '11 at 3:42
@Peter You are right, I need to modify the example. I created this example before pgf 2.1 and now it's necessary to use something like  \path[bottom color=white,top color=black,draw] (-1,-1) rectangle (1,1);. –  Alain Matthes Oct 20 '11 at 5:33
@Peter I modified the example, now you can modify the color or add shading only for the circle. –  Alain Matthes Oct 20 '11 at 5:43
I've never heard of the 3D library before, and now I see that it is in my TikZ/PGF system. Wow! I need to learn what it does. –  Loop Space Oct 20 '11 at 8:22
Did a bit of investigating. I think it's worth pointing out that the 3D library sets up some coordinate transformations and these apply to circles even when the radii are specified as dimensions. So the x=...,y=...,z=... apply to circle[radius=2cm] because the values of the x,y,z vectors are used to define a transformation. –  Loop Space Oct 20 '11 at 8:46

pst-3dplot forms part of the pstricks suite and provides macros to print regular 2D stuff on planes in 3D (amongst other things).

Here is a minimal example the plots a circle on the 3 orthogonal planes: xy, xz and yz:

\documentclass{article}
\usepackage{graphicx}% http://ctan.org/pkg/graphicx
\usepackage{pst-3dplot}% http://ctan.org/pkg/pst-3dplot
\usepackage{xcolor}% http://ctan.org/pkg/xcolor
\begin{document}

\begin{pspicture}(5,5)
\psset{linewidth=0.5pt,linecolor=black!50,unit=2cm}%
\pstThreeDLine(-2,0,0)(2,0,0)% x-axis
\pstThreeDLine(0,-2,0)(0,2,0)% y-axis
\pstThreeDLine(0,0,-2)(0,0,2)% z-axis

\psset{linewidth=1pt,linecolor=black}
\pstPlanePut[plane=xy](0,0,0){%
\pscircle[linestyle=solid](0,0){2cm}%
\rput{90}(-2.5cm,2.5cm){XY-plane}%
}
\pstPlanePut[plane=xz](0,0,0){%
\pscircle[linestyle=dashed](0,0){2cm}%
\rput{0}(-2.5cm,2.5cm){\reflectbox{XZ-plane}}%
}
\pstPlanePut[plane=yz](0,0,0){%
\pscircle[linestyle=dotted](0,0){2cm}%
\rput{0}(-2.5cm,2.5cm){YZ-plane}%
}
\end{pspicture}

\end{document}


The only reason for including graphicx was for \reflectbox.

Perspective of the object(s) can be modified using different values for Alpha or Beta (two components of spherical coordinates) or by using rotation angles \RotX, \RotY and \RotZ. See the package documentation for some more examples.

-