How can I draw the circle is inscribed in triangle in 3D?

I am trying to draw an inscribed circle of a triangle in 3D. If the vertices lie on the plane Oxy, I tried

\documentclass[12pt, border = 1mm,tikz]{standalone}
\usepackage{tikz-3dplot}
\begin{document}
\tdplotsetmaincoords{70}{123}
\begin{tikzpicture}[tdplot_main_coords,scale=1,tdplot_main_coords,declare function={xA = 8; yA=5;zA=0; xB=1;yB=2;zB=0; xC=5;yC=-5;zC=0 );%
}]
\path
(xA,yA,zA) coordinate (A)
(xB,yB,zB) coordinate (B)
(xC,yC,zC) coordinate (C) ;
\pgfmathsetmacro{\AB}{{sqrt((xB- xA)*(xB- xA) +(yB- yA)*(yB- yA) + (zB- zA)*(zB- zA))} };
\pgfmathsetmacro{\AC}{{sqrt((xC- xA)*(xC- xA) +(yC- yA)*(yC- yA) + (zC- zA)*(zC- zA))} };
\pgfmathsetmacro{\BC}{{sqrt((xC- xB)*(xC- xB) +(yC- yB)*(yC- yB) + (zC- zB)*(zC- zB))} };
\pgfmathsetmacro{\P}{{(\AB + \BC + \AC)/2} };
\pgfmathsetmacro{\myarea}{{sqrt(\P*(\P - \AB) *(\P - \AC)*(\P - \BC) )} };\pgfmathsetmacro{\r}{{\myarea/\P} };
\coordinate (I) at ({(xA*\BC + xB*\AC + xC*\AB)/(\AB + \BC + \AC)},{(yA*\BC + yB*\AC + yC*\AB)/(\AB + \BC + \AC)},{(zA*\BC + zB*\AC + zC*\AB)/(\AB + \BC + \AC)});
\foreach \p in {A,B,C,I}
\draw[fill=black] (\p) circle (1.5 pt);
\foreach \p/\g in {A/-90,B/-90,C/-90,I/0}
\path (\p)+(\g:3mm) node{$\p$};
\draw[blue] (A) -- (B) -- (C) -- cycle;
\draw[red,dashed] (I) circle[radius= \r];
\end{tikzpicture}
\end{document}


When I change, e.g, zA = 5, the result incorrect. How can I repair it?

• The code provided in tex.stackexchange.com/q/353357/33514 enables to create new canvas for this type of drawings.
– Jhor
Commented Jan 10, 2020 at 9:35
• Is that not an ellipse? Commented Jan 10, 2020 at 17:59
• Yes. That is a circle Commented Jan 10, 2020 at 23:20

3 Answers

You provide some wonderful code! You only need to draw the circle in the plane of the triangle.

\documentclass[12pt, border = 1mm,tikz]{standalone}
\usepackage{tikz-3dplot}
\usetikzlibrary{3dtools}
\def\pgfmathsetmacroFPU#1#2{\begingroup%
\pgfkeys{/pgf/fpu,/pgf/fpu/output format=fixed}%
\pgfmathsetmacro{#1}{#2}%
\pgfmathsmuggle#1\endgroup}%
\begin{document}
\tdplotsetmaincoords{70}{123}
\begin{tikzpicture}[tdplot_main_coords,scale=1,tdplot_main_coords,
declare function={xA = 8; yA=5;zA=5; xB=1;yB=2;zB=0; xC=5;yC=-5;zC=0 );%
}]
\path
(xA,yA,zA) coordinate (A)
(xB,yB,zB) coordinate (B)
(xC,yC,zC) coordinate (C) ;
\pgfmathsetmacro{\AB}{{sqrt((xB- xA)*(xB- xA) +(yB- yA)*(yB- yA) + (zB- zA)*(zB- zA))} };
\pgfmathsetmacro{\AC}{{sqrt((xC- xA)*(xC- xA) +(yC- yA)*(yC- yA) + (zC- zA)*(zC- zA))} };
\pgfmathsetmacro{\BC}{{sqrt((xC- xB)*(xC- xB) +(yC- yB)*(yC- yB) + (zC- zB)*(zC- zB))} };
\pgfmathsetmacro{\P}{{(\AB + \BC + \AC)/2} };
\pgfmathsetmacro{\myarea}{{sqrt(\P*(\P - \AB) *(\P - \AC)*(\P - \BC) )} };
\pgfmathsetmacro{\r}{{\myarea/\P} };
\coordinate (I) at ({(xA*\BC + xB*\AC + xC*\AB)/(\AB + \BC + \AC)},{(yA*\BC + yB*\AC + yC*\AB)/(\AB + \BC + \AC)},{(zA*\BC + zB*\AC + zC*\AB)/(\AB + \BC + \AC)});
\foreach \p in {A,B,C,I}
\draw[fill=black] (\p) circle (1.5 pt);
\foreach \p/\g in {A/-90,B/-90,C/-90,I/0}
\path (\p)+(\g:3mm) node{$\p$};
\draw[blue] (A) -- (B) -- (C) -- cycle;
\path[overlay] [3d coordinate={(mya)=(C)-(B)},
3d coordinate={(myb)=(C)-(A)},3d coordinate={(myc)=(B)-(A)}];
\pgfmathsetmacroFPU{\tmpa}{1/sqrt(TD("(mya)o(mya)"))}
\pgfmathsetmacroFPU{\tmpb}{1/sqrt(TD("(myb)o(myb)"))}
\pgfmathsetmacroFPU{\tmpc}{1/sqrt(TD("(myc)o(myc)"))}
\path[overlay] [3d coordinate={(mya)=\tmpa*(mya)},
3d coordinate={(myb)=\tmpb*(myb)},3d coordinate={(myc)=\tmpc*(myc)},
3d coordinate={(myn)=(myc)x(mya)},
3d coordinate={(mym)=(myn)x(B)-(A)}];
\pgfmathsetmacroFPU{\tmpm}{1/sqrt(TD("(mym)o(mym)"))}
\path[overlay] [3d coordinate={(mym)=\tmpm*(mym)}];
\begin{scope}[x={(myc)},y={(mym)}]
\draw[red,dashed] (I) circle[radius= \r];
\end{scope}
\end{tikzpicture}
\end{document}


