function draw_circle(center, radius, color)

  full_circle_arc = 360*64;
  up_left = center + radius * [-1, 1];
  ofg = xget("foreground");
  xset("foreground", color);
  xarc(up_left(1),up_left(2),radius*2,radius*2,0,full_circle_arc);
  xset("foreground", ofg);

//end draw_circle

function vecout = complex_to_vector(compin);

  vecout = [real(compin) imag(compin)];

//end complex_to_vec

function animate_resonance_imp(pole, ball_radius, rotor_center, anim_delay);

  dscale = 5;

  rstate = 1;

  multiplier = exp(pole);

  ball_center = rotor_center + complex_to_vector(rstate);

  draw_ball(ball_center, ball_radius, 2);

  xsegs([0 ball_center(1)],[0 ball_center(2)]);

  while abs(rstate) > 0.05 do

      rdc = rstate * %i*imag(pole);

      rderiv = rstate * pole;

      rstate = rstate * multiplier;

      ball_center = rotor_center + complex_to_vector(rstate);

      xpause(anim_delay);

      xclea(rotor_center(1)-(1+ball_radius)*1.5, ...
            rotor_center(2)+(1+ball_radius)*1.5, ...
            2*(1+ball_radius)*1.5, ...
            2*(1+ball_radius)*1.5);

      draw_circle([0 0], 1, 1);

      draw_ball(ball_center, ball_radius, 2);

      xsegs([0 ball_center(1)], [0 ball_center(2)], 2);

      xsegs([ball_center(1) ball_center(1)+dscale*real(rderiv)], ...
            [ball_center(2) ball_center(2)+dscale*imag(rderiv)], 5);

      xsegs([ball_center(1) ball_center(1)+dscale*real(rdc)], ...
            [ball_center(2) ball_center(2)+dscale*imag(rdc)], 1);

      xsegs([ball_center(1)+dscale*real(rdc) ...
             ball_center(1)+dscale*real(rderiv)], ...
            [ball_center(2)+dscale*imag(rdc) ...
             ball_center(2)+dscale*imag(rderiv)], 1);

  end //while

//end animate_resonance_imp

function impulse_demo;

  expand_frame = 1.6;

  xset("window",1); xselect(); xclear();
  xstring(0., 0., ...
  [ ...
    'Animation of resonant impulse response' ;
    ' '                                                ;
    'The blue ball shows the state of a simple resonance.';
    'The red line shows the direction of state change.';
    'The black lines show the circumferential and radial';
    'components of the state change.';
    ' '                                                ;
    'To control the animation, use the ""Stop"" and ""Resume""';
    'commands in the ""Control"" menu on the main Scilab';
    'window. Do not use ""Restart""---it will restart your';
    'whole Scilab session. To exit the animation, first';
    'stop it, then type ""quit"" to the main Scilab menu.'
  ]);

  xset("window",0); xselect(); xbasc();
  xtitle(['Animation of resonant impulse response' ;
          'Use stop and resume on the Control menu' ;
          'in the main Scilab window to pause.' ;
          'When stopped, type quit to finish.']);
  square(-1,1,-1,1);
  plot2d(0,0,-1,"031"," ",[-1 -1 1 1]*expand_frame);

  animate_resonance_imp(-0.03+%i*0.07, 0.02, [0 0], 200000);

//end impulse_demo

function animate_resonance_hel(pole, st_state, inp_phase, inp_freq, ...
                               ball_radius, rotor_center, anim_delay);

  dscale = 5;

  inp_scale = 0.02;

  t_inc = imag(pole)*inp_freq;

  t = inp_phase/inp_freq;

  input = exp(%i*t) * inp_scale;

  rstate = st_state;

  multiplier = exp(pole);

  ball_center = rotor_center + complex_to_vector(rstate);

  draw_ball(ball_center, ball_radius, 2);

  xsegs([0 ball_center(1)],[0 ball_center(2)]);

  while %T do

      t = t + t_inc;

      input = exp(%i*t) * inp_scale;

      rdc = rstate * %i*imag(pole);

      rderiv = rstate * pole;

      rstate = rstate * multiplier + input;

      ball_center = rotor_center + complex_to_vector(rstate);

      xpause(anim_delay);

      xclea(rotor_center(1)-(1+ball_radius)*1.5, ...
            rotor_center(2)+(1+ball_radius)*1.5, ...
            2*(1+ball_radius)*1.5, ...
            2*(1+ball_radius)*1.5);

      draw_circle([0 0], 1, 1);

      draw_ball(ball_center, ball_radius, 2);

      xsegs([0 ball_center(1)], [0 ball_center(2)], 2);

      xsegs([ball_center(1) ball_center(1)+dscale*real(rderiv)], ...
            [ball_center(2) ball_center(2)+dscale*imag(rderiv)], 5);

      xsegs([ball_center(1) ball_center(1)+dscale*real(rdc)], ...
            [ball_center(2) ball_center(2)+dscale*imag(rdc)], 1);

      xsegs([ball_center(1)+dscale*real(rdc) ...
             ball_center(1)+dscale*real(rderiv)], ...
            [ball_center(2)+dscale*imag(rdc) ...
             ball_center(2)+dscale*imag(rderiv)], 1);

      xsegs([ball_center(1) ball_center(1)+dscale*real(input)], ...
            [ball_center(2) ball_center(2)+dscale*imag(input)], 15);

  end //while %T

