Home Reference Source
import {Vector2} from 'math-ds'
public class | source

Vector2

A vector with two components.

Constructor Summary

Public Constructor
public

Constructs a new vector.

Member Summary

Public Members
public get

The height.

public set

Sets the height.

public get

The width.

public set

Sets the width.

public

The X component.

public

The Y component.

Method Summary

Public Methods
public

Adds a vector to this one.

public

Adds a scalar to this vector.

public

Adds a scaled vector to this one.

public

Sets this vector to the sum of two given vectors.

public

Computes the angle in radians with respect to the positive X-axis.

public

Applies the given matrix to this vector.

public

Ceils this vector.

public

clamp(min: Vector2, max: Vector2): Vector2

Clamps this vector.

public

Clones this vector.

public

Copies the values of another vector.

public

Calculates the cross product with another vector.

public

Calculates the distance to a given vector.

public

Calculates the squared distance to a given vector.

public

Divides this vector by another vector.

public

Divides this vector by a given scalar.

public

Calculates the dot product with another vector.

public

Checks if this vector equals the given one.

public

Floors this vector.

public

fromArray(array: Number[], offset: Number): Vector2

Copies values from an array.

public

Calculates the length of this vector.

public

Calculates the squared length of this vector.

public

lerp(v: Vector2, alpha: Number): Vector2

Lerps towards the given vector.

public

Sets this vector to the lerp result of the given vectors.

public

Calculates the Manhattan distance to a given vector.

public

Calculates the Manhattan length of this vector.

public

adopts the max value for each component of this vector and the given one.

public

Adopts the min value for each component of this vector and the given one.

public

Multiplies this vector with another vector.

public

Multiplies this vector with a given scalar.

public

Negates this vector.

public

Normalizes this vector.

public

Randomizes the values of this vector

public

rotateAround(center: Vector2, angle: Number): Vector2

Rotates this vector around a given center.

public

Rounds this vector.

public

Sets the values of this vector

public

Sets the length of this vector.

public

Subtracts a vector from this vector.

public

Subtracts a scalar from this vector.

public

Sets this vector to the difference between two given vectors.

public

toArray(array: Array, offset: Number): Number[]

Stores this vector in an array.

Public Constructors

public constructor(x: Number, y: Number) source

Constructs a new vector.

Params:

NameTypeAttributeDescription
x Number
  • optional
  • default: 0

The X component.

y Number
  • optional
  • default: 0

The Y component.

Public Members

public get height: Number source

The height. This is an alias for Y.

public set height: Number source

Sets the height.

public get width: Number source

The width. This is an alias for X.

public set width: Number source

Sets the width.

public x: Number source

The X component.

public y: Number source

The Y component.

Public Methods

public add(v: Vector2): Vector2 source

Adds a vector to this one.

Params:

NameTypeAttributeDescription
v Vector2

The vector to add.

Return:

Vector2

This vector.

public addScalar(s: Number): Vector2 source

Adds a scalar to this vector.

Params:

NameTypeAttributeDescription
s Number

The scalar to add.

Return:

Vector2

This vector.

public addScaledVector(v: Vector2, s: Number): Vector2 source

Adds a scaled vector to this one.

Params:

NameTypeAttributeDescription
v Vector2

The vector to scale and add.

s Number

A scalar.

Return:

Vector2

This vector.

public addVectors(a: Vector2, b: Vector2): Vector2 source

Sets this vector to the sum of two given vectors.

Params:

NameTypeAttributeDescription
a Vector2

A vector.

b Vector2

Another vector.

Return:

Vector2

This vector.

public angle(): Number source

Computes the angle in radians with respect to the positive X-axis.

Return:

Number

The angle.

public applyMatrix3(m: Matrix3): Vector2 source

Applies the given matrix to this vector.

Params:

NameTypeAttributeDescription
m Matrix3

A matrix.

Return:

Vector2

This vector.

public ceil(): Vector2 source

Ceils this vector.

Return:

Vector2

This vector.

public clamp(min: Vector2, max: Vector2): Vector2 source

Clamps this vector.

Params:

NameTypeAttributeDescription
min Vector2

A vector, assumed to be smaller than max.

max Vector2

A vector, assumed to be greater than min.

Return:

Vector2

This vector.

public clone(): Vector2 source

Clones this vector.

Return:

Vector2

A clone of this vector.

public copy(v: Vector2): Vector2 source

Copies the values of another vector.

Params:

NameTypeAttributeDescription
v Vector2

A vector.

Return:

Vector2

This vector.

public cross(v: Vector2): Number source

Calculates the cross product with another vector.

This method calculates a scalar that would result from a regular 3D cross product of the input vectors, while taking their Z values implicitly as 0.

Params:

NameTypeAttributeDescription
v Vector2

A vector.

Return:

Number

The cross product.

public distanceTo(v: Vector2): Number source

Calculates the distance to a given vector.

Params:

NameTypeAttributeDescription
v Vector2

