TL;DR
This blog post about isometric projection math and code.
From 2D
SR45⁰
Rotate
45°=4π
and scale vertical direction with
0.577≈tan(30°)=tan(6π)=33
.
Preview
Math
asf(x,y)=π/4=tan(π/6)=[xcos(a)−ysin(a)(xsin(a)+ycos(a))s]
Code
const ANGLE = Math.PI / 4;
const SCALE = Math.tan(Math.PI / 6);
function sr45(x: number, y: number): [number, number] {
return [
x * Math.cos(ANGLE) - y * Math.sin(ANGLE),
(x * Math.sin(ANGLE) + y * Math.cos(ANGLE)) * SCALE,
];
}
SSR30⁰
Scale vertical direction with
0.866≈cos(30°)=cos(6π)=23
, Skew horizontal direction
±30°
, Rotate
±30°
.
Preview
Math
sMscale(x,y)Mskew(x,y,θ)Mrotate(x,y,θ)=cos(π/6)=[100s][xy]=[xys]=[10tan(θ)1][xy]=[x+ytan(θ)y]=[cos(θ)sin(θ)−sin(θ)cos(θ)][xy]=[xcos(θ)−ysin(θ)xsin(θ)+ycos(θ)]
Code
const ANGLE = Math.PI / 6;
const SCALE = Math.cos(ANGLE);
function scale(x: number, y: number): [number, number] {
return [x, y * SCALE];
}
function skew(x: number, y: number, angle: number): [number, number] {
return [x + y * Math.tan(angle), y];
}
function rotate(x: number, y: number, angle: number): [number, number] {
return [
x * Math.cos(angle) - y * Math.sin(angle),
x * Math.sin(angle) + y * Math.cos(angle),
];
}
From 3D
Projection
You can directly project 3D point to 2D point and swap the up axis easily.
Preview
Math
aPy(x,y,z)Pz(x,y,z)=π/6=[(x−z)cos(a)(x+z)sin(a)+y]=[(x−y)cos(a)(x+y)sin(a)+z]
Code
const ANGLE = Math.PI / 6;
function upY(x: number, y: number, z: number): [number, number] {
return [(x - z) * Math.cos(ANGLE), (x + z) * Math.sin(ANGLE) + y];
}
function upZ(x: number, y: number, z: number): [number, number] {
return [(x - y) * Math.cos(ANGLE), (x + y) * Math.sin(ANGLE) + z];
}
References
Top comments (0)