Skip to main content

Vector

Vector

A representation of a vector in 2D space. Commonly used to represent points, but also sizes, rays, and any other collection of 2-numbers.

It should be noted that although the Vector itself is not immutable (since its x and y properties are settable), all methods on the Vector will return new Vectors with the performed operation.


Constructor

public Vector

new Vector()

Initializes a blank Vector with default (x, y) values of (0, 0).

new Vector(other)

Creates a Vector with the x and y values of the other Vector.

Parameters

  •  other: Vector – The vector to copy.

new Vector(val)

Initialize a Vector with the same x and y values as val.

Parameters

  •  val: number – The value to initialize the vector with.

new Vector(x, y)

Initialize a Vector with x values x and y value y.

Parameters

  •  x: number – The x-component.

  •  y: number – The y-component.


Properties

public x: number

The x-component of this Vector.

public y: number

The y-component of this Vector.


Methods

public add

add(other) => Vector

Returns a new Vector with this Vector's x and y components added to other's components.

Parameters

  •  other: Vector – The vector to add to this.

Returns

  •   VectorDescription needed

add(val) => Vector

Returns a new Vector with this Vector's x and y components added by val to each component.

Parameters

  •  val: number – The value to add to this.

Returns

  •   VectorDescription needed

add(x, y) => Vector

Return a new Vector with this Vector's x and y components added by x and y respectively.

Parameters

  •  x: number – The x-component to add to this.

  •  y: number – The y-component to add to this.

Returns

  •   Vector – A vector with this added to x and y.

public sub

sub(v) => Vector

Return a new Vector with 'this' Vector's x and y components substracted by 'v''s components.

Parameters

  •  v: Vector – The vector to substract ot 'this'.

Returns

  •   VectorDescription needed

sub(x) => Vector

Return a new vector with 'this' Vector's x and y components substracted by 'x' to each component.

Parameters

  •  x: number – The x-component to substract to 'this'.

Returns

  •   VectorDescription needed

sub(x, y) => Vector

Return a new vector with 'this' Vector's x and y components substracted by 'x' and 'y'respectively.

Parameters

  •  x: number – The x-component to substract to 'this'.

  •  y: number – The y-component to substract to 'this'.

Returns

  •   Vector – A new vector with 'this' substracted to 'x' and 'y'.

public scale

scale(v) => Vector

Return a new vector with 'this' vector's 'x' and 'y' scalar multiplied by vector 'v''s 'x' and 'y' respectively.

Parameters

  •  v: Vector – The vector to scalar multiply to 'this'.

Returns

  •   VectorDescription needed

scale(x) => Vector

Return a new vector with 'this' vector's 'x' and 'y' scalar multiplied by number 'x'.

Parameters

  •  x: number – The number to scalar multiply to 'this'.

Returns

  •   Vector – A new vector with 'this' scalar multiplied by 'a'.

public abs

abs() => Vector

Return a Vector with 'this' Vector's absolute value of 'x' and 'y'.

Returns

  •   Vector – Return a new vector that hold the absolute value of original vector.

public normalize

normalize() => Vector

Return a vector that is normalized 'this' Vector.

Returns

  •   Vector – Return a new vector that is normalized 'this'.

public len

len() => number

Return the length of the vector.

Returns

  •   number – Return the length of the vector.

public len2

len2() => number

Return the dot product of 'this' to itself, which is the squared length of 'this'.

Returns

  •   number – The dot product of 'this' . 'this'.

public angle

angle() => number

Return the angle of 'this' that respects to the x-axis.

Returns

  •   number – The arctan value of 'this''s 'x' and 'y'.

public distanceTo

distanceTo(v) => number

Return the distance from 'this' to 'v'.

Parameters

  •  v: Vector – The vector we need to mearsure the distance to.

Returns

  •   number – The length of the vector of 'this' sub 'v'.

public dot

dot(v) => number

Return the dot product of 'this' and 'v'.

Parameters

  •  v: Vector – The vector to dot multiply to 'this'.

Returns

  •   number – 'this' dot multiplied by 'v'.

public rotate

rotate(a) => Vector

Returns a new vector rotated a radians from this one.

Parameters

  •  a: number – The angle in radians.

Returns

  •   Vector – A new, rotated vector.

public project

project(v) => Vector

Return the projection of 'this' on 'v'.

Parameters

  •  v: Vector – The vector that 'this' projects to.

Returns

  •   Vector – The projection of 'this' on vector 'v'.

public negativeReciprocal

negativeReciprocal() => Vector

Return a negative reciprocal vector of 'this'.

Returns

  •   Vector – A new vector with negative reciprocal 'this'.

public copy

copy() => Vector

Return a new vector that copies 'this''s data.

Returns

  •   Vector – A Vector with 'this''s 'x' and 'y'.

public toString

toString() => string

Description needed

Returns

  •   stringDescription needed

Static Methods

public Min

Min(vectors) => Vector

Return a vector that has mininum 'x' and 'y' components from vectors within the array 'vectors'.

Parameters

  •  vectors: Vector[] – The array that holds vectors.

Returns

  •   Vector – A Vector with the smallest 'x' and 'y' that from vector(s) in the array.

public Max

Max(vectors) => Vector

Return a vector that has maxium 'x' and 'y' components from vectors within the array 'vectors'.

Parameters

  •  vectors: Vector[] – The array that holds vectors.

Returns

  •   Vector – A Vector with the biggest 'x' and 'y' that from vector(s) in the array.

public Clamp

Clamp(x, lo, hi) => Vector

Keep the vector 'x' within the range that formed by 'lo' and 'hi'.

Parameters

  •  x: Vector – The vector that need to be examined.

  •  lo: Vector – The minimum vector of the range.

  •  hi: Vector – The maximum vector of the range.

Returns

  •   Vector – Return 'x' itself if it is in the range of 'lo' and 'hi'. If one of the component of 'x' out of the range, it will be respectively change to corresponding compoenent of 'lo' or 'hi' and return. If both component of 'x' out of the range, it will return 'lo' or 'hi' depend on the valueof 'x'.

Functions

V

V() => Vector

The useful utils that make the claim of the vector much easier.

Returns

  •   VectorDescription needed

V(v) => Vector

Description needed

Parameters

  •  v: VectorDescription needed

Returns

  •   VectorDescription needed

V(x) => Vector

Description needed

Parameters

  •  x: numberDescription needed

Returns

  •   VectorDescription needed

V(x, y) => Vector

Description needed

Parameters

  •  x: numberDescription needed

  •  y: numberDescription needed

Returns

  •   VectorDescription needed