vector2

Adds two vectors.

Type signature

([x1, y1]: [number, number], [x2, y2]: [number, number]) => [number, number]

Examples

add([3, 5], [-1, 8]);
// ⇒ [2, 13]
Try in REPL

Questions

  • How to add two vectors?

TypeScript sourceJavaScript source

Applies transformations to the given vector.

Type signature

(space: {
    a: number;
    c: number;
    e: number;
    b: number;
    d: number;
    f: number;
}) => ([x, y]: [number, number]) => number[]

Examples

convertSpace(rotate(Math.PI))([
  2, 3,
]);
// ⇒ [-2, -3]
Try in REPL

Questions

  • How to transform a vector with a matrix?

TypeScript sourceJavaScript source

Calculates a cross product of the given vectors. Returns a scalar.

Type signature

([a, b]: [number, number], [c, d]: [number, number]) => number

Examples

cross([3, 5], [-1, 8]);
// ⇒ 29
Try in REPL
cross([3, 5], [-1, -8]);
// ⇒ -19
Try in REPL

Questions

  • How to compute a cross product of two vectors?
  • How to check on which side of a line a point is?

TypeScript sourceJavaScript source

Calculates a dot product of the given vectors. Returns a scalar.

Type signature

([a, b]: [number, number], [c, d]: [number, number]) => number

Examples

dot([3, 5], [-1, 8]);
// ⇒ 37
Try in REPL
dot([3, 5], [-1, -8]);
// ⇒ -43
Try in REPL

Questions

  • How to compute a dot product of two vectors?

TypeScript sourceJavaScript source

Calculates the length/magnitude of the given vector.

Type signature

([x, y]: [number, number]) => number

Examples

length([3, 5]);
// ⇒ Math.sqrt(3 * 3 + 5 * 5)
Try in REPL

Questions

  • How to compute the length of a vector?
  • How to compute the magnitude of a vector?

TypeScript sourceJavaScript source

Applies matrix transformation to the given vector.

Type signature

({ a, b, c, d, e, f }: {
    a: number;
    c: number;
    e: number;
    b: number;
    d: number;
    f: number;
}, [x, y]: [number, number]) => number[]

Examples

mul(scale(4, 5), [2, 3]);
// ⇒ [8, 15]
Try in REPL

Questions

  • How to apply a matrix transformation to a vector?

TypeScript sourceJavaScript source

Multiples two matrices.

Type signature

(m1: {
    a: number;
    c: number;
    e: number;
    b: number;
    d: number;
    f: number;
}, m2: {
    a: number;
    b: number;
    c: number;
    d: number;
    e: number;
    f: number;
}) => {
    a: number;
    c: number;
    e: number;
    b: number;
    d: number;
    f: number;
}

Examples

multiply(
  {
    a: 1,
    c: 2,
    e: 3,
    b: 4,
    d: 5,
    f: 6,
  },
  {
    a: 7,
    c: 8,
    e: 9,
    b: 10,
    d: 11,
    f: 12,
  },
);
// ⇒ { a: 27, b: 78, c: 30, d: 87, e: 36, f: 102 }
Try in REPL

Questions

  • How to multiply two matrices?

TypeScript sourceJavaScript source

Normalizes the given vector. Returns [0, 0] vector for points.

Type signature

(vector: [number, number]) => [number, number]

Examples

normalize([2, 3]);
// ⇒ [2 / length([2, 3]), 3 / length([2, 3])]
Try in REPL

Questions

  • How to normalize a vector?

TypeScript sourceJavaScript source

Reflects the given vector on the given surface.

Type signature

(a: [number, number], v: [number, number]) => [number, number]

Examples

reflect([1, -2], [1, 0]);
// ⇒ [0.6, 0.8]
Try in REPL

Questions

  • How to reflect a vector on another vector?

TypeScript sourceJavaScript source

Creates a rotation matrix around given origin [0, 0] by default.

Type signature

(angle?: number, cx?: number, cy?: number) => {
    a: number;
    c: number;
    e: number;
    b: number;
    d: number;
    f: number;
}

Examples

const angle = Math.PI;
const sine = Math.sin(angle);
const cosine = Math.cos(angle);

rotate(angle); // { a: cosine, b: sine, c: -sine, d: cosine, e: 0, f: 0 }
Try in REPL

Questions

  • How to create a rotation matrix?

TypeScript sourceJavaScript source

Creates a scale matrix.

Type signature

(sx?: number, sy?: number) => {
    a: number;
    c: number;
    e: number;
    b: number;
    d: number;
    f: number;
}

Examples

scale(2, 3);
// ⇒ { a: 2, b: 0, c: 0, d: 3, e: 0, f: 0 }
Try in REPL

Questions

  • How to create a scale matrix?

TypeScript sourceJavaScript source

Subtracts two vectors.

Type signature

([x1, y1]: [number, number], [x2, y2]: [number, number]) => [number, number]

Examples

sub([3, 5], [-1, 8]);
// ⇒ [4, -3]
Try in REPL

Questions

  • How to subtract two vectors?

TypeScript sourceJavaScript source

Composes a single transformation by matrix multiplication.

Type signature

(...matrices: {
    a: number;
    c: number;
    e: number;
    b: number;
    d: number;
    f: number;
}[]) => {
    a: number;
    c: number;
    e: number;
    b: number;
    d: number;
    f: number;
}

Examples

const originX = 5;
const originY = 6;
const angle = Math.PI;

transform(
  translate(originX, originY),
  rotate(angle),
  translate(-originX, -originY),
);
// ⇒ rotate(Math.PI, originX, originY)
Try in REPL

Questions

  • How to compose multiple matrix transformations into a single matrix?

TypeScript sourceJavaScript source

Creates a translation matrix.

Type signature

(tx?: number, ty?: number) => {
    a: number;
    c: number;
    e: number;
    b: number;
    d: number;
    f: number;
}

Examples

translate(2, 3);
// ⇒ { a: 1, b: 0, c: 0, d: 1, e: 2, f: 3 }
Try in REPL

Questions

  • How to create a translation matrix?

TypeScript sourceJavaScript source