Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Vector

A two-dimensional vector. The vector is defined by an x magnitude and a y magnitude.

Example

import { Vector } from 'shapetypes';

// Create a new vector
const v = new Vector(10, 0);

// Get properties of the vector
console.log(v.x);
// => 10
console.log(v.length);
// => 10

// Measure the angle between two vectors
const other = new Vector(0,1);
const angle = v.angle(other);
console.log(angle);
// => 1.57

// Rotate a vector
const rotated = v.rotate(Math.PI / 2);
console.log(rotated.toString());
// => `⟨0,10⟩`

Hierarchy

Index

Constructors

constructor

  • new Vector(x: number, y: number): Vector
  • Parameters

    • x: number

      Magnitude in the x-direction.

    • y: number

      Magnitude in the y-direction.

    Returns Vector

Accessors

isUnit

  • get isUnit(): boolean
  • Checks whether the vector's length is 1. Returns true if it is.

    Returns boolean

isZero

  • get isZero(): boolean
  • Checks whether the vector's length is 0. Returns true if it is.

    Returns boolean

length

  • get length(): number

x

  • get x(): number
  • Gets the vector's magnitude in the x-direction.

    Returns number

xAxis

  • Splits the vector into its x & y components and returns the x component.

    This is the same as worldX * x.

    Returns Vector

y

  • get y(): number
  • Gets the vector's magnitude in the y-direction.

    Returns number

yAxis

  • Splits the vector into its x & y components and returns the y component.

    This is the same as worldY * y.

    Returns Vector

Create Methods

Static fromPoints

  • Creates a new vector from two points. The vector starts at from and ends at to.

    Parameters

    • from: Point

      Start of the vector.

    • to: Point

      End of the vector.

    Returns Vector

Static worldX

  • Returns the world's x-axis as a unit vector. Equivalent to new Vector(1, 0).

    Returns Vector

Static worldY

  • Returns the world's y-axis as a unit vector. Equivalent to new Vector(0, 1).

    Returns Vector

Other Methods

add

  • Adds the x & y magnitudes of another vector and returns the resulting vector.

    Parameters

    • addend: Vector

      Vector to add.

    Returns Vector

  • Adds an x & y value to the vector and returns the resulting vector.

    Parameters

    • x: number

      Value to add to the x magnitude.

    • y: number

      Value to add to the y magnitude.

    Returns Vector

angle

  • angle(other: Vector): number
  • Calculates the smallest angle between this vector and another vector. The result is measured in radians and will be between 0 and Math.PI.

    Parameters

    • other: Vector

      The vector to measure angle between.

    Returns number

    The smallest angle between the two vectors in radians.

angleSigned

  • angleSigned(other: Vector): number
  • Calculates the signed angle between this vector and another vector. The result is measured in radians.

    A positive value means that other is counter-clockwise from the vector (assuming the environment's y-axis points upwards, otherwise it's clockwise).

    A negative value means that other is clockwise from the vector (assuming the environment's y-axis points upwards, otherwise it's counter-clockwise).

    Parameters

    • other: Vector

      The vector to measure the angle between.

    Returns number

    The signed angle in radians.

divide

  • divide(denominator: number): Vector
  • divide(denominatorX: number, denominatorY: number): Vector
  • Divides the x & y magnitudes by a value and returns the resulting vector.

    Parameters

    • denominator: number

      Amount to divide the magnitudes by.

    Returns Vector

  • Divides the x & y magnitudes by different amounts in the x- and y-direction. Returns the resulting vector.

    Parameters

    • denominatorX: number

      Amount to divide the x magnitude by.

    • denominatorY: number

      Amount to divide the y magnitude by.

    Returns Vector

dotProduct

  • dotProduct(other: Vector): number
  • Calculates the dot product of this vector and another vector. Returns the resulting vector.

    Parameters

    • other: Vector

      Vector to calculate dot product with.

    Returns number

equals

  • equals(otherVector: Vector, tolerance?: number): boolean
  • Checks whether another vector has the same x and y values. Returns true if it does.

    Parameters

    • otherVector: Vector

      The vector to compare against.

    • Default value tolerance: number = shapetypesSettings.absoluteTolerance

      The amount that the x and y values of the vectors can differ and still be considered equal.

    Returns boolean

isParallelTo

  • isParallelTo(test: Vector, angleTolerance?: number): boolean
  • Checks whether another vector is parallel. Returns true if it is.

    Parameters

    • test: Vector

      Vector to compare against.

    • Default value angleTolerance: number = shapetypesSettings.angleTolerance

      Acceptable deviation from parallel, measured as the angle between vectors (in radians).

    Returns boolean

isPerpendicularTo

  • isPerpendicularTo(test: Vector, angleTolerance?: number): boolean
  • Checks whether another vector is perpendicular. Returns true if it is.

    Parameters

    • test: Vector

      Vector to compare against.

    • Default value angleTolerance: number = shapetypesSettings.angleTolerance

      Acceptable deviation from perpendicular, measured as the angle between vectors (in radians).

    Returns boolean

isXAxis

  • isXAxis(): boolean
  • Checks whether the vector is parallel to worldX. Returns true if it is.

    Returns boolean

isYAxis

  • isYAxis(): boolean
  • Checks whether the vector is parallel to worldY. Returns true if it is.

    Returns boolean

