[Tex/LaTex] How to draw dashed lines of this picture

tikz-3dplot

I am trying to draw this picture

enter image description here

I tried

    \documentclass[border=2mm,tikz]{standalone} 
\usepackage{tikz-3dplot}
\usetikzlibrary{3dtools}
\tikzset{intersection of line trough/.code args={#1 and #2 with plane containing #3 and normal #4}{%
        \pgfmathsetmacro{\ltest}{abs(TD("#2o#4")-TD("#1o#4"))}%
        \ifdim\ltest pt<0.01pt
        \message{Plane and line are parallel!^^J}
        \pgfmathsetmacro{\myd}{0}
        \else
        \pgfmathsetmacro{\myd}{(TD("#3o#4")-TD("#1o#4"))/(TD("#2o#4")-TD("#1o#4"))}%
        \fi
        \pgfmathsetmacro{\myP}{TD("#1+\myd*#2-\myd*#1")}%
        \pgfkeysalso{insert path={%
                (\myP)
        }}
}}

\begin{document} 
      \tdplotsetmaincoords{60}{65} 
    \begin{tikzpicture}[scale=1,tdplot_main_coords,line join = round, line cap = round, declare function={a = 3;b = 4;}]
    \path
    (0,0,b+2) coordinate (M)
    (0,0,-b) coordinate (N);

    \begin{scope} [canvas is xy plane at z=0] 
      \draw[thick] (-a,-a) rectangle (a,a); 
    \end{scope}

    \begin{scope} [canvas is xy plane at z=a] 
    \draw[thick] (-a,-a) rectangle (a,a); 
    \end{scope}

\path[overlay][intersection of line trough={(M) and (N) with plane containing (0,0,0) and normal (0,0,1)}] coordinate (I);

\path[overlay][intersection of line trough={(M) and (N) with plane containing (0,0,a) and normal (0,0,1)}] coordinate (J);

    \foreach \p in {M,N,I,J}
    \draw[fill=black] (\p) circle (1pt);
    \foreach \p/\g in {M/90,N/-90,I/0,J/0}
    \path (\p)+(\g:3mm) node{$\p$};

    \draw[thick] (M) -- (N) ;
    \end{tikzpicture} 
\end{document}

enter image description here

How can I draw dashed lines correctly?

Best Answer

Here is a little tool box that allows you to protect some (closed) path. It is based on the links mentioned in the code (and possibly more). The important pieces are

\begin{scope}
 \tikzset{protect=\rectA}
 \draw[thick,use path=\rectB];
 \draw[thick] (I) -- (M);
 \tikzset{protect=\rectB}
 \draw[thick,use path=\lineA];
\end{scope}

where the paths \rectA, \rectB and \lineA have been used before and saved with save path. \tikzset{protect=\rectA} "protects" the interior of the \rectA path, and likewise for \rectB.

\documentclass[border=2mm,tikz]{standalone} 
\usepackage{tikz-3dplot}
\usetikzlibrary{3dtools}
% based on
% https://tex.stackexchange.com/a/38995/121799
% https://tex.stackexchange.com/a/76216
% https://tex.stackexchange.com/a/59168/194703
% https://tex.stackexchange.com/q/448920/194703
\makeatletter 
\tikzset{
  reuse path/.code={\pgfsyssoftpath@setcurrentpath{#1}}
}
\tikzset{even odd clip/.code={\pgfseteorule},
    protect/.code={
    \clip[overlay,even odd clip,reuse path=#1]
     (-16383.99999pt,-16383.99999pt) rectangle (16383.99999pt,16383.99999pt);
    }}
\makeatother

\tikzset{intersection of line trough/.code args={#1 and #2 with plane containing #3 and normal #4}{%
        \pgfmathsetmacro{\ltest}{abs(TD("#2o#4")-TD("#1o#4"))}%
        \ifdim\ltest pt<0.01pt
        \message{Plane and line are parallel!^^J}
        \pgfmathsetmacro{\myd}{0}
        \else
        \pgfmathsetmacro{\myd}{(TD("#3o#4")-TD("#1o#4"))/(TD("#2o#4")-TD("#1o#4"))}%
        \fi
        \pgfmathsetmacro{\myP}{TD("#1+\myd*#2-\myd*#1")}%
        \pgfkeysalso{insert path={%
                (\myP)
        }}
}}

\begin{document} 
  \tdplotsetmaincoords{60}{65} 
  \begin{tikzpicture}[scale=1,tdplot_main_coords,line join = round, line cap = round, declare function={a = 3;b = 4;}]
    \path
    (0,0,b+2) coordinate (M)
    (0,0,-b) coordinate (N);

    \begin{scope} [canvas is xy plane at z=0] 
      \draw[dashed,save path=\rectB] (-a,-a) rectangle (a,a); 
    \end{scope}

    \begin{scope} [canvas is xy plane at z=a] 
    \draw[thick,save path=\rectA] (-a,-a) rectangle (a,a); 
    \end{scope}
    \draw[dashed,save path=\lineA] (M) -- (N) ;
    \path[overlay][intersection of line trough={(M) and (N) with plane containing (0,0,0) and normal (0,0,1)}] coordinate (I);
    \path[overlay][intersection of line trough={(M) and (N) with plane containing (0,0,a) and normal (0,0,1)}] coordinate (J);
    \begin{scope}
     \tikzset{protect=\rectA}
     \draw[thick,use path=\rectB];
     \draw[thick] (I) -- (M);
     \tikzset{protect=\rectB}
     \draw[thick,use path=\lineA];
    \end{scope}

    \foreach \p in {M,N,I,J}
    \draw[fill=black] (\p) circle (1pt);
    \foreach \p/\g in {M/90,N/-90,I/0,J/0}
    \path (\p)+(\g:3mm) node{$\p$};
    \draw[thick] (M) -- (J);
  \end{tikzpicture} 
\end{document}

enter image description here

It turns out that for some (to me obscure) reasons it can happen in some cases that the insanely large bounding box is too large. However, for most practical applications something more "modest" like

\tikzset{even odd clip/.code={\pgfseteorule}, 
protect/.code={ 
\clip[overlay,even odd clip,reuse path=#1] 
(-6383.99999pt,-6383.99999pt) rectangle (6383.99999pt,6383.99999pt); 
}} 

will be more than sufficient.