package math.geometry;

import math.Maths;

/**
 * Work with 2D point using rectangular coordinates.
 * @author Daniel Weschke
 */
public class Point2D extends Point {
	
	/**
	 * null point.
	 */
	public Point2D(){
		super(2);
	}
	
	/**
	 * (x,y)-point.
	 * @param a x-coordinate
	 * @param b y-coordinate
	 */
	public Point2D(double a, double b){
		super(a, b);
	}
	
	public Point2D(Point2D p){
		super(p);
	}
	
	/**
	 * Create random point.
	 */
	public static Point2D random(){
        return new Point2D(Math.random(), Math.random());
	}
	
	/**
	 * Get angle between first axis (x) and the line from origin to the point.
	 * @return angle
	 */
    public double theta(){ return Math.atan2(y(), x()); }
	
	/**
	 * Set x-coordinate.
	 * @param a x-coordinate
	 */
	public void setX(double a){ set(0, a); }
	
	/**
	 * set y-coordinate.
	 * @param b y-coordinate
	 */
	public void setY(double b){ set(1, b); }
	
	public static Point2D polarToCartesianD(double length, double angle){
		return new Point2D(length*Maths.cosd(angle), length*Maths.sind(angle));
	}
	
	public static double[] cartesianToPolar(double x, double y){
		return new double[]{
				Maths.hypot(x, y),
				Math.atan2(y, x)
		};
	}
	
    /**
     * test client
     * @param args
     */
    public static void main(String[] args) {
        Point2D p = Point2D.random();
        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();

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