# Rotated tikz-3dplot: How to automatically distinguish between visible and hidden lines?

So I have a script that generates a figure of points and triangular elements representing a discretised hemisphere:

See below for code. Now I'm wondering if there is some good way of drawing this hemisphere and distinguishing between visible and hidden lines due to the perspective? As you can see it is almost working, but only because of the order in which the triangles are parsed.

Edit: Now all points and cells are included.

\documentclass{article}

\usepackage{tikz}
\usepackage{tikz-3dplot}

\begin{document}
\tdplotsetmaincoords{60}{125}

%
\pgfmathsetmacro{\rvec}{1.5}
\pgfmathsetmacro{\thetavec}{30}
\pgfmathsetmacro{\phivec}{45}
%
\begin{tikzpicture}[scale=3,tdplot_main_coords]

% Coordinate axes
\draw[thick,->] (0,0,0) -- (-1,0,0) node[anchor=south]{$z$};
\draw[thick,->] (0,0,0) -- (0,1,0) node[anchor=north west]{$x$};
\draw[thick,->] (0,0,0) -- (0,0,1) node[anchor=south]{$y$};

% Points
\coordinate (p0) at (0,1,0);
\coordinate (p1) at (0,0,-1);
\coordinate (p2) at (-1,0,0);
\coordinate (p3) at (0,0,1);
\coordinate (p4) at (1,0,0);
\coordinate (p5) at (0,0.707107,-0.707107);
\coordinate (p6) at (0.707107,0,-0.707107);
\coordinate (p7) at (0.707107,0.707107,0);
\coordinate (p8) at (-0.707107,0,-0.707107);
\coordinate (p9) at (-0.707107,0.707107,0);
\coordinate (p10) at (-0.707107,0,0.707107);
\coordinate (p11) at (0,0.707107,0.707107);
\coordinate (p12) at (0.707107,0,0.707107);
\coordinate (p13) at (0,0.92388,-0.382683);
\coordinate (p14) at (0.408248,0.816497,-0.408248);
\coordinate (p15) at (0.382683,0.92388,0);
\coordinate (p16) at (0,0.382683,-0.92388);
\coordinate (p17) at (0.408248,0.408248,-0.816497);
\coordinate (p18) at (0.382683,0,-0.92388);
\coordinate (p19) at (0.816497,0.408248,-0.408248);
\coordinate (p20) at (0.92388,0,-0.382683);
\coordinate (p21) at (0.92388,0.382683,0);
\coordinate (p22) at (-0.408248,0.816497,-0.408248);
\coordinate (p23) at (-0.382683,0.92388,0);
\coordinate (p24) at (-0.408248,0.408248,-0.816497);
\coordinate (p25) at (-0.382683,0,-0.92388);
\coordinate (p26) at (-0.816497,0.408248,-0.408248);
\coordinate (p27) at (-0.92388,0,-0.382683);
\coordinate (p28) at (-0.92388,0.382683,0);
\coordinate (p29) at (-0.408248,0.816497,0.408248);
\coordinate (p30) at (0,0.92388,0.382683);
\coordinate (p31) at (-0.816497,0.408248,0.408248);
\coordinate (p32) at (-0.92388,0,0.382683);
\coordinate (p33) at (-0.408248,0.408248,0.816497);
\coordinate (p34) at (-0.382683,0,0.92388);
\coordinate (p35) at (0,0.382683,0.92388);
\coordinate (p36) at (0.408248,0.816497,0.408248);
\coordinate (p37) at (0.408248,0.408248,0.816497);
\coordinate (p38) at (0.382683,0,0.92388);
\coordinate (p39) at (0.816497,0.408248,0.408248);
\coordinate (p40) at (0.92388,0,0.382683);

