// Javascript Point object

function Point(x,y) {
	this.x = x;
	this.y = y;
}

// Return magnitude of point
Point.prototype.magnitude = function() {
	return(Math.sqrt(this.x*this.x + this.y*this.y));
}

// Methods for division, multiplication, subtraction, and addition
Point.prototype.normalize = function(a) {
	if (arguments.length == 0) {
		// No arguments, make a unit vector
		d = this.magnitude();
		a = new Point(d,d);
	} else if (arguments.length == 2) {
		// Two arguments, assume it's x,y
		a = new Point(arguments[0],arguments[1]);
	}
	this.x = this.x / a.x;
	this.y = this.y / a.y;
}

Point.prototype.scale = function(a) { 
	b = arguments.length == 2 ? arguments[1] : arguments[0];
	return (new Point(this.x*a, this.y*b)); 
}

Point.subtract = function(a,b) { return (new Point(a.x-b.x, a.y-b.y)); }
Point.add      = function(a,b) { return (new Point(a.x+b.x, a.y+b.y)); }
