# TikZ: two ellipses in different planes

How can show that two ellipses are in different planes?

\documentclass[convert = false, border = 1cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\pgfmathsetmacro{\as}{2};
\pgfmathsetmacro{\bs}{1.95};
\pgfmathsetmacro{\cs}{sqrt(\as^2 - \bs^2)}
\pgfmathsetmacro{\al}{3};
\pgfmathsetmacro{\bl}{2.25};
\pgfmathsetmacro{\cl}{sqrt(\al^2 - \bl^2)}
\pgfmathsetmacro{\xs}{abs(\cs - \cl)}

\draw (0, 0) ellipse [x radius = \as cm, y radius = \bs cm];
\draw (\xs, 0) ellipse [x radius = \al cm, y radius = \bl cm];

\filldraw[black] (-\cs, 0) circle [radius = .1cm];

\filldraw[black] (-\cl + \xs, 0) circle [radius = .1cm];
\end{tikzpicture}
\end{document}


From the image, we see that both ellipses are in the same plane. How can I rotate the small ellipse to make it appear as if the smaller ellipse is in a different plane?

Using rotate around doesn't achieve that look.

Edit 2:

I am a little hesitant about using xslant and yslant since it appears that the ellipse is being shifted and stretched.

Here is a poor picture(my phone camera flash refused to work) of two ellipse in different planes.

If adjust my smaller ellipse, it stretches and appears to shift dramatically.

From the image below, the focus appears to be in the center of smaller ellipse now and it has elongated.

Edit:

So I found this post Why isn't the arc drawn on the good plane using tikz-3dplot in Tait-Bryan convention but I don't fully understand the code. However, the poster was able to rotate ellipse and have a better visual appeal and the poster could define the plane it is in such as xy, yz, and xz. How could I adapt this code to my situation?

-
Hi. I was the author of the post pointed out in your las edit. The topic of this post was about tikz-3dplot which works like a charm for what you want to do if you don't try to mess up with him. I tried, so that's why the post. I'll add an answer with tikz-3dplot. – Vser Jan 5 '15 at 15:29

One possibility would be to use xslant, yslant; the effect is better is one draws some containing planes:

\documentclass[convert = false, border = 1cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\pgfmathsetmacro{\as}{2};
\pgfmathsetmacro{\bs}{1.95};
\pgfmathsetmacro{\cs}{sqrt(\as^2 - \bs^2)}
\pgfmathsetmacro{\al}{3};
\pgfmathsetmacro{\bl}{2.25};
\pgfmathsetmacro{\cl}{sqrt(\al^2 - \bl^2)}
\pgfmathsetmacro{\xs}{abs(\cs - \cl)}

\begin{scope}[xslant=1,yslant=-1.2]
\draw (0, 0) ellipse [x radius = \as cm, y radius = \bs cm];
\draw[blue] (-2.5,-2.5) rectangle (3,2.5);
\end{scope}
\begin{scope}[xslant=0.2,yslant=-1.2]
\draw[red] (\xs, 0) ellipse [x radius = \al cm, y radius = \bl cm];
\draw[green] (-3,-2.5) rectangle (5.5,2.5);
\end{scope}
\filldraw[black] (-\cs, 0) circle [radius = .1cm];

\filldraw[black] (-\cl + \xs, 0) circle [radius = .1cm];
\end{tikzpicture}
\end{document}


A brief description of xslant and yslant:

\documentclass{article}
\usepackage[margin=3cm]{geometry}
\usepackage{amsmath}

\begin{document}

\verb|xslant| is the high-level version of \verb|\pgftransformxslant|. In \verb|pgfcoretransformations.code.tex| one finds
\begin{verbatim}
\def\pgftransformxslant#1{\pgftransformcm{1.0}{0}{#1}{1.0}{\pgfpointorigin}}
\end{verbatim}
where \verb|\pgftransformcm{<a>}{<b>}{<c>}{<d>}{<coordinate>}| is the low-level equivalent to
\begin{verbatim}
cm={<a>,<b>,<c>,<d>,<coordinate>}
\end{verbatim}
which has the following effect: if \verb|<coordinate>| specifies the point $(t_x,t_y)$, a given point $(x,y)$ will be transformed in $(x',y')$, where
$\begin{bmatrix} x' \\ y' \end{bmatrix} = \begin{bmatrix} a & c \\ b & d \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix} + \begin{bmatrix} t_x \\ t_y \end{bmatrix}.$
In particular, for \verb|xslant=k|, we have
$\begin{bmatrix} x' \\ y' \end{bmatrix} = \begin{bmatrix} 1 & k \\ 0 & 1 \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix} + \begin{bmatrix} t_x \\ t_y \end{bmatrix}$
and from here,
\begin{align*}
x' &= x + ky + t_x, \\
y' &= y + t_y.
\end{align*}

