% M-file DrawPolygons2
%
% Draw polygons with 2^n sides that approximates the unit circle

clear

% np_max = 2^n_max will be the maximum number of sides allowed

n_max = 8;
np_max = 2^n_max;

% Create a vector with the angles: theta( i ) = (i-1)* 2*pi/np_max

theta = linspace( 0, 2*pi, np_max+1 );

% The i-th point on the circle is ( c(i), s( i ) ) where
% c( i _ = cos( theta( i ) ) and s( i ) = sin( theta( i ) )

c = cos( theta );
s = sin( theta );

% Show x and y values of the points as a function of theta

plot( theta, c, '-', ...
      theta, s, '-.' );
   
%Show x and y values of the points as a function of i
   

plot( 1:np_max+1, c, '-', ...
   1:np_max+1, s, '-.' );

input('continue:  ');
   
for n=1:n_max
   % wait until the user wants to see the next polygon
   
   input('continue:  ');

   
	% Draw a polygon with np = 2^n sides
   
   np = 2^n;
   
   % plot the polygon defined by theta in steps of np_max/np
   
   plot( c( 1:np_max/np:np_max+1 ), s( 1:np_max/np:np_max+1) );
   
   % make sure axes work out when n = 1
   
   axis( [ -1 1 -1 1 ] );
   
   hold on
   
   % Add the pie cuts
   
   for i=1:np_max/np:np_max+1
      plot( c( i:np_max/2:np_max ), s( i:np_max/2:np_max ) )
   end
   
   hold off
   
end

