2

I would like (see picture) to have a line (calculated automatically) that is partially dashed and partially visible. I hope there is a way to do this. Thanks in advance!

enter image description here

\documentclass[a4paper]{scrartcl}
\usepackage{tikz}
\begin{document} 
\begin{tikzpicture}[x={(-0.3cm,-0.3cm)},y={(1.2cm,0cm)}, z={(0cm,1.2cm)}]
\coordinate (O) at (0,0,0);
\coordinate[label=left:{$A$}] (A) at (-4,-2,0);
\coordinate[label=left:{$B$}] (B) at (4,2,0);
\coordinate[label=below right:{$C$}] (C) at (6,6,0);
\coordinate[label=below right:{$D$}] (D) at (-3,7,0);
\coordinate[label=right:{$E$}] (E) at (4,6,4);
\coordinate[label=above right:{$F$}] (F) at (-5,7,5);
\coordinate[label=above left:{$G$}] (G) at (2,0,6);
\coordinate[label=right:{$H$}] (H) at (4,2,4);
\draw[ultra thick,black] (A) -- (B) -- (C) -- (D) -- (A);
\draw[ultra thick,black] (D) -- (F) -- (E) -- (C);
\draw[ultra thick,black] (B) -- (H) -- (G) -- (A);
\draw[fill=gray,opacity=0.4] (A)--(B)-- (H) -- (G);
\draw[fill=gray,opacity=0.4] (C)--(D)--(F)--(E);
\draw[fill=gray,opacity=0.4] (A)--(B)--(C)--(D)--(A);
\end{tikzpicture}
\end{document}
Nero
  • 29

2 Answers2

5

One method is to use the intersections library as demonstrated below to find the points where the line changes from dashed to solid. The dashed and solid parts are drawn as separate paths. The library is described in section 13.3.2 Intersections of arbitrary paths in the TikZ manual.

This is done more concisely using the intersection of coordinate specification that AndréC used in his answer. As he says this is a leftover from older versions of TikZ, and the description is gone from the manual. I don't know why it was deprecated though.

I moved the drawing of the lines to after the filling of the polygons, as otherwise the fill covers half the lines.

enter image description here

\documentclass[a4paper]{scrartcl}
\usepackage{tikz}
\usetikzlibrary{intersections}
\begin{document} 
\begin{tikzpicture}[x={(-0.3cm,-0.3cm)},y={(1.2cm,0cm)}, z={(0cm,1.2cm)}]
\coordinate (O) at (0,0,0);
\coordinate[label=left:{$A$}] (A) at (-4,-2,0);
\coordinate[label=left:{$B$}] (B) at (4,2,0);
\coordinate[label=below right:{$C$}] (C) at (6,6,0);
\coordinate[label=below right:{$D$}] (D) at (-3,7,0);
\coordinate[label=right:{$E$}] (E) at (4,6,4);
\coordinate[label=above right:{$F$}] (F) at (-5,7,5);
\coordinate[label=above left:{$G$}] (G) at (2,0,6);
\coordinate[label=right:{$H$}] (H) at (4,2,4);

\draw[fill=gray,opacity=0.4] (A)--(B)-- (H) -- (G); \draw[fill=gray,opacity=0.4] (C)--(D)--(F)--(E); \draw[fill=gray,opacity=0.4] (A)--(B)--(C)--(D)--(A);

% the following three paths doesn't draw anything \path [name path=BH] (B) -- (H); \path [name path=CE] (C) -- (E); \path [name path=AD] (A) -- (D); % path that draws the dashed segments \draw [dashed, ultra thick, % find the intersections of the named paths, then draw the two segments name intersections={of=BH and AD, by={bh}}, name intersections={of=CE and AD, by={ce}}] (A) -- (bh) (ce) -- (D);

% then draw the solid line \draw[ultra thick,black] (A) -- (B) -- (C) -- (D) (ce) -- (bh);

\draw[ultra thick,black] (D) -- (F) -- (E) -- (C); \draw[ultra thick,black] (B) -- (H) -- (G) -- (A);

\end{tikzpicture} \end{document}

Torbjørn T.
  • 206,688
4

I used an old syntax from TikZ version 1.0 and 2.0 which allows to calculate the coordinates of the point of intersection of two straight lines.

You can find the old manuals here Where can we find the old TikZ and pgf manuals? See page 87 and 88 of the 1.18 manual.

You can also use the new syntax, but here, it it doesn't bring anything.

screenshot

\documentclass[a4paper]{scrartcl}
\usepackage{tikz}
\begin{document} 
\begin{tikzpicture}[x={(-0.3cm,-0.3cm)},y={(1.2cm,0cm)}, z={(0cm,1.2cm)}]
\coordinate (O) at (0,0,0);
\coordinate[label=left:{$A$}] (A) at (-4,-2,0);
\coordinate[label=left:{$B$}] (B) at (4,2,0);
\coordinate[label=below right:{$C$}] (C) at (6,6,0);
\coordinate[label=below right:{$D$}] (D) at (-3,7,0);
\coordinate[label=right:{$E$}] (E) at (4,6,4);
\coordinate[label=above right:{$F$}] (F) at (-5,7,5);
\coordinate[label=above left:{$G$}] (G) at (2,0,6);
\coordinate[label=right:{$H$}] (H) at (4,2,4);
\draw[ultra thick,black] (A) -- (B) -- (C) -- (D);% -- (A);
\draw[ultra thick,black] (D) -- (F) -- (E) -- (C);
\draw[ultra thick,black] (B) -- (H) -- (G) -- (A);
\draw[fill=gray,opacity=0.4] (A)--(B)-- (H) -- (G);
\draw[fill=gray,opacity=0.4] (C)--(D)--(F)--(E);
\draw[fill=gray,opacity=0.4] (A)--(B)--(C)--(D);%--(A);
% auxiliary point I and J
\coordinate (I) at (intersection of B--H and A--D);
\coordinate (J) at (intersection of C--E and A--D);
\draw [dashed,thick](A)--(I)(J)--(D);
\draw [ultra thick](I)--(J);
\end{tikzpicture}
\end{document}
AndréC
  • 24,137