package real;

public class Real2DVector {
	public double x, y;
	
	public Real2DVector() {
		x = 0; y = 0;
	}
	
	public Real2DVector(double x, double y) {
		this.x = x; this.y = y;
	}
	
	public Real2DVector(double u[]) {
		x = u[0]; y = u[1];
	}
	
	public Real2DVector plus(Real2DVector u) {
		return(new Real2DVector(x + u.x, y + u.y));
	}
	
	public Real2DVector minus(Real2DVector u) {
		return(new Real2DVector(x - u.x, y - u.y));
	}

	public Real2DVector times(double c) {
		return(new Real2DVector(x*c, y*c));
	}
	
	public double length() {
		double X = Math.abs(x), Y = Math.abs(y);
		double r;
		if (X > Y) {
			r = X*Math.sqrt(1 + (Y/X)*(Y/X));
		} else {
			r = Y*Math.sqrt(1 + (X/Y)*(X/Y));
		}
		return(r);
	}
	
	public Real2DVector rotatedBy(double a) {
		double c = Math.cos(a);
		double s = Math.sin(a);
		double X = c*x - s*y;
		double Y = s*x + c*y;
		return(new Real2DVector(X, Y));
	}
	
	// negative if oriented negatively
	public double angleBetween(Real2DVector u) {
		double t = (u.x*x + u.y*y)/(length()*u.length());
		double d = u.y*x - u.x*y;
		// d = u.perp
		double a = Math.acos(t);
		if (d < 0) a = -a;
		return(a);
	}
	
	public String toString() {
		String s = new String("[");
		s = s + x + ", " + y + "]";
		return(s);
	}
}