package org.crusty.math;

public class Vec2 {
	
	float x, y;
	
	public Vec2(float x, float y) {
		this.x = x;
		this.y = y;
	}
	
	public void setX(float x) {
		this.x = x;
	}
	
	public void setY(float y) {
		this.y = y;
	}

	public void set(float f, float g) {
		this.x = f;
		this.y = g;
	}

	/** Returns length */
	public float length() {
		return (float) Math.sqrt(x*x + y*y);
	}

	/** Scales vector to length 1 */
	public Vec2 normalise() {
		float len = (float) (1 / Math.sqrt(x*x + y*y));
		x *= len;
		y *= len;
		return this;
	}

	/** Returns a new Vec2 */
	public Vec2 sub(Vec2 v) {
		return new Vec2(this.x - v.x, this.y - v.y);
	}
	
	/** Returns a new Vec2 */
	public Vec2 sub(Vec2int v) {
		return new Vec2(this.x - v.x, this.y - v.y);
	}

	/** Returns a new Vec2 */
	public Vec2 add(Vec2 v) {
		return new Vec2(this.x + v.x, this.y + v.y);
	}

	/** Returns a new Vec2 */
	public Vec2 clone() {
		return new Vec2(this.x, this.y);
	}
	
//	/** Exact equality check */
//	public boolean equalsTo(Vec2 v) {
//		return (v.x == this.x && v.y == this.y);
//	}
	
	/** Equality check with variance range */
	public boolean equalsTo(Vec2 v, float range) {
		return (Math.abs(this.x - v.x) < range && Math.abs(this.y - v.y) < range);
	}
	
	/** Dot Product */
	public static float dotProd(Vec2 v1, Vec2 v2) {
		return (v1.x*v2.x + v1.y*v2.y);
	}
	
	/** Perpendicular */
	public Vec2 perp() {
		return new Vec2(-this.y, this.x);
	}
	
	/** Formats Vec2 into (x, y) */
	public String toString() {
		return "(" + x + ", " + y + ")";
	}
	
}
