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

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

我可以(我在这里学到的!)画画,但我不能改变颜色或制作几个不同的单元格。

\documentclass[12pt]{article}
\usepackage{tikz}
\usepackage{verbatim}
\usetikzlibrary{positioning}
\begin{document}

\begin{tikzpicture}[on grid]
  \shade[yslant=-0.5,right color=gray!10, left color=black!50]
    (0,0) rectangle +(3,3);
  \draw[yslant=-0.5] (0,0) grid (3,3);
  \shade[yslant=0.5,right color=gray!70,left color=gray!10]
    (3,-3) rectangle +(3,3);
  \draw[yslant=0.5] (3,-3) grid (6,0);
  \shade[yslant=0.5,xslant=-1,bottom color=gray!10,
    top color=black!80] (6,3) rectangle +(-3,-3);
  \draw[yslant=0.5,xslant=-1] (3,0) grid (6,3);
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案1

这是非常原始的代码,但是它的颜色

\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}

\begin{tikzpicture}[on grid]
  \draw[yslant=-0.5,]  (0,0) rectangle +(3,3);
  \draw[yslant=-0.5] (0,0) grid (3,3);
  \foreach \x in {0,2}{
  \foreach \y in {0,2}{
  \fill[yslant=-0.5, blue] (\x,\y) rectangle +(1,1);
  }}
  \draw[yslant=0.5] (3,-3) rectangle +(3,3);
  \draw[yslant=0.5] (3,-3) grid (6,0);
  \foreach \x in {3,5}{
  \foreach \y in {-3,-1}{
  \fill[yslant=0.5, blue] (\x,\y) rectangle +(1,1);
  }}
  \draw[yslant=0.5,xslant=-1,] (6,3) rectangle +(-3,-3);
  \draw[yslant=0.5,xslant=-1] (3,0) grid (6,3);
  \foreach \x in {5,3}{
  \foreach \y in {2,0}{
  \fill[yslant=0.5,,xslant=-1, blue] (\x,\y) rectangle +(1,1);
  }}
\end{tikzpicture}
\end{document}

在此处输入图片描述

\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}

\begin{tikzpicture}[on grid]
  \draw[yslant=-0.5,]  (0,0) rectangle +(3,2);
  \draw[yslant=-0.5] (2,0) grid (3,2);
  \draw[yslant=-0.5] (0,0) grid (2,3);
  \foreach \x in {0,2}{
  \foreach \y in {0}{
  \fill[yslant=-0.5, blue] (\x,\y) rectangle +(1,1);
  }}
  \draw[yslant=0.5] (3,-3) rectangle +(3,2);
  \draw[yslant=0.5] (3,-3) grid (6,-1);
  \draw[yslant=0.5] (2,0) grid (5,1);
  \foreach \x in {3,5}{
  \foreach \y in {-3}{
  \fill[yslant=0.5, blue] (\x,\y) rectangle +(1,1);
  }}
  \draw[yslant=0.5,xslant=-1,] (6,3) rectangle +(-3,-2);
  \draw[yslant=0.5,xslant=-1] (3,1) grid (6,3);
  \draw[yslant=0.5,xslant=-1] (2,-1) grid (5,0);
  \foreach \x in {5,3}{
  \foreach \y in {2}{
  \fill[yslant=0.5,,xslant=-1, blue] (\x,\y) rectangle +(1,1);
  }}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

如果您创建一个新命令来绘制立方体,则可以将它们用作构建块。以下是示例:

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

该命令接受 4 个参数:一个 x 坐标、一个 y 坐标、一个 z 坐标和一个填充颜色。((1,1,1)是最左边和最下面的块)。

现在你可以通过堆叠立方体来开始构建所需的结构:

\documentclass[border=2mm]{standalone}
\usepackage{tikz}

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

\begin{document}
\begin{tikzpicture}
    \drawbox{1}{2}{1}{white}
    \drawbox{2}{2}{1}{white}
    \drawbox{1}{1}{1}{white}
    \drawbox{2}{1}{1}{white}
    \drawbox{2}{2}{2}{gray!25}
\end{tikzpicture}

\begin{tikzpicture}
    \drawbox{5}{5}{1}{blue!25}\drawbox{5}{5}{2}{white}\drawbox{5}{5}{3}{blue!25}\drawbox{5}{5}{4}{white}\drawbox{1}{5}{5}{blue!25}\drawbox{2}{5}{5}{white}\drawbox{3}{5}{5}{blue!25}\drawbox{4}{5}{5}{white}\drawbox{5}{5}{5}{blue!25}
    \drawbox{5}{4}{1}{white}\drawbox{5}{4}{2}{white}\drawbox{5}{4}{3}{white}\drawbox{5}{4}{4}{white}\drawbox{1}{4}{5}{white}\drawbox{2}{4}{5}{white}\drawbox{3}{4}{5}{white}\drawbox{4}{4}{5}{white}\drawbox{5}{4}{5}{white}
    \drawbox{5}{3}{1}{blue!25}\drawbox{5}{3}{2}{white}\drawbox{5}{3}{3}{blue!25}\drawbox{5}{3}{4}{white}\drawbox{1}{3}{5}{blue!25}\drawbox{2}{3}{5}{white}\drawbox{3}{3}{5}{blue!25}\drawbox{4}{3}{5}{white}\drawbox{5}{3}{5}{blue!25}
    \drawbox{5}{2}{1}{white}\drawbox{5}{2}{2}{white}\drawbox{5}{2}{3}{white}\drawbox{5}{2}{4}{white}\drawbox{1}{2}{5}{white}\drawbox{2}{2}{5}{white}\drawbox{3}{2}{5}{white}\drawbox{4}{2}{5}{white}\drawbox{5}{2}{5}{white}
    \drawbox{1}{1}{1}{blue!25}\drawbox{2}{1}{1}{white}\drawbox{3}{1}{1}{blue!25}\drawbox{4}{1}{1}{white}\drawbox{1}{1}{2}{white}\drawbox{2}{1}{2}{white}\drawbox{3}{1}{2}{white}\drawbox{4}{1}{2}{white}\drawbox{1}{1}{3}{blue!25}\drawbox{2}{1}{3}{white}\drawbox{3}{1}{3}{blue!25}\drawbox{4}{1}{3}{white}\drawbox{1}{1}{4}{white}\drawbox{2}{1}{4}{white}\drawbox{3}{1}{4}{white}\drawbox{4}{1}{4}{white}
    \drawbox{5}{1}{1}{blue!25}\drawbox{5}{1}{2}{white}\drawbox{5}{1}{3}{blue!25}\drawbox{5}{1}{4}{white}\drawbox{1}{1}{5}{blue!25}\drawbox{2}{1}{5}{white}\drawbox{3}{1}{5}{blue!25}\drawbox{4}{1}{5}{white}\drawbox{5}{1}{5}{blue!25}
