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.).

Vec2

Liam Egan liam@wethecollective.com

1.0.0

Jan 07, 2020

Constructors

  • The Vector Class constructor

    Parameters

    • Rest...args: number[]

    Returns Vec2

Accessors

  • get angle(): number
  • Returns number

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

    Parameters

    • radian: number

    Returns void

    0
    
  • get angleInDegrees(): number
  • Returns number

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

    Parameters

    • degrees: number

    Returns void

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

    Returns number

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

    Returns number[]

  • get height(): number
  • Returns number

  • set height(h): void
  • (getter/setter) Vector height. Alias of Vector#x x

    Parameters

    • h: number

    Returns void

  • get length(): number
  • Returns number

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

    Parameters

    • length: number

    Returns void

    0
    
  • get lengthSquared(): number
  • Returns number

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

    Parameters

    • length: number

    Returns void

    0
    
  • get slope(): number
  • Returns number

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

    Parameters

    • value: number

    Returns void

  • get width(): number
  • Returns number

  • set width(w): void
  • (getter/setter) Vector width. Alias of Vector#x x

    Parameters

    • w: number

    Returns void

  • get x(): number
  • Returns number

  • set x(x): void
  • Parameters

    • x: number

    Returns void

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

    Returns any

  • set xx(v): void
  • Parameters

    • v: any

    Returns void

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

    Returns number[] | Vec2

  • set xy(v): void
  • Parameters

    Returns void

  • get y(): number
  • Returns number

  • set y(y): void
  • Parameters

    • y: number

    Returns void

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

    Returns any

  • set yx(v): void
  • Parameters

    • v: any

    Returns void

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

    Returns any

  • set yy(v): void
  • Parameters

    • v: any

    Returns void

