![盒状平面剖分的周期染色](https://linux22.com/image/328536/%E7%9B%92%E7%8A%B6%E5%B9%B3%E9%9D%A2%E5%89%96%E5%88%86%E7%9A%84%E5%91%A8%E6%9C%9F%E6%9F%93%E8%89%B2.png)
我正在使用下面的代码,它创建了不同颜色的图层。很难让装饰同时在三个轴上的每一层工作。请帮忙。我希望使用 3 种颜色 a、b、c 为位置 (a, b, c) 的框着色,颜色 a+b+c mod 3。
代码
% Plane partition
% Author: Jang Soo Kim
\documentclass{standalone}
\usepackage[danish]{babel}
\usepackage{ifthenx}
\usepackage{verbatim}
\usepackage{tikz}
% Three counters
\newcounter{x}
\newcounter{y}
\newcounter{z}
% The angles of x,y,z-axes
\newcommand{\xaxis}{210}
\newcommand{\yaxis}{-30}
\newcommand{\zaxis}{90}
% The top side of a cube
\newcommand{\topside}[3]{%
\fill[fill=cubecolor, draw=black,shift={(\xaxis:#1)},shift={(\yaxis:#2)},
shift={(\zaxis:#3)}] (0,0) -- (30:1) -- (0,1) --(150:1)--(0,0);
}
% The left side of a cube
\newcommand{\leftside}[3]{%
\fill[fill=cubecolor, draw=black,shift={(\xaxis:#1)},shift={(\yaxis:#2)},
shift={(\zaxis:#3)}] (0,0) -- (0,-1) -- (210:1) --(150:1)--(0,0);
}
% The right side of a cube
\newcommand{\rightside}[3]{%
\fill[fill=cubecolor, draw=black,shift={(\xaxis:#1)},shift={(\yaxis:#2)},
shift={(\zaxis:#3)}] (0,0) -- (30:1) -- (-30:1) --(0,-1)--(0,0);
}
% The cube
\newcommand{\cube}[3]{%
\topside{#1}{#2}{#3} \leftside{#1}{#2}{#3} \rightside{#1}{#2}{#3}
}
% Definition cubecolors
\newcommand*\cubecolors[1]{%
\ifcase#1\relax
\or\colorlet{cubecolor}{green}%
\or\colorlet{cubecolor}{blue}%
\or\colorlet{cubecolor}{red}%
\or\colorlet{cubecolor}{green}%
\or\colorlet{cubecolor}{blue}%
\or\colorlet{cubecolor}{red}%
\else
\colorlet{cubecolor}{white}%
\fi
}
% Definition of \planepartition
\newcommand\planepartition[1]{
\setcounter{x}{-1}
\foreach \a in {#1} {
\addtocounter{x}{1}
\setcounter{y}{-1}
\foreach \b in \a {
\addtocounter{y}{1}
\setcounter{z}{-1}
\foreach \c in {0,...,\b} {
\addtocounter{z}{1}
\cubecolors{\a}
\ifthenelse{\c=0}{\setcounter{z}{-1},\addtocounter{y}{0}}{
\cube{\value{x}}{\value{y}}{\value{z}}}
}
}
}
}
\begin{document}
\begin{tikzpicture}
\planepartition{{5,3,2,2},{4,2,2,1},{3,2,1},{2,1},{1}}
\end{tikzpicture}
\end{document}
答案1
我认为一定有一种方法可以使用 进行着色,mod
但目前我不确定如何操作(为自己辩解,有点晚了)。然而,与此同时,我可以为您提供另一种选择,让您摆脱计数器,这很有用,但它仍然是额外的代码,以及包ifthenx
和\ifthenelse
命令,由 代替\ifnum### ... \else ... \fi
。
因此,您可以\planepartition
用以下命令替换您的命令:
\newcommand\planepartition[1]{
\foreach \a [count=\x starting from -1] in {#1} {
\foreach \b [count=\y starting from -1] in \a {
\foreach \c [count=\z starting from -1] in {0,...,\b} {
\cubecolors{\a}
\ifnum\c=0
\else
\cube{\x}{\y}{\z}
\fi
}
}
}
}