//end animate_resonance_hel

function hel_resp_demo_1;

  expand_frame = 1.6;

  xset("window",1); xselect(); xclear();
  xstring(0., 0., ...
  [ ...
    'Animation of resonant helix response' ;
    ' '                                                ;
    'The blue ball shows the state of a simple resonance.';
    'The red line shows the direction of state change';
    'due to the resonance itself.';
    'The black lines show the circumferential and radial';
    'components of the state change.';
    'The green line shows the input to the resonance.';
    ' '                                                ;
    'The input starts in phase with the resonance, and';
    'is too weak to maintain the resonant amplitude.';
    ' '                                                ;
    'To control the animation, use the ""Stop"" and ""Resume""';
    'commands in the ""Control"" menu on the main Scilab';
    'window. Do not use ""Restart""---it will restart your';
    'whole Scilab session. To exit the animation, first';
    'stop it, then type ""quit"" to the main Scilab menu.'
  ]);

  xset("window",0); xselect(); xbasc();
  xtitle(['Animation of resonant helix response' ;
          'Use stop and resume on the Control menu' ;
          'in the main Scilab window to pause.' ;
          'When stopped, type quit to finish.']);
  square(-1,1,-1,1);
  plot2d(0,0,-1,"031"," ",[-1 -1 1 1]*expand_frame);

  animate_resonance_hel(-0.03+%i*0.07, 1, 0, 1, 0.02, [0 0], 200000);

//end hel_resp_demo_1

function hel_resp_demo_2;

  expand_frame = 1.6;

  xset("window",1); xselect(); xclear();
  xstring(0., 0., ...
  [ ...
    'Animation of resonant helix response' ;
    ' '                                                ;
    'The blue ball shows the state of a simple resonance.';
    'The red line shows the direction of state change';
    'due to the resonance itself.';
    'The black lines show the circumferential and radial';
    'components of the state change.';
    'The green line shows the input to the resonance.';
    ' '                                                ;
    'The resonance starts at 0, and the input pulls it';
    'up to steady state.';
    ' '                                                ;
    'To control the animation, use the ""Stop"" and ""Resume""';
    'commands in the ""Control"" menu on the main Scilab';
    'window. Do not use ""Restart""---it will restart your';
    'whole Scilab session. To exit the animation, first';
    'stop it, then type ""quit"" to the main Scilab menu.'
  ]);

  xset("window",0); xselect(); xbasc();
  xtitle(['Animation of resonant helix response' ;
          'Use stop and resume on the Control menu' ;
          'in the main Scilab window to pause.' ;
          'When stopped, type quit to finish.']);
  square(-1,1,-1,1);
  plot2d(0,0,-1,"031"," ",[-1 -1 1 1]*expand_frame);

  animate_resonance_hel(-0.03+%i*0.07, 0, 0, 1, 0.02, [0 0], 200000);

//end hel_resp_demo_2

function hel_resp_demo_3;

  expand_frame = 1.6;

  xset("window",1); xselect(); xclear();
  xstring(0., 0., ...
  [ ...
    'Animation of resonant helix response' ;
    ' '                                                ;
    'The blue ball shows the state of a simple resonance.';
    'The red line shows the direction of state change';
    'due to the resonance itself.';
    'The black lines show the circumferential and radial';
    'components of the state change.';
    'The green line shows the input to the resonance.';
    ' '                                                ;
    'The input starts 90 degrees ahead of the resonance.';
    ' '                                                ;
    'To control the animation, use the ""Stop"" and ""Resume""';
    'commands in the ""Control"" menu on the main Scilab';
    'window. Do not use ""Restart""---it will restart your';
    'whole Scilab session. To exit the animation, first';
    'stop it, then type ""quit"" to the main Scilab menu.'
  ]);

  xset("window",0); xselect(); xbasc();
  xtitle(['Animation of resonant helix response' ;
          'Use stop and resume on the Control menu' ;
          'in the main Scilab window to pause.' ;
          'When stopped, type quit to finish.']);
  square(-1,1,-1,1);
  plot2d(0,0,-1,"031"," ",[-1 -1 1 1]*expand_frame);

  animate_resonance_hel(-0.03+%i*0.07, 1, %pi/2, 1, 0.02, [0 0], 200000);

