% PhasorMovie.m
%
% Shows rotating stick in the complex plane whose shadow on the real axis
% is v(t)=Acos(wt+phi)
% A snapshot of the stick at t=0 is the phasor for v(t)
% Shows shadow as shadow of stick not moving to right.
% To save as .avi file:
% movie2avi(M,'PhasorMovie0.avi','compression','None');

A = 1.5; % (V) Length of stick.
w = 2*pi; % (rad/s) rotations of stick per second.
phi = 30; % (deg) Phase shift.

frames_per_sec = 30; % (s^-1)
tilt = 30; % (deg) tilt into page for 3-D view of x-axis

% Use 'for' loop for frames.
for ind_frame = 0:3*frames_per_sec % Three seconds at frames per second rate.
% Draw axes
plot([-2,8],[0,0],'k-') % t-axis
hold on
plot([0,0],[-2,2],'k-') % y-axis
[x_axis3D,y_axis3D] = three_D(tilt,[0,0],[-2,2],[0,0]); % x-axis end points.
plot(x_axis3D,y_axis3D,'k-') % x-axis

% Current time for this frame.
t = ind_frame/frames_per_sec;
% Draw unit circle in which stick rotates.
t_param = (2*pi)/60 * (1:60); % Array of times for parametric drawing of circle.
x_circ = cos(t_param);
y_circ = sin(t_param);

% Tilt circle into paper.
[x_circ3D, y_circ3D] = ...
three_D(tilt,0*ones(1,length(t_param)),x_circ,y_circ);

% Plot circle in which phasor rotates.
plot(x_circ3D,y_circ3D,'b--') % Unit circle drawn in 3-D in blue.
axis('equal') % Use same scale in t and y directions.
axis([-2,8,-2,2]) % t axis from -2 to 8 and y axis is from -2 to 2 in 2-D
% projection. Format is axis([XMIN,XMAX,YMIN,YMAX]).

% Rotate phasor around circle at rate w.
x_stick = A*cos(w*t+phi*pi/180);
y_stick = A*sin(w*t+phi*pi/180);
t_stick = 0;
% Project stick into 3-D.
[x_stick3D, y_stick3D] = three_D(tilt,t_stick,x_stick,y_stick);
plot([0,x_stick3D],[0,y_stick3D],'g-')
% Show waveform for shadow of stick.
[x_shadow3Dt, y_shadow3Dt] = three_D(tilt,t_stick,x_stick,0);
% Show shadow of stick from origin out to t,x position in red.
plot([0,x_shadow3Dt],[0,y_shadow3Dt],'r-')
% Draw vertical from end of stick to end of shadow.
plot([x_stick3D, x_shadow3Dt],[y_stick3D, y_shadow3Dt],'y-')
% Get the frames for making a movie.
M(ind_frame+1) = getframe;
% Slow down the frame rate when running the movie directly in Matlab.
pause(0.2)
if ind_frame == 0
x_stick3D = x_stick3D
y_stick3D = y_stick3D
pause(2.8)
end
% Draw the entire picture again next time.
hold off
end