Tikz 分形 - 坎托塵埃

Tikz 分形 - 坎托塵埃

有人可以幫我在 Tikz 畫下面的分形嗎? 在此輸入影像描述

在這裡,我們從單位正方形開始,將其分成 16 個相等的正方形,並刪除其中的 12 個 - 留下 4 個按所示排列。對剩餘的方塊重複此過程,依此類推。

我很確定我需要使用類似的東西嵌套分形 tikz 裝飾品但我對 TeX 的掌握不夠好,無法自己編譯程式碼。

先感謝您。

到目前為止我的程式碼:

 \newcommand{\dust}[1]{
     \foreach \i in {1,...,#1}{decorate\{}
     (0,0)--(1,1)
     \foreach \i in {1,...,#1}{\}};

      SOMETHING IN HERE (DON'T KNOW)
 }

 \begin{tikzpicture}
     \path (0,0) pic {dust=5};
 \end{tikzpicture}

答案1

這是一個很好的 Lindenmayer 系統練習。

\documentclass[border=9,tikz]{standalone}
\usetikzlibrary{lindenmayersystems}
\begin{document}
\pgfdeclarelindenmayersystem{Cantor dust}{
    \symbol{S}{\pgfpathrectangle{\pgfpointorigin}{\pgfpoint{\pgflsystemcurrentstep}{\pgflsystemcurrentstep}}}
    \symbol{U}{\pgftransformyshift{\pgflsystemcurrentstep}}
    \symbol{R}{\pgftransformxshift{\pgflsystemcurrentstep}}
    \rule{S -> [UUSURRS][RSRRUS]}
    \rule{U -> UUUU}
    \rule{R -> RRRR}
}
\tikz;
\tikz\fill[lindenmayer system={Cantor dust,axiom=S,step=320pt,order=1}]lindenmayer system;
\tikz\fill[lindenmayer system={Cantor dust,axiom=S,step= 80pt,order=2}]lindenmayer system;
\tikz\fill[lindenmayer system={Cantor dust,axiom=S,step= 20pt,order=3}]lindenmayer system;
\tikz\fill[lindenmayer system={Cantor dust,axiom=S,step=  5pt,order=4}]lindenmayer system;
\end{document}

答案2

我剛剛發現這個主題,它與另一個最近的主題相關。它可以被認為是遞歸程式設計的應用,其中元郵報通常很適合。因此,這是一個 MetaPost 解決方案,可能會引起人們的興趣。

def cantor_dust(expr x, y, d, n) = 
    if n > 0:
        cantor_dust(x+.25d, y, .25d, n-1);
        cantor_dust(x+.75d, y+.25d, .25d, n-1);
        cantor_dust(x+.5d, y+.75d, .25d, n-1);
        cantor_dust(x, y+.5d, .25d,  n-1);
    else: fill (x, y) -- (x+d, y) -- (x+d, y+d) -- (x, y+d) -- cycle; fi
enddef;
beginfig(1);
    for i = 0 upto 4:
        draw image(cantor_dust(0, 0, 4cm, i)) shifted (4.25cm*i, 0);
    endfor;
endfig;
end.

在此輸入影像描述

相關內容