# Plotting the intersection of a sphere with a plane

I have to plot, in a 3D coordinate system, the intersection of the sphere having the center at (1,0,0) and the radius 1, i.e. (x-1)^2+y^2+z^2=1, with the plane x=z, whose projection onto the plane xOy is an ellipse.

The parametric equations of this intersection are (thanks to @marmot):

x=1/2+1/2*cos(t), y=1/sqrt(2)*sin(t), z=1/2+1/2*cos(t), t\in [0,2*pi].


The code for plotting the intersection is:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usetikzlibrary{decorations.markings}

\begin{document}

\begin{figure}[h]
\begin{tikzpicture}[
arrow inside/.style = {
postaction={decorate},
decoration={
markings,
mark=at position #1,
}
}]
\begin{axis}
[view={60}{30}, axis lines=center,axis on top,
xlabel=$x$,ylabel=$y$,zlabel=$z$,
xtick={100},ytick={100},ztick={100},
no marks,axis equal,
xmin=-.5,xmax=1.5,ymin=-.5,ymax=1.5,zmin=-.5,zmax=1.5,
enlargelimits={upper=0.1}]
\addplot3+[color=black, no markers,samples=1001, samples y=0, domain=0:2*pi, variable=\t, arrow inside=.2 with {\arrow[rotate=180]{latex}}] ({1+cos(\t r)}, {sqrt(2)*sin(\t r)}, {1+cos(\t r)});

\path node[below left] at (0,0,0) {$O$} ;

\end{axis}

\end{tikzpicture}

\end{figure}

\end{document}


the output being

How can I plot on the same coordinate system the initial sphere ?

Later edit: By using the code provided by @marmot, the output is:

• How do you get an ellipse when cutting a piece of a sphere??
– user2478
Commented Dec 13, 2017 at 16:39
• @ Herbert There is no ellipse. The proposed parametrization does not fulfill x^2+y^2+z^2=constant.
– user121799
Commented Dec 13, 2017 at 18:17
• @Herbert, if you will replace z by x within the equation of the sphere, you will get 4 (x-1/2)^2+2y^2=1, which obviously is an ellipse. So, the projection of the intersection onto the plane xOy is an ellipse. The parameterization is: x=1/2+1/2*cos(t), y=1/root(2)*sin(t). The intersection represents a circle in the plane x=z !
– Cris
Commented Dec 13, 2017 at 18:28
• It is still not an ellipse. This is because, even though you have x=z, there are three coordinates. You hence end up on the diagonal in the x-z plane, which yields another factor \sqrt{2}, and @Herbert is correct.
– user121799
Commented Dec 13, 2017 at 18:32
• @marmot I agree and I have edited my question. Sorry, the projection of the intersection onto xOy is an ellipse.
– Cris
Commented Dec 13, 2017 at 20:00

I guess that the reason why this question has not gotten the attention you might have hoped for is that there seems to be an issue with the parametrization of the "ellipse".

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usetikzlibrary{decorations.markings}

\begin{document}

The intersection between the plane $x=z$ and the circle at $(1,0,0)$ can be
parametrized as
$x=z=\frac{1}{2}+\frac{1}{2}\cos t \quad\text{and}\quad y=\frac{1}{\sqrt{2}}\sin t\;.$
\begin{center}
\begin{tikzpicture}[
arrow inside/.style = {
postaction={decorate},
decoration={
markings,
mark=at position #1,
}
}]
\begin{axis}
[view={60}{30}, axis lines=center,axis on top,
xlabel=$x$,ylabel=$y$,zlabel=$z$,
xtick={100},ytick={100},ztick={100},
no marks,axis equal,
xmin=-.5,xmax=1.5,ymin=-.5,ymax=1.5,zmin=-.5,zmax=1.5,
enlargelimits={upper=0.1}]
\addplot3+[color=black, no markers,samples=1001, samples y=0,
domain=0:2*pi, variable=\t, arrow inside=.2 with
{\arrow[rotate=180]{latex}}] ({1/2+cos(\t r)/2}, {sin(\t
r)/sqrt(2)}, {1/2+ cos(\t r)/2});
\path node[below left] at (0,0,0) {$O$} ;
\shade[ball color=blue!10!white,opacity=0.20] (axis cs:1,0,0) circle (1.2cm);
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}


As you can see, I had to adjust the radius by hand. So this is a starting point of a full answer. You may also use asymptote, which has a true 3D engine.

UPDATE With asymptote it is fairly easy to draw these things.

 \documentclass{standalone}
