Options
All
• Public
• Public/Protected
• All

# Class Vec2

Representation of 2D vectors and points.

## Constructors

### constructor

• newVec2(other: Vec2): Vec2
• newVec2(x?: undefined | number, y?: undefined | number): Vec2

## Properties

x: number

x component.

y: number

y component.

### Static NEGONE

NEGONE: {} = Object.freeze(new Vec2(-1, -1))

### Static ONE

ONE: {} = Object.freeze(new Vec2(1, 1))

### Static UNITX

UNITX: {} = Object.freeze(new Vec2(1, 0))

### Static UNITY

UNITY: {} = Object.freeze(new Vec2(0, 1))

### Static ZERO

ZERO: {} = Object.freeze(new Vec2(0, 0))

## Methods

• Adds the current vector with another one and return this

#### Parameters

• ##### other: Vec2

specified vector

#### Returns this

• add2f(x: number, y: number): this
• Adds the current vector with another one and return this

#### Parameters

• ##### x: number

The x value of specified vector

• ##### y: number

The y value of specified vector

### angle

• angle(other: Vec2): number
• Calculates radian angle between two vectors

#### Parameters

• ##### other: Vec2

specified vector

#### Returns number

The angle between the current vector and the specified vector (in radians); if there are zero vectors in the current vector and the specified vector, 0 is returned.

### clampf

• clampf(minInclusive: Vec2, maxInclusive: Vec2): this
• Clamp the vector between minInclusive and maxInclusive.

#### Parameters

• ##### minInclusive: Vec2

Minimum value allowed

• ##### maxInclusive: Vec2

Maximum value allowed

#### Returns this

`this`

### cross

• cross(other: Vec2): number
• Calculates the cross product with another vector.

#### Parameters

• ##### other: Vec2

specified vector

#### Returns number

`out`

### divide

• divide(other: Vec2): this
• Element-wisely divides this vector with another one, and return this.

#### Parameters

• ##### other: Vec2

specified vector

### divide2f

• divide2f(x: number, y: number): this
• Element-wisely divides this vector with another one, and return this.

#### Parameters

• ##### x: number

The x value of specified vector

• ##### y: number

The y value of specified vector

### dot

• dot(other: Vec2): number
• Calculates the dot product with another vector

#### Parameters

• ##### other: Vec2

specified vector

#### Returns number

The result of calculates the dot product with another vector

### equals

• equals(other: Vec2, epsilon?: number): boolean
• Check whether the vector approximately equals another one.

#### Parameters

• ##### other: Vec2

Specified vector

• ##### Default value epsilon: number = EPSILON

The error allowed. It`s should be a non-negative number.

#### Returns boolean

Returns `true` when the components of both vectors are equal within the specified range of error; otherwise it returns `false`.

### equals2f

• equals2f(x: number, y: number, epsilon?: number): boolean
• Check whether the vector approximately equals another one.

#### Parameters

• ##### x: number

The x value of specified vector

• ##### y: number

The y value of specified vector

• ##### Default value epsilon: number = EPSILON

The error allowed. It`s should be a non-negative number.

#### Returns boolean

Returns `true` when the components of both vectors are equal within the specified range of error; otherwise it returns `false`.

### length

• length(): number

### lengthSqr

• lengthSqr(): number
• Returns the squared length of this vector.

#### Returns number

the squared length of this vector

### lerp

• lerp(to: Vec2, ratio: number): this
• Calculate linear interpolation result between this vector and another one with given ratio.

#### Parameters

• ##### to: Vec2

Target vector

• ##### ratio: number

The interpolation coefficient.The range is [0,1].

### multiply

• multiply(other: Vec2): this
• Multiplies the current vector with another one and return this

#### Parameters

• ##### other: Vec2

specified vector

### multiply2f

• multiply2f(x: number, y: number): this
• Multiplies the current vector with another one and return this

#### Parameters

• ##### x: number

The x value of specified vector

• ##### y: number

The y value of specified vector

### multiplyScalar

• multiplyScalar(scalar: number): this
• Multiplies the current vector with a number, and returns this.

#### Parameters

• ##### scalar: number

scalar number

### negative

• negative(): this

### normalize

• normalize(): this

### project

• project(other: Vec2): this
• Projects the current vector on another one

#### Parameters

• ##### other: Vec2

specified vector

### rotate

• Rotates the current vector by an angle in radian value

### set

• set(other: Vec2): any
• set(x?: undefined | number, y?: undefined | number): any
• Set the current vector value with the given vector.

#### Parameters

• ##### other: Vec2

Specified vector

#### Returns any

`this`

• Set the value of each component of the current vector.

x value

y value

#### Returns any

`this`

### signAngle

• signAngle(other: Vec2): number
• Get angle in radian between this and vector with direction.

#### Parameters

• ##### other: Vec2

specified vector

#### Returns number

The signed angle between the current vector and the specified vector (in radians); if there is a zero vector in the current vector and the specified vector, 0 is returned.

### strictEquals

• strictEquals(other: Vec2): boolean
• Check whether the current vector strictly equals another Vec2.

#### Parameters

• ##### other: Vec2

specified vector

#### Returns boolean

Returns `true` when the components of both vectors are equal within the specified range of error; otherwise it returns `false`.

### strictEquals2f

• strictEquals2f(x: number, y: number): boolean
• Check whether the current vector strictly equals another Vec2.

#### Parameters

• ##### x: number

The x value of specified vector

• ##### y: number

