/*
 * CenterEllipse.java
 *
 * Created on 22 grudzień 2004, 12:00
 */

package JSciDraw.physics.forces.constraints;

import JSci.maths.*;
import JSci.physics.*;
import JSciDraw.*;
import JSciDraw.physics.*;
import JSciDraw.physics.forces.*;
import java.awt.*;

/**
 *
 * @author  Barbara 
 */
public class CenterEllipse extends PolarChartConstraints{
    
    /** Creates a new instance of CenterEllipse */
    public CenterEllipse(double a, double b,AbstractDoubleVector cent)
    {
      super(cent);
      double c;
      points = new AbstractDoubleVector[1];
      points[0]=cent;
      if (a>b) {
          c=Math.sqrt((a*a)-(b*b));
          center=cent.add(new Double2Vector(c,0));
      } else{
          c=Math.sqrt((-a*a)+(b*b));
            center=cent.add(new Double2Vector(0,c));
      }
      params=new double[4];
      params[0]=a;
      params[1]=b;
      params[2]=(Math.min(a,b)*Math.min(a,b)/Math.max(a,b));
      params[3]=c/Math.max(a,b);
    }
    public void draw(ClassicalParticleV2D p, java.awt.Graphics2D g2D) {
      if (draw){
       double x0=points[0].getComponent(0)-params[0];
       double y0=points[0].getComponent(1)-params[1];
       g2D.setColor(Color.black);
       g2D.drawOval((int)x0,(int)y0,(int)(2*params[0]),(int)(2*params[1]));
      }
    }
    public void erase(ClassicalParticleV2D p, java.awt.Graphics2D g2D) {
      if (draw){ 
       double x0=points[0].getComponent(0)-params[0];
       double y0=points[0].getComponent(1)-params[1];
       g2D.setColor(Color.white);
       g2D.drawOval((int)x0,(int)y0,(int)(2*params[0]),(int)(2*params[1]));
      }
    }
    public AbstractDoubleVector getTangent(AbstractDoubleVector x0) {
        AbstractDoubleVector r1=x0.subtract(center);
        AbstractDoubleVector tan=new Double2Vector(-2.*r1.getComponent(1)/params[1]/params[1],
                                 2.*r1.getComponent(0)/params[0]/params[0]);
        tan.normalize();
        return tan;
    }
    
   protected double getRadius(double ang) {
     return  params[2]/(1+(params[3]*Math.sin(ang)));
   }
 
}
