使用 LaTeX/TikZ 製作分形花

使用 LaTeX/TikZ 製作分形花

我正在嘗試生成一朵花的這種特殊的分形圖像,但甚至不知道從哪裡開始或如何開始。 TikZ 可以用來產生“葉序螺旋「?

我在這裡看到了:https://www.reddit.com/r/FractalPorn/comments/xr17t1/fractal_rose/

圖像如下所示: 分形影像

它看起來像很多點,所以我開始嘗試在 上使用散點圖pgfplots,但完全失敗了。沒有值得在這裡展示的程式碼。

編輯: 如何為多個答案打上綠色勾號?你們給的結果令人震驚!

答案1

我在用著桑迪·G 的公式到這裡,基本上就是這個.707^<level>部分了。

兩種方法:

  1. 使用 PGF 和 LuaLaTeXJLDiaz'偉大的泊松lua腳本
  2. TikZ 和 PGFmath 只使用該rnd函數。

此巨集\pgfpointspiralifdefined確保不需要重新計算已經計算的座標。

除了座標abcd,您還可以使用 的vertex錨點風箏形狀Sandy G 的答案使用的(當然你需要命名節點,例如spiral-\l-\n)。

在這兩種解決方案中,我都會對其中一個隨機值進行立方,以便這些點聚集到風箏的一側。

PGF + LuaLaTeX

\documentclass{standalone}
\usepackage{pgf,pgffor}
\usepackage{jldiaz-poisson}% https://tex.stackexchange.com/a/185423/16595
\usepackage{xcolor} % colorwheel
\definecolor{cw0}{HTML}{9AFF00}\definecolor{cw1}{HTML}{FFA500}
\definecolor{cw2}{HTML}{FF001A}\definecolor{cw3}{HTML}{FF00D9}
\definecolor{cw4}{HTML}{6500FF}\definecolor{cw5}{HTML}{005AFF}
\definecolor{cw6}{HTML}{00FFE5}\definecolor{cw7}{HTML}{00FF25}
\pgfset{
  declare function={
    spiralAngle(\level,\spiral) = \directlua{tex.print(
      180/(\pgfkeysvalueof{/pgf/spiral\space N})*\level
     +360/(\pgfkeysvalueof{/pgf/spiral\space N})*\spiral)};
    spiralRadius(\level)        = \directlua{tex.print(
      .707^\level*(\pgfkeysvalueof{/pgf/spiral\space radius}))};
    xSpread(\n)=\n^3*.8+.1; ySpread(\n)=\n*.8+.1;},
  spiral radius/.initial=5, spiral N/.initial=8}