//end hel_resp_demo_3

function hel_resp_demo_4;

  expand_frame = 1.6;

  xset("window",1); xselect(); xclear();
  xstring(0., 0., ...
  [ ...
    'Animation of resonant helix response' ;
    ' '                                                ;
    'The blue ball shows the state of a simple resonance.';
    'The red line shows the direction of state change';
    'due to the resonance itself.';
    'The black lines show the circumferential and radial';
    'components of the state change.';
    'The green line shows the input to the resonance.';
    ' '                                                ;
    'The input starts 90 degrees behind of the resonance.';
    ' '                                                ;
    'To control the animation, use the ""Stop"" and ""Resume""';
    'commands in the ""Control"" menu on the main Scilab';
    'window. Do not use ""Restart""---it will restart your';
    'whole Scilab session. To exit the animation, first';
    'stop it, then type ""quit"" to the main Scilab menu.'
  ]);

  xset("window",0); xselect(); xbasc();
  xtitle(['Animation of resonant helix response' ;
          'Use stop and resume on the Control menu' ;
          'in the main Scilab window to pause.' ;
          'When stopped, type quit to finish.']);
  square(-1,1,-1,1);
  plot2d(0,0,-1,"031"," ",[-1 -1 1 1]*expand_frame);

  animate_resonance_hel(-0.03+%i*0.07, 1, -%pi/2, 1, 0.02, [0 0], 200000);

//end hel_resp_demo_4

function hel_resp_demo_5;

  expand_frame = 1.6;

  xset("window",1); xselect(); xclear();
  xstring(0., 0., ...
  [ ...
    'Animation of resonant helix response' ;
    ' '                                                ;
    'The blue ball shows the state of a simple resonance.';
    'The red line shows the direction of state change';
    'due to the resonance itself.';
    'The black lines show the circumferential and radial';
    'components of the state change.';
    'The green line shows the input to the resonance.';
    ' '                                                ;
    'The input has higher frequency than the resonance.';
    ' '                                                ;
    'To control the animation, use the ""Stop"" and ""Resume""';
    'commands in the ""Control"" menu on the main Scilab';
    'window. Do not use ""Restart""---it will restart your';
    'whole Scilab session. To exit the animation, first';
    'stop it, then type ""quit"" to the main Scilab menu.'
  ]);

  xset("window",0); xselect(); xbasc();
  xtitle(['Animation of resonant helix response' ;
          'Use stop and resume on the Control menu' ;
          'in the main Scilab window to pause.' ;
          'When stopped, type quit to finish.']);
  square(-1,1,-1,1);
  plot2d(0,0,-1,"031"," ",[-1 -1 1 1]*expand_frame);

  animate_resonance_hel(-0.03+%i*0.07, 0, -%pi/2, 1.25, 0.02, [0 0], 200000);

//end hel_resp_demo_5

function hel_resp_demo_6;

  expand_frame = 1.6;

  xset("window",1); xselect(); xclear();
  xstring(0., 0., ...
  [ ...
    'Animation of resonant helix response' ;
    ' '                                                ;
    'The blue ball shows the state of a simple resonance.';
    'The red line shows the direction of state change';
    'due to the resonance itself.';
    'The black lines show the circumferential and radial';
    'components of the state change.';
    'The green line shows the input to the resonance.';
    ' '                                                ;
    'The input has lower frequency than the resonance.';
    ' '                                                ;
    'To control the animation, use the ""Stop"" and ""Resume""';
    'commands in the ""Control"" menu on the main Scilab';
    'window. Do not use ""Restart""---it will restart your';
    'whole Scilab session. To exit the animation, first';
    'stop it, then type ""quit"" to the main Scilab menu.'
  ]);

  xset("window",0); xselect(); xbasc();
  xtitle(['Animation of resonant helix response' ;
          'Use stop and resume on the Control menu' ;
          'in the main Scilab window to pause.' ;
          'When stopped, type quit to finish.']);
  square(-1,1,-1,1);
  plot2d(0,0,-1,"031"," ",[-1 -1 1 1]*expand_frame);

  animate_resonance_hel(-0.03+%i*0.07, 0, -%pi/2, 0.75, 0.02, [0 0], 200000);

//end hel_resp_demo_6
