<!--                                                         
// By Oliver Knill,
// February 23,  2000
var interrupt=0;                   // interrupt the motion?

//   Parameters to adjust the motion to the picture 

var size=315;                      // scale of picture in pixels
var corr_x=10;                     // ball correction in pixel coordinates
var corr_y=10;                     // ball correction in pixel coordinates
var r_inner=0.50;                  // radius of inner table in absolute coordinates

var alpha   = 0.65;                // x-position of obstacle 
var beta    = 0.65;                // y-position of obstacle
var g       = 0.0;                 // gravitational acceleration

var q_x=alpha;                     // x-coordinates of square table center
var q_y=beta;                      // y-coordinates of square table center

rho=-1.0;                          // table is infinitely heavy 
mu=0.0; 

var dt      = 0.04;                // time step
var T       = 10;                  // number of Euler steps until display
var t;                             // time variable in loop
var ds      = dt/T;                // time interval for Euler step


var p_x=0.0;                       // x-speed of table center
var p_y=0.0;                       // y-speed of table center

var p1_x;                          // in collision
var p1_y;                          // 
 
var rr; 
var res; 

var a_x  = new Array();            // x-positions of particles
var a_y  = new Array();            // y-positions of particles

var b_x  = new Array();            // x-momenta of particles
var b_y  = new Array();            // y-momenta of particles

var a1_x;                          // in collision
var a1_y;                          //

var b1_x;                          // in collision
var b1_y;                          // 

var b2_x;                          // in collision 
var b2_y;                          //

var b3_x;                          // in collision 
var b3_y;                          //

var b4_x;                          // in collision 
var b4_y;                          //

var i;                             // global index for ball number 
var j;                             // general index for ball number 
var M=1;                           // number of balls


function initial_conditions(){
  for (j=0; j<M; j++) {
    a_x[j] = 1.25;
    a_y[j] = 0.95;

    b_x[j] = 0.1*Math.random()-1.5;
    b_y[j] = 0.1*Math.random()-1.7;
  }
}

if (document.getElementsByTagName && !document.all)    // to make it run
    document.all = document.getElementsByTagName("*"); // also with NS 6


if (document.layers) {   
  layerStyleRef  ="layer.";
  layerRef       ="document.layers";
  styleSwitch    ="";
}
else{ 
  layerStyleRef  ="layer.style.";
  layerRef       ="document.all";
  styleSwitch    =".style";
}

function move_body(layerName,left,top){
  eval(layerRef+'["'+layerName+'"]'+styleSwitch+'.top=top');
  eval(layerRef+'["'+layerName+'"]'+styleSwitch+'.left=left');
}

function show_body(layerName){
  eval(layerRef+'["'+layerName+'"]'+styleSwitch+'.visibility="visible"');
}

function hide_body(layerName){
  eval(layerRef+'["'+layerName+'"]'+styleSwitch+'.visibility="hidden"');
}

function inside_inner_table() { 

  if ( (a_x[i]-alpha)*(a_x[i]-alpha) + (a_y[i]-beta)*(a_y[i]-beta)< (r_inner*r_inner) )
  res=1; else res=0; 
  return res;

}

function reflect_inner_table() {

  a1_x  = a_x[i] - alpha;                  // ball position relative to T-center
  a1_y  = a_y[i] - alpha; 
  rr    = Math.sqrt(a1_x*a1_x+a1_y*a1_y);// distance from table (T) center 
 
  cosphi= a1_x/rr;                       // rr is nonzero because i inside
  sinphi= a1_y/rr;                       // when reflect(i) is called

  b1_x  =  b_x[i] - p_x;                 // (I) go into system, where T
  b1_y  =  b_y[i] - p_y;                 // is at rest 

  b2_x  =  cosphi*b1_x + sinphi*b1_y;    // (II) ball speed, where ball 
  b2_y  =- sinphi*b1_x + cosphi*b1_y;    // hits T at right side

  b3_x  = rho*b2_x;                      // (III) ball x-speed after collision
  b3_y  = b2_y;                          //       ball y-speed after collision

  p3_x  = mu*b2_x;                       // (III) T x-speed after collision 
  p3_y  = 0.0;                           //       T y-speed after collision

  b4_x   = cosphi*b3_x - sinphi*b3_y;    // ball speed old (II) inverse
  b4_y   = sinphi*b3_x + cosphi*b3_y;    // 

  p2_x  = cosphi*p3_x - sinphi*p3_y;     // table speed in old system
  p2_y  = sinphi*p3_x + cosphi*p3_y;     // 

  b_x[i]= b4_x+p_x;                      // back to moving system, reverse (I)
  b_y[i]= b4_y+p_y;                      // ball speed

  p_x   = p2_x + p_x;                    // T speed in moving system, reverse (I) 
  p_y   = p2_y + p_y;                    // 

}

function reflect_outer_table() {
  a1_x  = a_x[i] - q_x;  
  a1_y  = a_y[i] - q_y;
  
  if (a1_x> q_x) { b_x[i]=-Math.abs(b_x[i]); }
  if (a1_x<-q_y) { b_x[i]=+Math.abs(b_x[i]); }

  if (a1_y> q_x) { b_y[i]=-Math.abs(b_y[i]); }
  if (a1_y<-q_y) { b_y[i]=+Math.abs(b_y[i]); }

}

function run(){
  for (t=0; t<T; t++) {

    for (j=0; j<M; j++) {
      i=j; 
      a_x[j] = a_x[j] + ds*b_x[j];       //  evolve the balls
      a_y[j] = a_y[j] + ds*b_y[j];       //  evolve the balls
 
      b_y[j] = b_y[j] + ds*g; 

      if (inside_inner_table() == 1) { reflect_inner_table(); }
      reflect_outer_table();

    }
  }

  move_body("B0",200+corr_x+Math.floor(size*a_x[0]),200+corr_y+Math.floor(size*a_y[0]));
  show_body("B0");
  
  move_body("B2",200,200); 
  show_body("B2");

  move_body("B1",200,200);
  show_body("B1");

  if (!interrupt) {setTimeout("run();",50);}
}

function change(name,place) {
   if (document.images != null) {
      name = eval(name + ".src");
      document [place].src = name;
   }
} 

function start_allover(){
  initial_conditions(); 
  run();
}  

// -->
