Vector2D = function (x, y) {
	return this.reset (x, y);
};

Vector2D.prototype = {
	DEG2RAD: Math.PI / 180,
	RAD2DEG: 180 / Math.PI,
	// saves creating a new vector
	cache: {
		x: 0,
		y: 0
	},
	// add Vector2D v to this
	add: function (v) {
		this.x += v.x;
		this.y += v.y;
		return this;
	},
	 // subtract Vector2D v to this
	subtract: function (v) {
		this.x -= v.x;
		this.y -= v.y;
		return this;
	},
	// multiply this by scalar
	multiply: function (s) {
		this.x *= s;
		this.y *= s;
		return this;
	},
	divide: function (s) {
		this.x /= s;
		this.y /= s;
		return this;
	},
	reset: function (x, y) {
		this.x = x || 0;
		this.y = y || 0;
		return this;
	},
	copyFrom: function (v) {
		this.x = v.x;
		this.y = v.y;
	},
	// return a vector with the same direction and magnitude
	clone: function () {
		return new Vector2D (this.x, this.y);
	},
	rotate: function (theta) {
		var sin = Math.sin (theta * Vector2D.prototype.DEG2RAD),
			cos = Math.cos (theta * Vector2D.prototype.DEG2RAD),
			cache = {
						x: this.x,
						y: this.y
					}


		this.x = cache.x * cos - cache.y * sin;
		this.y = cache.x * sin + cache.y * cos;
		return this;
	},
	isSameDirection: function (v) {
		return (this.angle() == v.angle());
	},
	isSameMagnitude: function (v) {
		return (this.magnitudeSquared() == v.magnitudeSquared());
	},
	magnitude: function () {
		return Math.sqrt(this.x * this.x + this.y * this.y);
	},
	magnitudeSquared: function () {
		return this.x * this.x + this.y * this.y;
	},
	normalise: function () {
		return this.divide (this.magnitude());
	},
	reverse: function () {
		this.x = -this.x;
		this.y = -this.y;
		return this;
	},
	angle: function () {
		return Math.atan2(this.y,this.x) * this.RAD2DEG;
	},
	toString: function () {
		return "Vector2D [" + this.x + ", " + this.y + "]";
	}
};