\usepackage[inline]{asymptote}
\begin{document}
\thispagestyle{empty}
\begin{asy}
import three;
import graph3;
size(200);
currentprojection=orthographic(-3,-4,1);
// axes
real r=3.5;
draw(Label("$x$",1), O--r*X, Arrow3(HookHead3));
draw(Label("$y$",1), O--r*Y, Arrow3(HookHead3));
draw(Label("$z$",1), O--r*Z, Arrow3(HookHead3));
// sphere
draw(shift(1,0,0)*unitsphere,green,render(compression=Zero,merge=true));
// plane
pen bg=gray(0.9)+opacity(0.5);
draw(scale3(2)*surface((-1,-1,-1)--(1,-1,1)--(1,1,1)--(-1,1,-1)--cycle),bg);
//
real x(real t) {return 0.5+0.5 cos(t);}
real y(real t) {return sin(t)/sqrt(2);}
real z(real t) {return 0.5+0.5 cos(t);}
path3 p=graph(x,y,z,0,2*pi,operator ..);
draw(p,Arrow3);
\end{asy}
\end{document}


• Indeed, the initial parameterization was incorrect. I have edited my question. Thank you for also for the code using asymptote.
– Cris
Commented Dec 13, 2017 at 18:26
• How can I plot, not by hand and without asymptote (which seems to be very difficult to me) the sphere having the center at (1,0,0) and of radius 1 on the same system ?
– Cris
Commented Dec 13, 2017 at 18:43
• @Cris Short answer : I dunno. Longer answer: to the best of my knowledge, TikZ does not have a true 3D engine. So you may always end up adjusting things by hand. Even though the computation of the radius may be feasible, you will have problems drawing the objects in such a way that one gets a real 3D feel. I am hoping that one day there is a powerful 3D engine for TikZ. But before this happens, I personally would use asymptote, where you also have possibility to use TeX commands for annotations and so on.
– user121799
Commented Dec 13, 2017 at 18:56
• Just one more thing. I used your code, but it seems that the sphere in the output is different by yours. There are some shaded circles and the colors are different. How can I fix these ?
– Cris
Commented Dec 13, 2017 at 20:35
• @Cris Hmmh, the screenshots were produced by running xelatex on the codes posted here. I'm using TeXLive, which I updated a few days ago. Maybe you need to update your TeX distribution.
– user121799
Commented Dec 13, 2017 at 20:46

Needs some time if you create all three images. Run it with xelatex or latex->dvips->ps2psdf

\documentclass[pstricks]{standalone}
\usepackage{pst-solides3d}
\begin{document}

\begin{pspicture}[solidmemory](-4,-2)(4,4)
\psset{viewpoint=50 170 20 rtp2xyz,Decran=70,lightsrc=viewpoint}
\axesIIID(7,2.5,2.5)
\psSolid[object=plan,definition=equation,args={[-1 0 1 0]},base=-1 2 -2 2,
ngrid=40 40,fillcolor=red!30,linewidth=0pt,name=B1,action=none]
\psSolid[object=sphere,r=1,fillcolor=cyan,ngrid=72 72,name=C1,action=none](1,0,0)
\psSolid[object=fusion,linewidth=0.01pt,base=B1_s C1,linewidth=0.01pt,]
\defFunction[algebraic]{Circle}(t){0.5+0.5*cos(t)}{1/sqrt(2)*sin(t)}{0.5+0.5*cos(t)}
\psSolid[object=courbe,r=0,function=Circle,range=0 6.3,linecolor=red,linewidth=1.5pt]
\end{pspicture}

\begin{pspicture}[solidmemory](-4,-3)(2,4)
\psset{viewpoint=50 100 10 rtp2xyz,Decran=70,lightsrc=-20 50 20}
\axesIIID(2.5,6,2)
\psSolid[object=plan,definition=equation,args={[-1 0 1 0]},base=-1 2 -2 2,
ngrid=40 40,fillcolor=red!30,linewidth=0pt,name=B1,action=none]
\psSolid[object=sphere,r=1,fillcolor=cyan,ngrid=72 72,name=C1,action=none](1,0,0)
\psSolid[object=fusion,linewidth=0.01pt,base=B1_s C1,linewidth=0.01pt,]
\defFunction[algebraic]{Circle}(t){0.5+0.5*cos(t)}{1/sqrt(2)*sin(t)}{0.5+0.5*cos(t)}
\psSolid[object=courbe,r=0,function=Circle,range=0 3.14,linecolor=red,linewidth=1.5pt]
\end{pspicture}

\begin{pspicture}[solidmemory](-4,-3)(2,4)
\psset{viewpoint=50 90 10 rtp2xyz,Decran=70,lightsrc=-50 20 20}
\axesIIID(2.5,6,2)
\psSolid[object=plan,definition=equation,args={[-1 0 1 0]},base=-1 2 -2 2,
ngrid=40 40,fillcolor=red!30,linewidth=0pt,name=B1,action=none]
\psSolid[object=sphere,r=1,fillcolor=cyan,ngrid=72 72,name=C1,action=none](1,0,0)
\psSolid[object=fusion,linewidth=0.01pt,base=B1_s C1,linewidth=0.01pt,]
\defFunction[algebraic]{Circle}(t){0.5+0.5*cos(t)}{1/sqrt(2)*sin(t)}{0.5+0.5*cos(t)}
\psSolid[object=courbe,r=0,function=Circle,range=0 3.14,linecolor=red,linewidth=1.5pt]
\end{pspicture}

\end{document}