/*************************************************************************
 *  Compilation:  javac Point.java
 *  Execution:    java Point
 *
 *  Implementation of 2D point using rectangular coordinates.
 *
 *************************************************************************/

public class Point { 
    private double x;
    private double y; 
   
    // create and initialize a random point in unit square
    public Point() {
        this.x = Math.random();
        this.y = Math.random();
    }

    // create and initialize a point with given (x, y)
    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }
  
    // accessor methods
    public double x() { return x; }
    public double y() { return y; }

    public boolean eq(Point q) {
        Point p = this;
        return (p.x == q.x && p.y == q.y);
    }

    // return Euclidean distance between invoking point p and q
    public double distanceTo(Point q) {
        Point p = this;
        double dx = p.x - q.x;
        double dy = p.y - q.y;
        return Math.sqrt(dx*dx + dy*dy);
    }

    // draw spot at (x, y)
    public void draw() {
        StdDraw.point(x, y);
    }

    // draw the line from the invoking point p to q
    public void drawTo(Point q) {
        Point p = this;
        StdDraw.line(p.x, p.y, q.x, q.y);
    }

    // return string representation of this point
    public String toString() { return "(" + x + ", " + y + ")"; } 


    // test client
    public static void main(String[] args) {
        Point p = new Point();
        System.out.println("p  = " + p);
        Point q = new Point(0.5, 0.5);
        System.out.println("q  = " + q);
        System.out.println("dist(p, q) = " + p.distanceTo(q) + " = " + q.distanceTo(p));
    }
}

