我想用长方体(a、b、c,它们是 L 的分母)以棋盘样式填充立方体(边长为 L)。
出了点问题。我该怎么办?
(我暂时没有填屋顶。)
\documentclass[margin=5pt, tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{intersections}
\begin{document}
\pgfmathsetmacro{\L}{10}
\pgfmathsetmacro{\a}{2}
\pgfmathsetmacro{\b}{2}
\pgfmathsetmacro{\c}{1}
\begin{tikzpicture}[scale=1.5,
z ={(0,0,-cos(45))}, % z ={(0,0,-1)},
>=latex,
font=\footnotesize,
]
% Big Cube
\coordinate[] (A) at (0,0,0);
\coordinate[] (B) at (\L,0,0);
\coordinate[] (C) at (\L,0,\L);
\coordinate[] (D) at (0,0,\L);
\coordinate[] (A-s) at (0,\L,0);
\coordinate[] (B-s) at (\L,\L,0);
\coordinate[] (C-s) at (\L,\L,\L);
\coordinate[] (D-s) at (0,\L,\L);
\draw[] (A) -- (B) -- (C) -- (D) --cycle;
\draw[] (A-s) -- (B-s) -- (C-s) -- (D-s) --cycle;
\foreach \Punkt in {A,B,C,D} \draw[gray] (\Punkt) -- (\Punkt-s);
% Front Filling
\pgfmathsetmacro{\A}{int(\L-1)}
\foreach \x in {0,...,\A} \foreach \y in {0,...,\A}{%
\pgfmathparse{mod(\x+\y,2)==0 ? "lightgray" : "red!50"}
\colorlet{Color}{\pgfmathresult}
\pgfmathsetmacro\M{mod(\x,2)==0 && mod(\y,2)==0 ? 1 : 0}
\ifnum\M=1
\draw[shift={(\x,\y)}, very thick, fill=Color] (0,0) rectangle (\a,\b);
\else\fi
}%% %
% Side Filling
\foreach \y in {0,...,\A} \foreach \z in {0,...,\A} {%
\pgfmathparse{mod(\y+\z,2)==0 ? "lightgray" : "red!50"}
\colorlet{Color}{\pgfmathresult}
\pgfmathsetmacro\M{mod(\y,2)==0 ? 1 : 0}
\ifnum\M=1
\draw[shift={(0,\y,\z)}, very thick, fill=Color] (10,0,0) -- (10,0,\c) -- (10,\b,\c) -- (10,\b,0) --cycle;
\else\fi
}
% Roof Filling
% ......
% CoSy
\draw[->] (-0.5,0,0) -- (3,0,0) node[right]{$x$};
\draw[->] (0,-0.5,0) -- (0,3,0) node[above]{$y$};
\draw[->] (0,0,-1) -- (0,0,3) node[pos=1.05]{$z$};
\end{tikzpicture}
\end{document}
答案1
\documentclass[margin=5pt, tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{intersections}
\begin{document}
\pgfmathsetmacro{\L}{10}
\pgfmathsetmacro{\a}{2}
\pgfmathsetmacro{\b}{2}
\pgfmathsetmacro{\c}{1}
\def\ColorA{yellow!33} % lightgray
\def\ColorB{red!50} % red!50
\begin{tikzpicture}[scale=1.0,
z={({0.5cm*cos(45)},{0.5cm*sin(45)})},
>=latex,
font=\footnotesize,
]
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}
% Big Cube ================
\coordinate[] (A) at (0,0,0);
\coordinate[] (B) at (\L,0,0);
\coordinate[] (C) at (\L,\L,0);
\coordinate[] (D) at (0,\L,0);
\coordinate[] (A-s) at (0,0,\L);
\coordinate[] (B-s) at (\L,0,\L);
\coordinate[] (C-s) at (\L,\L,\L);
\coordinate[] (D-s) at (0,\L,\L);
\begin{pgfonlayer}{foreground}
\draw[gray] (A) -- (B) -- (C) -- (D) --cycle;
\foreach \Punkt in {B,C,D} \draw[gray] (\Punkt) -- (\Punkt-s);
\end{pgfonlayer}
\draw[gray] (A) -- (A-s);
\draw[gray] (A-s) -- (B-s) -- (C-s) -- (D-s) --cycle;
% =======================
%% Cuboids ==============
\pgfmathsetmacro{\A}{floor(\L/\a-1)}
\pgfmathsetmacro{\B}{floor(\L/\b-1)}
\pgfmathsetmacro{\C}{floor(\L/\c-1)}
\pgfmathsetmacro{\Xmax}{floor(\L/\a)*\a}
\pgfmathsetmacro{\Ymax}{floor(\L/\b)*\b}
% Front Filling
\foreach \x in {0,...,\A} \foreach \y in {0,...,\B}{%
\pgfmathparse{mod(\x+\y,2)==0 ? "\ColorA" : "\ColorB"}
\colorlet{Color}{\pgfmathresult}
\draw[shift={(\x*\a,\y*\b)}, very thick, fill=Color] (0,0) rectangle (\a,\b);
}%
% Side Filling
\foreach \y in {0,...,\B} \foreach \z in {0,...,\C} {%
\pgfmathparse{mod(\y+\z,2)==0 ? "\ColorA" : "\ColorB"}
\colorlet{Color}{\pgfmathresult}
\pgfmathsetmacro\M{mod(\y,2)==0 ? 1 : 0}
\draw[shift={(0,\y*\b,\z*\c)}, very thick, fill=Color] (\Xmax,0,0) -- (\Xmax,0,\c) -- (\Xmax,\b,\c) -- (\Xmax,\b,0) --cycle;
}
% Roof Filling
\foreach \x in {0,...,\A} \foreach \z in {0,...,\C} {%
\pgfmathparse{mod(\x+\z,2)==0 ? "\ColorA" : "\ColorB"}
\colorlet{Color}{\pgfmathresult}
\draw[shift={(\x*\a,0,\z*\c)}, very thick, fill=Color] (0,\Ymax,0) -- (\a,\Ymax,0) -- (\a,\Ymax,\c) -- (0,\Ymax,\c) --cycle;
}
%% ================
% Annotation
\draw[] (\Xmax-\a,0) --+ (0,0,-0.2*\L) coordinate[pos=0.5] (X1);
\draw[] (\Xmax,0) --+ (0,0,-0.2*\L) coordinate[pos=0.5] (X2);
\draw[<->] (X1) -- (X2) node[midway, above]{$a$};
\draw[] (\Xmax,0) --+ (0.2*\L,0) coordinate[pos=0.5] (Y1);
\draw[] (\Xmax,\b) --+ (0.2*\L,0) coordinate[pos=0.5] (Y2);
\draw[<->] (Y1) -- (Y2) node[near start, right]{$b$};
\draw[] (\Xmax,0,0) --+ (0.2*\L,0) coordinate[pos=0.7] (Z1);
\draw[] (\Xmax,0,\c) --+ (0.2*\L,0) coordinate[pos=0.7] (Z2);
\draw[<->] (Z1) -- (Z2) node[midway, right]{$c$};
\end{tikzpicture}
\end{document}