Vector2

Class representing a 2D vector. A 2D vector is an ordered pair of numbers (labeled x and y).

Math Class

Class representing a 2D vector. A 2D vector is an ordered pair of numbers (labeled x and y).

Implements

  • Vector

Constructors

Constructor

new Vector2(_x?, _y?): Vector2

Parameters

_x?

number

_y?

number

Returns

Vector2

Properties

x

x: number

the x value of this vector.

Default Value

0.


y

y: number

the y value of this vector.

Default Value

0.


isVector2

isVector2: boolean

Check the type whether it belongs to Vector2. This value should not be changed by user.

Accessors

width

Get Signature

get width(): number

Same to x.

Returns

number

Set Signature

set width(w): void

Parameters
w

number

Returns

void


height

Get Signature

get height(): number

Same to y.

Returns

number

Set Signature

set height(h): void

Parameters
h

number

Returns

void

Methods

set()

set(x, y): Vector2

Sets the x and y components of this vector.

Parameters

x

number

y

number

Returns

Vector2


setScalar()

setScalar(scalar): Vector2

Sets the x and y values of this vector both equal to scalar.

Parameters

scalar

number

Returns

Vector2


setX()

setX(x): Vector2

Replaces this vector’s x value with Float| x.

Parameters

x

number

Returns

Vector2


setY()

setY(y): Vector2

Replaces this vector’s y value with Float| y.

Parameters

y

number

Returns

Vector2


setComponent()

setComponent(index, value): Vector2

If index equals 0 set x to Float| value. If index equals 1 set y to Float| value.

Parameters

index

number

value

number

Returns

Vector2

Implementation of

Vector.setComponent


getComponent()

getComponent(index): number

If index equals 0 returns the x value. If index equals 1 returns the y value.

Parameters

index

number

0 or 1.

Returns

number

Implementation of

Vector.getComponent


clone()

clone(): Vector2

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

Returns

Vector2

Implementation of

Vector.clone


cloneReadonly()

cloneReadonly(): Readonly<Pick<ReadonlyMarked, "_readonly_mark" | "cloneReadonly" | "clone" | "equals" | "getSerializeData" | "x" | "y" | "getNumberCount" | "toArray" | "width" | "height" | "isVector2" | "dot" | "cross" | "lengthSq" | "length" | "angle" | "manhattanLength" | "distanceTo" | "distanceToSquared" | "manhattanDistanceTo" | "intoSize">>

Returns

Readonly<Pick<ReadonlyMarked, "_readonly_mark" | "cloneReadonly" | "clone" | "equals" | "getSerializeData" | "x" | "y" | "getNumberCount" | "toArray" | "width" | "height" | "isVector2" | "dot" | "cross" | "lengthSq" | "length" | "angle" | "manhattanLength" | "distanceTo" | "distanceToSquared" | "manhattanDistanceTo" | "intoSize">>


copy()

copy(v): Vector2

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

Parameters

v

Vector2

Returns

Vector2

Implementation of

Vector.copy


add()

add(v): Vector2

Adds v to this vector.

Parameters

v

Vector2

Returns

Vector2

Implementation of

Vector.add


addScalar()

addScalar(s): Vector2

Adds the scalar value s to this vector’s x and y values.

Parameters

s

number

Returns

Vector2


addVectors()

addVectors(a, b): Vector2

Sets this vector to a + b.

Parameters

a

Vector2

b

Vector2

Returns

Vector2

Implementation of

Vector.addVectors


addScaledVector()

addScaledVector(v, s): Vector2

Adds the multiple of v and s to this vector.

Parameters

v

Vector2

s

number

Returns

Vector2


sub()

sub(v): Vector2

Subtracts v from this vector.

Parameters

v

Vector2

Returns

Vector2

Implementation of

Vector.sub


subScalar()

subScalar(s): Vector2

Subtracts s from this vector’s x and y components.

Parameters

s

number

Returns

Vector2


subVectors()

subVectors(a, b): Vector2

Sets this vector to a - b.

Parameters

a

Vector2

b

Vector2

Returns

Vector2

Implementation of

Vector.subVectors


multiply()

multiply(v): Vector2

Multiplies this vector by v.

Parameters

v

Vector2

Returns

Vector2


multiplyScalar()

multiplyScalar(scalar): Vector2

Multiplies this vector by scalar s.

Parameters

scalar

number

Returns

Vector2

Implementation of

Vector.multiplyScalar


divide()

divide(v): Vector2

Divides this vector by v.

Parameters

v

Vector2

Returns

Vector2


divideScalar()

divideScalar(scalar): Vector2

Divides this vector by scalar s.
Sets vector to ( 0, 0 ) if s = 0.

Parameters

scalar

number

Returns

Vector2

Implementation of

Vector.divideScalar


applyMatrix3()

applyMatrix3(m): Vector2

Multiplies this vector (with an implicit 1 as the 3rd component) by m.

Parameters

m

Matrix3

Returns

Vector2


applyMatrix4()

applyMatrix4(m): Vector2

Multiplies this vector (with an implicit 1 as the 4rd component) by m.

Parameters

m

Matrix4

Returns

Vector2


min()

min(v): Vector2

If this vector’s x or y value is greater than the max vector’s x or y value, it is replaced by the corresponding value. If this vector’s x or y value is less than the min vector’s x or y value, it is replaced by the corresponding value.

Parameters

v

Vector2

