我正在尝试生成这种特殊的花朵分形图像,但甚至不知道从哪里开始或如何开始。TikZ 可以用来生成“叶序螺旋“?
我在这里看到了:https://www.reddit.com/r/FractalPorn/comments/xr17t1/fractal_rose/
看起来像是有很多点,所以我开始尝试使用散点图pgfplots
,但完全失败了。这里没有值得展示的代码。
编辑: 我如何为多个答案打上绿色勾?你们给出的结果令人震惊!
答案1
我在用着Sandy G 的配方基本上就是这个.707^<level>
部分。
两种方法:
- 使用 PGF 和 LuaLaTeXJLDiaz 的出色 poisson lua 脚本。
- TikZ 和 PGFmath 仅使用该
rnd
函数。
该宏\pgfpointspiralifdefined
确保已经计算过的坐标不需要重新计算。
除了坐标a
、b
和c
,d
您还可以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 + PGFmath
也可以\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
这是玫瑰,它使用了kite
tikz 库中的节点形状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
的定义中;每个花瓣将由碎片组成。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}