Matlab: compute Moore curve help.

조회 수: 6 (최근 30일)
Lam Chun Ting
Lam Chun Ting 2012년 5월 18일
댓글: DGM 2025년 6월 27일
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;

답변 (1개)

Henning U. Voss
Henning U. Voss 2023년 8월 1일
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
DGM
DGM 2025년 6월 27일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Fractals에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by