如何制作谢尔宾斯基地毯的迭代

如何制作谢尔宾斯基地毯的迭代

我想要做的是创建谢尔宾斯基地毯的迭代序列。谢尔宾斯基地毯的创建方法是将正方形分成九个全等的子正方形并移除中间的子正方形,然后将剩余的子正方形分成九个较小的子正方形并移除中间的子正方形,然后重复该过程。

下图是我使用 tikz 绘制的,并在必要时填充每个矩形。然而,这是一个繁琐且低效的过程,特别是因为下一次迭代需要额外的 512 个白色矩形。

谢尔宾斯基地毯

\begin{tikzpicture}
\fill (0, 0) rectangle (1, 1);
\begin{scope}[xshift = 1.5 cm]
\fill (0, 0) rectangle (1, 1);
\fill[color=white] (1/3, 1/3) rectangle (2/3, 2/3);
\end{scope}
\begin{scope}[xshift = 3 cm]
\fill (0, 0) rectangle (1, 1);
\fill[color=white] (1/3, 1/3) rectangle (2/3, 2/3);
\fill[color=white] (1/9, 1/9) rectangle (2/9, 2/9);
\fill[color=white] (4/9, 1/9) rectangle (5/9, 2/9);
\fill[color=white] (7/9, 1/9) rectangle (8/9, 2/9);
\fill[color=white] (1/9, 4/9) rectangle (2/9, 5/9);
\fill[color=white] (7/9, 4/9) rectangle (8/9, 5/9);
\fill[color=white] (1/9, 7/9) rectangle (2/9, 8/9);
\fill[color=white] (4/9, 7/9) rectangle (5/9, 8/9);
\fill[color=white] (7/9, 7/9) rectangle (8/9, 8/9);
\end{scope}
\begin{scope}[xshift = 4.5 cm]
\fill (0, 0) rectangle (1, 1);
\fill[color=white] (1/3, 1/3) rectangle (2/3, 2/3);
\fill[color=white] (1/9, 1/9) rectangle (2/9, 2/9);
\fill[color=white] (4/9, 1/9) rectangle (5/9, 2/9);
\fill[color=white] (7/9, 1/9) rectangle (8/9, 2/9);
\fill[color=white] (1/9, 4/9) rectangle (2/9, 5/9);
\fill[color=white] (7/9, 4/9) rectangle (8/9, 5/9);
\fill[color=white] (1/9, 7/9) rectangle (2/9, 8/9);
\fill[color=white] (4/9, 7/9) rectangle (5/9, 8/9);
\fill[color=white] (7/9, 7/9) rectangle (8/9, 8/9);
\fill[color=white] (4/81, 4/81) rectangle (5/81, 5/81);
\fill[color=white] (13/81, 4/81) rectangle (14/81, 5/81);
\fill[color=white] (22/81, 4/81) rectangle (23/81, 5/81);
\fill[color=white] (31/81, 4/81) rectangle (32/81, 5/81);
\fill[color=white] (40/81, 4/81) rectangle (41/81, 5/81);
\fill[color=white] (49/81, 4/81) rectangle (50/81, 5/81);
\fill[color=white] (58/81, 4/81) rectangle (59/81, 5/81);
\fill[color=white] (67/81, 4/81) rectangle (68/81, 5/81);
\fill[color=white] (76/81, 4/81) rectangle (77/81, 5/81);
\fill[color=white] (4/81, 13/81) rectangle (5/81, 14/81);
\fill[color=white] (22/81, 13/81) rectangle (23/81, 14/81);
\fill[color=white] (31/81, 13/81) rectangle (32/81, 14/81);
\fill[color=white] (49/81, 13/81) rectangle (50/81, 14/81);
\fill[color=white] (58/81, 13/81) rectangle (59/81, 14/81);
\fill[color=white] (76/81, 13/81) rectangle (77/81, 14/81);
\fill[color=white] (4/81, 22/81) rectangle (5/81, 23/81);
\fill[color=white] (13/81, 22/81) rectangle (14/81, 23/81);
\fill[color=white] (22/81, 22/81) rectangle (23/81, 23/81);
\fill[color=white] (31/81, 22/81) rectangle (32/81, 23/81);
\fill[color=white] (40/81, 22/81) rectangle (41/81, 23/81);
\fill[color=white] (49/81, 22/81) rectangle (50/81, 23/81);
\fill[color=white] (58/81, 22/81) rectangle (59/81, 23/81);
\fill[color=white] (67/81, 22/81) rectangle (68/81, 23/81);
\fill[color=white] (76/81, 22/81) rectangle (77/81, 23/81);
\fill[color=white] (4/81, 31/81) rectangle (5/81, 32/81);
\fill[color=white] (13/81, 31/81) rectangle (14/81, 32/81);
\fill[color=white] (22/81, 31/81) rectangle (23/81, 32/81);
\fill[color=white] (58/81, 31/81) rectangle (59/81, 32/81);
\fill[color=white] (67/81, 31/81) rectangle (68/81, 32/81);
\fill[color=white] (76/81, 31/81) rectangle (77/81, 32/81);
\fill[color=white] (4/81, 40/81) rectangle (5/81, 41/81);
\fill[color=white] (22/81, 40/81) rectangle (23/81, 41/81);
\fill[color=white] (58/81, 40/81) rectangle (59/81, 41/81);
\fill[color=white] (76/81, 40/81) rectangle (77/81, 41/81);
\fill[color=white] (4/81, 49/81) rectangle (5/81, 50/81);
\fill[color=white] (13/81, 49/81) rectangle (14/81, 50/81);
\fill[color=white] (22/81, 49/81) rectangle (23/81, 50/81);
\fill[color=white] (58/81, 49/81) rectangle (59/81, 50/81);
\fill[color=white] (67/81, 49/81) rectangle (68/81, 50/81);
\fill[color=white] (76/81, 49/81) rectangle (77/81, 50/81);
\fill[color=white] (4/81, 58/81) rectangle (5/81, 59/81);
\fill[color=white] (13/81, 58/81) rectangle (14/81, 59/81);
\fill[color=white] (22/81, 58/81) rectangle (23/81, 59/81);
\fill[color=white] (31/81, 58/81) rectangle (32/81, 59/81);
\fill[color=white] (40/81, 58/81) rectangle (41/81, 59/81);
\fill[color=white] (49/81, 58/81) rectangle (50/81, 59/81);
\fill[color=white] (58/81, 58/81) rectangle (59/81, 59/81);
\fill[color=white] (67/81, 58/81) rectangle (68/81, 59/81);
\fill[color=white] (76/81, 58/81) rectangle (77/81, 59/81);
\fill[color=white] (4/81, 67/81) rectangle (5/81, 68/81);
\fill[color=white] (22/81, 67/81) rectangle (23/81, 68/81);
\fill[color=white] (31/81, 67/81) rectangle (32/81, 68/81);
\fill[color=white] (49/81, 67/81) rectangle (50/81, 68/81);
\fill[color=white] (58/81, 67/81) rectangle (59/81, 68/81);
\fill[color=white] (76/81, 67/81) rectangle (77/81, 68/81);
\fill[color=white] (4/81, 76/81) rectangle (5/81, 77/81);
\fill[color=white] (13/81, 76/81) rectangle (14/81, 77/81);
\fill[color=white] (22/81, 76/81) rectangle (23/81, 77/81);
\fill[color=white] (31/81, 76/81) rectangle (32/81, 77/81);
\fill[color=white] (40/81, 76/81) rectangle (41/81, 77/81);
\fill[color=white] (49/81, 76/81) rectangle (50/81, 77/81);
\fill[color=white] (58/81, 76/81) rectangle (59/81, 77/81);
\fill[color=white] (67/81, 76/81) rectangle (68/81, 77/81);
\fill[color=white] (76/81, 76/81) rectangle (77/81, 77/81);
\end{scope}
\end{tikzpicture}

