Class: Vector

Vector

Vector This is a basic vector class that is used for geometry, position inforamtion, movement infomation, and more complex structures. The vector class follows a immutable paradigm where changes are not made to the vectors themselves. Any change to a vector is returned as a new vector that must be captured.

new Vector(x, y)

Create a 2D Vector object

This vector class was constructed so that it can mirror two types of common point/vector type objects. This is having object properties stored as object properties (eg. vector.x, vector.y) or as list properties, [x, y] which can be accessed by vector[0], or vector[1].
Name Type Description
x number | Vector The x component or another vector
y number optional The y component
Properties:
Name Type Description
x number The x vector component
y number The y vector component
0 number The x vector component
1 number The y vector component

Methods

staticVector.add(a, b){Vector}

Add two vectors element wise
Name Type Description
a Vector The first vector
b Vector The second vector
Returns:
Type Description
Vector The vector result of adding the two vectors

staticVector.angle(a, b)

Get the angle between two vectors
Name Type Description
a Vector The frist vector
b Vector The second vector
Returns:
The angle between vector a and vector b

staticVector.avg(vectors)

Get the average location between several vectors
Name Type Description
vectors Array.<Vector> The list of vectors to average

staticVector.copy(v){Vector}

Get a copy of the input vector
Name Type Description
v Vector the vector to be coppied
Returns:
Type Description
Vector The vector copy

staticVector.cross(a, b){number}

Get the cross product of two vectors
Name Type Description
a Vector The first vector
b Vector The second vector
Returns:
Type Description
number The cross product of the two vectors

staticVector.dist2(a, b)

Get the euclidean distnace squared between two vectors. This is used as a helper for the distnace function but can be used to save on speed by not doing the square root operation.
Name Type Description
a Vector The first vector
b Vector The second vector
See:
  • distnace
Returns:
The euclidean distance squared between vector a and vector b

staticVector.distance(a, b)

Get the euclidean distance between two vectors
Name Type Description
a Vector The first vector
b Vector The second vector
See:
  • dist2
Returns:
The euclidean distance between a and b

staticVector.distToSeg(p, v, w)

Get the shortest distance between the point p and the line segment v to w.
Name Type Description
p Vector The vector point
v Vector The first line segment endpoint
w Vector The second line segment endpoint
See:
Returns:
The shortest euclidean distance between point

staticVector.distToSegSquared(p, v, w)

Get the shortest distance squared between the point p and the line segment v to w.
Name Type Description
p Vector The vector point
v Vector The first line segment endpoint
w Vector The second line segment endpoint
See:
Returns:
The shortest euclidean distance squared between point

staticVector.dot(a, b){number}

Get the dot product of two vectors
Name Type Description
a Vector The first vector
b Vector The second vector
Returns:
Type Description
number The dot product of the two vectors

staticVector.down(){Vector}

Get the unit vector pointing in the negative y direction
Returns:
Type Description
Vector Unit vector pointing down

staticVector.left(){Vector}

Get the unit vector pointing in the negative x direction
Returns:
Type Description
Vector Unit vector pointing right

staticVector.midpoint(a, b)

Get the midpoint between two vectors
Name Type Description
a Vector The first vector
b Vector The second vector
Returns:
The midpoint of two vectors

staticVector.proj(a, b)

Get the projection of vector a onto vector b
Name Type Description
a Vector The first vector
b Vector The second vector
TODO
  • Add assertion for non-zero length b vector
Returns:
The projection vector of a onto b

staticVector.right(){Vector}

Get the unit vector pointing in the positive x direction
Returns:
Type Description
Vector Unit vector pointing right

staticVector.subtract(a, b){Vector}

Subtract two vectors element wise
Name Type Description
a Vector The first vector
b Vector The second Vector
Returns:
Type Description
Vector The vector result of subtracting the two vectors

staticVector.up(){Vector}

Get the unit vector pointing in the positive y direction
Returns:
Type Description
Vector Unit vector pointing up

staticVector.zero(){Vector}

Get a vector of no magnitude and no direction
Returns:
Type Description
Vector Vector of magnitude zero
Add this vector with another vector element wise
Name Type Description
other Vector The other vector
Returns:
Type Description
Vector The vector result of adding the two vectors

cross(other){number}

Get the cross product of this and the other vector
Name Type Description
other Vector The other vector
Returns:
Type Description
number The cross product of this and the other vector
Divide the vector by a scalar value
Name Type Description
scalar number
Returns:
Type Description
Vector The result of multiplying the vector by a scalar

dot(other){number}

Get the dot product of this vector and another vector
Name Type Description
other Vector The other vector
Returns:
Type Description
number The dot product of this and the other vector

key(){Symbol}

Get the vector key:Symbol representation
Returns:
Type Description
Symbol The vector key element

list(){Array.<number>}

Get the vector in list form
Returns:
Type Description
Array.<number> List representation of the vector of length 2

magnitude(){number}

Get the magnitude of the vector
Returns:
Type Description
number The magniture of the vector

multiply(scalar){Vector}

Multiply the vector by a scalar value
Name Type Description
scalar number The number to multiply the vector by
Returns:
Type Description
Vector The result of multiplying the vector by a scalar element wise
Get the normal vector of the current vector.
Returns:
Type Description
Vector A vector that is the normal compenent of the vector

perpendiculars(){Array.<Vector>}

Get the two normal vectors that are perpendicular to the current vector
Returns:
Type Description
Array.<Vector> The two normal vectors that are perpendicular to the vector. The first vector is the normal vector that is +90 deg or +PI/2 rad. The second vector is the noraml vector that is -90 deg or -PI/2 rad.

rotate(radians){Vector}

Get the get the current vector rotated by a certain ammount
Name Type Description
radians number
Returns:
Type Description
Vector The vector that results from rotating the current vector by a particular ammount

subtract(other){Vector}

Subtract this vector with another vector element wise
Name Type Description
other Vector The other vector
Returns:
Type Description
Vector The vector result of subtracting the two vectors

toString(){string}

Returns the vector as a string of (x, y)
Returns:
Type Description
string The string representation of a vector in (x, y) form

toString(){string}

Returns the vector as a string of (x, y)
Returns:
Type Description
string The string representation of a vector in (x, y) form