# How to plot 3D convex hull using Tikz/pgf?

How to draw 3D convex hull using Tikz/pgf ?

For example, if we want to plot the following pyramid or Tetrahedron using Tikz/pgf ?

Say,the vertices are (1,1,1), (1,−1,−1), (−1,1,−1), (−1,−1,1).

However I have looked up the following code:

\pgfplotsset{width=7cm,compat=1.3}
\begin{tikzpicture}
\begin{axis}
% We have plotdata/first3d.dat' with
% ---------
% 0 0 0.8
% 1 0 0.56
% 2 0 0.5
% 3 0 0.75
%
% 0 1 0.6
% 1 1 0.3
% 2 1 0.21
% 3 1 0.3
%
% 0 2 0.68
% 1 2 0.22
% 2 2 0.25
% 3 2 0.4
%
% 0 3 0.7
% 1 3 0.5
% 2 3 0.58
% 3 3 0.9
% -> yields a 4x4 matrix:
\end{axis}
\end{tikzpicture}


giving the following convex hull

But the above code showing error and nor compiling in my latex editor.

are there other way/other codes to draw convex hull using Tikz/pgf ?

help me at least to draw the tetrahedron

• TikZ is not real 3D. For simple 3D figures, you can calculate yourself and ask TikZ to draw. A bit better way is using tikz-3d-plot, but it is far from real 3D. For example, in your question, to find a convex hull of 5 points, you need a procedure/function to check whether 5th point is inside the tetrahedron of the remaing 4 points or not. That is very very hard job. Computation ability of TikZ/LaTeX is quite limited! – Black Mild Oct 15 '20 at 2:43
• @BlackMild, Yes I understand that. Nice comment – M. A. SARKAR Oct 15 '20 at 11:57

You can try this code.

\documentclass[12 pt,border=1mm]{standalone}
\usepackage{tikz}
\usepackage{tikz-3dplot}
\begin{document}
\tdplotsetmaincoords{80}{120}
\begin{tikzpicture}[tdplot_main_coords,line join=round]
\path (1,1,1) coordinate (A)
(1,-1,-1)  coordinate (B)
(-1,1,-1)  coordinate (C)
(-1,-1,1)  coordinate (D);
\draw[thick,fill=gray] (D) -- (B) -- (C) -- cycle;
\draw[thick,fill=gray] (D) -- (A)  -- (C) -- cycle;
\draw[dashed] (A) -- (B);
\end{tikzpicture}
\end{document}


• What is the work of \tdplotsetmaincoords{80}{120} ? Please explain it – M. A. SARKAR Oct 14 '20 at 15:45
• What will be the code to simply join two 3D coordinates ? – M. A. SARKAR Oct 14 '20 at 15:48
• @M.A.SARKAR You can see document about tikz-3dplot at ctan.org/pkg/tikz-3dplot. – minhthien_2016 Oct 14 '20 at 15:49
• If you want to draw (dashed) the segment,AB you use \draw[dashed] (A) -- (B);`. – minhthien_2016 Oct 14 '20 at 15:50
• ok thank you very much – M. A. SARKAR Oct 14 '20 at 15:51