6

How to draw a gray strip realistically cut in half with a red plane? My idea is to paint some pieces on another layer. But here the joints turned out ugly. Maybe there is another tricky way?

\documentclass[tikz]{standalone}
\usetikzlibrary{3d}

\begin{document}

\pgfdeclarelayer{bg}    % declare background layer
\pgfdeclarelayer{layer1}
\pgfdeclarelayer{layer2}
\pgfdeclarelayer{layer3}
\pgfsetlayers{layer1,layer2,layer3,bg,main}  % set the order of the layers (main is the standard layer)

\begin{tikzpicture}
    %    \draw[red] (0,0,0) -- (0,0,1) ;
    %    \draw[blue] (0,0,0) -- (2,0,0);

    \begin{scope}[canvas is yz plane at x=1]
        \draw[line join=round, thick, fill=black!20, thin] (-1,-1) rectangle (1,1);
    \end{scope}
    \begin{pgfonlayer}{layer3}
        \begin{scope}[canvas is yz plane at x=0.9]
            \draw[line join=round, thick, fill=red!50,opacity=0.5] (-1.5,-1.5) rectangle (1.5,1.5);
        \end{scope}
    \end{pgfonlayer}
    \begin{scope}[canvas is xy plane at z=1]
        \draw[line join=round, thick, fill=black!40, thin] (1,1) rectangle ++(-0.1,-2);
        \draw[dashed, black!40, thick] (0.9,-1) -- ++(0,2);
    \end{scope}
    \begin{pgfonlayer}{layer2}
        \begin{scope}[canvas is xy plane at z=1]
            \draw[line join=round, thick, fill=black!40, thin] (0.9,1) rectangle ++(-0.1,-2);
            %                          \draw[dashed] (0.875,-1) -- ++(0,2);
        \end{scope}
    \end{pgfonlayer}
    \begin{scope}[canvas is xz plane at y=1]
        \draw[line join=round, thick, fill=black!40, thin] (1,1) rectangle ++(-0.1,-2);
        \draw[dashed, black!40, thick] (0.9,-1) -- ++(0,2);
    \end{scope}
    \begin{pgfonlayer}{layer2}
        \begin{scope}[canvas is xz plane at y=1]
            \draw[line join=round, thick, fill=black!40, thin] (0.9,1) rectangle ++(-0.1,-2);
        \end{scope}
    \end{pgfonlayer}
\end{tikzpicture}

\end{document}

[1]: https://i.sstatic.net/aRuca.png

2
  • Is the cutting line in dashed?
    – Black Mild
    Commented Aug 21, 2022 at 14:17
  • @BlackMild Yes. Dashed line is cutting by red plane Commented Aug 21, 2022 at 16:47

1 Answer 1

4
\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{3d}
\begin{document}
\begin{tikzpicture}
\begin{scope}[canvas is xy plane at z=1]
  \draw[line join=round, thick, fill=black!40] (1,1) rectangle ++(-0.25,-2);
  \draw[dashed] (0.875,-1) -- ++(0,2);
\end{scope}
\begin{scope}[canvas is xz plane at y=1]
  \draw[line join=round, thick, fill=black!40] (1,1) rectangle ++(-0.25,-2);
  \draw[dashed] (0.875,-1) -- ++(0,2);
\end{scope}
  \begin{scope}[canvas is yz plane at x=0.875]
  \draw[line join=round, thick, fill=red!50,opacity=0.5] (-1.5,-1.5) rectangle (1.5,1.5);
\end{scope}
\begin{scope}[canvas is yz plane at x=1]
  \draw[line join=round, thick, fill=black!20] (-1,-1) rectangle (1,1);
\end{scope}   
\begin{scope}[canvas is xy plane at z=1]
  \clip (1,2) rectangle ++(-0.125,-4);
  \draw[line join=round, thick, fill=black!40] (1,1) rectangle ++(-0.25,-2);
  \draw[dashed] (0.875,-1) -- ++(0,2);
\end{scope}
\begin{scope}[canvas is xz plane at y=1]
  \clip (1,2) rectangle ++(-0.125,-4);
  \draw[line join=round, thick, fill=black!40] (1,1) rectangle ++(-0.25,-2);
  \draw[dashed] (0.875,-1) -- ++(0,2);
\end{scope}
\draw (1,0,0) -- (2,0,0);
\end{tikzpicture}
\end{document}

Edit: In my opinion, this figure is not made better by the 3d library - here it is just with 3d coordinates:

\documentclass[tikz, border=1cm]{standalone}
\begin{document}
\begin{tikzpicture}[line join=round, thick]
\draw[fill=black!40] (-0.125,-1,1) -- (-0.125,1,1) -- (0.125,1,1) -- (0.125,-1,1) -- cycle;
\draw[fill=black!40]  (-0.125,1,-1) -- (-0.125,1,1) -- (0.125,1,1) -- (0.125,1,-1) -- cycle;
\draw[dashed, thin] (0,1,-1) -- (0,1,1) (0,-1,1) -- (0,1,1);
\draw[fill=red!50, fill opacity=0.5]  (0,-1.5,-1.5) -- (0,1.5,-1.5) -- (0,1.5,1.5) -- (0,-1.5,1.5) -- cycle;
\draw[fill=black!20]  (0.125,-1,-1) --(0.125,-1,1) -- (0.125,1,1) -- (0.125,1,-1)  -- cycle;
\clip(0,-2,-2) -- (0,-2,1) -- (0,1,1) -- (0,1,-2) -- cycle;
\draw[fill=black!40] (-0.125,-1,1) -- (-0.125,1,1) -- (0.125,1,1) -- (0.125,-1,1) -- cycle;
\draw[fill=black!40]  (-0.125,1,-1) -- (-0.125,1,1) -- (0.125,1,1) -- (0.125,1,-1) -- cycle;
\draw[dashed, thin] (0,1,-1) -- (0,1,1) (0,-1,1) -- (0,1,1);
\end{tikzpicture}
\end{document}

The fill opacity instead of opacity for the red plane makes the outer perimeter solid black instead of transparent gray.

Box cut in half by red plane

Zoom on box corner: Zoom on box corner

You must log in to answer this question.

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