% Triangular cells
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p0) -- (p13) -- (p15) -- (p0);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p5) -- (p13) -- (p14) -- (p5);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p13) -- (p14) -- (p15) -- (p13);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p7) -- (p14) -- (p15) -- (p7);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p1) -- (p16) -- (p18) -- (p1);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p5) -- (p16) -- (p17) -- (p5);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p16) -- (p17) -- (p18) -- (p16);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p6) -- (p17) -- (p18) -- (p6);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p5) -- (p17) -- (p14) -- (p5);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p6) -- (p17) -- (p19) -- (p6);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p17) -- (p19) -- (p14) -- (p17);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p7) -- (p19) -- (p14) -- (p7);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p4) -- (p20) -- (p21) -- (p4);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p6) -- (p20) -- (p19) -- (p6);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p20) -- (p19) -- (p21) -- (p20);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p7) -- (p19) -- (p21) -- (p7);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p0) -- (p13) -- (p23) -- (p0);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p5) -- (p13) -- (p22) -- (p5);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p13) -- (p22) -- (p23) -- (p13);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p9) -- (p22) -- (p23) -- (p9);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p1) -- (p16) -- (p25) -- (p1);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p5) -- (p16) -- (p24) -- (p5);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p16) -- (p24) -- (p25) -- (p16);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p8) -- (p24) -- (p25) -- (p8);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p5) -- (p24) -- (p22) -- (p5);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p8) -- (p24) -- (p26) -- (p8);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p24) -- (p26) -- (p22) -- (p24);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p9) -- (p26) -- (p22) -- (p9);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p2) -- (p27) -- (p28) -- (p2);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p8) -- (p27) -- (p26) -- (p8);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p27) -- (p26) -- (p28) -- (p27);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p9) -- (p26) -- (p28) -- (p9);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p0) -- (p23) -- (p30) -- (p0);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p9) -- (p23) -- (p29) -- (p9);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p23) -- (p29) -- (p30) -- (p23);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p11) -- (p29) -- (p30) -- (p11);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p2) -- (p28) -- (p32) -- (p2);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p9) -- (p28) -- (p31) -- (p9);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p28) -- (p31) -- (p32) -- (p28);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p10) -- (p31) -- (p32) -- (p10);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p9) -- (p31) -- (p29) -- (p9);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p10) -- (p31) -- (p33) -- (p10);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p31) -- (p33) -- (p29) -- (p31);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p11) -- (p33) -- (p29) -- (p11);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p3) -- (p34) -- (p35) -- (p3);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p10) -- (p34) -- (p33) -- (p10);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p34) -- (p33) -- (p35) -- (p34);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p11) -- (p33) -- (p35) -- (p11);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p0) -- (p30) -- (p15) -- (p0);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p11) -- (p30) -- (p36) -- (p11);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p30) -- (p36) -- (p15) -- (p30);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p7) -- (p36) -- (p15) -- (p7);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p3) -- (p35) -- (p38) -- (p3);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p11) -- (p35) -- (p37) -- (p11);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p35) -- (p37) -- (p38) -- (p35);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p12) -- (p37) -- (p38) -- (p12);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p11) -- (p37) -- (p36) -- (p11);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p12) -- (p37) -- (p39) -- (p12);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p37) -- (p39) -- (p36) -- (p37);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p7) -- (p39) -- (p36) -- (p7);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p4) -- (p40) -- (p21) -- (p4);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p12) -- (p40) -- (p39) -- (p12);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p40) -- (p39) -- (p21) -- (p40);
\draw[fill=green,draw=black,opacity=.8,very thin,line join=round] (p7) -- (p39) -- (p21) -- (p7);

\end{tikzpicture}
\end{document}

• Welcome to TeX.SX! In order to help us provide an answer your example code (MWE) should be a code that compiles... but your dots doesn't and so your code returns errors. Please add the definitions of the coordinates that missing from your code to compile – koleygr Mar 13 '19 at 17:11
• Thank you, code is now updated to include all points and cells and should compile. – Stiffy Mar 13 '19 at 17:40
• pgfplots comes with patch plots, which have such an ordering. It works for polygons of the type you are considering. – user121799 Mar 13 '19 at 18:17

Welcome to TeX-SE! You will have to sort points according to their projection on the line of visibility. The good news is that pgfplots does that for you. And the format you provide the data can be almost literally adapted. All I did was a regular expression search and replace to strip off the repeating elements of your data, put them into tables and plot them. The upper table corresponds to the lower part of your plot where you specify which vertices are to be connected, and the lower table defines these vertices.

