Magnitude in the x-direction.
Magnitude in the y-direction.
Checks whether the vector's length is 1. Returns true if it is.
Checks whether the vector's length is 0. Returns true if it is.
Gets the vector's length/magnitude.
Gets the vector's magnitude in the x-direction.
Gets the vector's magnitude in the y-direction.
Returns the world's x-axis as a unit vector. Equivalent to new Vector(1, 0)
.
Returns the world's y-axis as a unit vector. Equivalent to new Vector(0, 1)
.
Adds the x & y magnitudes of another vector and returns the resulting vector.
Vector to add.
Adds an x & y value to the vector and returns the resulting vector.
Value to add to the x magnitude.
Value to add to the y magnitude.
Calculates the smallest angle between this vector and another vector. The result is measured in radians and will be between 0 and Math.PI.
The vector to measure angle between.
The smallest angle between the two vectors in radians.
Calculates the signed angle between this vector and another vector. The result is measured in radians.
A positive value means that other
is counter-clockwise from the vector
(assuming the environment's y-axis points upwards, otherwise it's clockwise).
A negative value means that other
is clockwise from the vector
(assuming the environment's y-axis points upwards, otherwise it's counter-clockwise).
The vector to measure the angle between.
The signed angle in radians.
Divides the x & y magnitudes by a value and returns the resulting vector.
Amount to divide the magnitudes by.
Divides the x & y magnitudes by different amounts in the x- and y-direction. Returns the resulting vector.
Amount to divide the x magnitude by.
Amount to divide the y magnitude by.
Calculates the dot product of this vector and another vector. Returns the resulting vector.
Vector to calculate dot product with.
The vector to compare against.
Checks whether another vector is parallel. Returns true if it is.
Vector to compare against.
Acceptable deviation from parallel, measured as the angle between vectors (in radians).
Checks whether another vector is perpendicular. Returns true if it is.
Vector to compare against.
Acceptable deviation from perpendicular, measured as the angle between vectors (in radians).
Checks whether the vector is parallel to worldX. Returns true if it is.
Checks whether the vector is parallel to worldY. Returns true if it is.
Multiplies the x & y magnitudes by a value and returns the resulting vector.
Amount to multiply the magnitudes by.
Multiplies the x & y magnitudes by different amounts in the x- and y-direction. Returns the resulting vector.
Amount to multiply the x magnitude by.
Amount to multiply the y magnitude by.
Creates a new vector that is perpendicular to this vector.
The new vector will be on the left side of this vector if looking along [[from]]->[[to]] (assuming the environment's y-axis points upwards, if it doesn't it will be on the right side).
Inverts the direction of the vector and returns the inverted vector. This is the same as multiplying the vector by -1.
Subtracts the x & y magnitudes of another vector and returns the resulting vector.
Vector to subtract.
Subtracts an x & y value from the vector and returns the resulting vector.
Value to subtract from the x magnitude.
Value to subtract from the y magnitude.
Gets the vector as a string in the format: '⟨x,y⟩'.
Creates a copy of the vector where the length is equal to 1.
Creates a copy of the vector with the x and y magnitudes scaled to a given length.
Length of new vector. If negative, the vector will be inverted but the resulting length will be positive.
Creates a copy of the vector with a different x magnitude.
New value for x.
Creates a copy of the vector with a different y magnitude.
New value for y.
Translates the geometry from one coordinate system to another while keeping
the geometry in the same position. In other words, if the geometry is currently
described relative to planeFrom
, after changeBasis,
it will be in the same position but described relative to planeTo
.
The coordinate system the geometry is currently described relative to.
The coordinate system to describe the geometry relative to.
The geometry in the new coordinate system.
Rotates the geometry about (0,0).
Angle to rotate the geometry (in radians). The direction is counter-clockwise.
Rotates the geometry about a point.
Angle to rotate the geometry (in radians). If the environment's y-axis points upwards, the direction is counter-clockwise.
Point to pivot the geometry about.
Scales the geometry and returns the resized geometry. The geometry will be scaled about (0,0), meaning everything will shrink or expand away from this point.
Magnitude to scale in x- and y-direction. If less than 1, the object will shrink. If greater than 1, it will grow.
Scales the geometry and returns the resized geometry. The geometry will be scaled about (0,0), meaning everything will shrink or expand away from this point.
Magnitude to scale in x-direction. If less than 1, the object will shrink. If greater than 1, it will grow.
Magnitude to scale in y-direction. If less than 1, the object will shrink. If greater than 1, it will grow.
Scales the geometry about a point and returns the resized geometry.
Magnitude to scale in x-direction. If less than 1, the object will shrink. If greater than 1, it will grow.
Magnitude to scale in y-direction. If less than 1, the object will shrink. If greater than 1, it will grow.
Center of scaling. Everything will shrink or expand away from this point.
Transforms the vector by a Transform matrix and returns the result.
import { Transform, Vector } from 'shapetypes';
// Create a new vecctor
const vector = new Vector(3, 4);
console.log(vector.length);
// => 5
// Scale vector using a transform matrix
const matrix = Transform.scale(2);
const scaled = vector.transform(matrix);
console.log(vector.length);
// => 10
// Scale vector using the direct method
const otherScaled = vector.scale(2);
console.log(otherScaled.length);
// => 10
Moves the geometry along a vector and returns the moved geometry. The translation is always linear.
The direction and distance to move the geometry.
Moves the geometry along a vector and returns the moved geometry. The translation is always linear.
The direction to move the geometry.
The distance to move the geometry.
Generated using TypeDoc
A two-dimensional vector. The vector is defined by an x magnitude and a y magnitude.
Example
import { Vector } from 'shapetypes'; // Create a new vector const v = new Vector(10, 0); // Get properties of the vector console.log(v.x); // => 10 console.log(v.length); // => 10 // Measure the angle between two vectors const other = new Vector(0,1); const angle = v.angle(other); console.log(angle); // => 1.57 // Rotate a vector const rotated = v.rotate(Math.PI / 2); console.log(rotated.toString()); // => `⟨0,10⟩`