嵌套分形 tikz 装饰(科赫雪花)

嵌套分形 tikz 装饰(科赫雪花)

我正在尝试编写一些代码来允许使用装饰(例如来自的 Koch 雪花decorations.fractals),其中您提供一个参数来确定应用装饰的次数(以嵌套方式)。我尝试使用 pic,如下所述,但它会出现错误,因为装饰器要么有空参数,要么后面没有括号:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.fractals}

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

\tikzset{pics/koch/.style={decoration={Koch snowflake}
        code={\draw \koch{#1}}
    }
} 
\begin{tikzpicture}
    \path (0,0) pic {koch=4};
\end{tikzpicture}

\end{document}

我对此尝试了几种变体(它说\pic未定义)此代码说\tikz@parabola未定义并且错误似乎发生在pici和之间c(即错误消息分裂的地方)。

我最终想要的是一些代码,这样我就可以做类似的事情

\begin{tikzpicture}
  \draw (0,0) \koch{4};
  \draw (0,1) \koch{3};
\end{tikzpicture

获取 Koch 装饰器在某个间隔上的第 3 次和第 4 次迭代。我认为更好的方法是将装饰器和数字传递给某个对象,这样您就可以选择使用不同的分形装饰器。

答案1

这不是所需的答案,但是 Lindenmayer 系统怎么样?

\documentclass[tikz, border=5]{standalone}
\usetikzlibrary{lindenmayersystems}

\tikzset{koch snowflake/.style={insert path={%
   l-system [l-system={rule set={F -> F-F++F-F}, axiom=F++F++F,
    step=0.75cm/3^#1, angle=60, order=#1,anchor=center}] -- cycle}}}

\begin{document}
\tikz
  \foreach \i in {0,...,4}
    \fill (0,\i) [koch snowflake=\i] node [text=white, font=\sf] {\i}; 

\end{document}

在此处输入图片描述

答案2

\usepackage{tikz}
\usetikzlibrary{decorations.fractals}

循环

\def\deco{(C) -- (B) -- (A) --cycle}% "path at 0 decorations"
\let\decorationlist=\empty% create List
\foreach \n  in {1,...,\NoIterations}
{
  \ifx\empty\decorationlist{} \xdef\decorationlist{\deco}%
  \else \xdef\deco{decorate{\deco}} \xdef\decorationlist{\decorationlist,\deco}%
  \fi
}

生产

 (C) – (B) – (A)
–cycle ,decorate (C) – (B) – (A) –
cycle ,decoratedecorate (C) – (B) –
(A) –cycle ,decoratedecoratedecorate
(C) – (B) – (A) –cycle

这可能是一种蛮力方法,但它确实有效。

在此处输入图片描述

%\documentclass[margin=5pt, tikz]{standalone}
\documentclass[a4paper]{article}
\usepackage[margin=1cm]{geometry}
\usepackage{tikz}
\usetikzlibrary{decorations.fractals}

\begin{document}
\section{Simple Example}
\begin{tikzpicture}[decoration=Koch snowflake]
\coordinate (A) at (0,0);
\coordinate (B) at (3,0);
\coordinate (C) at (60:3);

\draw[] (A) -- (B) -- (C) --cycle; 
\draw[transform canvas={shift={(3.2,0)}}]  decorate{ (C) -- (B) -- (A) --cycle};
\draw[transform canvas={shift={(6.4,0)}}]  decorate{ decorate{ (C) -- (B) -- (A) --cycle} };
\end{tikzpicture}

%\newpage
\section{More systematic}
\pgfmathsetmacro\a{4.0}
\pgfmathtruncatemacro\NoIterations{6}
\pgfmathtruncatemacro\cols{3}

\pgfmathtruncatemacro\Cols{\cols-1}
\pgfmathtruncatemacro\Rows{ceil(\NoIterations/\cols)-1}
%\subsection{Info: cols: \cols, Cols: \Cols, Rows: \Rows}
\noindent\begin{tikzpicture}[decoration=Koch snowflake,
]
\coordinate (A) at (0,0);
\coordinate (B) at (\a,0);
\coordinate (C) at (60:\a);

\def\deco{(C) -- (B) -- (A) --cycle}% "path at 0 decorations"
\let\decorationlist=\empty% create List
\foreach \n  in {1,...,\NoIterations}
{
  \ifx\empty\decorationlist{} \xdef\decorationlist{\deco}%
  \else \xdef\deco{decorate{\deco}} \xdef\decorationlist{\decorationlist,\deco}%
  \fi
}
%\node[text width=6cm] at (0,-15) {Show decorationlist: \decorationlist};

\newcounter{mypos}
\setcounter{mypos}{-1}
\foreach \y in {0,...,\Rows} {
\foreach \x in {0,...,\Cols} {
\stepcounter{mypos}
\pgfmathsetmacro\Showcoord{\themypos < \NoIterations ? \themypos : ""}
\coordinate[label=left:{\Showcoord}] (Coord-\themypos) at (1.2*\a*\x,-1.2*\a*\y);
%\node[red, right] at (Coord-\themypos) {\x,\y};
}} 
%\draw[] (Coord-1) circle[radius=\a];

\foreach[count=\n from 0] \decorationset in \decorationlist {
%\node[blue, below] at (Coord-\n){ABC};
\draw[transform canvas={shift={(Coord-\n)}}] \decorationset;       
}
\end{tikzpicture}
\end{document}

相关内容