Vector2

The vector 2 class

Constructor

new Vector2(xopt, yopt)

Parameters:
NameTypeAttributesDefaultDescription
xnumber<optional>
0

the x value of this vector.

ynumber<optional>
0

the y value of this vector.

Methods

add(v) → {Vector2}

Adds v to this vector.

Parameters:
NameTypeDescription
vVector2
Returns:
Type: 
Vector2

addVectors(a, b) → {Vector2}

Sets this vector to a + b.

Parameters:
NameTypeDescription
aVector2
bVector2
Returns:
Type: 
Vector2

angle() → {number}

Computes the angle in radians of this vector with respect to the positive x-axis.

Returns:
Type: 
number

clone() → {Vector2}

Returns a new Vector2 with the same x and y values as this one.

Returns:
Type: 
Vector2

copy(v) → {Vector2}

Copies the values of the passed Vector2's x and y properties to this Vector2.

Parameters:
NameTypeDescription
vVector2
Returns:
Type: 
Vector2

distanceTo(v) → {number}

Computes the distance from this vector to v.

Parameters:
NameTypeDescription
vVector2
Returns:
Type: 
number

distanceToSquared(v) → {number}

Computes the squared distance from this vector to v. If you are just comparing the distance with another distance, you should compare the distance squared instead as it is slightly more efficient to calculate.

Parameters:
NameTypeDescription
vVector2
Returns:
Type: 
number

dot(a) → {number}

Calculate the dot product of this vector and v.

Parameters:
NameTypeDescription
aVector2
Returns:
Type: 
number

equals(v) → {boolean}

Checks for strict equality of this vector and v.

Parameters:
NameTypeDescription
vVector2
Returns:
Type: 
boolean

fromArray(array, offsetopt, denormalizeopt) → {Vector2}

Sets this vector's x value to be array[ offset ] and y value to be array[ offset + 1 ].

Parameters:
NameTypeAttributesDefaultDescription
arrayArray.<number>

the source array.

offsetnumber<optional>
0

offset into the array.

denormalizeboolean<optional>
false

if true, denormalize the values, and array should be a typed array.

Returns:
Type: 
Vector2

getLength() → {number}

Computes the Euclidean length (straight-line length) from (0, 0) to (x, y).

Returns:
Type: 
number

getLengthSquared() → {number}

Computes the square of the Euclidean length (straight-line length) from (0, 0) to (x, y). If you are comparing the lengths of vectors, you should compare the length squared instead as it is slightly more efficient to calculate.

Returns:
Type: 
number

lerpVectors(v1, v2, ratio) → {Vector2}

Sets this vector to be the vector linearly interpolated between v1 and v2 where ratio is the percent distance along the line connecting the two vectors

  • ratio = 0 will be v1, and ratio = 1 will be v2.
Parameters:
NameTypeDescription
v1Vector2

the starting Vector2.

v2Vector2

Vector2 to interpolate towards.

rationumber

interpolation factor, typically in the closed interval [0, 1].

Returns:
Type: 
Vector2

max(v) → {Vector2}

If this vector's x or y value is less than v's x or y value, replace that value with the corresponding max value.

Parameters:
NameTypeDescription
vVector2
Returns:
Type: 
Vector2

min(v) → {Vector2}

If this vector's x or y value is greater than v's x or y value, replace that value with the corresponding min value.

Parameters:
NameTypeDescription
vVector2
Returns:
Type: 
Vector2

multiplyScalar(scalar) → {Vector2}

Multiplies this vector by scalar.

Parameters:
NameTypeDescription
scalarnumber
Returns:
Type: 
Vector2

negate() → {Vector2}

Inverts this vector - i.e. sets x = -x, y = -y.

Returns:
Type: 
Vector2

normalize(thicknessopt) → {Vector2}

Converts this vector to a unit vector - that is, sets it equal to a vector with the same direction as this one, but length 1.

Parameters:
NameTypeAttributesDefaultDescription
thicknessnumber<optional>
1
Returns:
Type: 
Vector2

set(x, y) → {Vector2}

Sets the x and y components of this vector.

Parameters:
NameTypeDefaultDescription
xnumber0
ynumber0
Returns:
Type: 
Vector2

sub(v) → {Vector2}

Subtracts v from this vector.

Parameters:
NameTypeDescription
vVector2
Returns:
Type: 
Vector2

subVectors(a, b) → {Vector2}

Sets this vector to a - b.

Parameters:
NameTypeDescription
aVector2
bVector2
Returns:
Type: 
Vector2

subtract(a, target) → {Vector2}

Subtracts v from the vector.

Parameters:
NameTypeDescription
aVector2
targetVector2

the result vector2

Returns:
Type: 
Vector2

toArray(arrayopt, offsetopt, normalizeopt) → {Array.<number>}

Sets this array[ offset ] value to be vector's x and array[ offset + 1 ] to be vector's y.

Parameters:
NameTypeAttributesDefaultDescription
arrayArray.<number><optional>

the target array.

offsetnumber<optional>
0

offset into the array.

normalizeboolean<optional>
false

if true, normalize the values, and array should be a typed array.

Returns:
Type: 
Array.<number>