我希望做的是使用 lindenmayer 系统调整 Jake 的解决方案以如何在 LaTeX 中创建谢尔宾斯基三角形?变成矩形,就像马苏皮拉姆对六边形所做的那样Tikz 分形 - 谢尔宾斯基六边形

我知道之前有人问过关于制作谢尔宾斯基地毯的问题。Mark Wibrow 的回答使用 tikz 生成谢尔宾斯基地毯没有使用 lindenmayer 系统,导致我的系统挂起。Henri Menke 的回答在 Tikz 中绘制简单的分形很漂亮,但是当我降低分形的阶数并使其位于顶点而不是侧面时,它就变得很小了。我希望每次迭代都保持相同的大小,如上所示。

我想如果我设置 order=\level,我会想要设置 \squarewidth=9^\level,因为每个正方形被分成九个正方形,并且我应该设置 angle=90,因为每个角度都是直角。但是,我不知道如何调整 Jake 和 marsupilam 在他们的答案中使用的符号 X 和 Y 的规则来生成上面显示的模式。

答案1

这是一个将白色节点放置在适当位置的宏。

\sierpinski[options]{levels}

我想到的选项是scale=,但rotate=同样有效。或者xscale=如果你想要不是正方形的矩形。请务必使用transform shape

在此处输入图片描述

\documentclass{article}

\usepackage{tikz}

