package math.geometry;

/**
 *  Implementation of 2D point using polar coordinates.
 *  @author Daniel Weschke
 */
public final class PointPolar2D {
    private final double r;
    private final double theta; 
   
    // constructor
    PointPolar2D() {
        double x = Math.random();
        double y = Math.random();
        r = Math.sqrt(x*x + y*y); 
        theta = Math.atan2(y, x);
    }

    PointPolar2D(double x, double y) {
        r = Math.sqrt(x*x + y*y); 
        theta = Math.atan2(y, x);
    }

    double r()     { return r;                 }
    double theta() { return theta;             }
    double x()     { return r*Math.cos(theta); }
    double y()     { return r*Math.sin(theta); }

    double distanceTo(PointPolar2D that) {
        double dx = this.x() - that.x();
        double dy = this.y() - that.y();
        return Math.sqrt(dx*dx + dy*dy);
    }

    public String toString()  { return "(" + x() + ", " + y() + ")"; } 

    // test client
    public static void main(String[] args) {
        PointPolar2D p = new PointPolar2D();
        System.out.println("p  = " + p);
        System.out.println("   x     = " + p.x());
        System.out.println("   y     = " + p.y());
        System.out.println("   r     = " + p.r());
        System.out.println("   theta = " + p.theta());
        System.out.println();

        PointPolar2D q = new PointPolar2D(0.5, 0.5);
        System.out.println("q  = " + q);
        System.out.println("dist(p, q) = " + p.distanceTo(q));
    }
}
