Created
December 18, 2020 23:43
-
-
Save sinclairzx81/004d83d233300a0c3ad37e53563143e2 to your computer and use it in GitHub Desktop.
2D matrix / vector types for HTML Canvas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Vector } from "./vector" | |
const mi = { | |
m11: 0, m12: 1, m13: 2, | |
m21: 3, m22: 4, m23: 5, | |
m31: 6, m32: 7, m33: 8, | |
} | |
export class Matrix { | |
constructor(public v: number[]) { | |
} | |
public static identity(): Matrix { | |
return new Matrix([ | |
1, 0, 0, | |
0, 1, 0, | |
0, 0, 1 | |
]) | |
} | |
public static rotation(r: number): Matrix { | |
return new Matrix([ | |
Math.cos(r), Math.sin(r), 0, | |
-Math.sin(r), Math.cos(r), 0, | |
0, 0, 1 | |
]) | |
} | |
public static translate(v: Vector): Matrix { | |
return new Matrix([ | |
1, 0, v.v[0], | |
0, 1, v.v[1], | |
0, 0, 1 | |
]) | |
} | |
public static scale(v: Vector): Matrix { | |
return new Matrix([ | |
v.v[0], 0, 0, | |
0, v.v[1], 0, | |
0, 0, 1 | |
]) | |
} | |
public static multiply(a: Matrix, b: Matrix): Matrix { | |
return new Matrix([ | |
(((a.v[mi.m11] * b.v[mi.m11]) + (a.v[mi.m12] * b.v[mi.m21])) + (a.v[mi.m13] * b.v[mi.m31])), | |
(((a.v[mi.m11] * b.v[mi.m12]) + (a.v[mi.m12] * b.v[mi.m22])) + (a.v[mi.m13] * b.v[mi.m32])), | |
(((a.v[mi.m11] * b.v[mi.m13]) + (a.v[mi.m12] * b.v[mi.m23])) + (a.v[mi.m13] * b.v[mi.m33])), | |
(((a.v[mi.m21] * b.v[mi.m11]) + (a.v[mi.m22] * b.v[mi.m21])) + (a.v[mi.m23] * b.v[mi.m31])), | |
(((a.v[mi.m21] * b.v[mi.m12]) + (a.v[mi.m22] * b.v[mi.m22])) + (a.v[mi.m23] * b.v[mi.m32])), | |
(((a.v[mi.m21] * b.v[mi.m13]) + (a.v[mi.m22] * b.v[mi.m23])) + (a.v[mi.m23] * b.v[mi.m33])), | |
(((a.v[mi.m31] * b.v[mi.m11]) + (a.v[mi.m32] * b.v[mi.m21])) + (a.v[mi.m33] * b.v[mi.m31])), | |
(((a.v[mi.m31] * b.v[mi.m12]) + (a.v[mi.m32] * b.v[mi.m22])) + (a.v[mi.m33] * b.v[mi.m32])), | |
(((a.v[mi.m31] * b.v[mi.m13]) + (a.v[mi.m32] * b.v[mi.m23])) + (a.v[mi.m33] * b.v[mi.m33])) | |
]) | |
} | |
public clone(): Matrix { | |
return new Matrix(this.v.map(n => n)) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Matrix } from "./matrix" | |
export class Vector { | |
constructor(public v: number[]) { | |
} | |
public static create(x: number, y: number) { | |
return new Vector([x, y]) | |
} | |
public static zero(): Vector { | |
return new Vector([0, 0]) | |
} | |
public static add(a: Vector, b: Vector): Vector { | |
return new Vector([a.v[0] + b.v[0], a.v[1] + b.v[1]]) | |
} | |
public static sub(a: Vector, b: Vector): Vector { | |
return new Vector([a.v[0] - b.v[0], a.v[1] - b.v[1]]) | |
} | |
public static scale(a: Vector, v: number): Vector { | |
return new Vector([a.v[0] * v, a.v[1] * v]) | |
} | |
public static mul(a: Vector, b: Vector) { | |
return new Vector([a.v[0] * b.v[0], a.v[1] * b.v[1]]) | |
} | |
public static div(a: Vector, b: Vector) { | |
return new Vector([a.v[0] / b.v[0], a.v[1] / b.v[1]]) | |
} | |
public static len(a: Vector) { | |
return Math.sqrt(a.v[0] * a.v[0] + a.v[1] * a.v[1]) | |
} | |
public static normalize(a: Vector): Vector { | |
const length = Vector.len(a) | |
return new Vector([a.v[0] / length, a.v[1] / length]) | |
} | |
public static transform(v0: Vector, m0: Matrix): Vector { | |
return new Vector([ | |
(((v0.v[0] * m0.v[0]) + (v0.v[1] * m0.v[1])) + m0.v[2]), | |
(((v0.v[0] * m0.v[3]) + (v0.v[1] * m0.v[4])) + m0.v[5]) | |
]) | |
} | |
public clone(): Vector { | |
return new Vector(this.v.map(n => n)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment