如何绘制没有立方体分隔线的平面分区

如何绘制没有立方体分隔线的平面分区

我正在尝试适应“平面分区”例子由 Jang Soo Kim 绘制的平面分区,立方体之间没有线条。我尝试通过赋予这些线条与立方体表面相同的颜色来实现这一点。也就是说,我将立方体顶面的“draw=black”替换为“draw=yellow”(参见本问题底部的 MWE)。这导致下面的平面分区。

可以看出,在这个平面分区中,仍然有细线将立方体分开。我曾尝试过去除这些线条,但到目前为止还没有成功。下面给出了平面分区的 MWE,是否可以对其进行调整,以便完全去除立方体之间的线条?

在此处输入图片描述

\documentclass{minimal}
\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=yellow, draw=yellow,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=red, draw=red,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=blue, draw=blue,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 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}
        \cube{\value{x}}{\value{y}}{\value{z}}
      }
    }
  }
}

\begin{document} 

\begin{tikzpicture}
\planepartition{{5,3,2,2},{4,2,2,1},{2,1},{1}}
\end{tikzpicture}

\end{document}

答案1

我首先提出了解决方案 2,最后我注意到还有非常简单的解决方案 1!

解决方案 1(简单)

只需添加line width=0pt绘制边的三个命令。

解决方案 2(复杂)

只画出可见的东西。

改编

  • 使用count=\x等代替计数器foreach
  • leftside并将rightside高度作为第四个参数
  • remember最后一些变量
  • 保存一些变量以供以后在 for 循环之后使用\xdef

结果

在此处输入图片描述

代码

\documentclass[margin=3mm]{standalone}

\usepackage{etoolbox}

\usepackage{tikz}

% 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=yellow, draw=yellow, line width=0pt, shift={(\xaxis:#1)}, shift={(\yaxis:#2)},
    shift={(\zaxis:#3)}] (0,0) -- (30:1) -- (0,1) --(150:1) -- cycle;
}

% The left side of a cube
\newcommand\leftside[4]{
    \fill[fill=red, draw=red, line width=0pt, shift={(\xaxis:#1)}, shift={(\yaxis:#2)},
    shift={(\zaxis:#3)}] (0,0) -- (0,-#4) -- ++(150:1) --++ (0,#4) -- cycle;
}

% The right side of a cube
\newcommand{\rightside}[4]{
    % #1, #2, #3 = x, y, z
    % #4 = height
    \fill[fill=blue, draw=blue, line width=0pt, shift={(\xaxis:#1)}, shift={(\yaxis:#2)},
    shift={(\zaxis:#3)}] (0,0) -- (30:1) -- ++(0,-#4) --(0,-#4) -- cycle;
}

% 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]{
    \def\lasty{0}
    \foreach \a [count=\x, remember=\a as \lasta, remember=\x as \lastx] in {#1} {
        \foreach \height [
            count=\y,
            remember=\height as \lastheight,
            remember=\y as \yy,
        ] in \a {
            % topside
            \topside{\x}{\y}{\height}
            
            % right side
            \ifnumless{\height}{\lastheight}{
                \pgfmathsetmacro{\h}{\lastheight-\height}
                \rightside{\x}{\yy}{\lastheight}{\h}
            }{}
            
            % save values for last right side
            \xdef\height{\height}
            \xdef\y{\y}
            
            % draw left sides
            \ifnumgreater{\lasty}{\y-1}{
                \pgfmathsetmacro{\lastlineheight}{{\lasta}[\y-1]}
                \pgfmathsetmacro{\heightdiff}{int(\lastlineheight - \height)}
                \ifnumgreater{\heightdiff}{0}{
                    \leftside{\lastx}{\y}{\lastlineheight}{\heightdiff}
                }{}
            }{}
        }
        % draw last right side
        \rightside{\x}{\y}{\height}{\height}
        
        % draw left sides
        \ifnumgreater{\lasty}{\y}{
            \foreach \yi in {\y, ..., \lasty} {
                \pgfmathsetmacro{\lastlineheight}{{\lasta}[\yi-1]}
                \ifnumless{\yi-1}{\y}{
                    \pgfmathsetmacro{\currentlineheight}{{\a}[\yi-1]}
                }{
                    \def\currentlineheight{0}
                }
                \pgfmathsetmacro{\heightdiff}{int(\lastlineheight - \currentlineheight)}
                \ifnumgreater{\heightdiff}{0}{
                    \leftside{\lastx}{\yi}{\lastlineheight}{\heightdiff}
                }{}
            }
        }{}
        
        \xdef\lasty{\y}
        \xdef\a{\a}
        \xdef\x{\x}
    }
    
    % draw last left side
    \foreach \yi in {1, ..., \lasty} {
        \pgfmathsetmacro{\lastlineheight}{{\a}[\yi-1]}
        \pgfmathsetmacro{\heightdiff}{int(\lastlineheight - 0)}
        \ifnumgreater{\heightdiff}{0}{
            \leftside{\x}{\yi}{\lastlineheight}{\heightdiff}
        }{}
    }
}

\begin{document}

\begin{tikzpicture}
    \planepartition{{5,3,2,2},{4,2,2,1},{2,1},{1}}
\end{tikzpicture}

\end{document}

相关内容