# Drawing an intersection of two equations in 3D

I'm trying to draw the intersection of the following two equations: x^2+y^2=4 (a circle of radius 2) and z=x+4. I got kind of close with tikz-3dplot (which has preferrable output in my opinion), but when it got hard I couldn't find any examples to take code from. I also couldn't find any way to input any function (that would fix everything instantly). The output of the tikz-3dplot figure shows the cylinder nicely:

I also included some vague first attempt with pgfplots. It describes the line enclosing the interval nicely, but about everything is said with that: the colour is random, I wouldn't know how to fill the curve, etc.

I have a strong preference for the nice and clean output of tikz-3dplot as well and I'd still like to show the cylinder in the final picture.

So I can imagine I'm heading in the wrong direction since I've hit a brick wall now, which leads me to the following question:

What's a more accessible way to still get pretty output of the intersection of the cylinder and the plane?

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usepackage{tikz-3dplot}
\begin{document}

\begin{tikzpicture}
\begin{axis}[%
xlabel=$x$,ylabel=$y$%
]%
samples=30,%
domain=-1:0,%
y domain=0:2*pi%
]%
({ cos(deg(y))},%
{ sin(deg(y))},%
{4+cos(deg(y))});%
% Is ignored?
%     ({ 2cos(deg(y))},%
%     { 2sin(deg(y))},%
%     0);%
\end{axis}
\end{tikzpicture}
\def\a{155}
\tdplotsetmaincoords{65}{\a}
\begin{tikzpicture}
[scale=5,
tdplot_main_coords,
axis/.style={->,black,very thin},
curve/.style={black,thin}]

\def\axissize{.6}
\def\th{1.2}

%\node[left] at (0,0,.6) {$z=x+4$};

\draw[axis] (\radius,0,0) -- (\axissize,0,0) node[anchor=north east]{$x$};
\draw[axis] (0,\radius,0) -- (0,\axissize,0) node[anchor=north west]{$y$};
\draw[axis,-,dashed] (0,0,0) -- (0,0,\th);
\draw[axis] (0,0,\th) -- (0,0,\th+.5*\axissize) node[anchor=south]{$z$};

\draw[axis,dashed,-] (0,0,0) -- (-\radius-.03,0,0) node[anchor=south west]{};
\draw[axis] (-\radius-.03,0,0) -- (-\axissize,0,0) node[anchor=south west]{};
\draw[axis,dashed] (0,0,0) -- (0,-\axissize,0) node[anchor=south east]{};
\tdplotsinandcos{\sintheta}{\costheta}{0}
\foreach \height in {0,\th}{
}

\tdplotsinandcos{\sintheta}{\costheta}{\a}

\tdplotsinandcos{\sintheta}{\costheta}{\a+180}

% I tried something here but it doesn't work :-(
%   \tdplotsetthetaplanecoords{0}
%   \tdplotsetrotatedthetaplanecoords{45}
%   \tdplotsinandcos{\sintheta}{\costheta}{45}
\end{tikzpicture}

\end{document}

• Look at this example for cylinder, plane, and intersection. texample.net/tikz/examples/dandelin-spheres It has quite a bit of other items in the figure, but eliminating the unwanted is easier than creating new. – R. Schumacher Apr 11 '15 at 18:09
• @R.Schumacher Thanks. That example actually worked great! – 1010011010 Apr 11 '15 at 20:28

I settled for the following diagram for now (Special thanks to R. Schumacher). It does kind of get the point across. :-) If somebody has a better rendition I'll surely use that one.

\documentclass{article}
\usepackage{tikz}
\usepackage{mathtools}
\tikzset{
MyPersp/.style={scale=1,x={(0.8cm,0.4cm)},y={(-0.8cm,0.4cm)},
z={(0cm,1cm)}},
MyPoints/.style={fill=white,draw=black,thick}
}
\begin{document}

\begin{tikzpicture}[MyPersp,axis/.style={->,black,very thin}]
\def\h{4}% Heigth of the ellipse center (on the axis of the cylinder)
\def\a{45}% angle of the section plane with the horizontal