Methods

  • Adds iteration to the object, allowing it to be destructured and iterated upon in various useful ways.

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

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

  • Adds one vector to another.

    Parameters

    • vector: Vec2

      The vector to add to this one

    Returns Vec2

    Returns itself, modified

  • Clones the vector and adds the vector to it instead

    Parameters

    • vector: Vec2

      The vector to add to this one

    Returns Vec2

    Returns the clone of itself, modified

  • Adds a scalar to the vector, modifying both the x and y

    Parameters

    • scalar: number

      The scalar to add to the vector

    Returns Vec2

    Returns itself, modified

  • Clones the vector and adds the scalar to it instead

    Parameters

    • scalar: number

      The scalar to add to the vector

    Returns Vec2

    Returns the clone of itself, modified

  • Returns Vec2

  • Returns Vec2

  • Clones the vector

    Returns Vec2

    The cloned vector

  • Calculates the cross product between this and the supplied vector.

    Parameters

    • vector: Vec2

      The vector object against which to calculate the cross product

    Returns number

    The cross product of the two vectors

    // 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

    Returns 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

  • 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

  • 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

  • Divides one vector by another.

    Parameters

    • vector: Vec2

      The vector to divide this by

    Returns Vec2

    Returns itself, modified

  • Clones the vector and divides it by the vector instead

    Parameters

    • vector: Vec2

      The vector to divide the clone by

    Returns Vec2

    Returns the clone of itself, modified

  • Divides the vector by a scalar.

    Parameters

    • scalar: number

      The scalar to divide both x and y by

    Returns Vec2

    Returns itself, modified

  • Clones the vector and divides it by the provided scalar.

    Parameters

    • scalar: number

      The scalar to divide both x and y by

    Returns Vec2

    Returns the clone of itself, modified

  • Calculates the dot product between this and a supplied vectorT

    Parameters

    • vector: Vec2

      The vector object against which to calculate the dot product

    Returns number

    The dot product of the two vectors

    // 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))
  • Returns Vec2

  • Returns Vec2

  • Returns Vec2

  • Returns Vec2

  • Inverses the vector.

    Returns Vec2

    Returns itself, modified

  • Clones the vector and then inverses it.

    Returns Vec2

    Returns itself, modified

  • Parameters

    Returns Vec2

  • Parameters

    Returns Vec2

  • Multiplies one vector by another.

    Parameters

    • vector: Vec2

      The vector to multiply this by

    Returns Vec2

    Returns itself, modified

  • Clones the vector and multiplies it by the vector instead

    Parameters

    • vector: Vec2

      The vector to multiply the clone by

    Returns Vec2

    Returns the clone of itself, modified

  • Multiplies the vector by a scalar.

    Parameters

    • scalar: number

      The scalar to multiply both x and y by

    Returns Vec2

    Returns itself, modified

  • Clones the vector and multiplies it by the provided scalar.

    Parameters

    • scalar: number

      The scalar to multiply both x and y by

    Returns Vec2

    Returns the clone of itself, modified

  • Negates the vector.

    Returns Vec2

    Returns itself, modified

  • Clones the vector and negates it.

    Returns Vec2

    Returns itself, modified

  • Normalises the vector down to a length of 1 unit

    Returns Vec2

    Returns itself, modified

  • Clones the vector and normalises it

    Returns Vec2

    Returns a clone of itself, modified

  • Resets the vector coordinates

    Parameters

    • Rest...args: number[]

    Returns Vec2

  • Resets the vector coordinates to another vector object

    Parameters

    • v: Vec2

      The vector object to use to reset the coordinates

    Returns Vec2

  • Rotates a vecor by a given amount, provided in radians.

    Parameters

    • radian: number

      The angle, in radians, to rotate the vector by

    Returns Vec2

    Returns itself, modified

  • Alias of Vector#rotate__anchor rotate

    Parameters

    • radian: number

    Returns Vec2

  • Alias of Vector#rotateNew__anchor rotateNew

    Parameters

    • radian: number

    Returns Vec2

  • Rotates a vecor by a given amount, provided in degrees. Converts the degree value to radians and runs the rotaet method.

    Parameters

    • degrees: number

      The angle, in degrees, to rotate the vector by

    Returns Vec2

    Returns itself, modified

  • Alias of Vector#rotateDeg__anchor rotateDeg

    Parameters

    • degrees: number

    Returns Vec2

  • Alias of Vector#rotateDegNew__anchor rotateDegNew

    Parameters

    • radian: number

    Returns Vec2

  • Clones the vector and rotates it by the supplied degree value

    Parameters

    • degrees: number

      The angle, in degrees, to rotate the vector by

    Returns Vec2

    Returns the clone of itself, modified

  • Clones the vector and rotates it by the supplied radian value

    Parameters

    • radian: number

      The angle, in radians, to rotate the vector by

    Returns Vec2

    Returns the clone of itself, modified

  • Rotates a vector to a specific angle

    Parameters

    • radian: number

      The angle, in radians, to rotate the vector to

    Returns Vec2

    Returns itself, modified

  • Rotates a vecor to a given amount, provided in degrees. Converts the degree value to radians and runs the rotateTo method.

    Parameters

    • degrees: number

      The angle, in degrees, to rotate the vector to

    Returns Vec2

    Returns itself, modified

  • Clones the vector and rotates it to the supplied degree value

    Parameters

    • degrees: number

      The angle, in degrees, to rotate the vector to

    Returns Vec2

    Returns the clone of itself, modified

  • Clones the vector and rotates it to the supplied radian value

    Parameters

    • radian: number

      The angle, in radians, to rotate the vector to

    Returns Vec2

    Returns the clone of itself, modified

  • Returns Vec2

  • Returns Vec2

  • Alias of Vector#multiplyScalar__anchor multiplyScalar

    Parameters

    • scalar: number

    Returns Vec2

  • Alias of Vector#multiplyScalarNew__anchor multiplyScalarNew

    Parameters

    • scalar: number

    Returns Vec2

  • Parameters

    Returns number

  • Subtracts one vector from another.

    Parameters

    • vector: Vec2

      The vector to subtract from this one

    Returns Vec2

    Returns itself, modified

  • Clones the vector and subtracts the vector from it instead

    Parameters

    • vector: Vec2

      The vector to subtract from this one

    Returns Vec2

    Returns the clone of itself, modified

  • Subtracts a scalar from the vector, modifying both the x and y

    Parameters

    • scalar: number

      The scalar to subtract from the vector

    Returns Vec2

    Returns itself, modified

  • Clones the vector and subtracts the scalar from it instead

    Parameters

    • scalar: number

      The scalar to add to the vector

    Returns Vec2

    Returns the clone of itself, modified

  • Parameters

    • m: any

    Returns Vec2

  • Parameters

    • m: any

    Returns Vec2

  • Parameters

    • m: any

    Returns Vec2

  • Parameters

    • m: any

    Returns Vec2

  • Finds the angle between 2 vectors.

    Parameters

    • a: Vec2

      the first operand

    • b: Vec2

      the second operand

    Returns number

  • Iterpolates a provided anonymous value into a vew Vec2

    Parameters

    • v: any

      The value to interpolate

    Returns Vec2

    out

  • 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