6

One cut to a cuboid results into two cuboids at most. Two cuts to a cuboid results into four cuboids at most. Three cuts to a cuboid results into eight cuts at most.

I want to draw illustrations for some problems associated with this sort of dissection of cuboids. I learnt from codes of Maarten Dhondt and AboAmmar yet it still has lines along. Could you help?

How to draw stacked cubes of different sizes and colors?

Best illustrations should be like this in the photo for the case of 3 cuts:

\documentclass[tikz, border=5]{standalone} 
\newcommand{\drawbox}[4]{
     \pgfmathsetmacro \angle {30}
     \pgfmathsetmacro \xd {{2/3*cos(\angle)}}
     \pgfmathsetmacro \yd {{2/3*sin(\angle)}}
     \pgfmathsetmacro \x {{#1-1+(#2-1)*(\xd)}}
     \pgfmathsetmacro \y {{#3-1+(#2-1)*(\yd)}}
     \draw[fill=#4] (\x,\y) -- (\x+1,\y) -- (\x+1,\y+1) -- (\x,\y+1) -- cycle;
     \draw[fill=#4] (\x,\y+1) -- (\x+\xd,\y+1+\yd) -- (\x+1+\xd,\y+1+\yd) -- (\x+1,\y+1) -- cycle;
     \draw[fill=#4] (\x+1,\y+1) -- (\x+1+\xd,\y+1+\yd) -- (\x+1+\xd,\y+\yd) -- (\x+1,\y) -- cycle; } \usepackage{pgf,tikz}
 \usepackage{xcolor}

 \begin{document}

 \begin{tikzpicture}[scale=.7] \drawbox{3}{1}{1}{blue!21}
 \drawbox{4}{1}{1}{blue!21} \drawbox{3}{0}{1}{blue!21}
 \drawbox{4}{0}{1}{blue!21}

 \drawbox{3}{1}{2}{blue!21} \drawbox{4}{1}{2}{blue!21}
 \drawbox{3}{0}{2}{blue!21} \drawbox{4}{0}{2}{blue!21}

 \drawbox{8}{1}{.431}{blue!21} \drawbox{9.4}{1}{.431}{blue!21}
 \drawbox{8}{-.420}{.431}{blue!21} \drawbox{9.4}{-.420}{.431}{blue!21}

 \drawbox{8}{1}{2.2}{blue!21} \drawbox{9.32}{1}{2.2}{blue!21}
 \drawbox{8}{-.40}{2.2}{blue!21} \drawbox{9.32}{-.40}{2.2}{blue!21}
 \end{tikzpicture}
 \end{document}

enter image description here

0

1 Answer 1

14

One nice thing which I don't see mentioned on any of the answers to the other question is that tikz allows 3D coordinates (x,y,z). In the solution below I've defined a \cuboid macro which draws a cuboid with its back (hidden) corner at the coordinate given in the argument. Most of the work goes into making it customizable:

  • It takes keyword options width, height and depth (default 1) letting you set its size.
  • It takes keyword options front, top, and side which pass their contents as styles to the corresponding sides of the cuboid.
  • Other styles get applied to the whole cuboid (but are overridden by side-specific options).

Because it uses the built-in 3D coordinate system, you can also easily change the perspective by redefining the coordinates.

\documentclass[tikz, border=5pt]{standalone}
\usepackage{tikz,pgfkeys}
\makeatletter
\def\pgfutil@gobble#1{}
\pgfkeys{/handlers/.unknown to list/.code=%
  \let#1\pgfutil@gobble
  \pgfkeysalso{% or \pgfkeys{\pgfkeyscurrentpath/.cd,
    .unknown/.code=% how to handle the difference between 'key' and 'key=value'?
      \ifx\pgfkeyscurrentvalue\pgfkeysnovalue
        \let\pgfkeys@temp\pgfkeyscurrentname
      \else
        \expandafter\expandafter\expandafter\def\expandafter\expandafter\expandafter\pgfkeys@temp\expandafter\expandafter\expandafter{\expandafter\pgfkeyscurrentname\expandafter=\expandafter{\pgfkeyscurrentvalue}}%
      \fi
      \expandafter\expandafter\expandafter\def\expandafter\expandafter\expandafter#1\expandafter\expandafter\expandafter{\expandafter#1\expandafter,\pgfkeys@temp},%
     .@clear list/.code=\let#1\pgfutil@gobble}%
}
\makeatother

\pgfkeys{
 /cuboid/.is family, /cuboid,
 depth/.estore in = \cuboidz,
 width/.estore in = \cuboidx,
 height/.estore in = \cuboidy,
 front/.estore in = \cuboidzStyle,
 side/.estore in = \cuboidxStyle,
 top/.estore in = \cuboidyStyle,
 .unknown to list = \cuboidOptions,
 default/.style = {width=1, height=1, depth=1, front=, side=, top=, draw},
}

\newcommand{\cuboid}[2][]{
  \pgfkeys{/cuboid, default, #1}%
  \begingroup
    \edef\x{%
      \endgroup
      \noexpand\begin{scope}[shift={#2}, join=bevel, \cuboidOptions]
        \noexpand\draw[fill,\cuboidyStyle] (0,\cuboidy,0) -- (\cuboidx,\cuboidy,0) -- (\cuboidx,\cuboidy,\cuboidz) -- (0,\cuboidy,\cuboidz) -- cycle;
        \noexpand\draw[fill,\cuboidxStyle] (\cuboidx,0,0) -- (\cuboidx,\cuboidy,0) -- (\cuboidx,\cuboidy,\cuboidz) -- (\cuboidx,0,\cuboidz) -- cycle;
        \noexpand\draw[fill,\cuboidzStyle] (0,0,\cuboidz) -- (\cuboidx,0,\cuboidz) -- (\cuboidx,\cuboidy,\cuboidz) -- (0,\cuboidy,\cuboidz) -- cycle;
      \noexpand\end{scope}
      \noexpand\pgfkeys{/cuboid/.@clear list}
    }%
    \x
}

\begin{document}
\tikzstyle{interior}=[fill=blue!50]
\begin{tikzpicture}[scale=.7, fill=blue!20, z={(225:.8)}, x={(-10:1)}]
  \cuboid[interior]{(0,0,0)} \cuboid[front=interior, top=interior, width=2]{(1.5,0,0)}
  \cuboid[top=interior, side=interior]{(0,0,1.5)} \cuboid[top=interior, width=2]{(1.5,0,1.5)}

  \cuboid[front=interior, side=interior]{(0,1.5,0)} \cuboid[front=interior, width=2]{(1.5,1.5,0)}
  \cuboid[side=interior]{(0,1.5,1.5)} \cuboid[width=2, front={fill=white, draw=red, thick, dashed}]{(1.5,1.5,1.5)}
\end{tikzpicture}
\end{document}

Split cuboid

I'm just learning this stuff myself; I guess my next step should be to try to figure out how to make this a node shape so that one can define anchors for adding labels, etc.

You must log in to answer this question.

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