Analogously, one can ontain the transformation associated to \verb|yslant|, taking into account the following definition:
\begin{verbatim}
\def\pgftransformyslant#1{\pgftransformcm{1.0}{#1}{0}{1.0}{\pgfpointorigin}}
\end{verbatim}

\end{document}


-
@dustin please see my updated answer. If you compile the last code, you'll get the description for xslant (corresponding to the last attached image). For yslant one has a similar definition (that can be easily deduced from my answer). – Gonzalo Medina Jul 12 '13 at 3:26
@dustin I think I messed up the entries in the matrix multiplicatio. I'll review it in the morning; now it's bed time for me. – Gonzalo Medina Jul 12 '13 at 3:47
@GonzaloMedina It's a mistake of the manual. The matrix should be transposed. – percusse Jul 12 '13 at 5:47
I am not too sure about this method. It looks like the location of the ellipse is changing and the ellipse is getting stretched severely in some instances. – dustin Jul 12 '13 at 8:54
@percusse yes, I noticed that. Thanks. – Gonzalo Medina Jul 12 '13 at 13:47

This solution requires you to specify two ellipses and am "intersection line". I tried to do this in 3D at first, but TikZ is not great at that. The solution uses pgfkeys for a convineant key-value interface.

With the stndard configuration it look like this:

The macro divides the picture in a "right part" and a "left part", so the intersetion line has to start and end outside of both ellipses:

Then it draws the "back parts" first and the "front parts" afterwards:

## Code

\documentclass[tikz,border=2mm]{standalone}

\begin{document}

\tikzset{%
threedellipsesopt/.is family,%
threedellipsesopt,%
intersection start/.initial={-0.5,-2},%
intersection end/.initial={1,4},%
ellipse one center/.initial={-1,1},%
ellipse two center/.initial={1,2},%
ellipse one rotation/.initial=30,%
ellipse two rotation/.initial=-50,%
ellipse one fill/.initial=blue!50!cyan,%
ellipse two fill/.initial=orange!50!yellow,%
ellipse one draw/.initial=blue!50!black,%
ellipse two draw/.initial=orange!50!black,%
opacity/.initial=0.5,%
}

