/*
 * ForceField.java
 *
 * Created on 14 styczeń 2005, 00:58
 */

package JSciDraw.physics.fields;

import JSci.maths.*;
import JSci.physics.*;
import JSciDraw.interfaces.*;
import JSciDraw.physics.*;
import java.util.*;
/*
 *
 * @author  Barbara
 */
public class GridDumping extends ForceField{
    /** Creates a new instance of ForceField */
    private int[] hGrL;
    private int[] vGrL;
    private double[][] coeffValues;
    private class DForce extends ProdForce 
    {
        public DForce(ClassicalParticleV2D p){
           super(p);
       }
       private double getCoeff(){
         double xp = p.getPosition().getComponent(0);
         double yp = p.getPosition().getComponent(1);
         int i=0; 
          while ((i<hGrL.length)&&(yp>hGrL[i]))
           i++;
         int j=0;
          while ((j<vGrL.length)&&(xp>vGrL[j]))
           j++;
         return coeffValues[i][j];  
       }
       public AbstractDoubleVector getVector(double t) {
         double co = getCoeff();
          return p.getVelocity().scalarMultiply(-1.*co);
       }
       
    }
    
    private void init(){
      java.util.Arrays.sort(hGrL);
      java.util.Arrays.sort(vGrL);
      coeffValues = new double[hGrL.length+1][vGrL.length+1];
      for (int i=0; i<hGrL.length+1; i++)
       for (int j=0; j<vGrL.length+1; j++)
         coeffValues[i][j] = 0;
        
    }

    public GridDumping(int[] horizontalGridLines, int[] verticalGridLines, double[][] coeffValues) {
      this.hGrL=horizontalGridLines;
      this.vGrL=verticalGridLines;
      init();
      this.coeffValues=coeffValues;
    }
    public ProdForce getForce(ClassicalParticleV2D p){
        return new DForce(p);
    }
   
}
