TL;DR
This blog post about isometric projection math and code.
From 2D
SR45⁰
Rotate
45 ° = π 4 45\degree = \frac{\pi}{4} 45° = 4 π
and scale vertical direction with
0.577 ≈ t a n ( 30 ° ) = t a n ( π 6 ) = 3 3 0.577 \approx tan(30\degree) = tan(\frac{\pi}{6}) = \frac{\sqrt 3}{3} 0.577 ≈ t an ( 30° ) = t an ( 6 π ) = 3 3
.
Preview
Math
a = π / 4 s = t a n ( π / 6 ) f ( x , y ) = [ x cos ( a ) − y sin ( a ) ( x sin ( a ) + y cos ( a ) ) s ]
\begin{aligned}
a &= \pi/4\\
s &= tan(\pi/6)\\
f(x,y) &= \begin{bmatrix}
x \cos(a) - y \sin(a)\\
(x \sin(a) + y \cos(a)) s
\end{bmatrix}
\end{aligned}
a s f ( x , y ) = π /4 = t an ( π /6 ) = [ x cos ( a ) − y sin ( a ) ( x sin ( a ) + y cos ( 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 ,
];
}
Enter fullscreen mode
Exit fullscreen mode
SSR30⁰
Scale vertical direction with
0.866 ≈ cos ( 30 ° ) = cos ( π 6 ) = 3 2 0.866 \approx \cos(30\degree) = \cos(\frac{\pi}{6}) = \frac{\sqrt 3}{2} 0.866 ≈ cos ( 30° ) = cos ( 6 π ) = 2 3
, Skew horizontal direction
± 30 ° \pm30\degree ± 30°
, Rotate
± 30 ° \pm30\degree ± 30°
.
Preview
Math
s = cos ( π / 6 ) M s c a l e ( x , y ) = [ 1 0 0 s ] [ x y ] = [ x y s ] M s k e w ( x , y , θ ) = [ 1 tan ( θ ) 0 1 ] [ x y ] = [ x + y tan ( θ ) y ] M r o t a t e ( x , y , θ ) = [ cos ( θ ) − sin ( θ ) sin ( θ ) cos ( θ ) ] [ x y ] = [ x cos ( θ ) − y sin ( θ ) x sin ( θ ) + y cos ( θ ) ]
\begin{aligned}
s &= \cos(\pi/6)\\
M_{scale}(x,y) &= \begin{bmatrix}
1 & 0\\
0 & s
\end{bmatrix}
\begin{bmatrix}
x\\
y
\end{bmatrix}\\
&= \begin{bmatrix}
x\\
y s
\end{bmatrix}\\
M_{skew}(x,y, \theta) &= \begin{bmatrix}
1 & \tan(\theta)\\
0 & 1
\end{bmatrix}
\begin{bmatrix}
x\\
y
\end{bmatrix}\\
&= \begin{bmatrix}
x + y \tan(\theta)\\
y
\end{bmatrix}\\
M_{rotate}(x,y,\theta) & = \begin{bmatrix}
\cos(\theta) & -\sin(\theta) \\
\sin(\theta) & \cos(\theta)
\end{bmatrix}
\begin{bmatrix}
x \\
y
\end{bmatrix} \\
& = \begin{bmatrix}
x \cos(\theta) - y \sin(\theta) \\
x \sin(\theta) + y \cos(\theta)
\end{bmatrix}
\end{aligned}
s M sc a l e ( x , y ) M s k e w ( x , y , θ ) M ro t a t e ( x , y , θ ) = cos ( π /6 ) = [ 1 0 0 s ] [ x y ] = [ x ys ] = [ 1 0 tan ( θ ) 1 ] [ x y ] = [ x + y tan ( θ ) y ] = [ cos ( θ ) sin ( θ ) − sin ( θ ) cos ( θ ) ] [ x y ] = [ x cos ( θ ) − y sin ( θ ) x sin ( θ ) + y cos ( θ ) ]
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 ),
];
}
Enter fullscreen mode
Exit fullscreen mode
From 3D
Projection
You can directly project 3D point to 2D point and swap the up axis easily.
Preview
Math
a = π / 6 P y ( x , y , z ) = [ ( x − z ) cos ( a ) ( x + z ) sin ( a ) + y ] P z ( x , y , z ) = [ ( x − y ) cos ( a ) ( x + y ) sin ( a ) + z ]
\begin{aligned}
a &= \pi/6\\
P_{y}(x,y,z) &= \begin{bmatrix}
(x - z) \cos(a)\\
(x + z) \sin(a) + y
\end{bmatrix}\\
P_{z}(x,y,z) &= \begin{bmatrix}
(x - y) \cos(a)\\
(x + y) \sin(a) + z
\end{bmatrix}
\end{aligned}
a P y ( x , y , z ) P z ( 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 ];
}
Enter fullscreen mode
Exit fullscreen mode
References
Top comments (0)