Matrix67 博客里漸變的分形動畫效果是怎麼實現的?

7 個分形圖形的動畫演示

http://www.matrix67.com/blogimage_2014/fractal_gifs/koch.gif

類似 這種效果 是比較容易的


就是想辦法做了一下插值而已

放上我當時仿的代碼吧

order = 5;
step = 8;
next = If[#2 == 0, {#1}, N@{{#1[[1]], 2/3 #1[[1]] + 1/3 #1[[2]]},
{2/3 #1[[1]] + 1/3 #1[[2]],
2/3 #1[[1]] + 1/3 #1[[2]] +
Exp[#2 I [Pi]/3]/3 (#1[[2]] - #1[[1]])},
{1/3 #1[[1]] + 2/3 #1[[2]] +
Exp[-#2 I [Pi]/3]/3 (#1[[1]] - #1[[2]]),
1/3 #1[[1]] + 2/3 #1[[2]]},
{1/3 #1[[1]] + 2/3 #1[[2]], #1[[2]]}}] ;
nextstep = next[#, 1] ;
node = NestList[
Flatten[nextstep /@ #, 1] , {{0, 1}, {1,
Exp[-I [Pi]/3]}, {Exp[-I [Pi]/3], 0}}, order];
result = Table[
Flatten[next[#, FractionalPart[k]] /@ node[[IntegerPart[k]]],
1], {k, 1, order + 1 - 1/step, 1/step}];
preplot = Map[{Re[#], Im[#]} , result, {3}];

data = Table[
Graphics[{Blue, Line /@ (preplot[[i]])},
PlotRange -&> {{0, 1}, {-Sqrt[3]/2, Sqrt[3]/6}},
ImageSize -&> 600], {i, Length@preplot}];
data = Flatten[{data, Reverse[data]}, 1];

ListAnimate[data, AnimationRate -&> 12]

圖片見http://fmn.rrimg.com/fmn066/20141208/1345/original_HJ6M_78d300007343125f.gif

然後另外一個做了點漸變的處理,代碼如下

(*triangle*)
order = 5;
step = 100;
next = Table[
N@{#[[1]] + #[[2]] Exp[2 [Pi] I k/6], #[[2]]/3, (-1)^k}, {k,
6}] ;
node = NestList[Flatten[next /@ #, 1] , {{0, 1, 1}}, order];
result = Table[
Flatten[{{#[[1]], #[[2]], #[[3]] + 2*FractionalPart[k]} /@
node[[IntegerPart[k]]], node[[IntegerPart[k]]]}, 1], {k,
Table[IntegerPart[g] + Sin[FractionalPart[g] [Pi]/2]^2, {g, 1,
order - 1/step, 1/step}]}];
preplot =
Map[Function[{x}, {Re[x], Im[x]}] /@
Table[#[[1]] +
3/2 #[[2]] Exp[
2 [Pi] I/3*g + [Pi] I/6*(#[[3]] - 1)*Sign[#[[3]] - 1]], {g,
3}] , result, {2}];

data = Table[
Graphics[{FaceForm[Opacity[0]],
EdgeForm[{GrayLevel[0.33]}], Polygon[#] /@ (preplot[[k + 1]]),
EdgeForm[{GrayLevel[0.33]}], Polygon[#] /@ (preplot[[k + 2]]),
EdgeForm[{GrayLevel[0.33]}], Polygon[#] /@ (preplot[[k + 3]])
}, PlotRange -&> {{-1, 1}, {-1, 1}}*3/2]
, {k, 1, Length[result], 5}];
data = Flatten[{data, Reverse[data]}, 1];

ListAnimate[data, AnimationRate -&> 30]

圖片見http://fmn.rrimg.com/fmn066/20141208/1940/original_Qe7T_14360000c734118c.gif

下面這張是另外一個風格的,

order = 7;
step = order*8;
p = 1;
q = 0.9;
next = If[#2 == 0, {#1},
N@{{#1[[1]], #1[[1]] +
p/2 ( #1[[2]] - #1[[1]]) Exp[q I #2 #1[[3]] [Pi]/3], -#1[[
3]]},
{#1[[1]] + p/2 ( #1[[2]] - #1[[1]]) Exp[q I #2 #1[[3]] [Pi]/3],
p/2 (#1[[1]] - #1[[2]]) Exp[-q I #2 #1[[3]] [Pi]/3] + #1[[
2]], #1[[3]]},
{p/2 (#1[[1]] - #1[[2]]) Exp[-q I #2 #1[[3]] [Pi]/3] + #1[[
2]], #1[[2]], -#1[[3]]}}] ;
nextstep = next[#, 1] ;
node = NestList[Flatten[nextstep /@ #, 1] , {{0, 1, 1}}, order];
result = Table[
Flatten[next[#, FractionalPart[k]] /@ node[[IntegerPart[k]]],
1], {k, Table[
IntegerPart[g] + Sin[FractionalPart[g] [Pi]/2]^2, {g, 1,
order - 1/step, 1/step}]}];
preplot = Map[{Re[#], Im[#]} , result[[;; , ;; , {1, 2}]], {3}];
data = Table[
Graphics[{Blue, Line /@ (preplot[[i]])},
PlotRange -&> {{0, 1}, {0, Sqrt[3]/2}}, ImageSize -&> 600], {i,
Length@preplot}];
data = Flatten[{data, Reverse[data]}, 1];

ListAnimate[data, AnimationRate -&> 24]

圖片見http://fmn.rrimg.com/fmn066/20150129/2115/original_xCZs_648800016f571191.gif

圖比較大,請耐心載入

另外你可以看一下這本書 The Algorithmic Beauty of Plants

網上有電子版,那裡面描述了如何用分形結構(其實應該不叫分形了吧...叫L-system)去畫樹木...畫的非常像。但是我嘗試了一下,沒什麼好辦法將其連續化

隨便截了個


不是非常了解,僅看過Mandelbrot 的大自然的分形幾何學 (豆瓣)55,嘗試著回答一下。

你需要看一下分形圖形的生成機制,分形圖形最重要的一點就是自相似性(self-similarity),就是次一層級的圖形和原來的圖形相似,可以採用迭代器的方式進行產生。

題主給的例子是在一般的分形教材都會接觸到的,叫科赫曲線(Koch curve),就是每次將線段三等分,中間的那三分之一外延做正三角形。

比如迭代5次之後是這個樣子,代碼網上有很多,我貼一個 作者 Dagmar Scherzinger 用 MATLAB 寫的吧

function koch(level)

% KOCH(LEVEL) Draw the Koch curve fractal. LEVEL denotes the number

% of iterations to perform.

% Example:

% koch(5);

xl = zeros(10,1);

xr = xl;

yl = xl;

yr = yl;

xr(level) = 1;

r = sqrt(1/3^2 - 1/6^2);

clf;

set(gca,"FontSize",14);

set(gcf,"Color",[1,1,1]);

hold on;

subkoch(xl,xr,yl,yr,level,r);

title("Koch Curve");

text(0.5,-0.05,(["Number of iterations: " num2str(level)]), ...

"HorizontalAlign","center","FontSize",12);

hold off;

axis equal; axis tight; axis off;

%------------------------------------------------------------

function subkoch(xl,xr,yl,yr,level,r)

%Zeichne eine Linie auf der niedrigsten Level der Rekursion

if (level&<2)

plot([xl(1) xr(1)],[-yl(1) -yr(1)],"b-")

return

end

%Verzweige in niedrigere Stufen

level=level-1;

%Linker Zweig

xl(level)=xl(level+1);

yl(level)=yl(level+1);

xr(level)=1/3*xr(level+1)+2/3*xl(level+1);

yr(level)=1/3*yr(level+1)+2/3*yl(level+1);

subkoch(xl,xr,yl,yr,level,r);

%Mittlerer linker Zweig

xl(level)=xr(level);

yl(level)=yr(level);

xr(level)=.5*xr(level+1)+.5*xl(level+1)-r*(yl(level+1)-yr(level+1));

yr(level)=.5*yr(level+1)+.5*yl(level+1)+r*(xl(level+1)-xr(level+1));

subkoch(xl,xr,yl,yr,level,r);

%Mittlerer rechter Zweig

xl(level)=xr(level);

yl(level)=yr(level);

xr(level)=2/3*xr(level+1)+1/3*xl(level+1);

yr(level)=2/3*yr(level+1)+1/3*yl(level+1);

subkoch(xl,xr,yl,yr,level,r);

%Rechter Zweig

xl(level)=xr(level);

yl(level)=yr(level);

xr(level)=xr(level+1);

yr(level)=yr(level+1);

subkoch(xl,xr,yl,yr,level,r);

level=level+1;

return;

再來幾個分形圖


推薦閱讀:

下面這個mathematica語句怎麼理解?
Mathematica導入牆外網頁數據失敗,軟體內如何實現科學上網?
理論物理學生如何用mathematica記錄筆記並進行管理?
Matlab 怎麼填充曲線相交的區域呢?
Mathematica如何導入某個csv或者excel文件的指定行和列?

TAG:動畫製作 | WolframMathematica | 分形理論 |