% newstate  -  function updates current state in animation pane

% (c) 2014, Philip D Loewen; history at end of file

function newstate(hlist,theta,omega)

%%%%%%%% Update State in Phase Plane %%%%%%%%
phaseplane = hlist{2};
statedot   = hlist{5};

if false
  % Draw a little segment from old state to new state.
  % (But skip this if one of the states is at the origin.)
  axes(phaseplane);
  old_theta = get(statedot,'xdata');
  old_omega = get(statedot,'ydata');
  if (old_theta^2 + old_omega^2 > 1E-6) && (theta^2 + omega^2 > 1E-6)
    plot([old_theta,theta],[old_omega,omega]);
  end;
end;

set(statedot,'xdata',theta,'ydata',omega);

%%%%%%%% Update Pendulum Rod and Bob %%%%%%%%

rod = hlist{6};
bob = hlist{7};
L   = hlist{11};
rbob= hlist{12};

x  =  L*sin(theta);
y  = -L*cos(theta);
set(rod,'xdata',[0,x],'ydata',[0,y]);
tbob= linspace(0,2*pi,31);
set(bob,'xdata',x+rbob*cos(tbob),'ydata',y+rbob*sin(tbob));

%%%%%%%% Update Energy Monitor Box %%%%%%%%
g   = hlist{13};

PEbar = hlist{8};
KEbar = hlist{9};
Etot  = hlist{10};

energy= hlist{4};

eXLim = get(energy,'XLim');

KE   = 0.5*L^2*omega^2;
PE   = g*L*(1-cos(theta));
E    = KE + PE;
if(E>max(eXLim))
  set(energy,'XLim',[0,1.2*E]);
end;

set(PEbar,'xdata',PE*[0,1,1,0]);
set(KEbar,'xdata',PE + KE*[0,1,1,0]);
set(Etot, 'xdata',(PE+KE)*[1,1]);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

drawnow;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 2014-11-14: Added auto-ranging of energy box