You can choose the coordinates (more or less) as you wish

\documentclass[12pt, border = 1mm,tikz]{standalone}
\usepackage{tikz-3dplot}
\usetikzlibrary{3dtools}
\def\pgfmathsetmacroFPU#1#2{\begingroup%
\pgfkeys{/pgf/fpu,/pgf/fpu/output format=fixed}%
\pgfmathsetmacro{#1}{#2}%
\pgfmathsmuggle#1\endgroup}%
\begin{document}
\tdplotsetmaincoords{70}{123}
\foreach \X in {-6,-5.8,...,6}
{\begin{tikzpicture}[tdplot_main_coords,scale=1,tdplot_main_coords,
declare function={xA = 8; yA=5;zA=\X; xB=1;yB=2;zB=0; xC=5;yC=-5;zC=0 );%
}]
\draw[tdplot_screen_coords] (-8,-11) rectangle (3,3);
\path
(xA,yA,zA) coordinate (A)
(xB,yB,zB) coordinate (B)
(xC,yC,zC) coordinate (C) ;
\pgfmathsetmacro{\AB}{{sqrt((xB- xA)*(xB- xA) +(yB- yA)*(yB- yA) + (zB- zA)*(zB- zA))} };
\pgfmathsetmacro{\AC}{{sqrt((xC- xA)*(xC- xA) +(yC- yA)*(yC- yA) + (zC- zA)*(zC- zA))} };
\pgfmathsetmacro{\BC}{{sqrt((xC- xB)*(xC- xB) +(yC- yB)*(yC- yB) + (zC- zB)*(zC- zB))} };
\pgfmathsetmacro{\P}{{(\AB + \BC + \AC)/2} };
\pgfmathsetmacro{\myarea}{{sqrt(\P*(\P - \AB) *(\P - \AC)*(\P - \BC) )} };
\pgfmathsetmacro{\r}{{\myarea/\P} };
\coordinate (I) at ({(xA*\BC + xB*\AC + xC*\AB)/(\AB + \BC + \AC)},{(yA*\BC + yB*\AC + yC*\AB)/(\AB + \BC + \AC)},{(zA*\BC + zB*\AC + zC*\AB)/(\AB + \BC + \AC)});
\foreach \p in {A,B,C,I}
\draw[fill=black] (\p) circle (1.5 pt);
\foreach \p/\g in {A/-90,B/-90,C/-90,I/0}
\path (\p)+(\g:3mm) node{$\p$};
\draw[blue] (A) -- (B) -- (C) -- cycle;
\path[overlay] [3d coordinate={(mya)=(C)-(B)},
3d coordinate={(myb)=(C)-(A)},3d coordinate={(myc)=(B)-(A)}];
\pgfmathsetmacroFPU{\tmpa}{1/sqrt(TD("(mya)o(mya)"))}
\pgfmathsetmacroFPU{\tmpb}{1/sqrt(TD("(myb)o(myb)"))}
\pgfmathsetmacroFPU{\tmpc}{1/sqrt(TD("(myc)o(myc)"))}
\path[overlay] [3d coordinate={(mya)=\tmpa*(mya)},
3d coordinate={(myb)=\tmpb*(myb)},3d coordinate={(myc)=\tmpc*(myc)},
3d coordinate={(myn)=(myc)x(mya)},
3d coordinate={(mym)=(myn)x(B)-(A)}];
\pgfmathsetmacroFPU{\tmpm}{1/sqrt(TD("(mym)o(mym)"))}
\path[overlay] [3d coordinate={(mym)=\tmpm*(mym)}];
\begin{scope}[x={(myc)},y={(mym)}]
\draw[red,dashed] (I) circle[radius= \r];
\end{scope}
\end{tikzpicture}}
\end{document}