\end{tikzpicture}

\begin{tikzpicture}
    \drawbox{1}{2}{1}{white}
    \drawbox{2}{2}{1}{white}
    \drawbox{1}{2}{2}{white}
    \drawbox{2}{2}{2}{gray!25}
    \drawbox{1}{2}{3}{white}
    \drawbox{1}{1}{1}{white}
    \drawbox{2}{1}{1}{white}
    \drawbox{1}{1}{2}{white}
    \drawbox{2}{1}{2}{gray!25}
    \drawbox{1}{1}{3}{white}
\end{tikzpicture}
\end{document}

输出结果如下: 在此处输入图片描述

答案3

基于 Maarten 的回答,并使用\foreach\pgfmathparse{}\ifnum\pgfmathresult构造,答案可能会短得多。

\documentclass[border=1mm]{standalone}
\usepackage{tikz}
\newcommand{\drawbox}[4]{
\draw[fill=#4] ({#1-1+(#2-1)*2/3*cos(30)},{(#2-1)*2/3*sin(30)+#3-1}) -- ({#1-1+(#2-1)*2/3*cos(30)+1},{(#2-1)*2/3*sin(30)+#3-1}) -- ({#1-1+(#2-1)*2/3*cos(30)+1},{(#2-1)*2/3*sin(30)+#3-1+1}) -- ({#1-1+(#2-1)*2/3*cos(30)},{(#2-1)*2/3*sin(30)+#3-1+1}) -- cycle;
\draw[fill=#4] ({#1-1+(#2-1)*2/3*cos(30)},{(#2-1)*2/3*sin(30)+#3-1+1}) -- ({#1-1+#2*2/3*cos(30)},{(#2-1)*2/3*sin(30)+#3-1+1+2/3*sin(30)}) -- ({#1-1+(#2-1)*2/3*cos(30)+1+2/3*cos(30)},{(#2-1)*2/3*sin(30)+#3-1+1+2/3*sin(30)}) -- ({#1-1+(#2-1)*2/3*cos(30)+1},{(#2-1)*2/3*sin(30)+#3-1+1}) -- cycle;
\draw[fill=#4] ({#1-1+(#2-1)*2/3*cos(30)+1},{(#2-1)*2/3*sin(30)+#3-1+1}) -- ({#1-1+(#2-1)*2/3*cos(30)+1+2/3*cos(30)},{(#2-1)*2/3*sin(30)+#3-1+1+2/3*sin(30)}) -- ({#1-1+(#2-1)*2/3*cos(30)+1+2/3*cos(30)},{(#2-1)*2/3*sin(30)+#3-1+2/3*sin(30)}) -- ({#1-1+(#2-1)*2/3*cos(30)+1},{(#2-1)*2/3*sin(30)+#3-1}) -- cycle;}

\begin{document}
\begin{tikzpicture}
  \foreach [count=\i]\x in {1,...,5}
  \foreach [count=\j]\y in {5,...,1}
  \foreach [count=\k]\z in {1,...,5}{  
  \pgfmathparse{isodd(\i*\j*\k)}  
  \ifnum\pgfmathresult>0   
    \drawbox{\x}{\y}{\z}{blue!25};
  \else   
    \drawbox{\x}{\y}{\z}{white};
  \fi}
\end{tikzpicture}
\end{document}

得到所需结果:

在此处输入图片描述

答案4

我费了好大劲才弄到每个“堆栈”的三种不同颜色,所以决定发布解决方案,参考 AboAmmar 和 Maarten 的解决方案。注意,这会产生一个 24x24x3 的网格

\documentclass[border=2mm]{standalone}
\usepackage{tikz}

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

\begin{document}
\begin{tikzpicture}
\foreach \a in {1,...,24}{
\drawbox{24}{3}{\a}{blue!40}
\drawbox{\a}{3}{24}{blue!40}
}

\foreach \d in {1,...,24}{
\drawbox{24}{2}{\d}{green!40}
\drawbox{\d}{2}{24}{green!40}
}

\foreach \f in {1,...,24}{
\foreach \g in {1,...,24}{
\drawbox{\g}{1}{\f}{red!40}
}
}

\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容