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 aVector The first vector bVector 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 aVector The frist vector bVector 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 vectorsArray.<Vector> The list of vectors to average -
staticVector.copy(v){Vector}
-
Get a copy of the input vector
Name Type Description vVector 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 aVector The first vector bVector 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 aVector The first vector bVector 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 aVector The first vector bVector 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 pVector The vector point vVector The first line segment endpoint wVector 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 pVector The vector point vVector The first line segment endpoint wVector 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 aVector The first vector bVector 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 aVector The first vector bVector 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 aVector The first vector bVector 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 aVector The first vector bVector 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(other){Vector}
-
Add this vector with another vector element wise
Name Type Description otherVector 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 otherVector The other vector Returns:
Type Description number The cross product of this and the other vector -
divide(scalar){Vector}
-
Divide the vector by a scalar value
Name Type Description scalarnumber 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 otherVector 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 scalarnumber The number to multiply the vector by Returns:
Type Description Vector The result of multiplying the vector by a scalar element wise -
normalize(){Vector}
-
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 radiansnumber 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 otherVector 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