\def\axissize{4}

\def\clrcyl{purple!55!black}
\def\clrplane{green}
\def\clrplanex{\clrplane!50!black}
\def\clrplaney{\clrplane!20!white}

\pgfmathparse{270+\a}\edef\afirst{\pgfmathresult}
\pgfmathparse{275+\a}\edef\asecond{\pgfmathresult}
\pgfmathparse{450+\a}\edef\alast{\pgfmathresult}
\pgfmathparse{90+\a}\edef\bfirst{\pgfmathresult}
\pgfmathparse{95+\a}\edef\bsecond{\pgfmathresult}
\pgfmathparse{270+\a}\edef\blast{\pgfmathresult}

\draw[axis] (1.4*\radius,0,0) -- (\axissize,0,0) node[anchor=south west]{$x$};
\draw[axis] (0,1.4*\radius,0) -- (0,\axissize,0) node[anchor=south east]{$y$};
\draw[axis,-,dashed] (0,0,0) -- (0,0,\h+2);
\draw[axis] (0,0,\h+2) -- (0,0,\h+2+.5*\axissize) node[anchor=south]{$z$};
\draw[axis,dashed,-] (0,0,0) -- (0,-.7*\axissize,0);
\draw[axis] (0,-\radius,0) -- (0,-\axissize,0) node[right=.125in,below=-.025in]{$-y$};
\draw[axis] (-\radius,0,0) -- (-\axissize,0,0) node[below=.075in,left=-.1in]{$-x$};

\foreach \q in {1,2,3}
{
\draw (\q,0,-.1) -- (\q,0,.1) node[below=.1in] {$\q$};
\draw (-\q,0,-.1) -- (-\q,0,.1) node[below=.1in] {$\mathllap{-}\q$};
\draw (0,\q,-.1) -- (0,\q,.1) node[below=.1in] {$\q$};
\draw (0,-\q,-.1) -- (0,-\q,.1) node[below=.1in] {$\mathllap{-}\q$};
}

\foreach \q in {1,2,3}
{
\draw (0,-.1,\q) -- (0,.1,\q) node[left=.15in,above=-.03in] {$\q$};
}

\foreach \t in {135,315}%
\draw[\clrcyl, thick] ({2*cos(\t)},{2*sin(\t)},0)
--({2*cos(\t)},{2*sin(\t)},{2+\h});
\draw[\clrcyl,thick] ({2*cos(\bfirst)},{2*sin(\bfirst)},0) % lower circle
\foreach \t in {\bfirst,\bsecond,...,\blast}
{--({2*cos(\t)},{2*sin(\t)},0)};
\draw[\clrcyl,thick,dashed] ({2*cos(\afirst)},{2*sin(\afirst)},0) % lower circle dashed
\foreach \t in {\afirst,\asecond,...,\alast}
{--({2*cos(\t)},{2*sin(\t)},0)};
\fill[\clrplaney,draw=\clrplanex,very thick,opacity=0.5]
(2,0,\h+2) % elliptical section
\foreach \t in {5,10,...,360}
{--({2*cos(\t)},{2*sin(\t)},{2*cos(\t)+\h})};
\node[pin=45:{\color{\clrcyl}$x^2+y^2=4$}] at (1.25,-1.25,2) {};
\node[pin=135:{\color{\clrplanex}$z=x+4$}] at (-1.25,1.25,2) {};
\draw[thick,->,\clrplanex,opacity=.8] (0,0,4) -- (0,{.5*sqrt(3)},{4+.5}) node[above=.15in,left=-.05in] {$\mathbf{\hat N}$};
\foreach \q in {4,5,6}
{
\draw (0,-.1,\q) -- (0,.1,\q) node[left=.15in,above=-.03in] {$\q$};
}

\draw[\clrcyl, thick] (2,0,{\h+2}) % upper circle
\foreach \t in {10,20,...,360}
{--({2*cos(\t)},{2*sin(\t)},{\h+2})}--cycle;
\end{tikzpicture}

\end{document}

• aka 666 -- Nice conversion of the example. – R. Schumacher Apr 11 '15 at 22:10