The y value of specified vector

#### Returns boolean

Returns `true` when the components of both vectors are equal within the specified range of error; otherwise it returns `false`.

### subtract

• subtract(other: Vec2): this
• Subtracts one vector from this, and returns this.

#### Parameters

• ##### other: Vec2

specified vector

### subtract2f

• subtract2f(x: number, y: number): this
• Subtracts one vector from this, and returns this.

#### Parameters

• ##### x: number

The x value of specified vector

• ##### y: number

The y value of specified vector

### toString

• toString(): string

### transformMat4

• transformMat4(matrix: Mat4): this
• Transforms the vec2 with a mat4. 3rd vector component is implicitly '0', 4th vector component is implicitly '1'

#### Parameters

• ##### matrix: Mat4

matrix to transform with

#### Returns this

• add<Out>(out: Out, a: Out, b: Out): Out
• Element-wise vector addition and save the results to out vector object

### Static angle

• angle<Out>(a: Out, b: Out): number
• Calculates the radian angle between two vectors

### Static ceil

• ceil<Out>(out: Out, a: Out): Out
• Rounds up by elements of the vector and save the results to out vector object

### Static clone

• clone<Out>(a: Out): Vec2
• Obtains a clone of the given vector object

### Static copy

• copy<Out>(out: Out, a: Out): Out
• Copy the target vector and save the results to out vector object

### Static cross

• cross<Out>(out: Vec3, a: Out, b: Out): Vec3
• Calculates the cross product of the vector

### Static distance

• distance<Out>(a: Out, b: Out): number
• Calculates the euclidean distance of two vectors

### Static divide

• divide<Out>(out: Out, a: Out, b: Out): Out
• Element-wise vector division and save the results to out vector object

### Static dot

• dot<Out>(a: Out, b: Out): number
• Calculates the dot product of the vector

### Static equals

• equals<Out>(a: Out, b: Out, epsilon?: number): boolean
• Check whether the two given vectors are approximately equivalent

### Static floor

• floor<Out>(out: Out, a: Out): Out
• Element-wise rounds down of the current vector and save the results to the out vector

### Static fromArray

• fromArray<Out>(out: Out, arr: IWritableArrayLike<number>, ofs?: number): Out
• Converts the given array to a vector

#### Parameters

• ##### Default value ofs: number = 0

Array Start Offset

### Static inverse

• inverse<Out>(out: Out, a: Out): Out
• Sets each element to its inverse value, zero value will become Infinity

### Static inverseSafe

• inverseSafe<Out>(out: Out, a: Out): Out
• Sets each element to its inverse value, zero value will remain zero

### Static len

• len<Out>(a: Out): number
• Calculates the length of the vector

### Static lengthSqr

• lengthSqr<Out>(a: Out): number
• Calculates the squared length of the vector

### Static lerp

• lerp<Out>(out: Out, a: Out, b: Out, t: number): Out
• Calculates the linear interpolation between two vectors with a given ratio

### Static max

• max<Out>(out: Out, a: Out, b: Out): Out
• Calculates element-wise maximum values and save to the out vector

### Static min

• min<Out>(out: Out, a: Out, b: Out): Out
• Calculates element-wise minimum values and save to the out vector

### Static multiply

• multiply<Out>(out: Out, a: Out, b: Out): Out
• Element-wise vector multiplication and save the results to out vector object

### Static multiplyScalar

• multiplyScalar<Out>(out: Out, a: Out, b: number): Out
• Vector scalar multiplication and save the results to out vector object

### Static negate

• negate<Out>(out: Out, a: Out): Out
• Sets each element to its negative value

### Static normalize

• normalize<Out, Vec2Like>(out: Out, a: Vec2Like): Out
• Sets the normalized vector to the out vector

### Static random

• random<Out>(out: Out, scale?: undefined | number): Out
• Generates a uniformly distributed random vector points from center to the surface of the unit sphere

#### Parameters

• ##### Optional scale: undefined | number

vector length

### Static round

• round<Out>(out: Out, a: Out): Out
• Calculates element-wise round results and save to the out vector

#### Returns Out

• scaleAndAdd<Out>(out: Out, a: Out, b: Out, scale: number): Out
• Element-wise multiplication and addition with the equation: a + b * scale

### Static set

• set<Out>(out: Out, x: number, y: number): Out
• Sets the out vector with the given x and y values

### Static squaredDistance

• squaredDistance<Out>(a: Out, b: Out): number
• Calculates the squared euclidean distance of two vectors

### Static str

• str<Out>(a: Out): string
• Gets the string representation of the given vector

### Static strictEquals

• strictEquals<Out>(a: Out, b: Out): boolean
• Check the equality of the two given vectors

### Static subtract

• subtract<Out>(out: Out, a: Out, b: Out): Out
• Element-wise vector subtraction and save the results to out vector object

### Static toArray

• toArray<Out>(out: Out, v: IVec2Like, ofs?: number): Out
• Converts the given vector to an array

#### Parameters

• ##### Default value ofs: number = 0

Array Start Offset

### Static transformMat3

• transformMat3<Out, MatLike>(out: Out, a: Out, m: IMat3Like): Out
• Vector and third order matrix multiplication, will complete the vector with a third value as one

### Static transformMat4

• transformMat4<Out, MatLike>(out: Out, a: Out, m: IMat4Like): Out
• Vector and third order matrix multiplication, will complete the vector with a third and a fourth element as one

#### Returns Out

Generated using TypeDoc