functionS=DragonCurve(pattern,MaxRecursionLevel)% DRAGONCURVE% This function generates a fractal curve called the dragon curve.%% A dragon curve is any member of a family of self-similar fractal curves,% which can be approximated by recursive methods such as Lindenmayer% systems.%% Inputs :% - pattern : [Optional] Vector of complex that contain linear% transformation to apply to each line segment% - MaxRecursionLevel : [Optional] Maximum recusrion level% Remember each time a new level is added, the% number of line segments is doubled% - options : [Optional] Structure with display option%% Outputs :% - S : Structure that contains the line segment for each recursion level% Just enter "plot(S(end).LineSeg,'k');" to plot the finest level% generated%% See http://en.wikipedia.org/wiki/Dragon_curve%% 2011/07/10% Guillaume Jacquenot%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%ifnargin<3options.plot=true;options.generateGIFAnimate=true;options.plotConvergence=false;ifnargin<2MaxRecursionLevel=15;ifnargin<1pattern(1)=0.5*(1+1i);pattern(2)=0.5*(1-1i);endendendS(MaxRecursionLevel)=struct('LineSeg',[]);npat=numel(pattern);S(1).LineSeg=[0;pattern(1:(npat-1));1];forlevel=2:MaxRecursionLevel;delta=diff(S(level-1).LineSeg);S(level).LineSeg=zeros(npat^level+1,1);S(level).LineSeg(1:npat:end)=S(level-1).LineSeg;S(level).LineSeg(2:4:end-3)=...S(level-1).LineSeg(1:2:end-2)+delta(1:2:end-1)*pattern(1);S(level).LineSeg(4:4:end-1)=...S(level-1).LineSeg(2:2:end-1)+delta(2:2:end)*pattern(2);endifoptions.plot||options.generateGIFAnimateBBox=determineBoundingBox(S);plotDragonCurve(S,BBox);endifoptions.generateGIFAnimatecmd=generateGIFAnimationCommand(MaxRecursionLevel);eval(cmd);endifoptions.plotConvergenceplot_Convergence_Evolution(S);endreturn;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%functionplotDragonCurve(S,BBox)figure('Color','w');holdonboxonaxisoffMaxRecursionLevel=numel(S);forlevel=1:MaxRecursionLevelhp=plot(S(level).LineSeg,'k');axisequalaxis(BBox);saveas(gcf,[mfilename'_'sprintf('%03d',level)],'png');saveas(gcf,[mfilename'_'sprintf('%03d',level)],'jpg');plot2svg([mfilename'_'sprintf('%03d',level)'.svg']);pause(0.5);delete(hp);endplot(S(end).LineSeg,'k');return;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%functionBBox=determineBoundingBox(S,proportionalOffset)ifnargin==1proportionalOffset=0.1;endBBox=zeros(1,4);BBox(1)=min(real(S(end).LineSeg));BBox(2)=max(real(S(end).LineSeg));BBox(3)=min(imag(S(end).LineSeg));BBox(4)=max(imag(S(end).LineSeg));dBBoxX=BBox(2)-BBox(1);dBBoxY=BBox(4)-BBox(3);BBox=BBox+proportionalOffset*[-dBBoxXdBBoxX-dBBoxYdBBoxY];return;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%functionplot_Convergence_Evolution(S)figureholdonboxontitle('Evolution of the length of the curve with the number of levels');xlabel('Level');ylabel('Log of the length');MaxRecursionLevel=numel(S);lengthCurve=zeros(1,MaxRecursionLevel);forlevel=1:MaxRecursionLevellengthCurve(level)=sum(abs(diff(S(level).LineSeg)));endplot(log2(lengthCurve));gridonreturn;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%functioncmd=generateGIFAnimationCommand(MaxRecursionLevel)cmd='!convert -antialias -density 100 -delay 60 ';forlevel=[1:1:MaxRecursionLevelMaxRecursionLevel-1:-1:1]cmd=[cmdmfilename'_'sprintf('%03d',level)'.png '];endcmd=[cmdmfilename'.gif'];return;
chia sẻ – sao chép, phân phối và chuyển giao tác phẩm
pha trộn – để chuyển thể tác phẩm
Theo các điều kiện sau:
ghi công – Bạn phải ghi lại tác giả và nguồn, liên kết đến giấy phép, và các thay đổi đã được thực hiện, nếu có. Bạn có thể làm các điều trên bằng bất kỳ cách hợp lý nào, miễn sao không ám chỉ rằng người cho giấy phép ủng hộ bạn hay việc sử dụng của bạn.
chia sẻ tương tự – Nếu bạn biến tấu, biến đổi, hoặc làm tác phẩm khác dựa trên tác phẩm này, bạn chỉ được phép phân phối tác phẩm mới theo giấy phép y hệt hoặc tương thích với tác phẩm gốc.