A 2-dimensional vector.
new Vec2(x?: number | number[], y?: number): Vec2
Create a new Vec2 instance.
The x value. Defaults to 0. If x is an array of length 2, the array will be used to populate all components.
number | number
number = 0
The y value. Defaults to 0.
const v = new Vec2(1, 2)
x: number
The first component of the vector.
y: number
The second component of the vector.
readonly static DOWN: Vec2;
A constant vector set to 0, -1.
readonly static HALF: Vec2;
A constant vector set to 0.5, 0.5.
readonly static LEFT: Vec2;
A constant vector set to -1, 0.
readonly static ONE: Vec2;
A constant vector set to 1, 1.
readonly static RIGHT: Vec2;
A constant vector set to 1, 0.
readonly static UP: Vec2;
A constant vector set to 0, 1.
readonly static ZERO: Vec2;
A constant vector set to 0, 0.
add(rhs: Vec2): Vec2
Adds a 2-dimensional vector to another in place.
The vector to add to the specified vector.
Self for chaining.
const a = new Vec2(10, 10)
const b = new Vec2(20, 20)
a.add(b)
// Outputs [30, 30]
console.log('The result of the addition is: ' + a.toString())
add2(lhs: Vec2, rhs: Vec2): Vec2
Adds two 2-dimensional vectors together and returns the result.
The first vector operand for the addition.
The second vector operand for the addition.
Self for chaining.
const a = new Vec2(10, 10)
const b = new Vec2(20, 20)
const r = new Vec2()
r.add2(a, b)
// Outputs [30, 30]
console.log('The result of the addition is: ' + r.toString())
addScalar(scalar: number): Vec2
Adds a number to each element of a vector.
number
The number to add.
Self for chaining.
const vec = new Vec2(3, 4)
vec.addScalar(2)
// Outputs [5, 6]
console.log('The result of the addition is: ' + vec.toString())
addScaled(rhs: Vec2, scalar: number): Vec2
Adds a 2-dimensional vector scaled by scalar value. Does not modify the vector being added.
The vector to add to the specified vector.
number
The number to multiply the added vector with.
Self for chaining.
const vec = new Vec2(1, 2)
vec.addScaled(Vec2.UP, 2)
// Outputs [1, 4]
console.log('The result of the addition is: ' + vec.toString())
angle(): number
Returns the angle in degrees of the specified 2-dimensional vector.
number
The angle in degrees of the specified 2-dimensional vector.
const v = new Vec2(6, 0)
const angle = v.angle()
// Outputs 90..
console.log('The angle of the vector is: ' + angle)
angleTo(rhs: Vec2): number
Returns the shortest Euler angle between two 2-dimensional vectors.
The 2-dimensional vector to calculate angle to.
number
The shortest angle in degrees between two 2-dimensional vectors.
const a = new Vec2(0, 10) // up
const b = new Vec2(1, -1) // down-right
const angle = a.angleTo(b)
// Outputs 135..
console.log('The angle between vectors a and b: ' + angle)
ceil(src?: Vec2): Vec2
Each element is rounded up to the next largest integer.
Vec2 = ...
The vector to ceil. If not set, the operation is done in place.
Self for chaining.
clone(): Vec2
Returns an identical copy of the specified 2-dimensional vector.
A 2-dimensional vector containing the result of the cloning.
const v = new Vec2(10, 20)
const vclone = v.clone()
console.log('The result of the cloning is: ' + vclone.toString())
copy(rhs: Vec2): Vec2
Copies the contents of a source 2-dimensional vector to a destination 2-dimensional vector.
A vector to copy to the specified vector.
Self for chaining.
const src = new Vec2(10, 20)
const dst = new Vec2()
dst.copy(src)
console.log('The two vectors are ' + (dst.equals(src) ? 'equal' : 'different'))
cross(rhs: Vec2): number
Returns the result of a cross product operation performed on the two specified 2-dimensional vectors.
The second 2-dimensional vector operand of the cross product.
number
The cross product of the two vectors.
const right = new Vec2(1, 0)
const up = new Vec2(0, 1)
const crossProduct = right.cross(up)
// Prints 1
console.log('The result of the cross product is: ' + crossProduct)
distance(rhs: Vec2): number
Returns the distance between the two specified 2-dimensional vectors.
The second 2-dimensional vector to test.
number
The distance between the two vectors.
const v1 = new Vec2(5, 10)
const v2 = new Vec2(10, 20)
const d = v1.distance(v2)
console.log('The distance between v1 and v2 is: ' + d)
div(rhs: Vec2): Vec2
Divides a 2-dimensional vector by another in place.
The vector to divide the specified vector by.
Self for chaining.
const a = new Vec2(4, 9)
const b = new Vec2(2, 3)
a.div(b)
// Outputs [2, 3]
console.log('The result of the division is: ' + a.toString())
div2(lhs: Vec2, rhs: Vec2): Vec2
Divides one 2-dimensional vector by another and writes the result to the specified vector.
The dividend vector (the vector being divided).
The divisor vector (the vector dividing the dividend).
Self for chaining.
const a = new Vec2(4, 9)
const b = new Vec2(2, 3)
const r = new Vec2()
r.div2(a, b)
// Outputs [2, 3]
console.log('The result of the division is: ' + r.toString())
divScalar(scalar: number): Vec2
Divides each element of a vector by a number.
number
The number to divide by.
Self for chaining.
const vec = new Vec2(3, 6)
vec.divScalar(3)
// Outputs [1, 2]
console.log('The result of the division is: ' + vec.toString())
dot(rhs: Vec2): number
Returns the result of a dot product operation performed on the two specified 2-dimensional vectors.
The second 2-dimensional vector operand of the dot product.
number
The result of the dot product operation.
const v1 = new Vec2(5, 10)
const v2 = new Vec2(10, 20)
const v1dotv2 = v1.dot(v2)
console.log('The result of the dot product is: ' + v1dotv2)
equals(rhs: Vec2): boolean
Reports whether two vectors are equal.
The vector to compare to the specified vector.
boolean
True if the vectors are equal and false otherwise.
const a = new Vec2(1, 2)
const b = new Vec2(4, 5)
console.log('The two vectors are ' + (a.equals(b) ? 'equal' : 'different'))
equalsApprox(rhs: Vec2, epsilon?: number): boolean
Reports whether two vectors are equal using an absolute error tolerance.
The vector to be compared against.
number = 1e-6
The maximum difference between each component of the two vectors. Defaults to 1e-6.
boolean
True if the vectors are equal and false otherwise.
const a = new Vec2()
const b = new Vec2()
console.log(
'The two vectors are approximately ' +
(a.equalsApprox(b, 1e-9) ? 'equal' : 'different')
)
floor(src?: Vec2): Vec2
Each element is set to the largest integer less than or equal to its value.
Vec2 = ...
The vector to floor. If not set, the operation is done in place.
Self for chaining.
fromArray(arr: number[] | ArrayBufferView<ArrayBufferLike>, offset?: number): Vec2
Set the values of the vector from an array.
The array to set the vector values from.
number | ArrayBufferView<ArrayBufferLike>
number = 0
The zero-based index at which to start copying elements from the array. Default is 0.
Self for chaining.
const v = new Vec2()
v.fromArray([
20,
10
])
// v is set to [20, 10]
length(): number
Returns the magnitude of the specified 2-dimensional vector.
number
The magnitude of the specified 2-dimensional vector.
const vec = new Vec2(3, 4)
const len = vec.length()
// Outputs 5
console.log('The length of the vector is: ' + len)
lengthSq(): number
Returns the magnitude squared of the specified 2-dimensional vector.
number
The magnitude of the specified 2-dimensional vector.
const vec = new Vec2(3, 4)
const len = vec.lengthSq()
// Outputs 25
console.log('The length squared of the vector is: ' + len)
lerp(
lhs: Vec2,
rhs: Vec2,
alpha: number): Vec2
Returns the result of a linear interpolation between two specified 2-dimensional vectors.
The 2-dimensional to interpolate from.
The 2-dimensional to interpolate to.
number
The value controlling the point of interpolation. Between 0 and 1, the linear interpolant will occur on a straight line between lhs and rhs. Outside of this range, the linear interpolant will occur on a ray extrapolated from this line.
Self for chaining.
const a = new Vec2(0, 0)
const b = new Vec2(10, 10)
const r = new Vec2()
r.lerp(a, b, 0) // r is equal to a
r.lerp(a, b, 0.5) // r is 5, 5
r.lerp(a, b, 1) // r is equal to b
max(rhs: Vec2): Vec2
Each element is assigned a value from rhs parameter if it is larger.
The 2-dimensional vector used as the source of elements to compare to.
Self for chaining.
min(rhs: Vec2): Vec2
Each element is assigned a value from rhs parameter if it is smaller.
The 2-dimensional vector used as the source of elements to compare to.
Self for chaining.
mul(rhs: Vec2): Vec2
Multiplies a 2-dimensional vector to another in place.
The 2-dimensional vector used as the second multiplicand of the operation.
Self for chaining.
const a = new Vec2(2, 3)
const b = new Vec2(4, 5)
a.mul(b)
// Outputs 8, 15
console.log('The result of the multiplication is: ' + a.toString())
mul2(lhs: Vec2, rhs: Vec2): Vec2
Returns the result of multiplying the specified 2-dimensional vectors together.
The 2-dimensional vector used as the first multiplicand of the operation.
The 2-dimensional vector used as the second multiplicand of the operation.
Self for chaining.
const a = new Vec2(2, 3)
const b = new Vec2(4, 5)
const r = new Vec2()
r.mul2(a, b)
// Outputs 8, 15
console.log('The result of the multiplication is: ' + r.toString())
mulScalar(scalar: number): Vec2
Multiplies each element of a vector by a number.
number
The number to multiply by.
Self for chaining.
const vec = new Vec2(3, 6)
vec.mulScalar(3)
// Outputs [9, 18]
console.log('The result of the multiplication is: ' + vec.toString())
normalize(src?: Vec2): Vec2
Returns this 2-dimensional vector converted to a unit vector in place. If the vector has a length of zero, the vector's elements will be set to zero.
Vec2 = ...
The vector to normalize. If not set, the operation is done in place.
Self for chaining.
const v = new Vec2(25, 0)
v.normalize()
// Outputs 1, 0
console.log('The result of the vector normalization is: ' + v.toString())
rotate(degrees: number): Vec2
Rotate a vector by an angle in degrees.
number
The number to degrees to rotate the vector by.
Self for chaining.
const v = new Vec2(0, 10)
v.rotate(45) // rotates by 45 degrees
// Outputs [7.071068.., 7.071068..]
console.log('Vector after rotation is: ' + v.toString())
round(src?: Vec2): Vec2
Each element is rounded up or down to the nearest integer.
Vec2 = ...
The vector to round. If not set, the operation is done in place.
Self for chaining.
set(x: number, y: number): Vec2
Sets the specified 2-dimensional vector to the supplied numerical values.
number
The value to set on the first component of the vector.
number
The value to set on the second component of the vector.
Self for chaining.
const v = new Vec2()
v.set(5, 10)
// Outputs 5, 10
console.log('The result of the vector set is: ' + v.toString())
sub(rhs: Vec2): Vec2
Subtracts a 2-dimensional vector from another in place.
The vector to subtract from the specified vector.
Self for chaining.
const a = new Vec2(10, 10)
const b = new Vec2(20, 20)
a.sub(b)
// Outputs [-10, -10]
console.log('The result of the subtraction is: ' + a.toString())
sub2(lhs: Vec2, rhs: Vec2): Vec2
Subtracts two 2-dimensional vectors from one another and returns the result.
The first vector operand for the subtraction.
The second vector operand for the subtraction.
Self for chaining.
const a = new Vec2(10, 10)
const b = new Vec2(20, 20)
const r = new Vec2()
r.sub2(a, b)
// Outputs [-10, -10]
console.log('The result of the subtraction is: ' + r.toString())
subScalar(scalar: number): Vec2
Subtracts a number from each element of a vector.
number
The number to subtract.
Self for chaining.
const vec = new Vec2(3, 4)
vec.subScalar(2)
// Outputs [1, 2]
console.log('The result of the subtraction is: ' + vec.toString())
toArray(arr?: number[] | ArrayBufferView<ArrayBufferLike>, offset?: number): number[] | ArrayBufferView<ArrayBufferLike>
Converts the vector to an array.
The array to populate with the color components. If not specified, a new array is created.
number | ArrayBufferView<ArrayBufferLike>
number = 0
The zero-based index at which to start copying elements to the array. Default is 0.
number | ArrayBufferView<ArrayBufferLike>
The vector as an array.
const v = new Vec2(20, 10)
// Outputs [20, 10]
console.log(v.toArray())
toString(): string
Converts the vector to string form.
string
The vector in string form.
const v = new Vec2(20, 10)
// Outputs [20, 10]
console.log(v.toString())