@ishoa/vector2 v1.0.2
Vector2
A simple Vector2 object
A Vector representing X and Y values in 2D space
- Vector2
- new Vector2(x, y)
- .add(vector) ⇒ Vector2
- .addScalar(scalar) ⇒ Vector2
- .addVectors(vector1, vector2) ⇒ Vector2
- .angle() ⇒ Number
- .angleBetween(vector) ⇒ Number
- .angleTo(vector) ⇒ Number
- .cartesianToPolar(x, y) ⇒ Object
- .clone() ⇒ Vector2
- .cross() ⇒ Number
- .distanceTo(vector2) ⇒ Number
- .divide(vector) ⇒ Vector2
- .divideScalar(scalar) ⇒ Vector2
- .dot(vector) ⇒ Number
- .length(x, y) ⇒ Number
- .limit(value) ⇒ Vector2
- .mag() ⇒ Number
- .magnitude() ⇒ Number
- .magSq() ⇒
- .midpoint(vector) ⇒ Vector2
- .multiply(vector) ⇒ Vector2
- .multiplyScalar(scalar) ⇒ Vector2
- .normalize() ⇒
- .set(props)
- .setFromPolar(length, angle) ⇒ Vector2
- .sub(vector) ⇒ Vector2
- .subScalar(scalar) ⇒ Vector2
- .subVectors(vector1, vector2) ⇒ Vector2
new Vector2(x, y)
Constructor
Param | Type | Default | Description |
---|---|---|---|
x | Number | 0 | x value for vector |
y | Number | 0 | y value for vector |
vector2.add(vector) ⇒ Vector2
Add another vector2 to this vector2
Returns: Vector2 - - the modified vector (this)
Param | Type | Description |
---|---|---|
vector | Vector2 | Vector2 to add to this one |
Example
- vector1.add(vector2);
vector2.addScalar(scalar) ⇒ Vector2
Add a scalar value to our vector
Returns: Vector2 - - the modified vector (this)
Param | Type | Description |
---|---|---|
scalar | Number | number to add to both x and y |
Example
- vector.addScalar(5);
vector2.addVectors(vector1, vector2) ⇒ Vector2
Set this x and y vector to be the addition of two Vector2(s)
Returns: Vector2 - - the modified vector (this)
Param | Type | Description |
---|---|---|
vector1 | Vector2 | first vector to add to this |
vector2 | Vector2 | second vector to add to this |
Example
- let vector = new Vector2().addVectors(vector1, vector2);
vector2.angle() ⇒ Number
Calculate the angle to the coordinates 0, 0
Returns: Number - - the angle from (0, 0) of this vector
vector2.angleBetween(vector) ⇒ Number
Calculate the angle between this vector and another vector
Returns: Number - - the angle between this vector and another vector
Param | Type | Description |
---|---|---|
vector | Vector2 | a vector to get the angle of |
Example
- let angle = vector1.angleBetween(vector2);
vector2.angleTo(vector) ⇒ Number
Calculate the angle in radians from this vector to another
Returns: Number - - the angle in radians from this vector to another
Param | Type | Description |
---|---|---|
vector | Vector2 | Vector2 to get the angle too |
Example
- let angle = vector1.angleTo(vector2);
vector2.cartesianToPolar(x, y) ⇒ Object
Calculate polar coordinates from our current x and y properties OR from a passed in x and y value
Returns: Object - - The vector representation in polar coordinates (length and angle)
Param | Type | Description |
---|---|---|
x | Number | x cartesian value - defaults to this Vector2 |
y | Number | y cartesian value - defaults to this Vector2 |
Example
- let polar = vector.cartesianToPolar();
vector2.clone() ⇒ Vector2
Create a new vector of this vector
Returns: Vector2 - - the copy of this vector
vector2.cross() ⇒ Number
Calculate the cross product between this Vector2 and another Vector2
Returns: Number - - the cross product
Example
- let crossProduct = vector1.cross(vector2);
vector2.distanceTo(vector2) ⇒ Number
Calculate the distance between the 2 Vector2 Objects
Returns: Number - - the distance between this vector and another
Param | Type | Description |
---|---|---|
vector2 | Vector2 | vector 2 to measure distance to this vector |
Example
- let distance = vector1.distanceTo(vector2);
vector2.divide(vector) ⇒ Vector2
Divide a Vector2 from this
Returns: Vector2 - - the modified vector (this)
Param | Type | Description |
---|---|---|
vector | Vector2 | Vector2 to divide from this |
Example
- vector1.divide(vector2);
vector2.divideScalar(scalar) ⇒ Vector2
Divide a scalar value to our vector
Returns: Vector2 - - the modified vector (this)
Param | Type | Description |
---|---|---|
scalar | Number | scalar number to divide to both x and y |
Example
- vector.divide(5);
vector2.dot(vector) ⇒ Number
The dot product gives a number as a "scalar"
Returns: Number - - A number or scalar value which is the dot product
Param | Type | Description |
---|---|---|
vector | Vector2 | {x: 0, y: 0} |
Example
- let dot = vector1.dot(vector2);
vector2.length(x, y) ⇒ Number
Return the length of the x and y value
Returns: Number - - the length or magnitude of this vector
Param | Type | Description |
---|---|---|
x | Number | the x value - defaults to this.x |
y | Number | the y value - defaults to this.y |
Example
- let length = vector.length();
vector2.limit(value) ⇒ Vector2
Set or limit the magnitude to a value
Returns: Vector2 - - the modified vector (this)
Param | Type | Description |
---|---|---|
value | Number | Value to limit the magnitude too |
Example
- vector.limit(5);
vector2.mag() ⇒ Number
Calculate the magnitude or length of the vector (shorthand function)
Returns: Number - - the length or magnitude of this vector
Example
- let mag = vector.mag();
vector2.magnitude() ⇒ Number
Calculate the magnitude or length of the vector
Returns: Number - - the length or magnitude of this vector
Example
- let mag = vector.magnitude();
vector2.magSq() ⇒
Calculate the magnitude squared
Returns: - Vector2 magnitude squared
Example
= let magSq = vector.magSq();
vector2.midpoint(vector) ⇒ Vector2
Get the midpoint between this Vector2 to another Vector2
Returns: Vector2 - - {x: x, y: y} midpoint values
Param | Type | Description |
---|---|---|
vector | Vector2 | {x: 0, y: 0} |
Example
- let midpoint = vector1.midpoint(vector2);
vector2.multiply(vector) ⇒ Vector2
Multiply a Vector2 from this
Returns: Vector2 - - The modified Vector (this)
Param | Type | Description |
---|---|---|
vector | Vector2 | Vector2 to multiply from this |
Example
- vector1.multiply(vector2);
vector2.multiplyScalar(scalar) ⇒ Vector2
Multiply a scalar value to our vector
Returns: Vector2 - - The modified Vector (this)
Param | Type | Description |
---|---|---|
scalar | Number | scalar number to multiply to both x and y |
Example
- vector.multiplyScalar(5);
vector2.normalize() ⇒
The normalized vector or keep the direction of the Vector2 the same but the length is 1
Returns: normalized value
Example
- vector.noramlize();
vector2.set(props)
Set the properties on this to be the properties of a passed in Object
Param | Type | Description |
---|---|---|
props | Object | object with properties to set on this |
Example
- {x: 50, y: 100}
vector2.setFromPolar(length, angle) ⇒ Vector2
Set our X and Y Cartesian Coordiantes based off the provided Polar Coordinates
Returns: Vector2 - - The modified Vector (this)
Param | Type | Description |
---|---|---|
length | Number | the length or radius of the Polar Coordinates |
angle | Number | the angle or theta of the Polar Coordinates |
Example
- vector.setFromPolar(5, Math.PI() / 2);
vector2.sub(vector) ⇒ Vector2
Subtract a Vector2 from this
Returns: Vector2 - - The modified Vector (this)
Param | Type | Description |
---|---|---|
vector | Vector2 | Vector2 to subtract from this |
Example
- vector1.sub(vector2);
vector2.subScalar(scalar) ⇒ Vector2
Subtract a scalar value to our vector
Returns: Vector2 - - The modified Vector (this)
Param | Type | Description |
---|---|---|
scalar | Number | number to subtract to both x and y |
Example
- vector.subScalar(5);
vector2.subVectors(vector1, vector2) ⇒ Vector2
Set this x and y vector to be the subtraction of two Vector2(s)
Returns: Vector2 - - The modified Vector (this)
Param | Type | Description |
---|---|---|
vector1 | Vector2 | first vector to subtract to this |
vector2 | Vector2 | second vector to subtract to this |
Example
- let vector = new Vector2().subVectors(vector1, vector2);