為遠離腿的 3D 分區著色

為遠離腿的 3D 分區著色

有多種方法可以對平面分區進行逐層著色;例如,我從中學到了很多東西這個帖子。我想用兩種顏色為分區著色,一種顏色用於角落中的給定腿,另一種顏色用於所有其他盒子。

下面是我正在使用的程式碼。在範例中,有一條「長」黃色腿(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

首先讓我承認我甚至沒有嘗試理解你的程式碼的邏輯。然而,只需引入一個開關

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

相關內容