\documentclass[tikz,border=3.14pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
\begin{axis}[view={135}{45},axis equal image,scale=4,
xmin=-1,xmax=1,ymin=-1,ymax=1,zmin=-1,zmax=1,
colormap={greenblue}{color=(green) color=(blue)}]
0 13 15 \\
5 13 14 \\
13 14 15 \\
7 14 15 \\
1 16 18 \\
5 16 17 \\
16 17 18 \\
6 17 18 \\
5 17 14 \\
6 17 19 \\
17 19 14 \\
7 19 14 \\
4 20 21 \\
6 20 19 \\
20 19 21 \\
7 19 21 \\
0 13 23 \\
5 13 22 \\
13 22 23 \\
9 22 23 \\
1 16 25 \\
5 16 24 \\
16 24 25 \\
8 24 25 \\
5 24 22 \\
8 24 26 \\
24 26 22 \\
9 26 22 \\
2 27 28 \\
8 27 26 \\
27 26 28 \\
9 26 28 \\
0 23 30 \\
9 23 29 \\
23 29 30 \\
11 29 30 \\
2 28 32 \\
9 28 31 \\
28 31 32 \\
10 31 32 \\
9 31 29 \\
10 31 33 \\
31 33 29 \\
11 33 29 \\
3 34 35 \\
10 34 33 \\
34 33 35 \\
11 33 35 \\
0 30 15 \\
11 30 36 \\
30 36 15 \\
7 36 15 \\
3 35 38 \\
11 35 37 \\
35 37 38 \\
12 37 38 \\
11 37 36 \\
12 37 39 \\
37 39 36 \\
7 39 36 \\
4 40 21 \\
12 40 39 \\
40 39 21 \\
7 39 21 \\
}] table [row sep=\\,point meta=\thisrow{c}] {
x y z c\\
0 1 0 0\\
0 0 -1 0\\
-1 0 0 0\\
0 0 1 0\\
1 0 0 0\\
0 0.707107 -0.707107 0\\
0.707107 0 -0.707107 0\\
0.707107 0.707107 0 0\\
-0.707107 0 -0.707107 0\\
-0.707107 0.707107 0 0\\
-0.707107 0 0.707107 0\\
0 0.707107 0.707107 0\\
0.707107 0 0.707107 0\\
0 0.92388 -0.382683 0\\
0.408248 0.816497 -0.408248 0\\
0.382683 0.92388 0 0\\
0 0.382683 -0.92388 0\\
0.408248 0.408248 -0.816497 0\\
0.382683 0 -0.92388 0\\
0.816497 0.408248 -0.408248 0\\
0.92388 0 -0.382683 0\\
0.92388 0.382683 0 0\\
-0.408248 0.816497 -0.408248 0\\
-0.382683 0.92388 0 0\\
-0.408248 0.408248 -0.816497 0\\
-0.382683 0 -0.92388 0\\
-0.816497 0.408248 -0.408248 0\\
-0.92388 0 -0.382683 0\\
-0.92388 0.382683 0 0\\
-0.408248 0.816497 0.408248 0\\
0 0.92388 0.382683 0\\
-0.816497 0.408248 0.408248 0\\
-0.92388 0 0.382683 0\\
-0.408248 0.408248 0.816497 0\\
-0.382683 0 0.92388 0\\
0 0.382683 0.92388 0\\
0.408248 0.816497 0.408248 0\\
0.408248 0.408248 0.816497 0\\
0.382683 0 0.92388 0\\
0.816497 0.408248 0.408248 0\\
0.92388 0 0.382683 0\\
};
\end{axis}
\end{tikzpicture}
\end{document}


If you change the view to become view={45}{45}, you get

That is, you retain the freedom of adjusting the view that you know from tikz-3dplot but the ordering is done automatically.

• Thank you very much for this solution, really beautiful! I'm relieved that i didn't have to calculate the projections manually. – Stiffy Mar 13 '19 at 19:52