\newcommand*\pgfpointspiral[2]{% #1 = level, #2 = spiral
  \pgfpointpolarxy{spiralAngle(#1,#2)}{spiralRadius(#1)}}
\makeatletter
\newcommand*\pgfpointspiralifdefined[3]{%
  % if spiral-#2-#3 doesn't exist, define it
  % if it does do nothing
  \pgfutil@ifundefined{pgf@sh@ns@spiral-#2-#3}{%
    \pgfcoordinate{spiral-#2-#3}{\pgfpointspiral{#2}{#3}}%
  }{}% and make it an alias for #1
  \pgfnodealias{#1}{spiral-#2-#3}}
\makeatother
\begin{document}
\begin{pgfpicture}
\pgfsetxvec{\pgfqpoint{5mm}{0mm}}
\pgfsetyvec{\pgfqpoint{0mm}{5mm}}
\foreach \l in {0,...,6}{
  \foreach \n in {0,...,7}{
    \pgfpointspiralifdefined{a}{\l}            {\n}
    \pgfpointspiralifdefined{b}{\inteval{\l+1}}{\n}
    \pgfpointspiralifdefined{c}{\l}            {\inteval{\n+1}}
    \pgfpointspiralifdefined{d}{\inteval{\l-1}}{\inteval{\n+1}}
    \pgfsetfillcolor{cw\n}
    \foreach[expand list, evaluate={\xSpread=xSpread(\x);}]
      \x/\y in {\poissonpointslist{1}{1}{.02+.0\l}{10}} {
      \pgfpathcircle{
        \pgfpointlineattime{ySpread(\y)}
          {\pgfpointlineattime{\xSpread}
            {\pgfpointanchor{a}{center}}{\pgfpointanchor{b}{center}}}
          {\pgfpointlineattime{\xSpread}
            {\pgfpointanchor{d}{center}}{\pgfpointanchor{c}{center}}}
      }{+.25pt}
      \pgfusepath{fill}
    }
  }
}
\end{pgfpicture}
\end{document}

TikZ + PGF 數學

也可以\pgfpointspiralifdefined透過自訂 TikZ 座標系來實現,但為什麼還要麻煩…

\documentclass[tikz]{standalone}
\usetikzlibrary{calc}
\pgfset{
  declare function={
    xSpread(\n)=\n^3*.8+.1; ySpread(\n)=\n*.8+.1;},
  spiral radius/.initial=5,
  spiral N/.initial=8}
\newcommand*\pgfpointspiral[2]{% #1 = level, #2 = spiral
  \pgfpointpolarxy{180/(\pgfkeysvalueof{/pgf/spiral N})*(#1)
                  +360/(\pgfkeysvalueof{/pgf/spiral N})*(#2)}
                  {.707^(#1)*(\pgfkeysvalueof{/pgf/spiral radius})}}
\makeatletter
\newcommand*\pgfpointspiralifdefined[3]{%
  \pgfutil@ifundefined{pgf@sh@ns@spiral-#2-#3}{%
    \pgfcoordinate{spiral-#2-#3}{\pgfpointspiral{#2}{#3}}%
  }{}%
  \pgfnodealias{#1}{spiral-#2-#3}}
\makeatother
\begin{document}
\begin{tikzpicture}[x=+5mm, y=+5mm]
\foreach \l[evaluate={\Dots=250*.7^\l}] in {0,...,6} {
  \foreach \n in {0,...,7} {
    \pgfpointspiralifdefined{a}{\l}            {\n}
    \pgfpointspiralifdefined{b}{\inteval{\l+1}}{\n}
    \pgfpointspiralifdefined{c}{\l}            {\inteval{\n+1}}
    \pgfpointspiralifdefined{d}{\inteval{\l-1}}{\inteval{\n+1}}
    \fill[radius=+.4pt] foreach[
      evaluate={\xSpread=xSpread rnd; \ySpread=ySpread rnd;}]
      \dot in {0,...,\Dots} {
        ($($(a)!\ySpread!(d)$)!\xSpread!($(b)!\ySpread!(c)$)$)
        circle[radius=+.4pt]};
  }
}
\end{tikzpicture}
\end{document}

輸出

在此輸入影像描述 在此輸入影像描述

答案2

這是玫瑰,它使用kitetikz 庫中的節點形狀shapes.geometric。我將其留給其他人用點填充節點。

在此輸入影像描述

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{shapes.geometric}

\tikzset{mykite/.style={inner sep=.1pt, kite, fill=red!30, kite vertex angles=120 and 75}}

\begin{document}

\begin{tikzpicture}
\foreach \l in {1,...,12}{
\foreach \n[evaluate=\n as \t using \l*22.5+\n*45, % \t is the angle for node placement
    evaluate=\n as \s using .703^(\l-1), % \s is the scaling factor for the distance from 0 for each level
    evaluate=\n as \c using \s-.01] in {1,...,8} % \c is used to shrink each node just a bit.
    {\node[mykite, rotate=\t, minimum size=\c cm] at (\t-90:{\s*1.38}){};}
}
\end{tikzpicture}

\end{document}

要獲得 8 頂點玫瑰,風箏頂角必須相差 45(上面代碼中的 120 和 75)。您可以更改.703(sin((\aaa-45)/2)/sin(\aaa/2)),其中\aaa是較大的角度以獲得形狀的變化。該1.38因素也需要調整。

在此輸入影像描述

答案3

在此輸入影像描述測試

pic我嘗試使用 a和螺旋狀曲線來建構。所有計算都隱藏在創建四邊形的petalPiece(元素)的定義中;pic每個花瓣都由碎片組成\nbQuad

有兩個全域變數:花瓣數量和每個花瓣的片數。例如,有 9 個花瓣,我們得到下圖。 在此輸入影像描述

程式碼

\documentclass[11pt, margin=1cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{math, calc}
\begin{document}

\tikzmath{
  integer \nbPetals, \nbQuad;
  \nbPetals = 8;
  \nbQuad = 7;
  real \a, \base, \r;
  \a = 360/\nbPetals;  % petal's angle
  \base = 2;  % base of the exponential definig the helix
  \r = .35;  % scaling constant
  function tmpR(\i) {% integer giving a point along the helix
    return {\r*pow(\base, 3.145*\i/\nbQuad)};
  };
}
\tikzset{%
  pics/petalPiece/.style 2 args={% branch number, base point number
    code={%
      \tikzmath{integer \b, \p; \b = #1; \p = #2;}
      \path
      (\p/\nbQuad*180 +\b*\a: {tmpR(\p)}) coordinate (NW)
      ({(\p +1)/\nbQuad*180 +\b*\a}: {tmpR(\p +1)}) coordinate (NE)
      ({(\p +2)/\nbQuad*180 +(\b -1)*\a}: {tmpR(\p +2)}) coordinate (SE)
      ({(\p +1)/\nbQuad*180 +(\b -1)*\a}: {tmpR(\p +1)}) coordinate (SW);
      \foreach \i in {0, .05, ..., .9}{%
        \draw[white, thick, fill=magenta!90, opacity=.05]
        ($(NW)!\i!(SW)$) -- ($(NE)!\i/2!(SE)$) -- (SE) -- (SW) -- cycle;
      }
    }
  }
}
\begin{tikzpicture}
  \foreach \l in {1, 2, ..., \nbPetals}{
    \foreach \i in {-1, 0, 1, 2, ..., \nbQuad}{%
      \path (0, 0) pic {petalPiece={\l}{\i}};
    }
  }
\end{tikzpicture}

相關內容