Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Vec2

A basic 2D Vector class that provides simple algebraic functionality in the form of 2D Vectors.

We use Getters/setters for both principle properties (x & y) as well as virtual properties (rotation, length etc.).

author

Liam Egan liam@wethecollective.com

version

1.0.0

created

Jan 07, 2020

Hierarchy

  • Vec2

Index

Constructors

constructor

  • new Vec2(...args: number[]): Vec2
  • The Vector Class constructor

    chainable

    Parameters

    • Rest ...args: number[]

    Returns Vec2

Properties

Private #x

#x: number = 0

(getter/setter) The x value of the vector.

default

0

Private #y

#y: number = 0

(getter/setter) The y value of the vector.

default

0

Accessors

angle

  • get angle(): number
  • set angle(radian: number): void
  • (getter/setter) The angle of the vector, in radians

    Returns number

  • (getter/setter) The angle of the vector, in radians

    default

    0

    Parameters

    • radian: number

    Returns void

angleInDegrees

  • get angleInDegrees(): number
  • set angleInDegrees(degrees: number): void
  • (getter/setter) The angle of the vector, in radians

    Returns number

  • (getter/setter) The angle of the vector, in radians

    default

    0

    Parameters

    • degrees: number

    Returns void

area

  • get area(): number
  • (getter) Vector area.

    readonly

    Returns number

array

  • get array(): number[]
  • (getter) Returns the basic array representation of this vector.

    readonly

    Returns number[]

