# Define different planes for drawings in TikZ

I want to draw figures on different planes in TikZ. Consider an example of drawing a simple rectangle in the xy plane at z=0 and another rectangle in the xy plane at z=2. Generally, the planes I am considering will also include some rotations (see below for details) in addition to displacements from the origin O. I have seen this post which uses different canvases, but I'm not sure how to adapt that.

I am looking to use the scope environment that allows me to displace a plane and rotate it. That way any figures drawn within that scope appears within that plane.

Detail on the rotations considered here The rotations referred to here are along the axes x,y,z. For example, consider I have two planes: one that has the origin O = (0,0,0) at the bottom left corner and a second plane that has the origin at O' = (2,0,0). Rotations of plane 2 are along the axes that intersect the point O.

Here is a MWE that sets up the basic problem:

\documentclass{standalone}

\usepackage{tikz}
\usepackage{tikz-3dplot}
\usetikzlibrary{arrows.meta}

\begin{document}
\tdplotsetmaincoords{60}{130}

\begin{tikzpicture}[scale=2,tdplot_main_coords]

\draw[thick,->] (0,0,0) -- (5,0,0) node[right]{$x$};
\draw[thick,->] (0,0,0) -- (0,5,0) node[above]{$y$};
\draw[thick,->] (0,0,0) -- (0,0,5) node[below left]{$z$};

%Circle in xy plane at z=0
\filldraw[fill=blue!40!white, draw=black] (0,0,0) rectangle (4,4,0);

%Another circle at xy plane at z=2
%\filldraw[fill=blue!40!white, draw=black] (0,0) rectangle (4,4);

\end{tikzpicture}
\end{document}

