UNPKG

verlet

Version:
179 lines (144 loc) 5.71 kB
/* Copyright 2013 Sub Protocol and other contributors http://subprotocol.com/ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ // A simple 2-dimensional vector implementation module.exports = Vec2 function Vec2(x, y) { this.x = x || 0; this.y = y || 0; } Vec2.prototype.add = function(v) { return new Vec2(this.x + v.x, this.y + v.y); } Vec2.prototype.sub = function(v) { return new Vec2(this.x - v.x, this.y - v.y); } Vec2.prototype.mul = function(v) { return new Vec2(this.x * v.x, this.y * v.y); } Vec2.prototype.div = function(v) { return new Vec2(this.x / v.x, this.y / v.y); } Vec2.prototype.scale = function(coef) { return new Vec2(this.x*coef, this.y*coef); } Vec2.prototype.mutableSet = function(v) { this.x = v.x; this.y = v.y; return this; } Vec2.prototype.mutableAdd = function(v) { this.x += v.x; this.y += v.y; return this; } Vec2.prototype.mutableSub = function(v) { this.x -= v.x; this.y -= v.y; return this; } Vec2.prototype.mutableMul = function(v) { this.x *= v.x; this.y *= v.y; return this; } Vec2.prototype.mutableDiv = function(v) { this.x /= v.x; this.y /= v.y; return this; } Vec2.prototype.mutableScale = function(coef) { this.x *= coef; this.y *= coef; return this; } Vec2.prototype.equals = function(v) { return this.x == v.x && this.y == v.y; } Vec2.prototype.epsilonEquals = function(v, epsilon) { return Math.abs(this.x - v.x) <= epsilon && Math.abs(this.y - v.y) <= epsilon; } Vec2.prototype.length = function(v) { return Math.sqrt(this.x*this.x + this.y*this.y); } Vec2.prototype.length2 = function(v) { return this.x*this.x + this.y*this.y; } Vec2.prototype.dist = function(v) { return Math.sqrt(this.dist2(v)); } Vec2.prototype.dist2 = function(v) { var x = v.x - this.x; var y = v.y - this.y; return x*x + y*y; } Vec2.prototype.normal = function() { var m = Math.sqrt(this.x*this.x + this.y*this.y); return new Vec2(this.x/m, this.y/m); } Vec2.prototype.dot = function(v) { return this.x*v.x + this.y*v.y; } Vec2.prototype.angle = function(v) { return Math.atan2(this.x*v.y-this.y*v.x,this.x*v.x+this.y*v.y); } Vec2.prototype.angle2 = function(vLeft, vRight) { return vLeft.sub(this).angle(vRight.sub(this)); } Vec2.prototype.rotate = function(origin, theta) { var x = this.x - origin.x; var y = this.y - origin.y; return new Vec2(x*Math.cos(theta) - y*Math.sin(theta) + origin.x, x*Math.sin(theta) + y*Math.cos(theta) + origin.y); } Vec2.prototype.toString = function() { return "(" + this.x + ", " + this.y + ")"; } function test_Vec2() { var assert = function(label, expression) { console.log("Vec2(" + label + "): " + (expression == true ? "PASS" : "FAIL")); if (expression != true) throw "assertion failed"; }; assert("equality", (new Vec2(5,3).equals(new Vec2(5,3)))); assert("epsilon equality", (new Vec2(1,2).epsilonEquals(new Vec2(1.01,2.02), 0.03))); assert("epsilon non-equality", !(new Vec2(1,2).epsilonEquals(new Vec2(1.01,2.02), 0.01))); assert("addition", (new Vec2(1,1)).add(new Vec2(2, 3)).equals(new Vec2(3, 4))); assert("subtraction", (new Vec2(4,3)).sub(new Vec2(2, 1)).equals(new Vec2(2, 2))); assert("multiply", (new Vec2(2,4)).mul(new Vec2(2, 1)).equals(new Vec2(4, 4))); assert("divide", (new Vec2(4,2)).div(new Vec2(2, 2)).equals(new Vec2(2, 1))); assert("scale", (new Vec2(4,3)).scale(2).equals(new Vec2(8, 6))); assert("mutable set", (new Vec2(1,1)).mutableSet(new Vec2(2, 3)).equals(new Vec2(2, 3))); assert("mutable addition", (new Vec2(1,1)).mutableAdd(new Vec2(2, 3)).equals(new Vec2(3, 4))); assert("mutable subtraction", (new Vec2(4,3)).mutableSub(new Vec2(2, 1)).equals(new Vec2(2, 2))); assert("mutable multiply", (new Vec2(2,4)).mutableMul(new Vec2(2, 1)).equals(new Vec2(4, 4))); assert("mutable divide", (new Vec2(4,2)).mutableDiv(new Vec2(2, 2)).equals(new Vec2(2, 1))); assert("mutable scale", (new Vec2(4,3)).mutableScale(2).equals(new Vec2(8, 6))); assert("length", Math.abs((new Vec2(4,4)).length() - 5.65685) <= 0.00001); assert("length2", (new Vec2(2,4)).length2() == 20); assert("dist", Math.abs((new Vec2(2,4)).dist(new Vec2(3,5)) - 1.4142135) <= 0.000001); assert("dist2", (new Vec2(2,4)).dist2(new Vec2(3,5)) == 2); var normal = (new Vec2(2,4)).normal() assert("normal", Math.abs(normal.length() - 1.0) <= 0.00001 && normal.epsilonEquals(new Vec2(0.4472, 0.89443), 0.0001)); assert("dot", (new Vec2(2,3)).dot(new Vec2(4,1)) == 11); assert("angle", (new Vec2(0,-1)).angle(new Vec2(1,0))*(180/Math.PI) == 90); assert("angle2", (new Vec2(1,1)).angle2(new Vec2(1,0), new Vec2(2,1))*(180/Math.PI) == 90); assert("rotate", (new Vec2(2,0)).rotate(new Vec2(1,0), Math.PI/2).equals(new Vec2(1,1))); assert("toString", (new Vec2(2,4)) == "(2, 4)"); }