package com.ezardlabs.dethsquare;

/**
 * Representation of 2D vectors and points
 */
public final class Vector2 {
	public float x;
	public float y;
	private Vector2ChangeListener listener;

	/**
	 * Shorthand for writing Vector2(0, 0)
	 */
	public Vector2() {
	}

	/**
	 * Creates a new vector with the given x and y components
	 */
	public Vector2(float x, float y) {
		this.x = x;
		this.y = y;
	}

	/**
	 * Creates a new vector that has the same x and y values as the source vector
	 */
	public Vector2(Vector2 source) {
		this.x = source.x;
		this.y = source.y;
	}

	/**
	 * Sets the x and y components of the vector
	 *
	 * @param x the new x component
	 * @param y the new y component
	 */
	public void set(float x, float y) {
		if (listener != null) listener.onVector2Changed(x - this.x, y - this.y);
		this.x = x;
		this.y = y;
	}

	/**
	 * Sets the x and y components of the vector to be the same as teh given source vector
	 * @param source the source vector to copy x and y component values from
	 */
	public void set(Vector2 source) {
		if (listener != null) listener.onVector2Changed(source.x - x, source.y - y);
		x = source.x;
		y = source.y;
	}

	/**
	 * Returns a new {@link Vector2} that has been offset by the given parameters
	 * @param x The amount to offset the x coordinate by
	 * @param y The amount to offset the y coordinate by
	 * @return The offset {@link Vector2}
	 */
	public Vector2 offset(float x, float y) {
		return new Vector2(this.x + x, this.y + y);
	}

	/**
	 * Makes this vector have a magnitude of 1
	 *
	 * Note that this method modifies the vector it is called on; if you want to keep the vector
	 * unchanged, then use {@link #normalised()} instead
	 */
	public void normalise() {
		double length = Math.sqrt(x * x + y * y);
		x /= length;
		y /= length;
	}

	/**
	 * Returns this vector with a magnitude of 1
	 *
	 * Not that this method does not modify the vector it is caleld on; if you want to modify the
	 * vector then use {@link #normalise()} instead
	 *
	 * @return The normalised version of this vector
	 */
	public Vector2 normalised() {
		Vector2 v = new Vector2(this);
		v.normalise();
		return v;
	}

	/**
	 * Calculates the distance between 2 vector points
	 * @param a the first point
	 * @param b the second point
	 * @return The distance between the 2 points
	 */
	public static double distance(Vector2 a, Vector2 b) {
		return Math.sqrt(Math.pow(b.x - a.x, 2) + Math.pow(b.y - a.y, 2));
	}

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

	@Override
	public boolean equals(Object o) {
		return o instanceof Vector2 && ((Vector2) o).x == x && ((Vector2) o).y == y;
	}

	void setVector2ChangeListener(Vector2ChangeListener listener) {
		this.listener = listener;
	}

	interface Vector2ChangeListener {
		void onVector2Changed(float xDiff, float yDiff);
	}
}
@@ -1481,24 +1481,517 @@
 y + y);%0A%09%7D%0A%0A
+%09/**%0A%09 * Returns the length of this vector%0A%09 *%0A%09 * If you want to compare the magnitudes of 2 vectors then it is advisable to compare their%0A%09 * square magnitudes(%7B@link #sqrMagnitude()%7D) as they are faster to compute%0A%09 *%0A%09 * @return The length of this vector%0A%09 */%0A%09public double magnitude() %7B%0A%09%09return Math.sqrt(x * x + y * y);%0A%09%7D%0A%0A%09/**%0A%09 * Returns the squared length of this vector%0A%09 * @return The squared length of this vector%0A%09 */%0A%09public float sqrMagnitude() %7B%0A%09%09return x * x + y * y;%0A%09%7D%0A%0A
 %09/**%0A%09 * Mak

Add magnitude methods