A javascript 2d vector library for Node.js and client.
Install as a node module via npm.
npm install taksim-vec2 --save
and include in a file;
var Vec2 = require('taksim-vec2');You can pull down by using Bower
bower install taksim-vec2 --save
or just download the latest minified version and include in your document.
<script type='text/javascript' src='taksim-vec2.min.js'></script>Pass x and y to the constructor to create a new instance. If nothing supplied, x and y will be equal to zero.
var a = new Vec2(10, 5);
var b = new Vec2();
a.toString();
=> '(10, 5)'
b.toString();
=> '(0, 0)'Sets x and y coordinates of this vector to the incoming vector's coordinates. Returns this.
var a = new Vec2(10, 5);
var b = new Vec2(11, 7);
a.set(b).toString();
=> '(11, 7)'Sets x to the passed number. Returns this.
var a = new Vec2(10, 5);
a.setX(11).toString();
=> '(11, 5)'Sets y to the passed number. Returns this.
var a = new Vec2(10, 5);
a.setY(7).toString();
=> '(10, 7)'Sets x and y to the initial values. Returns this.
var a = new Vec2(10, 5);
a.setX(11).setY(7).toString();
=> '(11, 7)'
a.reset().toString();
=> '(10, 5)'Sets x to its initial value. Returns this.
var a = new Vec2(10, 5);
a.setX(11).toString();
=> '(11, 5)'
a.resetX().toString();
=> '(10, 5)'Sets y to its initial value. Returns this.
var a = new Vec2(10, 5);
a.setY(7).toString();
=> '(10, 7)'
a.resetY().toString();
=> '(10, 5)'Sets x and y to 0. Returns this.
var a = new Vec2(10, 5);
a.zero().toString();
=> '(0, 0)'Adds x and y coordinates of the incoming vector to this vector's coordinates. Returns this.
var a = new Vec2(10, 5);
var b = new Vec2(1, 2);
a.add(b).toString();
=> '(11, 7)'Adds the passed number to this vector's x coordinate. Returns this.
var a = new Vec2(10, 5);
a.addX(1).toString();
=> '(11, 5)'Adds the passed number to this vector's y coordinate. Returns this.
var a = new Vec2(10, 5);
a.addY(2).toString();
=> '(10, 7)'Subtracts x and y coordinates of the incoming vector from this vector's coordinates. Returns this.
var a = new Vec2(10, 5);
var b = new Vec2(1, 2);
a.subtract(b).toString();
=> '(9, 3)'Subtracts the passed number from this vector's x coordinate. Returns this.
var a = new Vec2(10, 5);
a.subtractX(1).toString();
=> '(9, 5)'Subtracts the passed number from this vector's y coordinate. Returns this.
var a = new Vec2(10, 5);
a.subtractY(2).toString();
=> '(10, 3)'Multiplies x and y coordinates of this vector with the incoming vector's coordinates. Returns this.
var a = new Vec2(10, 5);
var b = new Vec2(1, 3);
a.multiply(b).toString();
=> '(10, 15)'Multiplies x coordinate of this vector with the passed number. Returns this.
var a = new Vec2(10, 5);
a.multiplyX(3).toString();
=> '(30, 5)'Multiplies y coordinate of this vector with the passed number. Returns this.
var a = new Vec2(10, 5);
a.multiplyY(3).toString();
=> '(10, 15)'Divides x and y coordinates of this vector by the incoming vector's coordinates. Returns this.
var a = new Vec2(10, 5);
var b = new Vec2(1, 2);
a.divide(b).toString();
=> '(10, 2.5)'Divides x coordinate of this vector by the passed number. Returns this.
var a = new Vec2(10, 5);
a.divideX(2).toString();
=> '(5, 5)'Divides y coordinate of this vector by the passed number. Returns this.
var a = new Vec2(10, 5);
a.divideY(2).toString();
=> '(10, 2.5)'Applies Math.round to x and y. Returns this.
var a = new Vec2(10.8, 5.2);
a.round().toString();
=> '(11, 5)'Applies Math.round to x. Returns this.
var a = new Vec2(10.8, 5.2);
a.roundX().toString();
=> '(11, 5.2)'Applies Math.round to y. Returns this.
var a = new Vec2(10.8, 5.2);
a.roundY().toString();
=> '(10.8, 5)'Multiplies x and y with -1. Returns this.
var a = new Vec2(10, 5);
a.invert().toString();
=> '(-10, -5)'Multiplies x with -1. Returns this.
var a = new Vec2(10, 5);
a.invertX().toString();
=> '(-10, 5)'Multiplies y with -1. Returns this.
var a = new Vec2(10, 5);
a.invertY().toString();
=> '(10, -5)'Rotates this vector to supplied angle in degrees (CCW from +X axis). Returns this.
var a = new Vec2(10, 5);
a.rotateTo(180).toString();
=> '(-10, -5)'Adds supplied angle in degrees to this vector (CCW from +X axis). Returns this.
var a = new Vec2(100, 100);
a.rotateAdd(45).toString();
=> '(-100, 100)'Makes the length of this vector 1 while keeping its direction same. Returns this.
var a = new Vec2(10, 5);
a.normalize().toString();
=> '(0.8944271909999159, 0.4472135954999579)'Compares two vectors and sets this vector's coordinates to lowest x and y. Returns this.
var a = new Vec2(10, 5);
var b = new Vec2(11, 3);
a.min(b).toString();
=> '(10, 3)'Sets x coordinate to the lowest of supplied number and x. Returns this.
var a = new Vec2(10, 5);
a.minX(9).toString();
=> '(9, 5)'
a.minX(10).toString();
=> '(9, 5)'Sets y coordinate to the lowest of supplied number and y. Returns this.
var a = new Vec2(10, 5);
a.minY(3).toString();
=> '(10, 3)'
a.minY(5).toString();
=> '(10, 3)'Compares two vectors and sets this vector's coordinates to greatest x and y. Returns this.
var a = new Vec2(10, 5);
var b = new Vec2(11, 3);
a.max(b).toString();
=> '(11, 5)'Sets x coordinate to the greatest of supplied number and x. Returns this.
var a = new Vec2(10, 5);
a.maxX(11).toString();
=> '(11, 5)'
a.maxX(10).toString();
=> '(11, 5)'Sets y coordinate to the greatest of supplied number and y. Returns this.
var a = new Vec2(10, 5);
a.maxY(7).toString();
=> '(10, 7)'
a.maxY(5).toString();
=> '(10, 7)'Limits the coordinates with a bounding rectangle. After invoking this method, the resulting x and y of all other methods won't exceed the bounding rectangle. Returns this.
var a = new Vec2(10, 5);
var lowerBound = new Vec2(9, 2);
var upperBound = new Vec2(2, 4);
a.limit(lowerBound, upperBound).toString();
=> '(9, 4)'
// x and y are now limited with lower and upper boundaries.
a.addX(10).addY(10).toString();
=> '(9, 4)'
a.max(10).minY(1).toString();
=> '(9, 2)'Alias of limit
Frees the coordinates from bounding rectangle. Returns this.
var a = new Vec2(10, 5);
var lowerBound = new Vec2(9, 2);
var upperBound = new Vec2(2, 4);
a.limit(lowerBound, upperBound);
a.addX(10).addY(10).toString();
=> '(9, 4)'
a.unlimit().addX(10).addY(10).toString();
=> '(19, 14)'Alias of unlimit
Randomizes x and y within the bounding rectangle. If there is no bounding rectangle created with limit method, the coordinates is kept same. Returns this.
var a = new Vec2(10, 5);
var lowerBound = new Vec2(9, 2);
var upperBound = new Vec2(2, 4);
// Coordinates remain same here
a.random().toString();
=> '(10, 5)'
// After limiting the vector, you can use random method
a.limit(lowerBound, upperBound).random().toString();
=> '(6, 3)'Randomizes x within the bounding rectangle. Returns this.
var a = new Vec2(10, 5);
var lowerBound = new Vec2(9, 2);
var upperBound = new Vec2(2, 4);
// Coordinates remain same here
a.randomX().toString();
=> '(10, 5)'
// After limiting the vector, you can use random method
a.limit(lowerBound, upperBound).randomX().toString();
=> '(6, 5)'Randomizes y within the bounding rectangle. Returns this.
var a = new Vec2(10, 5);
var lowerBound = new Vec2(9, 2);
var upperBound = new Vec2(2, 4);
// Coordinates remain same here
a.randomY().toString();
=> '(10, 5)'
// After limiting the vector, you can use random method
a.limit(lowerBound, upperBound).randomY().toString();
=> '(10, 3)'Returns dot product of two vectors.
var a = new Vec2(10, 5);
var a = new Vec2(11, 7);
a.dot(b);
=> 145Returns cross product of two vectors.
var a = new Vec2(10, 5);
var a = new Vec2(11, 7);
a.dot(b);
=> 15Returns length/magnitude.
var a = new Vec2(3, 4);
a.length();
=> 5Returns angle in degrees with respect to +X axis.
var a = new Vec2(3, 4);
a.angle();
=> 53.13010235415598Returns distance between two vectors.
var a = new Vec2(10, 5);
var b = new Vec2(11, 7);
a.distance(b);
=> 2.23606797749979Returns a new instance of Vec2 with this vector's coordinates and bounding rectangle (See limit() method).
var a = new Vec2(10, 5);
a.clone().addX(1).toString();
=> '(11, 5)'
a.toString();
=> '(10, 5)'Returns true if two vectors coordinates are same.
var a = new Vec2(10, 5);
var b = new Vec2(10, 5);
a.isEqual(b);
=> true
a.toString();
=> '(10, 5)'Returns string representation of the coordinates.
var a = new Vec2(10, 5);
a.toString();
=> '(10, 5)'Returns array representation of the coordinates.
var a = new Vec2(10, 5);
a.toArray();
=> [10, 5]MIT Copyright (c) 2015 taksim.io