Matlab: compute Moore curve help.
Afficher commentaires plus anciens
Compute the fractal curve based on the following L system
Moore curve, 4 steps
Axiom: XFX+F+XFX
Production rules:
Newx=-YF+XFX+FYNewy=+
XF-YFY-FX+
Constants: α= π/2; θ=π/2
The following is my answer for Moore curve.But I know it's not a right curve, Because the picture is not same as what I have seen on wikipedia. I really don't know where am I go wrong. May anyone help me to slove this please? Many thanks for helping!!!
function [X,Y]=Moore_curve(Lmax)
Axiom='XFX+F+XFX';
Newf='F';
Newx='-YF+XFX+FY';
Newy='+XF-YFY-FX+';
theta=pi/2;
alpha=pi/2;
p=[0;0];
p=Coord(p,Lmax,Axiom,Newf,Newx,Newy,alpha,theta);
M=size(p,2);
X=p(1:1,1:M);
Y=p(2:2,1:M);
figure(1);
plot(X,Y,'Color','k');
set(gca,'xtick',[],'ytick',[]);
set(gca,'XColor','w','YColor','w');
function z=Coord(p,Lmax,Axiom,Newf,Newx,Newy,alpha,theta)
Rule=Moore_syst(Lmax,Axiom,Newf,Newx,Newy,1,'');
M=length(Rule);
for i=1:M
Tmp=p(1:2,size(p,2):size(p,2));
if Rule(i)=='F'
R=[cos(alpha);sin(alpha)];
R=R/(2^Lmax);
Tmp=Tmp+R;
p=cat(2,p,Tmp);
end
if Rule(i)=='+'
alpha=alpha+theta;
end
if Rule(i)=='-'
alpha=alpha-theta;
end;
end
z=p;
function z1=Moore_syst(Lmax,Axiom,Newf,Newx,Newy,n,tmp)
if n<=Lmax
if n==1
tmp=Axiom;
end
M=length(tmp);
tmp1='';
for i=1:M
if tmp(i)=='F'
tmp1=strcat(tmp1,Newf);
end
if tmp(i)=='X'
tmp1=strcat(tmp1,Newx);
end
if tmp(i)=='Y'
tmp1=strcat(tmp1,Newy);
end
if not(tmp(i)=='F') &¬(tmp(i)=='X') &¬(tmp(i)=='Y')
tmp1=strcat(tmp1,tmp(i));
end
end
tmp=tmp1;
n=n+1;
tmp=Moore_syst(Lmax,Axiom,Newf,Newx,Newy,n,tmp);
end
z1=tmp;
Réponses (1)
Henning U. Voss
le 1 Août 2023
0 votes
At the end of line 4 is a minus sign missing. It's apparently already missing in the task description... That's it.
Hope it's not too late.
1 commentaire
So we can have an example, I just applied the bugfix and fixed the formatting so that it's readable.
[X,Y] = Moore_curve(3);
function [X,Y] = Moore_curve(Lmax)
Axiom = 'XFX+F+XFX';
Newf = 'F';
Newx = '-YF+XFX+FY-';
Newy = '+XF-YFY-FX+';
theta = pi/2;
alpha = pi/2;
p = [0;0];
p = Coord(p,Lmax,Axiom,Newf,Newx,Newy,alpha,theta);
M = size(p,2);
X = p(1:1,1:M);
Y = p(2:2,1:M);
figure(1);
plot(X,Y,'Color','k');
set(gca,'xtick',[],'ytick',[]);
set(gca,'XColor','w','YColor','w');
end
function z = Coord(p,Lmax,Axiom,Newf,Newx,Newy,alpha,theta)
Rule = Moore_syst(Lmax,Axiom,Newf,Newx,Newy,1,'');
M = length(Rule);
for i = 1:M
Tmp = p(1:2,size(p,2):size(p,2));
if Rule(i) == 'F'
R = [cos(alpha);sin(alpha)];
R = R/(2^Lmax);
Tmp = Tmp+R;
p = cat(2,p,Tmp);
end
if Rule(i) == '+'
alpha = alpha+theta;
end
if Rule(i) == '-'
alpha = alpha-theta;
end
end
z = p;
end
function z1 = Moore_syst(Lmax,Axiom,Newf,Newx,Newy,n,tmp)
if n <= Lmax
if n == 1
tmp = Axiom;
end
M = length(tmp);
tmp1 = '';
for i = 1:M
if tmp(i) == 'F'
tmp1 = strcat(tmp1,Newf);
end
if tmp(i) == 'X'
tmp1 = strcat(tmp1,Newx);
end
if tmp(i) == 'Y'
tmp1 = strcat(tmp1,Newy);
end
if not(tmp(i) == 'F') && not(tmp(i) == 'X') && not(tmp(i) == 'Y')
tmp1 = strcat(tmp1,tmp(i));
end
end
tmp = tmp1;
n = n+1;
tmp = Moore_syst(Lmax,Axiom,Newf,Newx,Newy,n,tmp);
end
z1 = tmp;
end
Catégories
En savoir plus sur Fractals dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