Returns

Vector2


max()

max(v): Vector2

If this vector’s x or y value is greater than the max vector’s x or y value, it is replaced by the corresponding value. If this vector’s x or y value is less than the min vector’s x or y value, it is replaced by the corresponding value.

Parameters

v

Vector2

Returns

Vector2


clamp()

clamp(min, max): Vector2

If this vector’s x or y value is greater than the max vector’s x or y value, it is replaced by the corresponding value.

If this vector’s x or y value is less than the min vector’s x or y value, it is replaced by the corresponding value.

Parameters

min

Vector2

the minimum x and y values.

max

Vector2

the maximum x and y values in the desired range.

Returns

Vector2


clampScalar()

clampScalar(minVal, maxVal): Vector2

If this vector’s x or y values are greater than the max value, they are replaced by the max value.

If this vector’s x or y values are less than the min value, they are replaced by the min value.

Parameters

minVal

number

maxVal

number

Returns

Vector2


clampLength()

clampLength(min, max): Vector2

If this vector’s length is greater than the max value, it is replaced by the max value.

If this vector’s length is less than the min value, it is replaced by the min value.

Parameters

min

number

the minimum value the length will be clamped to.

max

number

the maximum value the length will be clamped to.

Returns

Vector2


floor()

floor(): Vector2

The components of this vector are rounded down to the nearest integer value.

Returns

Vector2


ceil()

ceil(): Vector2

The x and y components of this vector are rounded up to the nearest integer value.

Returns

Vector2


round()

round(): Vector2

The components of this vector are rounded to the nearest integer value.

Returns

Vector2


roundToZero()

roundToZero(): Vector2

The components of this vector are rounded towards zero (up if negative, down if positive) to an integer value.

Returns

Vector2


negate()

negate(): Vector2

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

Returns

Vector2

Implementation of

Vector.negate


dot()

dot(v): number

Calculates the dot product of this vector and v.

Parameters

v

Vector2

Returns

number

Implementation of

Vector.dot


cross()

cross(v): number

Calculates the cross product of this vector and v.

Parameters

v

Vector2

Returns

number

Tips

that a ‘cross-product’ in 2D is not well-defined. This function computes a geometric cross-product often used in 2D graphics


lengthSq()

lengthSq(): 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

number

Implementation of

Vector.lengthSq


length()

length(): number

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

Returns

number

Implementation of

Vector.length


manhattanLength()

manhattanLength(): number

Computes the Manhattan length of this vector.

Returns

number


normalize()

normalize(): 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| length 1.

Returns

Vector2

Implementation of

Vector.normalize


angle()

angle(): number

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

Returns

number


distanceTo()

distanceTo(v): number

Computes the distance from this vector to v.

Parameters

v

Vector2

Returns

number

Implementation of

Vector.distanceTo


distanceToSquared()

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

v

Vector2

Returns

number

Implementation of

Vector.distanceToSquared


manhattanDistanceTo()

manhattanDistanceTo(v): number

Computes the Manhattan distance from this vector to v.

Parameters

v

Vector2

Returns

number


setLength()

setLength(length): Vector2

Sets this vector to a vector with given length the same direction as this one.

Parameters

length

number

Returns

Vector2

Implementation of

Vector.setLength


lerp()

lerp(v, alpha): Vector2

Linearly interpolates between this vector and v, where alpha is the percent distance along the line - alpha = 0 will be this vector, and alpha = 1 will be v.

Parameters

v

Vector2

Vector2 to interpolate towards.

alpha

number

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

Returns

Vector2

Implementation of

Vector.lerp


lerpVectors()

lerpVectors(v1, v2, alpha): Vector2

Sets this vector to be the vector linearly interpolated between v1 and v2 where alpha is the percent distance along the line connecting the two vectors - alpha = 0 will be v1, and alpha = 1 will be v2.

Parameters

v1

Vector2

the starting Vector2.

v2

Vector2

Vector2 to interpolate towards.

alpha

number

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

Returns

Vector2


equals()

equals(v): boolean

Checks for strict equality of this vector and v.

Parameters

v

Vector2

Returns

boolean

Implementation of

Vector.equals


fromArray()

fromArray(array, offset?): Vector2

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

Parameters

array

ArrayLike<number>

the source array.

offset?

number

(optional) offset into the array.

Returns

Vector2

Default Value

is 0.

getNumberCount()

getNumberCount(): number

There are 2 elements in this vector.

Returns

number


toArray()

toArray(array?, offset?): number[]

Returns an array [x, y], or copies x and y into the provided array.

Parameters

array?

number[]

(optional) array to store this vector to. If this is not provided, a new array will be created.

offset?

number

(optional) optional offset into the array.

Returns

number[]

Implementation of

Vector.toArray


fromBufferAttribute()

fromBufferAttribute(attribute, index, offset?): Vector2

Parameters

attribute

the source attribute.

getX

(number) => number

getY

(number) => number

index

number

index in the attribute.

offset?

number

Returns

Vector2

Deprecated

please use BufferAttribute.getVector2 Sets this vector’s .x| x and .y| y values from the BufferAttribute| attribute.


rotateAround()

rotateAround(center, angle): Vector2

Rotates this vector around center by Float| angle radians.

Parameters

center

Vector2

the point around which to rotate.

angle

number

the angle to rotate, in radians.

Returns

Vector2


intoSize()

intoSize(): Size

Returns

Size