
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  Draw a figure of the Swinging Spring  %
%      THIS IS FOR THE SPRING IN 2D.     %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% figure('Position',[400,175,400,600]);
whitebg([1 1 1]); 
colormap('cool');

%%%   Draw a patch
xp(1)=-0.45; xp(2)=1.1; xp(3)=1.1;xp(4)=-0.45;
yp(1)=-1.45; yp(2)=-1.45;yp(3)=0.5;yp(4)=0.5;
patch(xp,yp,[1,1,1]); 
hold on

%%%   Draw a box around
xb(1)=-0.45; xb(2)=1.1; xb(3)=1.1;xb(4)=-0.45;xb(5)=-0.45;
yb(1)=-1.45; yb(2)=-1.45; yb(3)=0.5; yb(4)=0.5; yb(5)=-1.45;
plot(xb,yb,'b','LineWidth',1)
hold on;

%%%%    draw the spring
loops=35.5;
npt=100;
t=(0:loops*npt)*2*pi/npt+pi;
alpha=0.5;
x1=cos(t)+alpha*t;
y1=3*sin(t);
xmin=min(x1); xmax=max(x1);
ymin=min(y1); ymax=max(y1);
beta=1;
x2=beta*(x1-xmin)/(xmax-xmin);
y2=beta*(y1-ymin)/(xmax-xmin);

theta=65; theta=theta*(pi/180);
ct=cos(theta); st=sin(theta);
xdash= ct*x2+st*y2;
ydash=-st*x2+ct*y2;
xdash=xdash-xdash(1);
ydash=ydash-ydash(1);
plot(xdash,ydash,'b')
axis('off'); axis('on');
axis([-0.5,1.5,-1.5,0.5]);
axis('equal');
hold on

%%%%   Draw the pivot
xc=xdash(1); yc=ydash(1); rad=0.01;
for r=rad/10:rad/10:rad
  npt=30;
  t=(0:npt)*2*pi/npt;
  xx=xc+r*cos(t);
  yy=yc+r*sin(t);
  plot(xx,yy,'k'); hold on
end

%%%%   Draw the bob
nx=size(xdash,2);
xc=xdash(nx); yc=ydash(nx); rad=0.05;
for r=rad/20:rad/20:rad
  npt=30;
  t=(0:npt)*2*pi/npt;
  xx=xc+r*cos(t);
  yy=yc+r*sin(t);
  plot(xx,yy,'r'); hold on
end

%%%   Draw the spring axes.
% xa(1)=-0.0;xa(2)=-0.25;ya(1)=-1.1;ya(2)=-1.25;
% plot(xa,ya,'b'); text(-0.27,-1.3,'Y')
xa(1)=-0.0;xa(2)=0.8;ya(1)=-1.1;ya(2)=-1.1;
plot(xa,ya,'b'); text(0.82,-1.1,'Y','FontWeight','bold', ...
                                    'FontSize',20);
xa(1)=0;xa(2)=0;ya(1)=-1.1;ya(2)=0.2;
plot(xa,ya,'b'); text(-0.02,0.25,'Z','FontWeight','bold', ...
                                     'FontSize',20);

%%%%   Draw the polar variables.
text(0.03,-0.35,'\theta','FontSize',24);
text(0.3,-0.45,'r','FontSize',24);

% Put off the plot axes
axis('off')
print -deps spring2d.eps

hold off
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
