Published
Edited
Sep 21, 2019
1 star
Insert cell
Insert cell
Insert cell
vector = [100, 50, 1]
Insert cell
Insert cell
Insert cell
Insert cell
matrix = [
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
]
Insert cell
Insert cell
function multiply(matrix, vector) {
return [
matrix[0][0] * vector[0] + matrix[0][1] * vector[1] + matrix[0][2] * vector[2],
matrix[1][0] * vector[0] + matrix[1][1] * vector[1] + matrix[1][2] * vector[2],
matrix[2][0] * vector[0] + matrix[2][1] * vector[1] + matrix[2][2] * vector[2],
]
}
Insert cell
Insert cell
multiply(matrix, vector)
Insert cell
Insert cell
Insert cell
identityMatrix = [
[1, 0, 0],
[0, 1, 0],
[0, 0, 1]
]
Insert cell
multiply(identityMatrix, vector)
Insert cell
Insert cell
Insert cell
function translationMatrix(dx, dy) {
return [
[1, 0, dx],
[0, 1, dy],
[0, 0, 1]
];
}
Insert cell
Insert cell
Insert cell
Insert cell
multiply(translationMatrix(dx, dy), vector)
Insert cell
Insert cell
Insert cell
Insert cell
function scalingMatrix(sx, sy) {
return [
[sx, 0, 0],
[ 0, sy, 0],
[ 0, 0, 1],
];
}
Insert cell
Insert cell
Insert cell
Insert cell
cornerVectors = [
[-100, -50, 1],
[ 100, -50, 1],
[ 100, 50, 1],
[-100, 50, 1],
]
Insert cell
scaledCornerVectors = cornerVectors.map(v => multiply(scalingMatrix(sx, sy), v))
Insert cell
Insert cell
Insert cell
function rotationMatrix(theta) {
return [
[Math.cos(theta), -Math.sin(theta), 0],
[Math.sin(theta), Math.cos(theta), 0],
[0, 0, 1],
]
}
Insert cell
Insert cell
Insert cell
rotatedCornerVectors = cornerVectors.map(v => multiply(rotationMatrix(theta), v))
Insert cell
Insert cell
Insert cell
Insert cell
function multiplyMatrix(a, b) {
return [
[
(a[0][0]*b[0][0]) + (a[0][1]*b[0][1]) + (a[0][2]*b[0][2]),
(a[0][0]*b[1][0]) + (a[0][1]*b[1][1]) + (a[0][2]*b[1][2]),
(a[0][0]*b[2][0]) + (a[0][1]*b[2][1]) + (a[0][2]*b[2][2]),
],
[
(a[1][0]*b[0][0]) + (a[1][1]*b[0][1]) + (a[1][2]*b[0][2]),
(a[1][0]*b[1][0]) + (a[1][1]*b[1][1]) + (a[1][2]*b[1][2]),
(a[1][0]*b[2][0]) + (a[1][1]*b[2][1]) + (a[1][2]*b[2][2]),
],
[
(a[2][0]*b[0][0]) + (a[2][1]*b[0][1]) + (a[2][2]*b[0][2]),
(a[2][0]*b[1][0]) + (a[2][1]*b[1][1]) + (a[2][2]*b[1][2]),
(a[2][0]*b[2][0]) + (a[2][1]*b[2][1]) + (a[2][2]*b[2][2]),
],
]
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
transformationMatrix = multiplyMatrix(
translationMatrix(translateX, translateY),
rotationMatrix(rotateTheta),
)
Insert cell
Insert cell
transformedCornerVectors = cornerVectors.map(v => {
return multiply(transformationMatrix, v);
})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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