height

  • get height(): number
  • set height(h: number): void
  • (getter/setter) Vector height. Alias of {@link Vector#x x}

    Returns number

  • (getter/setter) Vector height. Alias of {@link Vector#x x}

    Parameters

    • h: number

    Returns void

length

  • get length(): number
  • set length(length: number): void
  • (getter/setter) The length of the vector

    Returns number

  • (getter/setter) The length of the vector

    default

    0

    Parameters

    • length: number

    Returns void

lengthSquared

  • get lengthSquared(): number
  • set lengthSquared(length: number): void
  • (getter/setter) The length of the vector presented as a square. If you're using length for comparison, this is quicker.

    Returns number

  • (getter/setter) The length of the vector presented as a square. If you're using length for comparison, this is quicker.

    default

    0

    Parameters

    • length: number

    Returns void

slope

  • get slope(): number
  • set slope(value: number): void
  • (getter/setter) Vector slope.

    Returns number

  • (getter/setter) Vector slope.

    Parameters

    • value: number

    Returns void

width

  • get width(): number
  • set width(w: number): void
  • (getter/setter) Vector width. Alias of {@link Vector#x x}

    Returns number

  • (getter/setter) Vector width. Alias of {@link Vector#x x}

    Parameters

    • w: number

    Returns void

x

  • get x(): number
  • set x(x: number): void

xx

  • get xx(): any
  • set xx(v: any): void
  • (getter/sette) Swizzle XX

    Returns any

  • (getter/sette) Swizzle XX

    Parameters

    • v: any

    Returns void

xy

  • get xy(): Vec2 | number[]
  • set xy(v: Vec2 | number[]): void
  • (getter/sette) Swizzle XY

    Returns Vec2 | number[]

  • (getter/sette) Swizzle XY

    Parameters

    • v: Vec2 | number[]

    Returns void

y

  • get y(): number
  • set y(y: number): void

yx

  • get yx(): any
  • set yx(v: any): void
  • (getter/sette) Swizzle YX

    Returns any

  • (getter/sette) Swizzle YX

    Parameters

    • v: any

    Returns void

yy

  • get yy(): any
  • set yy(v: any): void
  • (getter/sette) Swizzle YY

    Returns any

  • (getter/sette) Swizzle YY

    Parameters

    • v: any

    Returns void

Methods

[iterator]

  • [iterator](): { next: any }
  • Adds iteration to the object, allowing it to be destructured and iterated upon in various useful ways.

    Returns { next: any }

    • next: function
      • next(): { done: boolean; value: number } | { done: boolean; value: undefined }
      • Returns { done: boolean; value: number } | { done: boolean; value: undefined }

add

  • Adds one vector to another.

    chainable

    Parameters

    • vector: Vec2

      The vector to add to this one

    Returns Vec2

    Returns itself, modified

addNew

  • Clones the vector and adds the vector to it instead

    chainable

    Parameters

    • vector: Vec2

      The vector to add to this one

    Returns Vec2

    Returns the clone of itself, modified

addScalar

  • addScalar(scalar: number): Vec2
  • Adds a scalar to the vector, modifying both the x and y

    chainable

    Parameters

    • scalar: number

      The scalar to add to the vector

    Returns Vec2

    Returns itself, modified

addScalarNew

  • addScalarNew(scalar: number): Vec2
  • Clones the vector and adds the scalar to it instead

    chainable

    Parameters

    • scalar: number

      The scalar to add to the vector

    Returns Vec2

    Returns the clone of itself, modified

ceil

ceilNew

clone

  • Clones the vector

    chainable

    Returns Vec2

    The cloned vector

cross

  • cross(vector: Vec2): number
  • Calculates the cross product between this and the supplied vector.

    example

    // returns -2 new Vector(2, -3).cross(new Vector(-4, 2)) new Vector(-4, 2).cross(new Vector(2, -3)) // returns 2 new Vector(2, -4).cross(new Vector(-3, 2))

    Parameters

    • vector: Vec2

      The vector object against which to calculate the cross product

    Returns number

    The cross product of the two vectors

det

  • det(vector: Vec2): number

distance

  • distance(vector: Vec2): number
  • Calculates the distance between this and the supplied vector

    Parameters

    • vector: Vec2

      The vector to calculate the distance from

    Returns number

    The distance between this and the supplied vector

distanceX

  • distanceX(vector: Vec2): number
  • Calculates the distance on the X axis between this and the supplied vector

    Parameters

    • vector: Vec2

      The vector to calculate the distance from

    Returns number

    The distance, along the x axis, between this and the supplied vector

distanceY

  • distanceY(vector: Vec2): number
  • Calculated the distance on the Y axis between this and the supplied vector

    Parameters

    • vector: Vec2

      The vector to calculate the distance from

    Returns number

    The distance, along the y axis, between this and the supplied vector

divide

  • Divides one vector by another.

    chainable

    Parameters

    • vector: Vec2

      The vector to divide this by

    Returns Vec2

    Returns itself, modified

divideNew

  • Clones the vector and divides it by the vector instead

    chainable

    Parameters

    • vector: Vec2

      The vector to divide the clone by

    Returns Vec2

    Returns the clone of itself, modified

divideScalar

  • divideScalar(scalar: number): Vec2
  • Divides the vector by a scalar.

    chainable

    Parameters

    • scalar: number

      The scalar to divide both x and y by

    Returns Vec2

    Returns itself, modified

divideScalarNew

  • divideScalarNew(scalar: number): Vec2
  • Clones the vector and divides it by the provided scalar.

    chainable

    Parameters

    • scalar: number

      The scalar to divide both x and y by

    Returns Vec2

    Returns the clone of itself, modified

dot

  • dot(vector: Vec2): number
  • Calculates the dot product between this and a supplied vectorT

    example

    // returns -14 new Vector(2, -3).dot(new Vector(-4, 2)) new Vector(-4, 2).dot(new Vector(2, -3)) new Vector(2, -4).dot(new Vector(-3, 2))

    Parameters

    • vector: Vec2

      The vector object against which to calculate the dot product

    Returns number

    The dot product of the two vectors

floor

floorNew

fract

fractNew

inverse

  • Inverses the vector.

    chainable

    Returns Vec2

    Returns itself, modified

inverseNew

  • inverseNew(): Vec2
  • Clones the vector and then inverses it.

    chainable

    Returns Vec2

    Returns itself, modified

mod

modNew

multiply

  • Multiplies one vector by another.

    chainable

    Parameters

    • vector: Vec2

      The vector to multiply this by

    Returns Vec2

    Returns itself, modified

multiplyNew

  • Clones the vector and multiplies it by the vector instead

    chainable

    Parameters

    • vector: Vec2

      The vector to multiply the clone by

    Returns Vec2

    Returns the clone of itself, modified

multiplyScalar

  • multiplyScalar(scalar: number): Vec2
  • Multiplies the vector by a scalar.

    chainable

    Parameters

    • scalar: number

      The scalar to multiply both x and y by

    Returns Vec2

    Returns itself, modified

multiplyScalarNew

  • multiplyScalarNew(scalar: number): Vec2
  • Clones the vector and multiplies it by the provided scalar.

    chainable

    Parameters

    • scalar: number

      The scalar to multiply both x and y by

    Returns Vec2

    Returns the clone of itself, modified

negate

  • Negates the vector.

    chainable

    Returns Vec2

    Returns itself, modified

negateNew

  • Clones the vector and negates it.

    chainable

    Returns Vec2

    Returns itself, modified

normalise

  • Normalises the vector down to a length of 1 unit

    chainable

    Returns Vec2

    Returns itself, modified

normaliseNew

  • normaliseNew(): Vec2
  • Clones the vector and normalises it

    chainable

    Returns Vec2

    Returns a clone of itself, modified

reset

  • reset(...args: number[]): Vec2
  • Resets the vector coordinates

    chainable

    Parameters

    • Rest ...args: number[]

    Returns Vec2

resetToVector

  • Resets the vector coordinates to another vector object

    chainable

    Parameters

    • v: Vec2

      The vector object to use to reset the coordinates

    Returns Vec2

rotate

  • rotate(radian: number): Vec2
  • Rotates a vecor by a given amount, provided in radians.

    chainable

    Parameters

    • radian: number

      The angle, in radians, to rotate the vector by

    Returns Vec2

    Returns itself, modified

rotateBy

  • rotateBy(radian: number): Vec2
  • Alias of {@link Vector#rotate__anchor rotate}

    Parameters

    • radian: number

    Returns Vec2

rotateByNew

  • rotateByNew(radian: number): Vec2
  • Alias of {@link Vector#rotateNew__anchor rotateNew}

    Parameters

    • radian: number

    Returns Vec2

rotateDeg

  • rotateDeg(degrees: number): Vec2
  • Rotates a vecor by a given amount, provided in degrees. Converts the degree value to radians and runs the rotaet method.

    chainable

    Parameters

    • degrees: number

      The angle, in degrees, to rotate the vector by

    Returns Vec2

    Returns itself, modified

rotateDegBy

  • rotateDegBy(degrees: number): Vec2
  • Alias of {@link Vector#rotateDeg__anchor rotateDeg}

    Parameters

    • degrees: number

    Returns Vec2

rotateDegByNew

  • rotateDegByNew(radian: number): Vec2
  • Alias of {@link Vector#rotateDegNew__anchor rotateDegNew}

    Parameters

    • radian: number

    Returns Vec2

rotateDegNew

  • rotateDegNew(degrees: number): Vec2
  • Clones the vector and rotates it by the supplied degree value

    chainable

    Parameters

    • degrees: number

      The angle, in degrees, to rotate the vector by

    Returns Vec2

    Returns the clone of itself, modified

rotateNew

  • rotateNew(radian: number): Vec2
  • Clones the vector and rotates it by the supplied radian value

    chainable

    Parameters

    • radian: number

      The angle, in radians, to rotate the vector by

    Returns Vec2

    Returns the clone of itself, modified

rotateTo

  • rotateTo(radian: number): Vec2
  • Rotates a vector to a specific angle

    chainable

    Parameters

    • radian: number

      The angle, in radians, to rotate the vector to

    Returns Vec2

    Returns itself, modified

rotateToDeg

  • rotateToDeg(degrees: number): Vec2
  • Rotates a vecor to a given amount, provided in degrees. Converts the degree value to radians and runs the rotateTo method.

    chainable

    Parameters

    • degrees: number

      The angle, in degrees, to rotate the vector to

    Returns Vec2

    Returns itself, modified

rotateToDegNew

  • rotateToDegNew(degrees: number): Vec2
  • Clones the vector and rotates it to the supplied degree value

    chainable

    Parameters

    • degrees: number

      The angle, in degrees, to rotate the vector to

    Returns Vec2

    Returns the clone of itself, modified

rotateToNew

  • rotateToNew(radian: number): Vec2
  • Clones the vector and rotates it to the supplied radian value

    chainable

    Parameters

    • radian: number

      The angle, in radians, to rotate the vector to

    Returns Vec2

    Returns the clone of itself, modified

round

roundNew

scale

  • scale(scalar: number): Vec2
  • Alias of {@link Vector#multiplyScalar__anchor multiplyScalar}

    Parameters

    • scalar: number

    Returns Vec2

scaleNew

  • scaleNew(scalar: number): Vec2
  • Alias of {@link Vector#multiplyScalarNew__anchor multiplyScalarNew}

    Parameters

    • scalar: number

    Returns Vec2

slopeBetween

  • slopeBetween(vector: Vec2): number

subtract

  • Subtracts one vector from another.

    chainable

    Parameters

    • vector: Vec2

      The vector to subtract from this one

    Returns Vec2

    Returns itself, modified

subtractNew

  • Clones the vector and subtracts the vector from it instead

    chainable

    Parameters

    • vector: Vec2

      The vector to subtract from this one

    Returns Vec2

    Returns the clone of itself, modified

subtractScalar

  • subtractScalar(scalar: number): Vec2
  • Subtracts a scalar from the vector, modifying both the x and y

    chainable

    Parameters

    • scalar: number

      The scalar to subtract from the vector

    Returns Vec2

    Returns itself, modified

subtractScalarNew

  • subtractScalarNew(scalar: number): Vec2
  • Clones the vector and subtracts the scalar from it instead

    chainable

    Parameters

    • scalar: number

      The scalar to add to the vector

    Returns Vec2

    Returns the clone of itself, modified

transformByMat2

  • transformByMat2(m: any): Vec2

transformByMat2New

  • transformByMat2New(m: any): Vec2

transformByMat3

  • transformByMat3(m: any): Vec2

transformByMat3New

  • transformByMat3New(m: any): Vec2

Static getAngle

  • Finds the angle between 2 vectors.

    Parameters

    • a: Vec2

      the first operand

    • b: Vec2

      the second operand

    Returns number

Static interpolate

  • interpolate(v: any): Vec2
  • Iterpolates a provided anonymous value into a vew Vec2

    Parameters

    • v: any

      The value to interpolate

    Returns Vec2

    out

Static lerp

  • Performs a linear interpolation between two vec2's

    Parameters

    • v1: Vec2

      the first operand

    • v2: Vec2

      the second operand

    • d: number

      interpolation amount in the range of 0 - 1

    Returns Vec2

Generated using TypeDoc