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);
	}

	/**
	 * 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,690 @@
 y + y);%0A%09%7D%0A%0A
+%09/**%0A%09 * Makes this vector have a magnitude of 1%0A%09 *%0A%09 * Note that this method modifies the vector it is called on; if you want to keep the vector%0A%09 * unchanged, then use %7B@link #normalised()%7D instead%0A%09 */%0A%09public void normalise() %7B%0A%09%09double length = Math.sqrt(x * x + y * y);%0A%09%09x /= length;%0A%09%09y /= length;%0A%09%7D%0A%0A%09/**%0A%09 * Returns this vector with a magnitude of 1%0A%09 *%0A%09 * Not that this method does not modify the vector it is caleld on; if you want to modify the%0A%09 * vector then use %7B@link #normalise()%7D instead%0A%09 *%0A%09 * @return The normalised version of this vector%0A%09 */%0A%09public Vector2 normalised() %7B%0A%09%09Vector2 v = new Vector2(this);%0A%09%09v.normalise();%0A%09%09return v;%0A%09%7D%0A%0A
 %09/**%0A%09 * Cal

Add normalisation functions