![给腿部以外的 3D 分区上色](https://linux22.com/image/354140/%E7%BB%99%E8%85%BF%E9%83%A8%E4%BB%A5%E5%A4%96%E7%9A%84%203D%20%E5%88%86%E5%8C%BA%E4%B8%8A%E8%89%B2.png)
有多种方法可以逐层对平面分区进行着色;例如,我从这个帖子。我想用两种颜色来给隔板着色,一种颜色代表角落中的某条腿,另一种颜色代表所有其他箱子。
下面是我使用的代码。在示例中,有一条“长”黄色腿(x 轴),我希望其他所有东西都变成绿色。我可以让黄色腿上方的所有东西都变成绿色,但腿旁边的东西(即“地板上”)则不能。我确信这是一个微不足道的问题,但我真的不知道如何修改代码。
\documentclass{article}
\usepackage{xifthen}
\usepackage{verbatim}
\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}
}
\newcommand*\cubecolors[1]{%
\ifcase#1\relax
\or\colorlet{cubecolor}{yellow}%
\or\colorlet{cubecolor}{green}%
\or\colorlet{cubecolor}{green}%
\or\colorlet{cubecolor}{green}%
\or\colorlet{cubecolor}{green}%
\or\colorlet{cubecolor}{green}%
\or\colorlet{cubecolor}{green}%
\or\colorlet{cubecolor}{green}%
\else
\colorlet{cubecolor}{blue}%
\fi
}
% Definition of \planepartition
% To draw the following plane partition, just write \planepartition{ {a, b, c}, {d,e} }.
% a b c
% d e
\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 {1,...,\b} {
\addtocounter{z}{1}
\cubecolors{\c}
\cube{\value{x}}{\value{y}}{\value{z}}
}
}
}
}
\usepackage{tikz}
\begin{document}
\begin{figure}[h]
\centering
\begin{tikzpicture}[scale=0.26]
\planepartition{{5,4,2,1},{3,2,1},{1,1},{1,1},{1},{1},{1},{1},{1},{1}}
\end{tikzpicture}
\end{figure}
\end{document}
答案1
首先我承认我甚至没有尝试去理解你的代码的逻辑。然而,仅仅通过引入一个 switch
\pgfmathtruncatemacro{\myc}{ifthenelse(\value{y}+\value{z}==0,1,2)}
\cubecolors{\myc}
仅测试和是否y
为零z
(不要使用负数),我得到
如果你也有负坐标,请使用
\pgfmathtruncatemacro{\myc}{ifthenelse(abs(\value{y})+abs(\value{z})==0,1,2)}
反而。
完整代码:
\documentclass{article}
\usepackage{xifthen}
\usepackage{verbatim}
\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}
}
\newcommand*\cubecolors[1]{%
\ifcase#1\relax
\or\colorlet{cubecolor}{yellow}%
\or\colorlet{cubecolor}{green}%
\or\colorlet{cubecolor}{green}%
\or\colorlet{cubecolor}{green}%
\or\colorlet{cubecolor}{green}%
\or\colorlet{cubecolor}{green}%
\or\colorlet{cubecolor}{green}%
\or\colorlet{cubecolor}{green}%
\else
\colorlet{cubecolor}{blue}%
\fi
}
% Definition of \planepartition
% To draw the following plane partition, just write \planepartition{ {a, b, c}, {d,e} }.
% a b c
% d e
\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 {1,...,\b} {
\addtocounter{z}{1}
\pgfmathtruncatemacro{\myc}{ifthenelse(\value{y}+\value{z}==0,1,2)}
\cubecolors{\myc}
\cube{\value{x}}{\value{y}}{\value{z}}
}
}
}
}
\usepackage{tikz}
\begin{document}
\begin{figure}[h]
\centering
\begin{tikzpicture}[scale=0.26]
\planepartition{{5,4,2,1},{3,2,1},{1,1},{1,1},{1},{1},{1},{1},{1},{1}}
\end{tikzpicture}
\end{figure}
\end{document}