Vector is an extended 2D vector library with lots of utilitary methods.
<script src="Vector.js"></script>x-axis and y-axis values. These are the only properties of the Vector object.
let vec = new Vector(100, 50);
console.log(vec.x, vec.y);//--> 100, 50Length of the vector.
let vec = new Vector(100, 50);
vec.length//111.80339887498948Angle of the vector in radians.
let vec = new Vector(100, 50);
vec.angle//0.4636476090008061Set the length of the vector while maintaining the original angle of the vector.
let vec = new Vector(100, 50);
vec.length = 25;//{x: 22.360, y: 11.180}Set the angle of the vector in radians ie. rotate the vector to point at given angle.
let vec = new Vector(100, 50);
vec.angle = Math.PI;//{x: -111.80339887498948, y: 0}Sum of one or more vectors. Returns self so can be chained.
let vec = new Vector(50, 50);
let other = new Vector(100, 100);
let another = new Vector(5, 0);
vec.add(other, another);//{x: 155, y: 150}Returns a new vector with x & y values copied from self.
let vec = new Vector(50, 50);
let copy = vec.clone();
console.log(copy.x, copy.y);//--> 50, 50Set x & y properties from an array. Returns self so can be chained.
let vec = new Vector();
let arr = [10, 20];
vec.fromArray(arr);//{x: 10, y: 20}Set x & y properties from object. Returns self so can be chained.
let obj = {x: 10, y: 20};
let vec = new Vector();
vec.fromObject(obj);//{x: 10, y: 20}Set x & y properties from polar coordinates. Returns self so can be chained.
let vec = new Vector();
vec.fromAngleRadius(Math.PI, 50);//{x: -50, y: 0}Returns a new array with x & y values at index 0 (x) and 1 (y).
let vec = new Vector(10, 20);
let arr = vec.toArray();//[10, 20]Returns a new object with x & y properties copied from the vector.
let vec = new Vector(10, 20);
let obj = vec.toObject();//{x: 10, y: 20}Returns a string presentation of the vector properties.
let vec = new Vector(10, 20);
let str = vec.toString();//{x: 10, y: 20}'Set the x & y values. Can be used with one or two arguments:
- 1 argument: copy object's x & y properties to the vector
- 2 arguments: set provided x & y arguments to the vector Returns self so can be chained.
//one argument
let other = {x: 200, y: 300};//this can be a vector, too
let vec = new Vector();
vec.set(other);//{x: 200, y: 300};
//two arguments
let v = new Vector();
v.set(20, 50);//{x: 20, y: 50};Sets vector to point from one point to another. Returns self so can be chained.
//one argument
let from = new Vector(50, 50);
let to = new Vector(100, 300);
let vec = new Vector();
vec.setFromTo(from, to);//{x: 50, y: 250};Generates random vector with a length of 1. Optional argument can be passed to specify the length of the resulting vector. Returns self so can be chained.
let vec = new Vector();
vec.random();//possibly: {x: -0.23372631348780365, y: 0.9723024274285244}Subtract one or more vectors. Returns self so can be chained.
let vec = new Vector(50, 50);
let another = new Vector(10, 5);
vec.subtract(another);//{x: 40, y: 45}Sets vector value to the subtraction of two vectors. Doesn't mutate the vectors passed as arguments. Returns self so can be chained.
let vec = new Vector();
let other = new Vector(100, 100);
let another = new Vector(5, 5);
vec.subtraction(another, other);//{x: -95, y: 95}Add one or more vectors while multiplying the vectors by given scalar. Doesn't mutate vectors passed as arguments. Returns self so can be chained.
let position = new Vector(10, 10);
let velocity = new Vector(100, 100);
let dt = 0.1;
position.scaledAdd(dt, velocity);//{x: 20, y: 20}Multiply x & y values by given number. Returns self so can be chained.
let vec = new Vector(10, 10);
vec.scale(10);//{x: 100, y: 100}Rotate the vector to point to given angle. Returns self so can be chained.
let vec = new Vector(0, 100);
vec.toAngle(Math.PI);//{x: -100, y: 0}Align the vector to point towards the same direction as the vector provided. Returns self so can be chained.
let vec = new Vector(200, 100);
let another = new Vector(10, 0);
vec.alignWith(another);//{x: 223.60679774997897, y: 0}Returns the distance between the vector and another vector. Returns self so can be chained.
let vec = new Vector(200, 100);
let another = new Vector(10, 0);
vec.distance(another);//214.7091055358389Returns the distance between the vector and another vector in power of two. More performant than the distance method when comparing two distances.
let vec = new Vector(200, 100);
let another = new Vector(10, 0);
vec.distanceSq(another);//46100Returns boolean value depending wether given vector is within given distance of another vector when placing both vectors starting point to the origo.
let vec = new Vector(0, 0);
let another = new Vector(10, 0);
vec.withinRange(another, 9);//falseReturns closest distance between a point and a line segment defined by self and another vector.
let vec = new Vector(-50, 40);
let another = new Vector(10, 10);
let point = new Vector(5, 30);
vec.segmentDistance(another, point);//15.652475842498529Returns closest distance between a point and a line segment defined by self and another vector in power of two.
let vec = new Vector(-50, 40);
let another = new Vector(10, 10);
let point = new Vector(5, 30);
vec.segmentDistanceSq(another, point);//245Returns a boolean:
trueif length is less than given length- else return
false
let vec = new Vector(-50, 40);
vec.isShorter(200);//trueReturns a boolean:
trueif length is more than given length- else return
false
let vec = new Vector(-50, 40);
vec.isShorter(200);//falseReturns true if another vector has identical x & y properties as self, else return false.
let vec = new Vector(-50, 40);
let another = new Vector(-50, 40);
vec.isEqual(another);//trueReturns true if another vector has the same angle as self. Optional second argument, tolerance, can be provided to extend truethful difference between the vectors' angles.
let vec = new Vector(-500, 400);
let another = new Vector(-50, 40);
vec.isAligned(another);//trueReturn the dot product of self and another vector.
let vec = new Vector(10, 10);
let another = new Vector(2, 60);
vec.dot(another);//620Return the cross product of self and another vector.
let vec = new Vector(10, 10);
let another = new Vector(2, 60);
vec.cross(another);//580Return the sign (1 or -1) of the cross product of self and another vector.
let vec = new Vector(10, 10);
let another = new Vector(-2, -60);
vec.crossSign(another);//-1Sets the length of the vector to 1. Normalization doesn't have any effect on vectors with zero length.
let vec = new Vector(10, 10);
vec.normalize();//{x: 0.7071067811865475, y: 0.7071067811865475}Sets the length of the vector. Returns self so can be chained.
let vec = new Vector(10, 10);
vec.toLength(1);
console.log(vec);//{x: 0.7071067811865475, y: 0.7071067811865475}Increases the length of the vector by given length. Returns self so can be chained.
let vec = new Vector(10, 0);
vec.stretch(5);//{x: 15, y: 0}Limits the length of the vector. Returns self so can be chained.
let vec = new Vector(10, 0);
vec.limit(5);//{x: 5, y: 0}Stretches the length of the vector if its' length is less than given length. Arguments: unlimit(number). Returns self so can be chained.
let vec = new Vector(10, 0);
vec.unlimit(15);//{x: 15, y: 0}Clamps the length of the vector to be between two given values. Arguments can be provided at any order. Returns self so can be chained.
let vec = new Vector(1, 0);
vec.clampLength(5, 10);//{x: 5, y: 0}Clamps the angle of the vector to be between within given angle to another vector. If the vector is outside of the allowed range, closest allowed angle is set. Returns self so can be chained.
let vec = new Vector(1, 0);
let another = new Vector(30, 100);
vec.clampAngle(Math.PI / 10, another);//{x: 0.5692686788038682, y: 0.822151550100648}Returns the angle between self and another vector as absolute value in radians.
let vec = new Vector(-1, 0);
let another = new Vector(1, 1);
vec.angleBetween(another);//2.356194490192345Returns the angle between self and another vector where sign of the angle determines direction the another vector is angled from self.
let vec = new Vector(-1, 0);
let another = new Vector(1, 1);
vec.angleBetweenSigned(another);//-2.356194490192345Rotates the vector by 180 degrees or PI radians. Returns self so can be chained.
let vec = new Vector(-14, 50);
vec.mirror();//{x: 14, y: -50}Sets the x & y values to 0. Returns self so can be chained.
let vec = new Vector(-14, 50);
vec.zero();//{x: 0, y: 0}Rotates self by 90 degrees or PI / 2 radians. Returns self so can be chained. Optional argument can be provided to get mirrored normal. Returns self so can be chained.
let vec = new Vector(1, 0);
vec.toNormal();//{x: 0, y: 1}Sets the angle to be the normal of another vector. Optional argument can be provided to get mirrored normal. Returns self so can be chained.
let vec = new Vector(100, 0);
let another = new Vector(20, 50);
vec.normalTo(another);//{x: 92.84766908852593, y: 37.13906763541038}Rotate vector by given radians. Returns self so can be chained.
let vec = new Vector(100, 0);
vec.rotate(- Math.PI / 2);//{x: 0, y: -100}Rotate the vector towards aligning it with another vector by given radians if the vectors aren't already parallel. Rotates towards the direction with smallest angle difference (ie. doesn't take rotation direction of over PI angle). Returns self so can be chained.
let vec = new Vector(100, 0);
let another = new Vector(0, 100);
vec.lerpAlign(Math.PI / 10, another);//{x: 95.10565162951536, y: 30.901699437494745}Rotate the vector towards aligning it with another vector by given radians if the vectors aren't aligned. Rotation direction is determined by the second argument (clockwise), truethful value rotates clockwise and falsy value rotates anti-clockwise. Returns self so can be chained.
let vec = new Vector(100, 0);
let another = new Vector(0, 100);
vec.lerpAlignFixed(Math.PI / 10, false, another);//{x: 95.10565162951536, y: -30.901699437494745}