Last active
February 27, 2025 01:39
-
-
Save SharpCoder/2059d163f89bdf09948ec65defa2f3e7 to your computer and use it in GitHub Desktop.
Matrix example
This file contains hidden or 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
const result = m4.combine([ | |
m4.translate(x,y,z), | |
m4.rotateX(rads(35)), | |
m4.rotateY(rads(25)), | |
]) | |
// Here's how you can decompose the result back into a vector | |
const vector = [ | |
result[6], | |
result[7], | |
result[8] | |
]; |
This file contains hidden or 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
export class m4 { | |
static translate(x: number, y: number, z: number) { | |
return [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1]; | |
} | |
static rotateX(rads: number) { | |
const c = Math.cos(rads); | |
const s = Math.sin(rads); | |
return [1, 0, 0, 0, 0, c, s, 0, 0, -s, c, 0, 0, 0, 0, 1]; | |
} | |
static rotateY(rads: number) { | |
const c = Math.cos(rads); | |
const s = Math.sin(rads); | |
return [c, 0, -s, 0, 0, 1, 0, 0, s, 0, c, 0, 0, 0, 0, 1]; | |
} | |
static rotateZ(rads: number) { | |
const c = Math.cos(rads); | |
const s = Math.sin(rads); | |
return [c, s, 0, 0, -s, c, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]; | |
} | |
static identity(): number[] { | |
return [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]; | |
} | |
static combine(matrixes: number[][]): number[] { | |
let result = matrixes[0]; | |
for (let i = 1; i < matrixes.length; i++) { | |
result = m4.multiply(result, matrixes[i]); | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment