# Draw background lines dashed in TikZ-3dplot depending on view position

In the graphic the red line is partly covered (in the background) of the 3d-object. Is it possible to draw the background parts of the lines dashed (or calculate the "intersection" points)?

After that I like to fill the surfaces with transparent color. I fear that I have to declare the surfaces in a other way (like rectangles).

\documentclass[border=5pt]{standalone}   % for LuaLaTeX
\usepackage{pgfplots}
\usepackage{tikz, tikz-3dplot}
%\usepackage[european, cuteinductors]{circuitikz}
\begin{document}

\tdplotsetmaincoords{70}{10}    % set position of 3d-view
\begin{tikzpicture}[tdplot_main_coords]

% Set parameters
%\newcommand\hexX{1}        -> defined in loop
\newcommand\hexY{0}
%\newcommand\hexZ{0}        -> defined in loop
%\newcommand\hexAng{7.5}    -> defined in loop
\newcommand\hexlowerHeigth{0}
\newcommand\hexupperHeigth{1}

% Calculate 4*6 Corners and draw connections
\foreach \i/\n/\Ang in {1/2/0, 3/4/120, 5/6/240}{
\coordinate (LP_\NAME\i) at ({cos(\Ang+7.5)  *\hexX-sin(\Ang+7.5)  *\hexY}, {sin(\Ang+7.5)  *\hexX+cos(\Ang+7.5)  *\hexY}, \hexZ);
\coordinate (LP_\NAME\n) at ({cos(\Ang+112.5)*\hexX-sin(\Ang+112.5)*\hexY}, {sin(\Ang+112.5)*\hexX+cos(\Ang+112.5)*\hexY}, \hexZ);
}
\draw[-] (LP_\NAME1)--(LP_\NAME2)--(LP_\NAME3)--(LP_\NAME4)--(LP_\NAME5)--(LP_\NAME6)--cycle;
}

% Draw connections of each corner and numbering
\foreach \Number in {1,2,...,6}{
\draw[-] (LP_li\Number)--(LP_lo\Number)--(LP_uo\Number)--(LP_ui\Number)--cycle;
\node[label=above:{\Number}] (a\Number) at (LP_uo\Number) {};       % indicating corners only
}

\draw[-, red] (LP_li3)--(LP_li4); % example: partly covered line

\end{tikzpicture}
\end{document}


Edit 1

I expanded the example a little bit (just insert code above \end{tikzpicture}). A circle is added to show how each 6 points of each hexagon are calculated. Additional the top plain is filled. But still the problems with lines...

\foreach \Ang in {0,1,2,...,360}{                                                          %Calculate circle points
}

\foreach \Ang in {0,1,2,...,359}{
\pgfmathtruncatemacro{\Numberplusone}{\Ang+1}                                       % Calculate following point number
\draw[-, blue!80!black](LP_circ\Ang)--(LP_circ\Numberplusone);
}

\fill[green, opacity=0.3] (LP_lo4) -- (LP_uo4) -- (LP_uo5) -- (LP_lo5) -- cycle;        % Example for one segment, like loop below

\foreach \Number in {1,2,...,6}{                                                        % Fill top segments
\pgfmathtruncatemacro{\Numberplusone}{\Number+1}                                    % Calculate following point number
\ifthenelse{\equal{\Numberplusone}{7}}{\pgfmathtruncatemacro{\Numberplusone}{1}}{}  % Point 7 does not exist, will be changed to 1

\fill[blue, opacity=0.3] (LP_uo\Number) -- (LP_ui\Number) -- (LP_ui\Numberplusone) -- (LP_uo\Numberplusone) -- cycle;
}


Edit 2

Now I edited it a little bit more and it looks better, but dashed lines still would be nice, as well as a solution how to draw lines or arrows in the "middle" (see picture). One more problem is, that if you change the point of view, you have to draw nearly a new picture. Hope anyone has experience with such (3d-)problems.

\documentclass[border=5pt]{standalone}   % for LuaLaTeX
\usepackage{pgfplots}
\usepackage{tikz, tikz-3dplot}
\usetikzlibrary{patterns}
\begin{document}

\tdplotsetmaincoords{70}{10}    % set position of 3d-view
\begin{tikzpicture}[tdplot_main_coords]

% Set parameters------------------------------------------------------------------------------------------------------------
% First Platform LP
%\newcommand\HexX{1}        -> defined in loop
\newcommand\HexY{0}
%\newcommand\HexZ{0}        -> defined in loop
%\newcommand\hexAng{7.5}    -> defined in loop
\newcommand\HexLowerHeigthLP{0}
\newcommand\HexUpperHeigthLP{1}

% Coordinate System------------------------------------------------------------------------------------------------------------
\foreach \Ang in {0,1,2,...,360}{                                                      %Calculate circle points
}

