Public
Edited
Nov 29, 2023
Insert cell
Insert cell
identity = [1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1]
Insert cell
point = [4, 3, 2, 1]
Insert cell
point_obj = ({x: 4, y: 3, z: 2, w: 1})
Insert cell
someMatrix = [1, 1, 2, 2,
1, 1, 0, 0,
3, 3, 1, 0,
4, 0, 4, 1]
Insert cell
translatedPoint = multiplyMatrixAndPoint(translation1, point)
Insert cell
translation1 = translationMatrix(5, 10, 15)
Insert cell
multiplyMatrixAndPoint(identity, point)
Insert cell
multiplyMatrixAndPoint(scale1, point)
Insert cell
multiplied = multiplyMatrices(someMatrix, someMatrix)
Insert cell
scale1 = scaleMatrix(2, 2, 2)
Insert cell
rotatedz = multiplyMatrixAndPoint(rotationz1, [1, 0, 0, 1])
Insert cell
rotationz1 = rotationZMatrix(45)
Insert cell
scaleMatrix = (w, h, l) => [w, 0, 0, 0,
0, h, 0, 0,
0, 0, l, 0,
0, 0, 0, 1]
Insert cell
translationMatrix = (x, y, z) => [1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
x, y, z, 1]
Insert cell
rotationXMatrix = (a) => [1, 0, 0, 0,
0, c(a), -s(a), 0,
0, s(a), c(a), 0,
0, 0, 0, 1]
Insert cell
rotationYMatrix = (a) => [c(a), 0, s(a), 0,
0, 1, 0, 0,
-s(a), 0, c(a), 0,
0, 0, 0, 1]
Insert cell
rotationZMatrix = (a) => [c(a), -s(a), 0, 0,
s(a), c(a), 0, 0,
0, 0, 1, 0,
0, 0, 0, 1]
Insert cell
c = (a) => Math.cos(a * 2 * Math.PI / 360)
Insert cell
s = (a) => Math.sin(a * 2 * Math.PI / 360)
Insert cell
multiplyMatrixAndPoint = (matrix, point) => {
let rows = [matrix.slice(0, 4), matrix.slice(4, 8), matrix.slice(8, 12), matrix.slice(12, 16)]
let columns = Array.from({length: 4}, (v, i) => [rows[0][i], rows[1][i], rows[2][i], rows[3][i]])
let result = columns.map((v, i) => point[0]*v[0] + point[1]*v[1] + point[2]*v[2] + point[3]*v[3])
//return {matrix: matrix, point: point, rows: rows, columns: columns, result: result}
return result
}
Insert cell
multiplyMatrices = (matrix0, matrix1) => {
let rows1 = [matrix1.slice(0, 4), matrix1.slice(4, 8), matrix1.slice(8, 12), matrix1.slice(12, 16)]
let resultRows = rows1.map((v, i) => multiplyMatrixAndPoint(matrix0, v))
let result = resultRows.reduce((acc, cur) => [...acc, ...cur])
//return {matrix0, matrix1, rows1, resultRows, result}
return result
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more