/** * Flade - Flash Dynamics Engine * Release 0.4 alpha * Vector class * Copyright 2004, 2005 Alec Cove * * This file is part of Flade. The Flash Dynamics Engine. * * Flade is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Flade is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Flade; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * Flash is a registered trademark of Macromedia */ Vector = function(x,y) { this.x = x; this.y = y; } Vector.prototype.dot = function(v) { return this.x * v.x + this.y * v.y; } Vector.prototype.cross = function(v) { return this.x * v.y - this.y * v.x; } Vector.prototype.plus = function(v) { this.x += v.x; this.y += v.y; return this; } Vector.prototype.plusNew = function(v) { return new Vector(this.x + v.x, this.y + v.y); } Vector.prototype.minus = function(v) { this.x -= v.x; this.y -= v.y; return this; } Vector.prototype.minusNew = function(v) { return new Vector(this.x - v.x, this.y - v.y); } Vector.prototype.mult = function(s) { this.x *= s; this.y *= s; return this; } Vector.prototype.multNew = function(s) { return new Vector (this.x * s, this.y * s); } Vector.prototype.distance = function(p) { var dx = this.x - p.x; var dy = this.y - p.y; return Math.sqrt(dx * dx + dy * dy); } Vector.prototype.normalize = function() { var mag = Math.sqrt(this.x * this.x + this.y * this.y); this.x /= mag; this.y /= mag; return this; }