\newcommand{\ellkey}[1]% access a specific key by name
{\pgfkeysvalueof{/tikz/threedellipsesopt/#1}}

\newcommand{\threedellipses}[1]{
\tikzset{threedellipsesopt,#1} % Process Keys passed to command
\path (\ellkey{intersection start}) -- (\ellkey{intersection end});
\path[opacity=\ellkey{opacity},draw=\ellkey{ellipse one draw},rotate=\ellkey{ellipse one rotation}] (\ellkey{ellipse one center}) circle (\ellkey{ellipse one radius a} and \ellkey{ellipse one radius b});
\path[opacity=\ellkey{opacity},draw=\ellkey{ellipse two draw},rotate=\ellkey{ellipse two rotation}] (\ellkey{ellipse two center}) circle (\ellkey{ellipse two radius a} and \ellkey{ellipse two radius b});
\begin{scope}
\clip (current bounding box.north west) -| (\ellkey{intersection end}) -- (\ellkey{intersection start}) |- (current bounding box.south west) -- cycle;
\clip[rotate=\ellkey{ellipse one rotation}] (\ellkey{ellipse one center}) circle (\ellkey{ellipse one radius a}*1cm-0.2pt and \ellkey{ellipse one radius b}*1cm-0.2pt);
\fill[opacity=\ellkey{opacity},\ellkey{ellipse one fill}] (current bounding box.north east) rectangle (current bounding box.south west);
\end{scope}
\begin{scope}
\clip (current bounding box.north east) -| (\ellkey{intersection end}) -- (\ellkey{intersection start}) |- (current bounding box.south east) -- cycle;
\clip[rotate=\ellkey{ellipse two rotation}] (\ellkey{ellipse two center}) circle (\ellkey{ellipse two radius a}*1cm-0.2pt and \ellkey{ellipse two radius b}*1cm-0.2pt);
\fill[opacity=\ellkey{opacity},\ellkey{ellipse two fill}] (current bounding box.north east) rectangle (current bounding box.south west);
\end{scope}
\begin{scope}
\clip (current bounding box.north west) -| (\ellkey{intersection end}) -- (\ellkey{intersection start}) |- (current bounding box.south west) -- cycle;
\clip[rotate=\ellkey{ellipse two rotation}] (\ellkey{ellipse two center}) circle (\ellkey{ellipse two radius a}*1cm-0.2pt and \ellkey{ellipse two radius b}*1cm-0.2pt);
\fill[opacity=\ellkey{opacity},\ellkey{ellipse two fill}] (current bounding box.north east) rectangle (current bounding box.south west);
\end{scope}
\begin{scope}
\clip (current bounding box.north east) -| (\ellkey{intersection end}) -- (\ellkey{intersection start}) |- (current bounding box.south east) -- cycle;
\clip[rotate=\ellkey{ellipse one rotation}] (\ellkey{ellipse one center}) circle (\ellkey{ellipse one radius a}*1cm-0.2pt and \ellkey{ellipse one radius b}*1cm-0.2pt);
\fill[opacity=\ellkey{opacity},\ellkey{ellipse one fill}] (current bounding box.north east) rectangle (current bounding box.south west);
\end{scope}
}

\begin{tikzpicture}
\threedellipses{}
\end{tikzpicture}

\begin{tikzpicture}
\threedellipses{ellipse one draw=black,ellipse two draw=black,ellipse one fill=red,ellipse two fill=green,ellipse one center={0,0},ellipse two center={0,0},ellipse one rotation=45,ellipse two rotation=-45}
\end{tikzpicture}

\end{document}


## Output

Edit 1: I changed the code, now using rotate around instead of rotate, which makes specifying the ellipses easier. If I understand your request correctly, you want something like this: changing the intersection line.

## Code

\documentclass[tikz,border=2mm]{standalone}

\begin{document}

\tikzset{%
threedellipsesopt/.is family,%
threedellipsesopt,%
intersection start/.initial={-0.5,-2},%
intersection end/.initial={1,4},%
ellipse one center/.initial={-1,1},%
ellipse two center/.initial={1,2},%
ellipse one rotation/.initial=30,%
ellipse two rotation/.initial=-50,%
ellipse one fill/.initial=blue!50!cyan,%
ellipse two fill/.initial=orange!50!yellow,%
ellipse one draw/.initial=blue!50!black,%
ellipse two draw/.initial=orange!50!black,%
opacity/.initial=0.5,%
}

\newcommand{\ellkey}[1]% access a specific key by name
{\pgfkeysvalueof{/tikz/threedellipsesopt/#1}}

\newcommand{\threedellipses}[1]{
\tikzset{threedellipsesopt,#1} % Process Keys passed to command
\path (\ellkey{intersection start}) -- (\ellkey{intersection end});
\path[opacity=\ellkey{opacity},draw=\ellkey{ellipse one draw},rotate around={\ellkey{ellipse one rotation}:(\ellkey{ellipse one center})}] (\ellkey{ellipse one center}) circle (\ellkey{ellipse one radius a} and \ellkey{ellipse one radius b});
\path[opacity=\ellkey{opacity},draw=\ellkey{ellipse two draw},rotate around={\ellkey{ellipse two rotation}:(\ellkey{ellipse two center})}] (\ellkey{ellipse two center}) circle (\ellkey{ellipse two radius a} and \ellkey{ellipse two radius b});
\begin{scope}
\clip (current bounding box.north west) -| (\ellkey{intersection end}) -- (\ellkey{intersection start}) |- (current bounding box.south west) -- cycle;
\clip[rotate around={\ellkey{ellipse one rotation}:(\ellkey{ellipse one center})}] (\ellkey{ellipse one center}) circle (\ellkey{ellipse one radius a}*1cm-0.2pt and \ellkey{ellipse one radius b}*1cm-0.2pt);
\fill[opacity=\ellkey{opacity},\ellkey{ellipse one fill}] (current bounding box.north east) rectangle (current bounding box.south west);
\end{scope}
\begin{scope}
\clip (current bounding box.north east) -| (\ellkey{intersection end}) -- (\ellkey{intersection start}) |- (current bounding box.south east) -- cycle;
\clip[rotate around={\ellkey{ellipse two rotation}:(\ellkey{ellipse two center})}] (\ellkey{ellipse two center}) circle (\ellkey{ellipse two radius a}*1cm-0.2pt and \ellkey{ellipse two radius b}*1cm-0.2pt);
\fill[opacity=\ellkey{opacity},\ellkey{ellipse two fill}] (current bounding box.north east) rectangle (current bounding box.south west);
\end{scope}
\begin{scope}
\clip (current bounding box.north west) -| (\ellkey{intersection end}) -- (\ellkey{intersection start}) |- (current bounding box.south west) -- cycle;
\clip[rotate around={\ellkey{ellipse two rotation}:(\ellkey{ellipse two center})}] (\ellkey{ellipse two center}) circle (\ellkey{ellipse two radius a}*1cm-0.2pt and \ellkey{ellipse two radius b}*1cm-0.2pt);
\fill[opacity=\ellkey{opacity},\ellkey{ellipse two fill}] (current bounding box.north east) rectangle (current bounding box.south west);
\end{scope}
\begin{scope}
\clip (current bounding box.north east) -| (\ellkey{intersection end}) -- (\ellkey{intersection start}) |- (current bounding box.south east) -- cycle;
\clip[rotate around={\ellkey{ellipse one rotation}:(\ellkey{ellipse one center})}] (\ellkey{ellipse one center}) circle (\ellkey{ellipse one radius a}*1cm-0.2pt and \ellkey{ellipse one radius b}*1cm-0.2pt);
\fill[opacity=\ellkey{opacity},\ellkey{ellipse one fill}] (current bounding box.north east) rectangle (current bounding box.south west);
\end{scope}
}

\begin{tikzpicture}
\threedellipses
{   ellipse one center={-1,1},
ellipse two center={-1,2},
ellipse one rotation=-30,
ellipse two rotation=-50,
intersection start={-5,0},
intersection end={5,2},
}
\end{tikzpicture}

\end{document}


## Output

-
ellipse one center/.initial={-1,1},% ellipse two center/.initial={0,2},% ellipse one radius a/.initial={2},% ellipse two radius a/.initial={1.95},% ellipse one radius b/.initial={1.5},% ellipse two radius b/.initial={2.25},% ellipse one rotation/.initial=0,% ellipse two rotation/.initial=-50,% By changing these values, the the left ellipses, are almost what I am looking for but the blue ellipse sits in the 2D xy plane. I need it to sit in the 3D xy plane. How can this be achieved? – dustin Jul 17 '13 at 0:14

There is a solution using the tikz-3dplot package. It's indeed made for 3d rotations. My proposition would be:

\documentclass{standalone}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usepackage{tikz-3dplot}

\begin{document}

\def\roll{30}
\def\pitch{40}
\def\yaw{30}
\def\xMainRot{100}
\def\zMainRot{30}
%Setting the main coords
\tdplotsetmaincoords{\xMainRot}{\zMainRot}
\begin{tikzpicture}[tdplot_main_coords,]
%%%%%%%%%%%%%%%%%%%%%
%%%The second ellipse
%%%%%%%%%%%%%%%%%%%%%
\begin{scope}[canvas is yx plane at z=0]
\draw[red]  (0,0) ellipse (1cm and 2cm);
%I don't know exactly why, but I guess the "transform shape" command messes up with the position of the node, so I have to shift it.
\end{scope}
\begin{scope}[canvas is yx plane at z=0]
\node[yshift=-30,xshift=1,rotate=90,red,transform shape,sloped] (0,0) {first ellipse};
\end{scope}

%%%%%%%%%%%%%%%%%%%%%
%%%The second ellipse
%%%%%%%%%%%%%%%%%%%%%
%you can set the rotated ellipse in the rotation you want
%this is added to the main coords
\tdplotsetrotatedcoords{0}{\pitch}{0}

%you can set an offset with the x=offset option
\begin{scope}[tdplot_rotated_coords,canvas is yz plane at x=0]
\draw[blue,dashed] (0,-2) -- (0,2);
\draw[blue,dashed] (-2,0) -- (2,0);
\draw[blue,dashed]  (0,0) ellipse (1cm and 2cm);
%In case it's written upside down, change yscale to -1
\node[yshift=-20,xshift=10,yscale=1,rotate=90,blue,transform shape,sloped] (0,0) {second ellipse};
\end{scope}
\end{tikzpicture}
\end{document}


This gives the following output

Sorry, no idea how to put the Tom's beautiful color intersections :)

-