Note that I normalized various vectors to avoid dimension too large errors.

• Can you add it into 3dtools? Commented Jan 10, 2020 at 12:24
• @minhthien_2016 I will be very happy to do that (in a few weeks).
– user194703
Commented Jan 10, 2020 at 12:24

I try tkz-euclide in 3D and ...

\documentclass[]{standalone}
\usepackage{tkz-euclide}
\usetikzlibrary{3d}

\begin{document}
\thispagestyle{empty}

\begin{tikzpicture}[x  = {(-0.5cm,-0.5cm)},
y  = {(0.9659cm,-0.25882cm)},
z  = {(0cm,1cm)},
scale = .5,
color = {lightgray}]
\tikzset{facestyle/.style={fill=lightgray,draw=red,very thin,line join=round}}
% face "back"
\begin{scope}[canvas is zy plane at x=0]
\tkzDefPoints{0/0/A,6/0/B,4/6/C}
\tkzDrawPolygon[fill=blue!20](A,B,C)
\tkzDrawLines[add=1 and 1,dashed](A,B B,C C,A)
\tkzDrawCircle[in,fill=blue!50](A,B,C)
\end{scope}
% face  "left"
\begin{scope}[canvas is zx plane at y=0]
\tkzDefPoints{0/0/A,6/0/B,4/6/C}
\tkzDrawPolygon[fill=red!20](A,B,C)
\tkzDrawLines[add=1 and 1,dashed](A,B B,C C,A)
\tkzDrawCircle[in,fill=red!50](A,B,C)
\end{scope}
\end{tikzpicture}
\end{document}


• I hope You will write tkz-euclide - 3D. Commented Feb 2, 2020 at 14:12
• There is ´tikz-3dplot’ and I have enough work to finish the english doc for euclide Commented Feb 2, 2020 at 14:32

For both 2-dim and 3-dim, the incenter of the triangle ABC, the incenter3 of the tetrahedron ABCD can be calculated via the barycentric coordinates.

Similarly for circumcenter and circumcenter3.

The code is quite self-explained.

unitsize(1cm);
import three;
import solids; // for 'sphere'
triple bary(triple A, triple B=O, triple C=O, triple D=O, real a, real b=0, real c=0,real d=0){
return (a*A+b*B+c*C+d*D) / (a+b+c+d);}

triple incenter(triple A, triple B, triple C){
return bary(A,B,C,abs(B-C),abs(C-A),abs(A-B));}

real area(triple A, triple B, triple C){
return .5*abs(cross(A-B,A-C));}
// r = 2 S/ (a+b+c)
real inradius(triple A, triple B, triple C){
return 2*area(A,B,C) / (abs(A-B)+abs(B-C)+abs(C-A));}

// https://en.wikipedia.org/wiki/Tetrahedron
real volume(triple A, triple B, triple C, triple D){
return (1/6)*abs(dot(cross(D-A,D-B),D-C));}

triple incenter3(triple A, triple B, triple C,triple D){
return bary(A,B,C,D,area(B,C,D),area(C,D,A),area(D,A,B),area(B,C,A));}

// rabcd = 3 volume(A,B,C,D)/ (Sa+Sb+Sc)
real inradius3(triple A, triple B, triple C, triple D){
return 3*volume(A,B,C,D) / (area(A,B,C)+area(A,B,D)+area(B,C,D)+area(C,A,D));}

currentprojection=orthographic((1,1,.8),center=true,zoom=.9);

// The incircle of ABC
triple A=(0,0,0),B=(3,-1,0),C=(2,7,1),D=(2,3,5);
triple Iabc=incenter(A,B,C);
real   rabc=inradius(A,B,C);
path3  inABC=circle(Iabc,rabc,normal=cross(A-B,A-C));
draw(surface(inABC),yellow+opacity(.5));
draw(inABC);
dot(Iabc,yellow);

// The incircle of ACD
triple Iacd=incenter(A,C,D);
real   racd=inradius(A,C,D);
path3  inACD=circle(Iacd,racd,normal=cross(A-C,A-D));
draw(surface(inACD),lightblue+opacity(.2));
draw(inACD);
dot(Iacd,blue);

// The incircle of ABD
triple Iabd=incenter(A,B,D);
real   rabd=inradius(A,B,D);
path3  inABD=circle(Iabd,rabd,normal=cross(A-B,A-D));
//draw(surface(inABD),lightred,opacity(.2));
draw(inABD,red);
dot(Iabd,red);

// insphere
triple Iabcd = incenter3(A,B,C,D);
real   rabcd = inradius3(A,B,C,D);
revolution  inABCD=sphere(Iabcd,rabcd);
draw(surface(inABCD),green+opacity(.3));

draw(B--C--D--cycle);
draw(A--B^^A--C^^A--D,dashed);
label("$A$",A,NE);
label("$B$",B,W);
label("$C$",C,E);
label("$D$",D,N);