\newcommand{\sierpinski}[2][]{\tikz[#1]{
  \draw[fill=black] rectangle(1,1);
    \foreach \n[evaluate=\n as \m using \n-1, evaluate=\n as \s using 1/3^\n, evaluate=\m as \p using 3^\m] in {1,...,#2}{
      \foreach \k[evaluate=\k as \x using (2*\k-1)/2/3^\m] in {1,...,\p}{
        \foreach \j[evaluate=\j as \y using (2*\j-1)/2/3^\m] in {1,...,\p}{
          \node[fill=white, minimum size=\s cm, inner sep=0] at (\x,\y){};
}}}}}

\begin{document}

\tikz{\draw[fill=black] rectangle(1,1);}\quad\sierpinski{1}\quad\sierpinski{2}\quad\sierpinski{3}\quad\sierpinski{4}

\end{document}

这是\sierpinski[scale=3, transform shape]{5},这几乎就是我的机器可以处理的全部。它是 O(9ⁿ),所以请做好等待的准备。

在此处输入图片描述

答案2

拥有最新的https://ctan.org/pkg/pst-fractal并运行 lualatex

\documentclass[pstricks]{standalone}
\usepackage{pst-fractal} 
\begin{document}    

\begin{pspicture}(18,3)
\multido{\iA=1+1,\iB=0+12}{6}{%
  \psSierCarpet[scale=0.25,n=\iA](\iB,0.2)}
\end{pspicture}

\end{document}

在此处输入图片描述

并带有选项basecolor=red,linecolor=blue

在此处输入图片描述

答案3

您已经得到了想要的 tikz 答案,所以我可以随意添加使用 MetaPost/MetaFun 制作的答案。代码可能可以优化,也许可以用 tikz 做类似的事情(但我做不到)。第一个版本绘制一个正方形并循环并取消填充应该是白色的部分。第二个使用递归。

关于时间:

First version: 2.9s
Second version: 1.8s

我已将其包装到 ConTeXt lmtx 中的 MetaPost 页面中。该文件可以用 进行编译context

\starttext
\startMPpage[offset=1dk]
vardef sierpinski(expr w, n) =
image(
fill unitsquare scaled w ;
for i = 1 upto n :
    for j = 1 upto (3^(i-1)) :
        for k = 1 upto (3^(i-1)) :
            unfill unitsquare scaled (w/(3^i)) shifted ( (3*j-2)*w/(3^i), (3*k-2)*w/(3^i) ) ;
        endfor
    endfor
endfor)
enddef ;

for i = 1 upto 3 :
    draw sierpinski(3cm,i)   shifted (4*(i-1)*cm,  0  ) ;
    draw sierpinski(3cm,i+3) shifted (4*(i-1)*cm, -4cm) ;
endfor ;
\stopMPpage

\startMPpage[offset=1dk]
vardef Sierpinski(expr w,n) =
    save tmppic ;
    picture tmppic ;
    if n = 1 :
        image(
        fill unitsquare scaled w ;
        unfill unitsquare scaled (w/3) shifted (w/3,w/3) ;
        )
    else :
        tmppic := Sierpinski(w, n - 1) scaled 1/3 ;
        image(
        for i = 1 upto 3 :
            for j = 1 upto 3 :
                if ((i*j) <> 4) :
                    draw tmppic shifted (((i-1)/3)*w,((j-1)/3)*w) ;
                fi
            endfor
        endfor
        ) 
    fi
enddef ;

for i = 1 upto 3 :
    draw Sierpinski(3cm,i)   shifted (4*(i-1)*cm,  0  ) ;
    draw Sierpinski(3cm,i+3) shifted (4*(i-1)*cm, -4cm) ;
endfor ;
\stopMPpage
\stoptext

据我所知,两种变体的输出看起来相同,因此我只显示其中一种。

答案4

这是一个纯 LaTeX 和递归版本。

这显然只在您使用矩形(用 可以很容易地放置tabular)时才有效。

代码

\documentclass{article}
\newcommand*\startCarpet[2][1em]{{%
  \renewcommand*\arraystretch{0}%
  \setlength\fboxsep{0pt}\setlength\fboxrule{#1}%
  \edef\BOX{\noexpand\fbox{%
    \noexpand\rule[-\the\dimexpr#1/2\relax]{0pt}{#1}\noexpand\rule{#1}{0pt}}}%
  \def\TAB##1{\tabular{@{}c@{}c@{}c@{}}##1&##1&##1\\##1&&##1\\##1&##1&##1\endtabular}%
  \def\level{#2}%
  \def\doCarpet{%
    \ifnum\level=0
      \def\next{\TAB{\BOX}}%
    \else
      \def\next{%
        \edef\level{\inteval{\level-1}}%
        \TAB{\doCarpet}}%
    \fi
    \next}
  \doCarpet}}
\begin{document}
\centering
\startCarpet{0}
\startCarpet[.3333em]{1}
\startCarpet[.1111em]{2}

\startCarpet[.117em]{3}
\end{document}

输出

相关内容