/* Copyright 1996 Rujith de Silva.
   This code is in the public domain. */
package NET.industry.util;
import java.lang.Math;

public class Position extends Object
{
    public double x = 0.0, y = 0.0;
    public double azimuth = 0.0;
    private double oldx, oldy;
    private double nonRotateRadius = 0.0;

    public Position (double xa, double ya)
    {
        x = xa;
        y = ya;
    }

    public Position (double xa, double ya, double azimutha)
    {
        this (xa, ya);
        azimuth = azimutha;
    }

    /* Return true if the reset leads to a `rotating mode'. */
    public boolean reset (double x, double y)
    {
        oldx = x;
        oldy = y;
        azimuth = 0.0;
        double diffx = oldx - this.x;
        double diffy = oldy - this.y;
        return Math.sqrt (diffx * diffx + diffy * diffy) > nonRotateRadius;
    }

    public double getNonRotateRadius () 
    {
        return nonRotateRadius;
    }

    public void setNonRotateRadius (double r)
    {
        nonRotateRadius = r;
    }

    public void move (double newx, double newy)
    {
        double olddiffx, olddiffy, newdiffx, newdiffy;
        olddiffx = oldx - this.x;
        olddiffy = oldy - this.y;
        newdiffx = newx - this.x;
        newdiffy = newy - this.y;
        double lenold = Math.sqrt (olddiffx * olddiffx + olddiffy *
                                   olddiffy);
        if (lenold > nonRotateRadius)
        {
            double lennew = Math.sqrt (newdiffx * newdiffx + newdiffy *
                                       newdiffy);
            double oldangle = Math.atan2 (olddiffy, olddiffx);
            double newangle = Math.atan2 (newdiffy, newdiffx);
            double anglediff = newangle - oldangle;
            azimuth += anglediff;
            azimuth %= 2.0 * Math.PI;
            double movement = lennew - lenold;
            x += movement * Math.cos (newangle);
            y += movement * Math.sin (newangle);
        }
        else
        {
            x += newx - oldx;
            y += newy - oldy;
        }
        oldx = newx;
        oldy = newy;
    }
}
