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;
de partager – de copier, distribuer et transmettre cette œuvre
d’adapter – de modifier cette œuvre
Sous les conditions suivantes :
paternité – Vous devez donner les informations appropriées concernant l'auteur, fournir un lien vers la licence et indiquer si des modifications ont été faites. Vous pouvez faire cela par tout moyen raisonnable, mais en aucune façon suggérant que l’auteur vous soutient ou approuve l’utilisation que vous en faites.
partage à l’identique – Si vous modifiez, transformez, ou vous basez sur cette œuvre, vous devez distribuer votre contribution sous la même licence ou une licence compatible avec celle de l’original.