Matlab: compute Moore curve help.

1 view (last 30 days)
Lam Chun Ting
Lam Chun Ting on 18 May 2012
Commented: DGM on 27 Jun 2025
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') &&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;

Answers (1)

Henning U. Voss
Henning U. Voss on 1 Aug 2023
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 Comment
DGM
DGM on 27 Jun 2025
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

Sign in to comment.

Categories

Find more on Fractals in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!