Skip to main content

Vector2

From v1.0

A two dimensional vector

Usageโ€‹

import {Vector2} from '@math.gl/core/matrix';
const vector = new Vector2(1, 1);

Inheritanceโ€‹

Vector2 extends Vector extends MathArray extends Array

Many commonly used Vector2 methods are inherited from Vector and MathArray:

  • Vector2.clone()
  • Vector2.copy(array)
  • Vector2.set(...args)
  • Vector2.fromArray(array, offset = 0)
  • Vector2.toString()
  • Vector2.toArray(array = [], offset = 0)
  • Vector2.equals(array)
  • Vector2.exactEquals(array)
  • Vector2.validate()
  • Vector2.check()
  • vector2.normalize()

Also note that Vector2 is a subclass of the built in JavaScript Array and can thus be used wherever an Array is expected. It can e.g. supplied as a parameter to any function expecting an Array.

Membersโ€‹

x, yโ€‹

Gets or sets element 0 or 1 respectively

constructorโ€‹

Creates a new, empty Vector2, or copies an existing Vector2

constructor((x = 0), (y = 0));
constructor([x, y]);

setโ€‹

set(x, y)

addโ€‹

Add zero or more vectors to current vector.

add(...vectors)

subtractโ€‹

Subtract zero or more vectors from current vector

subtract(...vectors)

multiplyโ€‹

Multiply zero or more vectors with current vector

multiply(...vectors)

divideโ€‹

Divide zero or more vectors with current vector

divide(...vectors)

scaleโ€‹

scale(scale)

scaleAndAddโ€‹

scaleAndAdd(vector, scale)

negateโ€‹

negate()

normalizeโ€‹

normalize()

dotโ€‹

dot(vector)

lerpโ€‹

lerp(vector, coeff)

horizontalAngleโ€‹

Calculates counterclockwise angle in radians starting from positive x axis

horizontalAngle()

Note: returns Math.atan2(this.y, this.x)

verticalAngleโ€‹

Calculates clockwise angle in radians starting from positive y axis

verticalAngle()

Note: returns Math.atan2(this.x, this.y)

transform(matrix4 : Number[16]) : thisโ€‹

Equivalent to transformAsPoint.

transformAsPoint(matrix4 : Number[16]) : thisโ€‹

Transforms this vector by the provided 4x4 matrix as a point (i.e includes translations).

Note: Implicitly extends the vector to [x, y, 0, 1] before applying the 4x4 transformation.

transformAsVector(matrix4 : Number[16]) : thisโ€‹

Transforms this vector by the provided 4x4 matrix as a vector (i.e does not include translations).

Note: Implicitly extends the vector to [x, y, 0, 0] before applying the 4x4 transformation.

transformByMatrix3(matrix3 : Number[9]) : thisโ€‹

Transforms this vector by the provided 3x3 matrix.

transformByMatrix2x3(matrix2 : Number[6]) : thisโ€‹

Transforms this vector by the provided 2x3 matrix (A pure 2D transform that can incorporate translations).

transformByMatrix2(matrix2 : Number[4]) : thisโ€‹

Transforms this vector by the provided 2x2 matrix.