画一捆矩形块

画一捆矩形块

长方体切一次最多可切出两个长方体。长方体切两次最多可切出四个长方体。长方体切三次最多可切出八次。

我想画一些与这种长方体剖分相关的问题的图示。我从 Maarten Dhondt 和 AboAmmar 的代码中学到,但它仍然有线条。你能帮忙吗?

如何绘制不同大小和颜色的堆叠立方体?

最好的例证应该是照片中 3 次切割的情况:

\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}

在此处输入图片描述

答案1

另一个问题的任何答案都没有提到一个好东西,那就是tikz允许使用 3D 坐标(x,y,z)。在下面的解决方案中,我定义了一个\cuboid宏,它绘制一个长方体,其后角(隐藏角)位于参数中给出的坐标处。大部分工作都是为了使其可定制:

  • 它采用关键字选项widthheightdepth(默认值为 1)让您设置其大小。
  • 它采用关键字选项fronttop和,side将其内容作为样式传递到长方体的相应侧。
  • 其他样式应用于整个长方体(但被特定于边的选项覆盖)。

由于它使用内置的 3D 坐标系,您还可以通过重新定义坐标轻松更改视角。

\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}

分割长方体

我自己也在学习这些东西;我想我的下一步应该是试着弄清楚如何使其成为节点形状,以便可以定义用于添加标签的锚点等。

相关内容