Class: Vec2

Vec2(x, y)

Represents a two-component vector. Access the X component of the vector as v[0] and the Y component as v[1].

Constructor

new Vec2(x, y)

Constructs a two-component vector.
Parameters:
Name Type Description
x Number X component of vector.
y Number Y component of vector.
Source:

Extends

  • Float64Array

Methods

add(addend) → {Vec2}

Adds a vector to this vector.
Parameters:
Name Type Description
addend Vec2 The vector to add to this one.
Source:
Throws:
If the specified addend is null or undefined.
Type
ArgumentError
Returns:
This vector after adding the specified vector to it.
Type
Vec2

copy(vector) → {Vec2}

Copies the components of a specified vector to this vector.
Parameters:
Name Type Description
vector Vec2 The vector to copy.
Source:
Throws:
If the specified vector is null or undefined.
Type
ArgumentError
Returns:
This vector set to the values of the specified vector.
Type
Vec2

distanceTo(vector) → {Number}

Computes the distance from this vector to a specified vector.
Parameters:
Name Type Description
vector Vec2 The vector to compute the distance to.
Source:
Throws:
If the specified vector is null or undefined.
Type
ArgumentError
Returns:
The distance between the vectors.
Type
Number

distanceToSquared(vector) → {Number}

Computes the squared distance from this vector to a specified vector.
Parameters:
Name Type Description
vector Vec2 The vector to compute the distance to.
Source:
Throws:
If the specified vector is null or undefined.
Type
ArgumentError
Returns:
The squared distance between the vectors.
Type
Number

divide(divisor) → {Vec2}

Divide this vector by a scalar.
Parameters:
Name Type Description
divisor Number The scalar to divide this vector by.
Source:
Returns:
This vector divided by the specified scalar.
Type
Vec2

dot(vector) → {Number}

Computes the scalar dot product of this vector and a specified vector.
Parameters:
Name Type Description
vector Vec2 The vector to multiply.
Source:
Throws:
If the specified vector is null or undefined.
Type
ArgumentError
Returns:
The scalar dot product of the vectors.
Type
Number

equals(vector) → {Boolean}

Indicates whether the X and Y components of this vector are identical to those of a specified vector.
Parameters:
Name Type Description
vector Vec2 The vector to test.
Source:
Returns:
true if this vector's components are equal to those of the specified vector, otherwise false.
Type
Boolean

magnitude() → {Number}

Computes the magnitude of this vector.
Source:
Returns:
The magnitude of this vector.
Type
Number

magnitudeSquared() → {Number}

Computes the squared magnitude of this vector.
Source:
Returns:
The squared magnitude of this vector.
Type
Number

mix(vector, weight) → {Vec2}

Mixes (interpolates) a specified vector with this vector, modifying this vector.
Parameters:
Name Type Description
vector Vec2 The vector to mix.
weight Number The relative weight of this vector.
Source:
Throws:
If the specified vector is null or undefined.
Type
ArgumentError
Returns:
This vector modified to the mix of itself and the specified vector.
Type
Vec2

multiply(scalar) → {Vec2}

Multiplies this vector by a scalar.
Parameters:
Name Type Description
scalar Number The scalar to multiply this vector by.
Source:
Returns:
This vector multiplied by the specified scalar.
Type
Vec2

negate() → {Vec2}

Negates this vector.
Source:
Returns:
This vector, negated.
Type
Vec2

normalize() → {Vec2}

Normalizes this vector to a unit vector.
Source:
Returns:
This vector, normalized.
Type
Vec2

set(x, y) → {Vec2}

Assigns the components of this vector.
Parameters:
Name Type Description
x Number The X component of the vector.
y Number The Y component of the vector.
Source:
Returns:
This vector with the specified components assigned.
Type
Vec2

subtract(subtrahend) → {Vec2}

Subtracts a vector from this vector.
Parameters:
Name Type Description
subtrahend Vec2 The vector to subtract from this one.
Source:
Throws:
If the subtrahend is null or undefined.
Type
ArgumentError
Returns:
This vector after subtracting the specified vector from it.
Type
Vec2

swap(that) → {Vec2}

Swaps the components of this vector with those of another vector. This vector is set to the values of the specified vector, and the specified vector's components are set to the values of this vector.
Parameters:
Name Type Description
that Vec2 The vector to swap.
Source:
Returns:
This vector set to the values of the specified vector.
Type
Vec2

toString() → {String}

Returns a string representation of this vector.
Source:
Returns:
A string representation of this vector, in the form "(x, y)".
Type
String

toVec3() → {Vec3}

Creates a Vec3 using this vector's X and Y components and a Z component of 0.
Source:
Returns:
A new vector whose X and Y components are those of this vector and whose Z component is 0.
Type
Vec3

(static) average(vectors, result) → {Vec2}

Computes the average of a specified array of vectors.
Parameters:
Name Type Description
vectors Array.<Vec2> The vectors whose average to compute.
result Vec2 A pre-allocated Vec2 in which to return the computed average.
Source:
Throws:
If the specified array of vectors is null, undefined or empty, or the specified result argument is null or undefined.
Type
ArgumentError
Returns:
The result argument set to the average of the specified lists of vectors.
Type
Vec2