A vector.

Return:

Number

The distance.

public distanceToSquared(v: Vector2): Number source

Calculates the squared distance to a given vector.

Params:

NameTypeAttributeDescription
v Vector2

A vector.

Return:

Number

The squared distance.

public divide(v: Vector2): Vector2 source

Divides this vector by another vector.

Params:

NameTypeAttributeDescription
v Vector2

A vector.

Return:

Vector2

This vector.

public divideScalar(s: Number): Vector2 source

Divides this vector by a given scalar.

Params:

NameTypeAttributeDescription
s Number

A scalar.

Return:

Vector2

This vector.

public dot(v: Vector2): Number source

Calculates the dot product with another vector.

Params:

NameTypeAttributeDescription
v Vector2

A vector.

Return:

Number

The dot product.

public equals(v: Vector2): Boolean source

Checks if this vector equals the given one.

Params:

NameTypeAttributeDescription
v Vector2

A vector.

Return:

Boolean

Whether this vector equals the given one.

public floor(): Vector2 source

Floors this vector.

Return:

Vector2

This vector.

public fromArray(array: Number[], offset: Number): Vector2 source

Copies values from an array.

Params:

NameTypeAttributeDescription
array Number[]

An array.

offset Number

An offset.

Return:

Vector2

This vector.

public length(): Number source

Calculates the length of this vector.

Return:

Number

The length.

public lengthSquared(): Number source

Calculates the squared length of this vector.

Return:

Number

The squared length.

public lerp(v: Vector2, alpha: Number): Vector2 source

Lerps towards the given vector.

Params:

NameTypeAttributeDescription
v Vector2

The target vector.

alpha Number

The lerp factor.

Return:

Vector2

This vector.

public lerpVectors(v1: Vector2, v2: Vector2, alpha: Number): Vector2 source

Sets this vector to the lerp result of the given vectors.

Params:

NameTypeAttributeDescription
v1 Vector2

A base vector.

v2 Vector2

The target vector.

alpha Number

The lerp factor.

Return:

Vector2

This vector.

public manhattanDistanceTo(v: Vector2): Number source

Calculates the Manhattan distance to a given vector.

Params:

NameTypeAttributeDescription
v Vector2

A vector.

Return:

Number

The squared distance.

public manhattanLength(): Number source

Calculates the Manhattan length of this vector.

Return:

Number

The length.

public max(v: Vector2): Vector2 source

adopts the max value for each component of this vector and the given one.

Params:

NameTypeAttributeDescription
v Vector2

A vector.

Return:

Vector2

This vector.

public min(v: Vector2): Vector2 source

Adopts the min value for each component of this vector and the given one.

Params:

NameTypeAttributeDescription
v Vector2

A vector.

Return:

Vector2

This vector.

public multiply(v: Vector2): Vector2 source

Multiplies this vector with another vector.

Params:

NameTypeAttributeDescription
v Vector2

A vector.

Return:

Vector2

This vector.

public multiplyScalar(s: Number): Vector2 source

Multiplies this vector with a given scalar.

Params:

NameTypeAttributeDescription
s Number

A scalar.

Return:

Vector2

This vector.

public negate(): Vector2 source

Negates this vector.

Return:

Vector2

This vector.

public normalize(): Vector2 source

Normalizes this vector.

Return:

Vector2

This vector.

public random(): Vector2 source

Randomizes the values of this vector

Return:

Vector2

This vector.

public rotateAround(center: Vector2, angle: Number): Vector2 source

Rotates this vector around a given center.

Params:

NameTypeAttributeDescription
center Vector2

The center.

angle Number

The rotation in radians.

Return:

Vector2

This vector.

public round(): Vector2 source

Rounds this vector.

Return:

Vector2

This vector.

public set(x: Number, y: Number): Vector2 source

Sets the values of this vector

Params:

NameTypeAttributeDescription
x Number

The X component.

y Number

The Y component.

Return:

Vector2

This vector.

public setLength(length: Number): Vector2 source

Sets the length of this vector.

Params:

NameTypeAttributeDescription
length Number

The new length.

Return:

Vector2

This vector.

public sub(v: Vector2): Vector2 source

Subtracts a vector from this vector.

Params:

NameTypeAttributeDescription
v Vector2

The vector to subtract.

Return:

Vector2

This vector.

public subScalar(s: Number): Vector2 source

Subtracts a scalar from this vector.

Params:

NameTypeAttributeDescription
s Number

The scalar to subtract.

Return:

Vector2

This vector.

public subVectors(a: Vector2, b: Vector2): Vector2 source

Sets this vector to the difference between two given vectors.

Params:

NameTypeAttributeDescription
a Vector2

A vector.

b Vector2

A second vector.

Return:

Vector2

This vector.

public toArray(array: Array, offset: Number): Number[] source

Stores this vector in an array.

Params:

NameTypeAttributeDescription
array Array
  • optional

A target array.

offset Number

An offset.

Return:

Number[]

The array.