3

I am trying to draw a cube with dashed at some place of the segment EH.

enter image description here

I tried

\documentclass[tikz,border=3mm]{standalone}
\usepackage{tikz,tikz-3dplot} 
\begin{document}
    \tdplotsetmaincoords{70}{110}
    \begin{tikzpicture}[tdplot_main_coords,line cap=butt,line join=round,c/.style={circle,fill,inner sep=1pt},
        declare function={a=4;}]
        \path
        (0,0,0) coordinate (A)  
        (a,0,0) coordinate (B)
        (a,a,0) coordinate (C)
        (0,a,0) coordinate (D)
        (0,0,a) coordinate (E)
        (a,0,a) coordinate (F)
        (a,a,a) coordinate (G)
        (0,a,a) coordinate (H)
        (0,0,0)  coordinate (O)
        ($ (B)!0.5!(C) $)coordinate (N)
            ($ (G)!0.5!(H) $)coordinate (M)
            (a,a,2*a) coordinate (X)
            (a,3*a/4,a) coordinate (Y)
        ;
        \draw[dashed]  (A) -- (B)  (A) -- (E) (D) -- (A) (D) -- (N) (M) -- (Y) ;
        \draw (B) -- (C) -- (D) (H) -- (E) -- (F) -- (G) -- cycle  (B) -- (F) (C) -- (G) -- (C) (D) -- (H) (N) -- (X) -- (D);
        \path foreach \p/\g in {A/-90,B/-90,C/-90,D/-90,E/90,F/90,G/90,H/90,N/-90,M/90,X/90,Y/180}{(\p)node[c]{}+(\g:2.5mm) node{$\p$}};  
    \end{tikzpicture}
\end{document}

enter image description here

How can I get dashed line in the segment EH?

2 Answers 2

4

You can define a coordinate as an intersection of two lines:

\coordinate (A) at (intersection of P--Q and R--S);

For example:

\documentclass[tikz,border=3mm]{standalone}
\usepackage{tikz,tikz-3dplot} 
\begin{document}
    \tdplotsetmaincoords{70}{110}
    \begin{tikzpicture}[tdplot_main_coords,line cap=butt,line join=round,c/.style={circle,fill,inner sep=1pt},
        declare function={a=4;}]
        \path
        (0,0,0) coordinate (A)  
        (a,0,0) coordinate (B)
        (a,a,0) coordinate (C)
        (0,a,0) coordinate (D)
        (0,0,a) coordinate (E)
        (a,0,a) coordinate (F)
        (a,a,a) coordinate (G)
        (0,a,a) coordinate (H)
        (0,0,0) coordinate (O)
        ($ (B)!0.5!(C) $)coordinate (N)
            ($ (G)!0.5!(H) $)coordinate (M)
            (a,a,2*a) coordinate (X)
            (a,3*a/4,a) coordinate (Y)
        ;
        % INTERSECTIONS:
        \coordinate (EHL) at (intersection of E--H and X--N);
        \coordinate (EHR) at (intersection of E--H and X--D);
        %
        \draw[dashed]  (A) -- (B)  (A) -- (E) (D) -- (A) (D) -- (N) (M) -- (Y) (EHL) -- (EHR);
        \draw (B) -- (C) -- (D) (H) -- (EHR) (EHL) -- (E) -- (F) -- (G) -- (H)  (B) -- (F) (C) -- (G) -- (C) (D) -- (H) (N) -- (X) -- (D);
        \path foreach \p/\g in {A/-90,B/-90,C/-90,D/-90,E/90,F/90,G/90,H/90,N/-90,M/90,X/90,Y/180}{(\p)node[c]{}+(\g:2.5mm) node{$\p$}};  
    \end{tikzpicture}
\end{document}

enter image description here

4
  • +1. That is the undocumented function in TikZ, that I think I have seen on this site!? Oct 24, 2022 at 8:43
  • @hpekristiansen, yes, I think I've learned it here too. Oct 24, 2022 at 8:47
  • @JuanCastaño I do not think that XN cuts EH and XD cuts EH. Oct 24, 2022 at 8:54
  • @JohnPaulPeter. Yes they do. That is because the drawing is in 2D. TikZ converts the 3D coordinates into a 2D points in the plane. So the lines intersect. Oct 24, 2022 at 9:06
3

The tikz-3dtools package can draw ordered paths. The tikz intersections libary can find the points. -and PGFPlots can find segments like this:

\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\usepackage{tikz-3dplot} 
\begin{document}
\tdplotsetmaincoords{70}{110}
\begin{tikzpicture}[tdplot_main_coords, line cap=butt, line join=round, c/.style={circle, fill, inner sep=1pt},
declare function={a=4;}]
\path
(0,0,0) coordinate (A)  
(a,0,0) coordinate (B)
(a,a,0) coordinate (C)
(0,a,0) coordinate (D)
(0,0,a) coordinate (E)
(a,0,a) coordinate (F)
(a,a,a) coordinate (G)
(0,a,a) coordinate (H)
(0,0,0)  coordinate (O)
($ (B)!0.5!(C) $)coordinate (N)
($ (G)!0.5!(H) $)coordinate (M)
(a,a,2*a) coordinate (X)
(a,3*a/4,a) coordinate (Y)
;
\draw[dashed]  (A) -- (B)  (A) -- (E) (D) -- (A) (D) -- (N) (M) -- (Y) ;
\draw (B) -- (C) -- (D) (E) -- (F) -- (G) -- (H)  (B) -- (F) (C) -- (G) -- (C) (D) -- (H);
\path[name path=a] (E) -- (H);
\draw[name path=b] (N) -- (X) -- (D);
\draw [intersection segments={of=a and b, sequence={L1}}];
\draw [intersection segments={of=a and b, sequence={L2}}, dashed];
\draw [intersection segments={of=a and b, sequence={L3}}];
\path foreach \p/\g in {A/-90,B/-90,C/-90,D/-90,E/90,F/90,G/90,H/90,N/-90,M/90,X/90,Y/180}{(\p)node[c]{}+(\g:2.5mm) node{$\p$}};  
\end{tikzpicture}
\end{document}

Line diagram with some solid and dashed lines

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .