And the following Matlab code:
%
%L-system
%2D
clear all
clf
%Rules-- Cell array {1,x} is the xth string to be replaced
% -- {2,x}
rule(1).before = 'F';
rule(1).after = 'FF';
%rule(1).after = 'F[-F]F[+F]F';
rule(2).before = 'G';
rule(2).after = 'F[+G][-G]F[+G][-G]FG';
%rule(2).after = 'F[+G]F[-G]G';
nRules = length(rule);
%angle: +operator means turn left; -operator means turn right
%delta = 27.5; %degrees
delta = 25.0; % This is 'theta' in the project specifications
%length of the line segments corresponding to the symbols F and G
lenF = 1;
lenG = 1;
%starting seed
axiom = 'G';
%axiom = 'F';
%number of repititions -- This is 'N' in the project specification
nReps = 4;
for i=1:nReps
%one character/cell, with indexes the same as orilsys.mginal axiom string
axiomINcells = cellstr(axiom');
%axiom
for j=1:nRules
%the indexes of each 'before' string
hit = strfind(axiom, rule(j).before);
if (length(hit)>=1)
for k=hit
axiomINcells{k} = rule(j).after;
end
end
end
%now convert individual cells back to a string
axiom=[];0
for j=1:length(axiomINcells)
axiom = [axiom, axiomINcells{j}];
end
end
leaf_count = (axiom == 'G');
leaf_count = leaf_count(axiom == 'G');
sprintf('leaf_count = %d',length(leaf_count))
branch_count = (axiom == 'F');
branch_count = branch_count(axiom == 'F');
sprintf('branch_count = %d', length(branch_count))
sprintf('length(axiom) = %d', length(axiom))
axiom
% Now draw the string as turtle graphics
%Upper case (e.g. F or G) causes a line to be drawn in the current direction of the turtle
%Lower case causes a move with no draw
%angle +operator means turn left; -operator means turn right
%Init the turtle
xT = 0;
yT = 0;
aT = 0;
da = deg2rad(delta) ; %convert to radians
%init the turtle stack
stkPtr = 1;
%set(gca,'xlim', [-5 5], 'ylim', [-5 5]);
hold on
% Leaf counter
leafs = 0;
for i=1:length(axiom)
cmdT = axiom(i);
switch cmdT
case 'F' % Branches
newxT = xT + lenF*cos(aT);
newyT = yT + lenF*sin(aT);
%line([xT newxT], [yT newyT]);
line([yT newyT], [xT newxT],'color',[.3 .3 0], 'linewidth',2);
xT = newxT;
yT = newyT;
case 'G' % Leaves
leafs = leafs + 1;
newxT = xT + lenG*cos(aT);
newyT = yT + lenG*sin(aT);
%line([xT newxT], [yT newyT]);
line([yT newyT], [xT newxT],'color','g', 'linewidth',2);
xT = newxT;
yT = newyT;
case '+'
aT = aT + da;
case '-'
aT = aT - da;
case '[' %push the stack
stack(stkPtr).xT = xT ;
stack(stkPtr).yT = yT ;
stack(stkPtr).aT = aT ;
stkPtr = stkPtr +1 ;
case ']' %pop the stack
stkPtr = stkPtr -1 ;
xT = stack(stkPtr).xT ;
yT = stack(stkPtr).yT ;
aT = stack(stkPtr).aT ;
otherwise
disp('error')
return
end
%drawnow
end
sprintf('leafs = %d', leafs);
daspect([1,1,1])
%
%L-system
%2D
clear all
clf
%Rules-- Cell array {1,x} is the xth string to be replaced
% -- {2,x}
rule(1).before = 'F';
rule(1).after = 'FF';
%rule(1).after = 'F[-F]F[+F]F';
rule(2).before = 'G';
rule(2).after = 'F[+G][-G]F[+G][-G]FG';
%rule(2).after = 'F[+G]F[-G]G';
nRules = length(rule);
%angle: +operator means turn left; -operator means turn right
%delta = 27.5; %degrees
delta = 25.0; % This is 'theta' in the project specifications
%length of the line segments corresponding to the symbols F and G
lenF = 1;
lenG = 1;
%starting seed
axiom = 'G';
%axiom = 'F';
%number of repititions -- This is 'N' in the project specification
nReps = 4;
for i=1:nReps
%one character/cell, with indexes the same as orilsys.mginal axiom string
axiomINcells = cellstr(axiom');
%axiom
for j=1:nRules
%the indexes of each 'before' string
hit = strfind(axiom, rule(j).before);
if (length(hit)>=1)
for k=hit
axiomINcells{k} = rule(j).after;
end
end
end
%now convert individual cells back to a string
axiom=[];0
for j=1:length(axiomINcells)
axiom = [axiom, axiomINcells{j}];
end
end
leaf_count = (axiom == 'G');
leaf_count = leaf_count(axiom == 'G');
sprintf('leaf_count = %d',length(leaf_count))
branch_count = (axiom == 'F');
branch_count = branch_count(axiom == 'F');
sprintf('branch_count = %d', length(branch_count))
sprintf('length(axiom) = %d', length(axiom))
axiom
% Now draw the string as turtle graphics
%Upper case (e.g. F or G) causes a line to be drawn in the current direction of the turtle
%Lower case causes a move with no draw
%angle +operator means turn left; -operator means turn right
%Init the turtle
xT = 0;
yT = 0;
aT = 0;
da = deg2rad(delta) ; %convert to radians
%init the turtle stack
stkPtr = 1;
%set(gca,'xlim', [-5 5], 'ylim', [-5 5]);
hold on
% Leaf counter
leafs = 0;
for i=1:length(axiom)
cmdT = axiom(i);
switch cmdT
case 'F' % Branches
newxT = xT + lenF*cos(aT);
newyT = yT + lenF*sin(aT);
%line([xT newxT], [yT newyT]);
line([yT newyT], [xT newxT],'color',[.3 .3 0], 'linewidth',2);
xT = newxT;
yT = newyT;
case 'G' % Leaves
leafs = leafs + 1;
newxT = xT + lenG*cos(aT);
newyT = yT + lenG*sin(aT);
%line([xT newxT], [yT newyT]);
line([yT newyT], [xT newxT],'color','g', 'linewidth',2);
xT = newxT;
yT = newyT;
case '+'
aT = aT + da;
case '-'
aT = aT - da;
case '[' %push the stack
stack(stkPtr).xT = xT ;
stack(stkPtr).yT = yT ;
stack(stkPtr).aT = aT ;
stkPtr = stkPtr +1 ;
case ']' %pop the stack
stkPtr = stkPtr -1 ;
xT = stack(stkPtr).xT ;
yT = stack(stkPtr).yT ;
aT = stack(stkPtr).aT ;
otherwise
disp('error')
return
end
%drawnow
end
sprintf('leafs = %d', leafs);
daspect([1,1,1])
No comments:
Post a Comment