TiKz-用匹配绘制递归图

TiKz-用匹配绘制递归图

我想制作以下抽奖活动及更多活动。有人能给我一些提示,让我轻松实现这个目标吗?

我发现以下帖子可以绘制一个匹配项: 使用 TikZ 绘制匹配

请原谅我没有给出 MWE。

在此处输入图片描述

答案1

概念上与 Vinzza 的答案类似,但使用pics 代替宏,并且可以用 绘制单个塔matches。单个塔match的获得方式为pic=match

\documentclass[svgnames,tikz,border=3mm]{standalone}
\tikzset{pics/match/.style={code={%
    \fill [PeachPuff] (0,0) rectangle (0+4,0+0.2);
    \fill [PeachPuff!60!Black] (0,0) -- ++(4,0)-- ++(0.1,-0.05) -- ++(-4,0) -- ++(-0.1,0.05);
    \draw (0,0) -- ++(0,0.2) -- ++(4,0) -- ++(0,-0.2) -- ++(0.1,-0.05) -- ++(-4,0) -- ++(-0.1,0.05);
    \shade[ball color=red] (0+4,0+0.1) ellipse (0.25cm and 0.22cm);
    \draw (0+4,0+0.1) ellipse (0.25cm and 0.22cm);
}},pics/matches/.style={code={
    \foreach \YY in {0,...,#1}
    {\path foreach \XX in {0,...,\YY}
    { (-2.2-4.4*\XX,-2.2+4.8*#1-4.8*\YY)pic[rotate=90]{match} (-2.2-4.4*\XX,2.2+4.8*#1-4.8*\YY)pic{match}
    (-2.2+4.4*\XX,-2.2+4.8*#1-4.8*\YY)pic[rotate=90]{match} (-2.2+4.4*\XX,2.2+4.8*#1-4.8*\YY)pic{match}}
    (2.2+4.4*\YY,-2.2+4.8*#1-4.8*\YY)pic[rotate=90]{match};}
}}}

\begin{document}

\begin{tikzpicture}[scale=0.25,transform shape]
\path foreach \Z in {0,...,5} 
{({9*\Z*(\Z+1)/2+2*\Z},0) pic{matches=\Z}
node[below=3cm,scale=6]{$\the\numexpr\Z+1$ stories}};
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

您可以使用多个 \foreach 来完成此操作,这可能不是最漂亮的方法,但我发现它非常简单!

请注意,我在命令中添加了移动/缩放/旋转的范围\allumette(在您提供的链接中)!

\documentclass[border=5mm]{standalone}

\usepackage[svgnames]{xcolor}
\usepackage{tikz}

\newcommand\allumette[3]{
  \begin{scope}[line width=.2,shift={(#1,#2)},rotate=#3,scale=.2,shift={(.25,0)}]
    \fill [PeachPuff] (0,0) rectangle (0+4,0+.2);
    \fill [PeachPuff!60!Black] (0,0) -- ++(4,0)-- ++(0.1,-0.05) -- ++(-4,0) -- ++(-0.1,0.05);
    \draw (0,0) -- ++(0,.2) -- ++(4,0) -- ++(0,-.2) -- ++(0.1,-0.05) -- ++(-4,0) -- ++(-0.1,0.05);
    \shade[ball color=red] (0+4,0+0.1) ellipse (0.25cm and 0.22cm);
    \draw (0+4,0+0.1) ellipse (0.25cm and 0.22cm);
  \end{scope}
}

\begin{document}

\begin{tikzpicture}

  %% Set the position of the next tower
  \coordinate (dum) at (0,0);

  \foreach \t in {0,...,5}{  
    \begin{scope}[shift={(dum)}]

      %%%%%%%%%% Draw to tower of height \t+1 %%%%%
      \foreach \z in {0,...,\t}{
        \foreach \x in {-\z,...,\z}{
          \allumette{\t+\x}{\t+1-\z}{0} % Horizontal
          \allumette{\t+\x}{\t-\z}{90}  % Vertical
        }
        \allumette{\t+\z+1}{\t-\z}{90} % Last one on the line
      }
      %%%%%%%%%% Draw to tower of height \t+1 %%%%%

      %% The text under the tower
      \pgfmathtruncatemacro\foo{\t+1}
      \node[anchor=north] at (\t+.5,-.5) {\bfseries\foo{} \'etages};

      %% Set the position of the next tower
      \coordinate (dum) at (2*\t+2,0);
    \end{scope}
  }

\end{tikzpicture}

\end{document}

这使 结果,比赛塔

相关内容