Constructor
new Vector2d(x, yopt)
| Name | Type | Attributes | Description |
|---|---|---|---|
x | Number | | x coordinate OR an existing vector | |
y | Number | <optional> | y coordinate |
// A vector from two numbers
let vectorA = new gameify.Vector2d(5, 8);
// A vector from a formatted string
let vectorB = new gameify.Vector2d("<4, 6>");
// A vector from another vector (copying it)
let vectorC = new gameify.Vector2d(vectorA)Members
x :Number
The x (a) point of the vector
- Number
y :Number
The y (b) point of the vector
- Number
(static, readonly) I
The i vector <1, 0> for reference and calculation
(static, readonly) J
The j vector <0, 1> for reference and calculation
(static, readonly) ZERO
A zero vector for reference and calculation
let vectorA = new gameify.Vector2d(3, 2);
// Interpolate vectorA towards zero (using gameify.Vector2d.ZERO to avoid having to make a new vector)
let vectorB = vectorA.linearInterpolate(gameify.Vector2d.ZERO, 0.5); // vectorB = <1.5, 1>Methods
add(vectorB) → {gameify.Vector2d}
Adds this vector and another one, and returns the result as a new vector
| Name | Type | Description |
|---|---|---|
vectorB | gameify. | The vector to add |
- Type:
- gameify.
Vector2d
let vectorA = new gameify.Vector2d(3, 2);
let vectorB = new gameify.Vector2d(7, -3);
let vectorC = vectorA.add(vectorB); // vectorC = <10, -1>copy() → {gameify.Vector2d}
Returns a copy of the vector
- Type:
- gameify.
Vector2d
distanceTo(point) → {Number}
Calculate the distance between a this and another vector (From this vector's coordinates to the other vector's coordinates)
| Name | Type | Description |
|---|---|---|
point | gameify. | The start of the line segment |
The distance to the vector
- Type:
- Number
distanceTo(segmentStart, segmentEnd) → {Number}
Calculate the distance between a this and a line segment (From this vector's coordinates to the closest point on the line segment)
| Name | Type | Description |
|---|---|---|
segmentStart | gameify. | The start of the line segment |
segmentEnd | gameify. | The end of the line segment (start/end order does not matter) |
The distance between this point and the line segment
- Type:
- Number
getAngle() → {Number}
Find the angle from the vector (in radians)
- Type:
- Number
getAngleDegrees() → {Number}
Find the angle from the vector (in degrees)
- Type:
- Number
getMagnitude()
Returns the length (magnitude) of the vector.
getNormalized() → {gameify.Vector2d}
Returns a normalized copy of the vector (Sets the length equal to one while maintaining the direction)
- Type:
- gameify.
Vector2d
linearInterpolate(vectorB, t) → {gameify.Vector2d}
Linear interpolation from this vector to another
| Name | Type | Description |
|---|---|---|
vectorB | gameify. | The vector to interpolate to |
t | Number | A number from 0 to 1, with larger values closer to vectorB |
- Type:
- gameify.
Vector2d
let vectorA = new gameify.Vector2d(3, 2);
let vectorB = new gameify.Vector2d(7, 12);
// Get a vector half way between A and B
let vectorC = vectorA.linearInterpolate(vectorB, 0.5); // vectorC = <5, 7>multiply(value) → {gameify.Vector2d}
Multiplies this vector by an amount, and returns the result as a new vector
| Name | Type | Description |
|---|---|---|
value | Number | The amount to multiply by |
- Type:
- gameify.
Vector2d
let vectorA = new gameify.Vector2d(3, 2);
let vectorB = vectorA.multiply(4); // vectorB = <12, -8>multiplyComponents(vector) → {gameify.Vector2d}
Multiplies the components of this and another vector, and returns the result as a new vector
| Name | Type | Description |
|---|---|---|
vector | gameify. | The vector to multiply |
- Type:
- gameify.
Vector2d
let vectorA = new gameify.Vector2d(3, 2);
let vectorB = new gameify.Vector2d(4, -8)
let vectorC = vectorA.multiplyComponents(vectorB); // vectorC = <12, -16>normalize()
Normalizes the vector (Sets the length equal to one while maintaining the direction)
rotated(angle) → {gameify.Vector2d}
Returns a copy of this vector rotated by an angle, in radians (counterclockwise)
| Name | Type | Description |
|---|---|---|
angle | Number | The angle to rotate by, in degrees |
- Type:
- gameify.
Vector2d
let vectorA = new gameify.Vector2d(3, 2);
vectorA.rotated(Math.PI/2); // vectorA = <-2, 3>rotatedAbout(angle, point) → {gameify.Vector2d}
Returns a copy of this vector, rotated around a point by an angle, in radians (counterclockwise)
| Name | Type | Description |
|---|---|---|
angle | Number | The angle to rotate by, in radians |
point | gameify. | The point to rotate around |
- Type:
- gameify.
Vector2d
rotatedDegrees(angle) → {gameify.Vector2d}
Returns a copy of this vector rotated by an angle, in degrees (counterclockwise)
| Name | Type | Description |
|---|---|---|
angle | Number | The angle to rotate by, in degrees |
- Type:
- gameify.
Vector2d
let vectorA = new gameify.Vector2d(3, 2);
vectorA.rotatedDegrees(90); // vectorA = <-2, 3>rounded() → {gameify.Vector2d}
Returns a copy of the vector with both coordinates to the nearest integer
- Type:
- gameify.
Vector2d
subtract(vectorB) → {gameify.Vector2d}
Subtracts this vector and another one, and returns the result as a new vector
| Name | Type | Description |
|---|---|---|
vectorB | gameify. | The vector to subtract |
- Type:
- gameify.
Vector2d
let vectorA = new gameify.Vector2d(3, 2);
let vectorB = new gameify.Vector2d(7, -3);
let vectorC = vectorA.add(vectorB); // vectorC = <10, -1>toJSON()
Returns a JSON representation of the vector
toRawString()
Same as toString, but does not truncate the string.
toString() → {String}
Returns a string representing the vector in the form "<x, y>". Truncated to three decimal places.
- Type:
- String
truncated(precisionopt) → {gameify.Vector2d}
Returns a copy of this vector, width the x and y values truncated to a certain precision
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
precision | Number | <optional> | 0 | Values after the decimal to keep |
The vector with truncated values
- Type:
- gameify.
Vector2d
valueOf()
Returns a string representation of the vector, equivalent to toString
xComponent() → {gameify.Vector2d}
Returns the x component of this vector
- Type:
- gameify.
Vector2d
yComponent() → {gameify.Vector2d}
Returns the y component of this vector
- Type:
- gameify.
Vector2d
(static) assertIsCompatibleVector(vector)
Checks if a vector is compatible with operations in this one, and throws if not.
| Name | Type | Description |
|---|---|---|
vector | gameify. |
(static) from(x, yopt) → {gameify.Vector2d}
Creates a vector from an x and y, string, or existing vector
| Name | Type | Attributes | Description |
|---|---|---|---|
x | Number | | x coordinate OR an existing vector | |
y | Number | <optional> | y coordinate |
- Type:
- gameify.
Vector2d
(static) isCompatibleVector(vector)
Checks if a vector is compatible with operations in this one
| Name | Type | Description |
|---|---|---|
vector | gameify. |
(static) longestOf(vectorA, vectorB)
Given two vectors, returns the one with the greatest magnitude (length)
| Name | Type | Description |
|---|---|---|
vectorA | gameify. | |
vectorB | gameify. |
(static) segmentsIntersect(startA, endA, startB, endB, toleranceopt, collinearIntersectsopt) → {Boolean}
Check if two line segments intersect
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
startA | gameify. | The start of the first line segment | ||
endA | gameify. | The end of the first line segment | ||
startB | gameify. | The start of the second line segment | ||
endB | gameify. | The end of the second line segment | ||
tolerance | Number | <optional> | 1e-8 | Maximum tolerance (because of floating-point errors) |
collinearIntersects | Boolean | <optional> | true | Whether collinear lines should count as intersecting |
True if the line segments intersect, false otherwise
- Type:
- Boolean
(static) shortestOf(vectorA, vectorB)
Given two vectors, returns the one with the least magnitude (length)
| Name | Type | Description |
|---|---|---|
vectorA | gameify. | |
vectorB | gameify. |