% Script P3
% Draws a logarithmic spiral.

% Parameters of the spiral:
turnAngle = 65;
numEdges  = 400;
spiralColor1 = 'red'; 
spiralColor2 = 'blue';
% Set up the plot window...
close all
set(0,'defaultfigureposition',[128 64 800 600])
figure
axis equal off
set(gcf,'color','black')
r = floor(numEdges/2);
hold on
% Display the bounding box...
r = floor(numEdges/2);
plot([-r r r -r -r],[-r -r r r -r],'g','linewidth',3)

%%%%%%%%%%%%%   Do not alter any thing above this line   %%%%%%%%%%%%%%%%%%%%%

% Spiral starts at (0,0)
% (xc,yc) is the "current point...
x_c = 0; 
y_c = 0;
theta = turnAngle*(pi/180);
% (x_next,y_next )is the "next" point...
x_next = x_c + cos(theta);
y_next = y_c + sin(theta);
k = 1;
% Keep iterating as long as (x_next,y_next) is inside the box...
while(abs(x_next)<=r && abs(y_next)<=r && k<=numEdges)
   % plot the k-th boxed edge
   if rem(k,2)==1
       plot([x_c x_next],[y_c y_next],'color',spiralColor1)
   else
       plot([x_c x_next],[y_c y_next],'color',spiralColor2)
   end
   % The "start" of the next leg is the "end" of the current leg
   k = k+1;
   x_c = x_next;
   y_c = y_next;
   % Direction of the k-th edge
   theta= (k*turnAngle)*(pi/180);
   % k-th edge starts at (x_c,y_c) and ends at (x_next,y_next)
   x_next= x_c + k*cos(theta);
   y_next= y_c + k*sin(theta);
end

hold off
title(sprintf('Edges = %d    Turn Angle (degrees) = %3d',k-1,turnAngle),'fontsize',14,'color','cyan')
shg


