/**
 *
 *created by Przemek
 *
 */

package JSciDraw.physics.forces;
import JSciDraw.physics.*;
import JSciDraw.interfaces.*;
import JSci.maths.*;
import JSci.physics.Force;
import JSci.physics.AbstractClassicalParticle;
import java.awt.*;
import java.util.*;

public class AttachedSpring implements Drawable2D, Moveable2D{
    protected final double coefficient;
    protected final AbstractDoubleVector x;
    protected final ClassicalParticleV2D part1;
    protected final double bL;
    protected Color color=Color.blue;

    public AttachedSpring(AbstractDoubleVector pos, ClassicalParticleV2D p, double coeff, double bL) {
                coefficient = coeff;
                part1=p;
                x = pos;
                this.bL=bL;
                p.addExtForce(this.createForce(pos,p));
        }
 
    private class Force extends JSci.physics.Force {
       private final AbstractClassicalParticle p;
       private final AbstractDoubleVector pos;
       public Force(AbstractDoubleVector pos, ClassicalParticleV2D p) {
            this.p = p;
            this.pos = pos;
       }
       public AbstractDoubleVector getVector(double t) 
       {
        AbstractDoubleVector wodz =(x.subtract(p.getPosition()));
        double wart=wodz.norm();
        wodz.normalize();
        return wodz.scalarMultiply(coefficient*(wart-bL));
       }
    }
      
    public double energy() {
       AbstractDoubleVector wodz =(x.subtract(part1.getPosition()));
       double dX=wodz.norm()-bL;
       return dX*dX*coefficient/2.;
    }

    public Force createForce(AbstractDoubleVector pos, ClassicalParticleV2D p) 
    {
      return new Force(pos,p);
        
    }

    public void draw2D(Graphics2D g2D){
        g2D.setColor(color);
        g2D.drawLine((int)part1.getPosition().getComponent(0),(int)part1.getPosition().getComponent(1),
        (int)x.getComponent(0),(int)x.getComponent(1));
 
    }
    public void erase2D(Graphics2D g2D){
        g2D.setColor(Color.white);
        g2D.drawLine((int)part1.xOl,(int)part1.yOl,
        (int)x.getComponent(0),(int)x.getComponent(1));
    }
    
    public Color getColor() { 
        return color; 
    }
    public void setColor(Color c) { 
        color = c; 
    }
    public void move2D(double dt) {}
    public void calculateForces(double dt) {}
    
    public void addExtForce(JSci.physics.Force f) {
    }
}