\draw[-, dotted] \foreach \x [remember=\x as \lastx (initially 0)] in {1,...,360}{(LP_circ\lastx) -- (LP_circ\x)} -- (LP_circ0);
%\draw[-, fill=green] \foreach \Ang in {1,...,359}{\pgfmathtruncatemacro{\NumberPlusOne}{\Ang+1}
%(LP_circ\Ang) -- (LP_circ\NumberPlusOne)}-- cycle;
\fill[pattern color = gray, pattern = north east lines] (LP_circ0)--(LP_circ10)--(LP_circ20)--(LP_circ30)--(LP_circ40)--(LP_circ50)--(LP_circ60)--(LP_circ70)--(LP_circ80)--(LP_circ90)--(LP_circ100)--(LP_circ110)--(LP_circ120)--(LP_circ130)--(LP_circ140)--(LP_circ150)--(LP_circ160)--(LP_circ170)--(LP_circ180)--(LP_circ190)--(LP_circ200)--(LP_circ210)--(LP_circ220)--(LP_circ230)--(LP_circ240)--(LP_circ250)--(LP_circ260)--(LP_circ270)--(LP_circ280)--(LP_circ290)--(LP_circ300)--(LP_circ310)--(LP_circ320)--(LP_circ330)--(LP_circ340)--(LP_circ350)--cycle; % in loop it didn't work...

\draw[-stealth, thick] (0,0,0)--(8,0, 0) node[anchor=west]{$x$};
\draw[-stealth, thick] (0,0,0)--(0,-8, 0) node[anchor=north]{$y$};
\draw[-stealth, thick] (0,0,0)--(0,0,-2) node[anchor=north]{$z$};
\draw[-stealth, thick, red] (0,0,-1.8)--(0,0, 4) node[anchor=south]{arrow should be surrounded" by hexagon};

% Platform------------------------------------------------------------------------------------------------------------
% Calculate 4*6 Corners and draw connections
\foreach \i/\n/\Ang in {1/2/0, 3/4/120, 5/6/240}{
\coordinate (LP_\NAME\i) at ({cos(\Ang+7.5)  *\HexX-sin(\Ang+7.5)  *\HexY}, {sin(\Ang+7.5)  *\HexX+cos(\Ang+7.5)  *\HexY}, \HexZ);
\coordinate (LP_\NAME\n) at ({cos(\Ang+112.5)*\HexX-sin(\Ang+112.5)*\HexY}, {sin(\Ang+112.5)*\HexX+cos(\Ang+112.5)*\HexY}, \HexZ);
}
%\draw[-] (LP_\NAME1)--(LP_\NAME2)--(LP_\NAME3)--(LP_\NAME4)--(LP_\NAME5)--(LP_\NAME6)--cycle;
}

\foreach \NAMEa/\NAMEb/\op in {uo/ui/0.2, lo/li/0.8, ui/li/0.2, uo/lo/0.2}{                                     % Fill 4 outside walls
\foreach \Number in {1,2,...,6}{                                                        % Fill 6 segments (of the top plateau, for example)
\pgfmathtruncatemacro{\NumberPlusOne}{\Number+1}                                    % Calculate following point number
\ifthenelse{\equal{\NumberPlusOne}{7}}{\pgfmathtruncatemacro{\NumberPlusOne}{1}}{}  % Point 7 does not exist, will be changed to 1

\fill[blue, opacity=\op] (LP_\NAMEa\Number) -- (LP_\NAMEb\Number) -- (LP_\NAMEb\NumberPlusOne) -- (LP_\NAMEa\NumberPlusOne) -- cycle;
}
}

% Draw connections of each corner and numbering
\foreach \Number in {1,2,...,6}{
\draw[-] (LP_li\Number)--(LP_lo\Number)--(LP_uo\Number)--(LP_ui\Number)--cycle;
\node[label=above:{\Number}] (a\Number) at (LP_uo\Number) {};       % indicating corners only
}

\foreach \NAME in {li, lo, ui, uo}{
\draw[-] (LP_\NAME1)--(LP_\NAME2)--(LP_\NAME3)--(LP_\NAME4)--(LP_\NAME5)--(LP_\NAME6)--cycle;
}

\end{tikzpicture}
\end{document}


• I'd say one solution would be to use pgflayers. Not sure if that's compatible with 3 dimensional graphs, though. – 1010011010 Jul 22 '14 at 9:09
• I would try to fill the planes with a semi-transparent fill which will partially occlude what is behind. Of course you have to compute somehow the right order to draw the planes (from bottom to front). Perhaps tikz is not the right tool. You can have a look at sketch language (it can produce tikz code) – JLDiaz Jul 22 '14 at 10:29
• TikZ doesn't do z-ordering so you'd better use another tool and convert it to TikZ code. – percusse Jul 22 '14 at 12:07
• Thank you for the hints. Because I spent a lot of time in it, I think I leave it like that this time and try another tool like sketch next time. Are there more programs like sketch (for 3d) which produce TikZ-Code you can recommend? – Konne Jul 24 '14 at 10:05

TikZ doesn't understand anything about z-order (though Christian Feuersanger went a long way to implement a limited version for 3D plots of pgfplots).