 |
School of Computer Science
Computer Science COMP 199 (Winter term)
Excursions in Computing Science
% function parsed = parsexp(code)
% THM 060829
function parsed = parsexp(code)
parsed(1) = '(';
[parsed,inpos,outpos] = expression(code,parsed,1,2);
parsed(outpos) = ')';
% function [parsed,inpos,outpos] = expression(code,parsed,inpos,outpos)
% THM 060829
% used by parsexp.m; uses term.m, plussign.m; term.m uses letter.m
function [parsed,inpos,outpos] = expression(code,parsed,inpos,outpos)
parsed(outpos) = '('; outpos = outpos + 1;
[parsed,inpos,outpos] = term(code,parsed,inpos,outpos);
parsed(outpos) = ')'; outpos = outpos + 1;
[succ,parsed,inpos,outpos] = plussign(code,parsed,inpos,outpos);
if succ
[parsed,inpos,outpos] = expression(code,parsed,inpos,outpos);
end
% function [parsed,inpos,outpos] = term(code,parsed,inpos,outpos)
% THM 060829
% used by expression.m, uses letter.m
function [parsed,inpos,outpos] = term(code,parsed,inpos,outpos)
[succ,parsed,inpos,outpos] = letter(code,parsed,inpos,outpos);
if succ
[parsed,inpos,outpos] = term(code,parsed,inpos,outpos);
end
% function [succ,parsed,inpos,outpos] = plussign(code,parsed,inpos,outpos)
% THM 060829
% used by expression.m
function [succ,parsed,inpos,outpos] = plussign(code,parsed,inpos,outpos)
if inpos<=length(code)
current = code(inpos);
if current=='+', succ = 1;
parsed(outpos) = current; outpos = outpos + 1;
inpos = inpos + 1;
else succ = 0';
end
else succ = 0';
end
% function [succ,parsed,inpos,outpos] = letter(code,parsed,inpos,outpos)
% THM 060829
% used by term.m
function [succ,parsed,inpos,outpos] = letter(code,parsed,inpos,outpos)
if inpos<=length(code)
current = code(inpos);
if isletter(current)%|current=='('|current==')' why not code=a(b+c)d ?
succ = 1;
parsed(outpos) = current; outpos = outpos + 1;
inpos = inpos + 1;
else succ = 0;
end
else succ = 0;
end
% function peano(n)
% THM 060829
% uses peanoStep.m
function peano(n)
[X,Y] = meshgrid(0:2^n-1); % Initialize the
U = zeros(size(X)); % "drawing
V = zeros(size(Y)); % surfac"
j = 1; k = 1; % First point j:y,k:x
[X,Y,U,V,j,k] = peanoStep(n,X,Y,U,V,j,k); % Call recursive
quiver(X,Y,U,V,0,'.') % Plot it. ('.': no arrowheads)
axis([-1 2^n -1 2^n])
% function [X,Y,U,V,j,k] = peanoStep(n,X,Y,U,V,j,k)
% THM 060829
% called from peano(n); uses draw.m
function [X,Y,U,V,j,k] = peanoStep(n,X,Y,U,V,j,k)
if n>0
[X,Y,U,V,j,k] = peanoStep(n-1,X,Y,U,V,j,k);
x =1-2^(n-1); y = 1; draw
[X,Y,U,V,j,k] = peanoStep(n-1,X,Y,U,V,j,k);
x = 1; y =1-2^n; draw
[X,Y,U,V,j,k] = peanoStep(n-1,X,Y,U,V,j,k);
x =1-2^(n-1); y = 1; draw
[X,Y,U,V,j,k] = peanoStep(n-1,X,Y,U,V,j,k);
end
% draw.m THM 060829
% used by peanoStep.m
U(k,j) = x;
V(k,j) = y;
j = j + x;
k = k + y;
% function branchOL1(max,n)
% THM 070404 in file: branchOL1.m
% uses branchOL1Step.m
function branchOL1(max,n)
x = max/2; y = 1; h = pi/2; % First point, heading
for j = 1:n+1, sX(j) = 0; end % Stack: pos X n+1 el.
sY = zeros(size(sX)); % Stack: pos Y
sH = zeros(size(sX)); % Stack: heading H
sNext = 1; % Stack: next to push
step = max/(3^n+3); %this not quite right
delta = pi*25.7/180;
hold on
axis([1 max 1 max])
[x,y,h,sX,sY,sH,sNext] = branchOL1Step(n,step,delta,x,y,h,sX,sY,sH,sNext); % Call recursive
hold off
% usage branchOL1(100,0) ... branchOL1(100,4)
% function [x,y,h,sX,sY,sH,sNext] = branchOL1Step(n,step,delta,x,y,h,sX,sY,sH,sNext)
% THM 070404 in file: branchOL1Step.m
% called by branchOL1.m; uses forward.m popBOLstate.m, pushBOLstate.m
function [x,y,h,sX,sY,sH,sNext] = branchOL1Step(n,step,delta,x,y,h,sX,sY,sH,sNext)
if n==0, [x,y] = forward(step,x,y,h); else
[x,y,h,sX,sY,sH,sNext] = branchOL1Step(n-1,step,delta,x,y,h,sX,sY,sH,sNext);
[sX,sY,sH,sNext] = pushBOLstate(x,y,h,sX,sY,sH,sNext); %[ PUSH
h = h + delta; %+ left(delta)
[x,y,h,sX,sY,sH,sNext] = branchOL1Step(n-1,step,delta,x,y,h,sX,sY,sH,sNext);
[x,y,h,sX,sY,sH,sNext] = popBOLstate(sX,sY,sH,sNext); %] POP
[x,y,h,sX,sY,sH,sNext] = branchOL1Step(n-1,step,delta,x,y,h,sX,sY,sH,sNext);
[sX,sY,sH,sNext] = pushBOLstate(x,y,h,sX,sY,sH,sNext); %[ PUSH
h = h - delta; %- left(-delta)
[x,y,h,sX,sY,sH,sNext] = branchOL1Step(n-1,step,delta,x,y,h,sX,sY,sH,sNext);
[x,y,h,sX,sY,sH,sNext] = popBOLstate(sX,sY,sH,sNext); %] POP
[x,y,h,sX,sY,sH,sNext] = branchOL1Step(n-1,step,delta,x,y,h,sX,sY,sH,sNext);
end
% function [sX,sY,sH,sNext] = pushBOLstate(x,y,h,sX,sY,sH,sNext)
% THM 061023
% called by branchOL1Step.m
function [sX,sY,sH,sNext] = pushBOLstate(x,y,h,sX,sY,sH,sNext)
sX(sNext) = x;
sY(sNext) = y;
sH(sNext) = h;
sNext = sNext + 1;
% function [x,y,h,sX,sY,sH,sNext] = popBOLstate(sX,sY,sH,sNext)
% THM 061023
% called by branchOL1Step.m
function [x,y,h,sX,sY,sH,sNext] = popBOLstate(sX,sY,sH,sNext)
sNext = sNext - 1;
x = sX(sNext);
y = sY(sNext);
h = sH(sNext);
% function [U,V,x,y] = forward(step,U,V,x,y,h)
% THM 070404 in file: forward.m
% called by branchOL1Step.m
function [x,y] = forward(step,x,y,h)
dx = (step*cos(h));
dy = (step*sin(h));
X(1) = x;
Y(1) = y;
U(1) = dx;
V(1) = dy;
x = x + dx;
y = y + dy;
quiver(X,Y,U,V,0,'b.')