• Opps, forgot that I have super powers with tikz-pgf and the question gets closed after a single vote as a duplicate. I will try to remdy that, and in the mean time if you don't think that Define different planes for drawings in TikZ sufficiently answers your question, please include a comment to that affect. Jun 4 '20 at 20:41
• Can you make the question more precise? What precisely is a rotation about a point in 3d? You can rotate about an axis in 3d, rotations about points are only really defined in 2d. And then please add an explicit example for what you want to rotate about what. (Rotations about arbitrary axes may not be explicitly implemented in tikz-3dplot but that's not very difficult to do, I think.)
– user194703
Jun 4 '20 at 20:42
• @Schrödinger'scat, thanks for the prompt. I've update the question with more info.
– Sid
Jun 4 '20 at 21:06
• At this point it seems that all you need is one of the answers of this question plus a translation, shift={(O')}.
– user194703
Jun 4 '20 at 21:16

In order to rotate and shift you need only one of the answers to this question for the rotation, and add a shift. To the best of my knowledge both answers work fine.

However, one may want to go another way. One of the answers does not allow the user to accumulate transformations. The other answer does that, but at the expense of keeping track of the rotation matrix. This works fine as long as the user does not add further transformations by other means. So here is a third approach in which the current basis vectors get used to allow the user to stack transformations. The keys introduced are rotate about x axis and so on. Crucially, they do not rely on tikz-3dplot, they also work if you use the 3d view of the perspective library1, say. These preparations allow you to do something like

\begin{scope}[rotate about x axis=-20,canvas is xy plane at z=0,
shift={(O')}]
%Circle in transformed xy plane at z=0
\end{scope}


Full code:

\documentclass[tikz,border=3mm]{standalone}
\usepackage{tikz-3dplot}
\usetikzlibrary{arrows.meta}
\makeatletter
\def\tikz@td@retrieve@current@basis{%
\pgfmathsetmacro{\tikz@td@currentxx}{\pgf@xx/1cm}%
\pgfmathsetmacro{\tikz@td@currentxy}{\pgf@xy/1cm}%
\pgfmathsetmacro{\tikz@td@currentyx}{\pgf@yx/1cm}%
\pgfmathsetmacro{\tikz@td@currentyy}{\pgf@yy/1cm}%
\pgfmathsetmacro{\tikz@td@currentzx}{\pgf@zx/1cm}%
\pgfmathsetmacro{\tikz@td@currentzy}{\pgf@zy/1cm}%
\pgfmathsetmacro{\tikz@td@currentxz}{(\tikz@td@currentyx)*(\tikz@td@currentzy)-(\tikz@td@currentzx)*(\tikz@td@currentyy)}%
\pgfmathsetmacro{\tikz@td@currentyz}{(\tikz@td@currentzx)*(\tikz@td@currentxy)-(\tikz@td@currentxx)*(\tikz@td@currentzy)}%
\pgfmathsetmacro{\tikz@td@currentzz}{(\tikz@td@currentxx)*(\tikz@td@currentyy)-(\tikz@td@currentyx)*(\tikz@td@currentxy)}%
}
\tikzset{rotate about z axis/.code={%
\tikz@td@retrieve@current@basis
\pgfmathsetmacro{\newxx}{(\tikz@td@currentxx)*cos(#1)+(\tikz@td@currentxy)*sin(#1)}%
\pgfmathsetmacro{\newxy}{-1*(\tikz@td@currentxx)*sin(#1)+(\tikz@td@currentxy)*cos(#1)}%
\pgfmathsetmacro{\newyx}{(\tikz@td@currentyx)*cos(#1)+(\tikz@td@currentyy)*sin(#1)}%
\pgfmathsetmacro{\newyy}{-1*(\tikz@td@currentyx)*sin(#1)+(\tikz@td@currentyy)*cos(#1)}%
\tikzset{x={(\newxx cm,\newxy cm)},y={(\newyx cm,\newyy cm)},z={(\tikz@td@currentzx cm,\tikz@td@currentzy cm)}}%
},rotate about y axis/.code={%
\tikz@td@retrieve@current@basis
\pgfmathsetmacro{\newxx}{(\tikz@td@currentxx)*cos(#1)+(\tikz@td@currentxz)*sin(#1)}%
\pgfmathsetmacro{\newzx}{(\tikz@td@currentzx)*cos(#1)+(\tikz@td@currentzz)*sin(#1)}%
\tikzset{x={(\newxx cm,\tikz@td@currentxy cm)},
y={(\tikz@td@currentyx cm,\tikz@td@currentyy cm)},z={(\newzx cm,\newzy cm)}}%
},,rotate about x axis/.code={%
\tikz@td@retrieve@current@basis
\pgfmathsetmacro{\newyy}{(\tikz@td@currentyy)*cos(#1)+(\tikz@td@currentyz)*sin(#1)}%
\pgfmathsetmacro{\newzy}{(\tikz@td@currentzy)*cos(#1)+(\tikz@td@currentzz)*sin(#1)}%
\tikzset{x={(\tikz@td@currentxx cm,\tikz@td@currentxy cm)},
y={(\tikz@td@currentyx cm,\newyy cm)},z={(\tikz@td@currentzx cm,\newzy cm)}}%
}}
\makeatother
\begin{document}
\tdplotsetmaincoords{60}{130}

\begin{tikzpicture}[scale=2,tdplot_main_coords,>=Stealth]

\path (0,0,0) coordinate (O) (2,0,0) coordinate (O');

\begin{scope}[canvas is xy plane at z=0]
%Circle in xy plane at z=0
\end{scope}

\begin{scope}[rotate about x axis=-20,canvas is xy plane at z=0,
shift={(O')}]
%Circle in transformed xy plane at z=0
\end{scope}
\draw[thick,->] (0,0,0) -- (5,0,0) node[pos=1.05]{$x$};
\draw[thick,->] (0,0,0) -- (0,5,0) node[pos=1.05]{$y$};
\draw[thick,->] (0,0,0) -- (0,0,5) node[pos=1.05]{$z$};

\end{tikzpicture}
\end{document}


1This statement refers to the orthographic transformations introduced by this library. It does not apply to the perspective view.

• Great! How can the perspective view also be changed?
– Sid
Jun 5 '20 at 17:30
• @Sid You can change the orthographic view angles with tikz-3dplot or also the perspective library. Doing a rotation in perspective view is unfortunately way harder. The relative orientation of the view angles and the p, q and r vectors is important. So the short answer is that it would be a lot of work. However, you are not using a perspective view here.
– user194703
Jun 5 '20 at 17:42