% Cartoon of a rounded polygon whereby the straight sectors between the
% arcs shrinks from having a length equal to the arc radius down to zero.
% The purpose of this video is simply to show that one must consider only
% the arc sectors when calculating the electrons' angular frequency in a
% storage ring

clear; close all;

prompt = 'Number sides of the polygon [default = 12 (dodecahedron)]: ';
Pnum = input(prompt);
if isempty(Pnum)
    Pnum = 12;
end

v = VideoWriter('polygonShrink.mp4','MPEG-4');
v.FrameRate = 30;    % default 30
v.Quality = 100;    % default 75
open(v);
figure('ToolBar','none');
set(0,'defaultfigurecolor',[1 1 1]); % white background

phi = (-pi/Pnum:pi/180:pi/Pnum);
radius = 2; % length of straight
N=40; % number of frames
axisSize = Pnum^2/(2*(Pnum-2)); % size of axes

for j = 0:N % frame loop
    newplot;
    for i = 0:Pnum-1 % draw each sector of rounded dodecagon
        axis([-axisSize axisSize -axisSize axisSize]);
        axis square; % square plot
        axis off;
        straight = radius*(N - j)/N; % length of straight. Shrinks to zero
        theta0 = (2*pi/Pnum)*i; % angle of centre line for each sector
        L1 = straight/(2*tan(pi/Pnum));
        L = L1 + radius; % distance from centre of straight to geometric centre
        
        x0 = L*sin(theta0); % x-coordinate of centre of straight
        y0 = L*cos(theta0); % y-coordinate of centre of straight
        
        straightStartx = x0 - (straight/2)*cos(theta0); % x-coord of start of straight
        straightStarty = y0 + (straight/2)*sin(theta0); % y-coord of start of straight
        straightEndx = x0 + (straight/2)*cos(theta0); % x-coord of end of straight
        straightEndy = y0 - (straight/2)*sin(theta0); % y-coord of end of straight
        
        straightx = [straightStartx straightEndx];
        straighty = [straightStarty straightEndy];
        hold on;
        plot(straightx,straighty,'color',[1,0,0], 'LineWidth', 8.0); % plot in red
        
        phiNow = phi + theta0 + pi/Pnum; % angular range of the arc
        arcCentre = straight/(2*sin(pi/Pnum)); % Distance geometric centre to origin of arc
        arcCentrex = arcCentre*sin(theta0 + pi/Pnum); % x-coord of arc origin
        arcCentrey = arcCentre*cos(theta0 + pi/Pnum); % x-coord of arc origin
        arcX = arcCentrex + radius*sin(phiNow); % set of x-coords for arc
        arcY = arcCentrey + radius*cos(phiNow); % set of y-coords for arc
        plot(arcX,arcY,'color',[0,0,1], 'LineWidth', 8.0); % plot in blue
        hold off;
    end
    frame = getframe(gcf);
    writeVideo(v,frame);
    hold off;
end
% Output the movie as an mpg file
close(v);