multiply

  • multiply(factor: number): Vector
  • multiply(factorX: number, factorY: number): Vector
  • Multiplies the x & y magnitudes by a value and returns the resulting vector.

    Parameters

    • factor: number

      Amount to multiply the magnitudes by.

    Returns Vector

  • Multiplies the x & y magnitudes by different amounts in the x- and y-direction. Returns the resulting vector.

    Parameters

    • factorX: number

      Amount to multiply the x magnitude by.

    • factorY: number

      Amount to multiply the y magnitude by.

    Returns Vector

perpendicular

  • Creates a new vector that is perpendicular to this vector.

    The new vector will be on the left side of this vector if looking along [[from]]->[[to]] (assuming the environment's y-axis points upwards, if it doesn't it will be on the right side).

    Returns Vector

reverse

  • Inverts the direction of the vector and returns the inverted vector. This is the same as multiplying the vector by -1.

    Returns Vector

subtract

  • Subtracts the x & y magnitudes of another vector and returns the resulting vector.

    Parameters

    • subtrahend: Vector

      Vector to subtract.

    Returns Vector

  • Subtracts an x & y value from the vector and returns the resulting vector.

    Parameters

    • x: number

      Value to subtract from the x magnitude.

    • y: number

      Value to subtract from the y magnitude.

    Returns Vector

toString

  • toString(): string
  • Gets the vector as a string in the format: '⟨x,y⟩'.

    Returns string

unitize

withLength

  • withLength(newLength: number): Vector
  • Creates a copy of the vector with the x and y magnitudes scaled to a given length.

    Parameters

    • newLength: number

      Length of new vector. If negative, the vector will be inverted but the resulting length will be positive.

    Returns Vector

withX

  • Creates a copy of the vector with a different x magnitude.

    Parameters

    • newX: number

      New value for x.

    Returns Vector

withY

  • Creates a copy of the vector with a different y magnitude.

    Parameters

    • newY: number

      New value for y.

    Returns Vector

Transform Methods

changeBasis

  • changeBasis(planeFrom: Plane, planeTo: Plane): this
  • Translates the geometry from one coordinate system to another while keeping the geometry in the same position. In other words, if the geometry is currently described relative to planeFrom, after changeBasis, it will be in the same position but described relative to planeTo.

    see

    Transform.changeBasis.

    Parameters

    • planeFrom: Plane

      The coordinate system the geometry is currently described relative to.

    • planeTo: Plane

      The coordinate system to describe the geometry relative to.

    Returns this

    The geometry in the new coordinate system.

planeToPlane

  • planeToPlane(planeFrom: Plane, planeTo: Plane): this

rotate

  • rotate(angle: number): this
  • rotate(angle: number, pivot: Point): this
  • Rotates the geometry about (0,0).

    see

    Transform.rotate.

    Parameters

    • angle: number

      Angle to rotate the geometry (in radians). The direction is counter-clockwise.

    Returns this

  • Rotates the geometry about a point.

    see

    Transform.rotate.

    category

    Transform

    Parameters

    • angle: number

      Angle to rotate the geometry (in radians). If the environment's y-axis points upwards, the direction is counter-clockwise.

    • pivot: Point

      Point to pivot the geometry about.

    Returns this

scale

  • scale(amount: number): this
  • scale(x: number, y: number): this
  • scale(x: number, y: number, center: Point): this
  • Scales the geometry and returns the resized geometry. The geometry will be scaled about (0,0), meaning everything will shrink or expand away from this point.

    see

    Transform.scale.

    Parameters

    • amount: number

      Magnitude to scale in x- and y-direction. If less than 1, the object will shrink. If greater than 1, it will grow.

    Returns this

  • Scales the geometry and returns the resized geometry. The geometry will be scaled about (0,0), meaning everything will shrink or expand away from this point.

    see

    Transform.scale.

    category

    Transform

    Parameters

    • x: number

      Magnitude to scale in x-direction. If less than 1, the object will shrink. If greater than 1, it will grow.

    • y: number

      Magnitude to scale in y-direction. If less than 1, the object will shrink. If greater than 1, it will grow.

    Returns this

  • Scales the geometry about a point and returns the resized geometry.

    see

    Transform.scale.

    category

    Transform

    Parameters

    • x: number

      Magnitude to scale in x-direction. If less than 1, the object will shrink. If greater than 1, it will grow.

    • y: number

      Magnitude to scale in y-direction. If less than 1, the object will shrink. If greater than 1, it will grow.

    • center: Point

      Center of scaling. Everything will shrink or expand away from this point.

    Returns this

transform

  • Transforms the vector by a Transform matrix and returns the result.

    Example

    import { Transform, Vector } from 'shapetypes';
    
    // Create a new vecctor
    const vector = new Vector(3, 4);
    console.log(vector.length);
    // => 5
    
    // Scale vector using a transform matrix
    const matrix = Transform.scale(2);
    const scaled = vector.transform(matrix);
    console.log(vector.length);
    // => 10
    
    // Scale vector using the direct method
    const otherScaled = vector.scale(2);
    console.log(otherScaled.length);
    // => 10
    note

    If you're applying the same transformation a lot of geometry, creating the Transform matrix once and calling this function is faster than using the direct methods.

    Parameters

    Returns this

translate

  • translate(move: Vector): this
  • translate(move: Vector, distance: number | undefined): this
  • Moves the geometry along a vector and returns the moved geometry. The translation is always linear.

    see

    Transform.translate.

    Parameters

    • move: Vector

      The direction and distance to move the geometry.

    Returns this

  • Moves the geometry along a vector and returns the moved geometry. The translation is always linear.

    see

    Transform.translate.

    category

    Transform

    Parameters

    • move: Vector

      The direction to move the geometry.

    • distance: number | undefined

      The distance to move the geometry.

    Returns this

Generated using TypeDoc