clear all; close all; clc;

totalT = 8;
dt = 1/20;
t = linspace(0,totalT,totalT/dt);

w = 2*pi*(1/2);
A = 10;
theta = 45/180*pi;

yc = A*exp(1j*w*t + 1j*theta);
y = real(yc);

%
%% Just Vector



figure(1)

plot([-A A], [0 0], 'k');
hold on;
plot([0 0], [-A A],  'k');

axis square
box on
xlim([-A A]);
ylim([-A A]);
title('Plot of {\it Ae}^{{\it j\omega t }+ \theta}')

l = plot([0 real(yc(1))], [0,imag(yc(1))]);


for i = 1:1:length(t)
   delete(l)
    l = quiver(0,0,real(yc(i)),imag(yc(i)),0, 'r');
    drawnow limitrate
    axis square
    pause(dt)
end


%
%% Vector + Projections



figure(2)

[rs, cs] = meshgrid(1:5, 1:5);
complexPlotLoc = sub2ind([10,10], rs(:),cs(:));

[rs, cs] = meshgrid(6:10, 1:5);
sinPlotLoc = sub2ind([10,10], rs(:),cs(:));

[rs, cs] = meshgrid(1:5, 6:10);
cosPlotLoc = sub2ind([10,10], rs(:),cs(:));


subplot(10,10,complexPlotLoc)
    plot([-A A], [0 0], 'k');
    hold on;
    plot([0 0], [-A A],  'k');

%     axis square
    box on
    xlim([-A A]);
    ylim([-A A]);
    title('Plot of {\it Ae}^{{\it j\omega t }+ \theta}')
    set(gca, 'xTickLabel',[]);

    l = plot([0 real(yc(1))], [0,imag(yc(1))], 'LineWidth',2);
    lreal = plot([real(yc(1)) real(yc(1))], [-A imag(yc(1))], '-.m', 'LineWidth',2);
    limag = plot([real(yc(1)) A], [imag(yc(1)) imag(yc(1))], '-.b', 'LineWidth',2);

subplot(10,10,sinPlotLoc)
    plot([0 4*pi/w], [0 0], 'k');
    hold on;
    box on
    set(gca, 'yTickLabel',[]);
    xlim([0 4*pi/w]);
    ylim([-A A]);

    l2 = plot(t, imag(yc), 'b', 'LineWidth',2);
    
subplot(10,10,cosPlotLoc)
    plot([0 0], [0 4*pi/w], 'k', 'LineWidth',2);
    hold on;
    box on
    xlim([-A A]);
    ylim([0, 4*pi/w]);
    set(gca, 'YDir','reverse')

    l3 = plot(real(yc), t, 'm', 'LineWidth',2);


for i = 1:1:length(t)
    subplot(10,10,complexPlotLoc)
        delete(l); delete(lreal); delete(limag)
        l = quiver(0,0,real(yc(i)),imag(yc(i)),0, 'r');
        lreal = plot([real(yc(i)) real(yc(i))], [-A imag(yc(i))], ':m');
        limag = plot([real(yc(i)) A], [imag(yc(i)) imag(yc(i))], ':b');
    subplot(10,10,sinPlotLoc)
        delete(l2)
        l2 = plot(t(1:i), imag(yc(i:-1:1)), 'b');
    subplot(10,10,cosPlotLoc)
        delete(l3)
        l3 = plot(real(yc(i:-1:1)), t(1:i), 'm');
    drawnow limitrate
    pause(dt)
end


%
%% Spin the Axes



figure(3)

[rs, cs] = meshgrid(3:6, 1:4);
stationaryFramePlot = sub2ind([10,10], rs(:),cs(:));

[rs, cs] = meshgrid(3:6, 7:10);
rotatingFramePlot = sub2ind([10,10], rs(:),cs(:));

[rs, cs] = meshgrid(1, 1:10);
meterPlot = sub2ind([10,10], rs(:),cs(:));

subplot(10,10,stationaryFramePlot)
    xas = plot([-A A], [0 0], 'k');
    hold on;
    yas = plot([0 0], [-A A],  'k');

    axis square
    box on
    xlim([-A A]);
    ylim([-A A]);
    title('Stationary Frame Plot of {\it Ae}^{{\it j\omega t }+ \theta}')

    l = plot([0 real(yc(1))], [0,imag(yc(1))]);
    

subplot(10,10,rotatingFramePlot)
    xar = plot([-A A], [0 0], 'k');
    hold on;
    yar = plot([0 0], [-A A],  'k');

    axis square
    box on
    xlim([-A A]);
    ylim([-A A]);
    title('Rotating Frame Plot of {\it Ae}^{{\it j\omega t }+ \theta}')

    l2 = plot([0 real(yc(1))], [0,imag(yc(1))]);  
    
subplot(10,10,meterPlot)    
    plot([0 1], [w w]/w, 'r', 'LineWidth',2);
    hold on;
    lr = plot([0 1], [0 0]/w, 'b', 'LineWidth',2);
    xlim([0 1]);
    ylim([0 2]);
    set(gca,'XTickLabel',[]);
    ylabel('Axis Rotation Speed')

waxes = linspace(0, 2*w, length(t));
waxes(waxes>w) = w;
    
for i = 1:1:length(t)
    thetaAxes = sum(waxes(1:i))*diff(t(1:2));
    
    subplot(10,10,stationaryFramePlot)
        delete(l); delete(xas); delete(yas)
        l = quiver(0,0,real(yc(i)),imag(yc(i)),0, 'r');
        xas = plot(A*sqrt(2)*[cos(thetaAxes) -cos(thetaAxes)],...
            A*sqrt(2)*[sin(thetaAxes) -sin(thetaAxes)], 'k');
        yas = plot(A*sqrt(2)*[sin(-thetaAxes) -sin(-thetaAxes)],...
            A*sqrt(2)*[cos(-thetaAxes) -cos(-thetaAxes)], 'k');
        hold on;
        axis square
   
    subplot(10,10,rotatingFramePlot)
        delete(l2); 
        ycr(i) = A*exp(1j*w*t(i) + 1j*theta - 1j*thetaAxes);
        l2 = quiver(0,0,real(ycr(i)),imag(ycr(i)),0, 'r');

    subplot(10,10,meterPlot) 
        delete(lr)
        lr = plot([0 1], [waxes(i) waxes(i)]/w, 'b', 'LineWidth',2);
        
    drawnow limitrate
    pause(dt)
end