McGill University
School of Computer Science

Computer Science COMP 199 (Winter term)
Excursions in Computing Science

%MATLABpak08cII: moireVert.m, moireSquare.m

% function moireVert(n,paramax)			THM			090427
% Draw moire patterns with vertical lines -n:n 
% paramax is range parameter for animation: +ve for stretch, -ve for rotate
% Try 0.1 for stretch, -0.02 for rotate.
function moireVert(n,paramax)
% Initialize adjacency matrix
A = zeros(2*(2*n+1));		% adjacency matrix
for k = 1:2:4*n+1
  A(k,k+1) = 1;
end % for k
for param = -paramax:paramax/21:paramax
  % Part I Draw the base lines
  for k = 1:2*n + 1
    base(2*k-1,1) = k-1-n;	% x component of from-coordinate
    base(2*k-1,2) = -n;		% y component "
    base(2*k,1) = k-1-n;		% x component of to-coordinate
    base(2*k,2) = n;		% y component "
  end % for k
  hold off
  gplot(A,base)
  % Part II Draw the lines stretched sideways by delta:1 XOR
  if paramax >= 0		% do the stretch
    for k = 1:2*n+1
      stretch(2*k-1,1) = base(2*k-1,1) + (k-1-n)*param;
      stretch(2*k-1,2) = base(2*k-1,2)/2;
      stretch(2*k,1) = base(2*k,1) + (k-1-n)*param;
      stretch(2*k,2) = base(2*k,2)/2;
    end % for k
    hold on
    gplot(A,stretch,'g')
  else				% do the rotate
    theta = -2*pi*param;
    R = [cos(theta),-sin(theta);sin(theta),cos(theta)];
    for k = 1:2*n+1
      XY = R*[base(2*k-1,1) base(2*k-1,2)]';
      rotate(2*k-1,1) = XY(1);
      rotate(2*k-1,2) = XY(2);
      XY = R*[base(2*k,1) base(2*k,2)]';
      rotate(2*k,1) = XY(1);
      rotate(2*k,2) = XY(2);
    end % for k
    hold on
    gplot(A,rotate,'r')
  end % if paramax
  pause(1/5)
end %for delta

% function moireSquare(n,paramax)		THM			090427
% Draw moire patterns with vertical and horizontal lines -n:n X -n:n
% paramax is range parameter for animation: +ve for stretch, -ve for rotate
% Try 0.1 for stretch, -0.02 for rotate.
function moireSquare(n,paramax)
% Initialize adjacency matrix
A = zeros(2*(2*n+1));		% adjacency matrix
for k = 1:2:4*(2*n+1)		% vertical liunes doubled by horizontal lines
  A(k,k+1) = 1;
end % for k
for param = -paramax:paramax/21:paramax
  % Part I Draw the base lines
  for k = 1:2*n + 1
    base(2*k-1,1) = k-1-n;	% x component of from-coordinate
    base(2*k-1,2) = -n;		% y component "
    base(2*k,1) = k-1-n;		% x component of to-coordinate
    base(2*k,2) = n;		% y component "
  end % for k
  for k = 1:2*(2*n + 1)		% now swap x,y to get horizontal coordinates
    base(2*(2*n + 1)+k,1) = base(k,2);
    base(2*(2*n + 1)+k,2) = base(k,1);
  end % for k
  hold off
  gplot(A,base)
  % Part II Draw the lines stretched sideways by delta:1 XOR
  if paramax >= 0		% do the stretch
    for k = 1:2*n+1
      stretch(2*k-1,1) = base(2*k-1,1) + (k-1-n)*param;
      stretch(2*k-1,2) = base(2*k-1,2)/2;
      stretch(2*k,1) = base(2*k,1) + (k-1-n)*param;
      stretch(2*k,2) = base(2*k,2)/2;
    end % for k
    for k = 1:2*(2*n + 1)	% now swap x,y to get horizontal coordinates
      stretch(2*(2*n + 1)+k,1) = stretch(k,2);
      stretch(2*(2*n + 1)+k,2) = stretch(k,1);
    end % for k
    hold on
    gplot(A,stretch,'g')
  else				% do the rotate
    theta = -2*pi*param;
    R = [cos(theta),-sin(theta);sin(theta),cos(theta)];
    for k = 1:2*(2*n+1)
      XY = R*[base(2*k-1,1) base(2*k-1,2)]';
      rotate(2*k-1,1) = XY(1);
      rotate(2*k-1,2) = XY(2);
      XY = R*[base(2*k,1) base(2*k,2)]';
      rotate(2*k,1) = XY(1);
      rotate(2*k,2) = XY(2);
    end % for k
    hold on
    gplot(A,rotate,'r')
  end % if paramax
  pause(1/5)
end %for delta