Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm working on building tetris now in Java and am at the point of rotations...

I originally hardcoded all of the rotations, but found that linear algebra (matrix rotations) was the better way to go.

I'm trying to use a rotation matrix to rotate my pieces, and found I need a good understanding of trigonometry.

I don't understand how $R(90^\circ)$ equals a rotation matrix of $R(-\theta)$ =` $$\begin{bmatrix}cos\theta &sin\theta \\-sin\theta & cos\theta\end{bmatrix}$$, aka $$\begin{bmatrix} 0 & 1 \\ -1& 0 \end{bmatrix}$$ (btw, if you know how to make a matrix on stackoverflow, please let me know). How does that equal a 90 degree rotation? Where do those zeros and ones come from? Moreover, how would that translate into code? I'm not looking for someone to code it for me, I'm looking for a concept with maybe a snippet of pseudocode.

I'm trying to visualize it with this drawing by putting the grid and tiles on a graph and drawing out the angles... but still don't understand it.

enter image description here

Can anyone help?



enter image description here

share|cite|improve this question
For some basic information about writing math at this site see e.g. here, here, here and here. Particularly the first answer to the question in the first link will help you typeset matrices. – Lord_Farin Apr 16 '13 at 18:09
Very helpful! Thanks – Growler Apr 16 '13 at 18:17
Your rotation matrix is a vector space rotation matrix and you want an affine transformation, not a linear one. – xavierm02 Apr 16 '13 at 18:23
Since you're looking as much for coding help as for mathematics help, I'd suggest that you start at gamedev.SE; unless you're physically rotating pieces for display (e.g., you expect to have a piece rotated by $45^\circ$ at some point), then hard-coding is almost certainly better than rotation matrices, but that's more a gamedev discussion than a mathematical one. – Steven Stadnicki Apr 16 '13 at 19:06
@StevenStadnicki Well my goal is to 1) Learn some useful math, 2) I want to code a function that can handle the rotation of any shape... so I want to stay away from hardcoding rotations for 7 shapes. – Growler Apr 16 '13 at 19:15
up vote 1 down vote accepted

The operations you are wanting to perform can be easily implemented without the need of rotation matrices since the transforms you desire are limited to right angle rotations and mirroring.

Assuming $B$ is a two dimensional array representing a block and $B[i,j]$ is the $i^{\text{th}}$ row and $j^{\text{th}}$ column of the block taking on values of 1 for a single block segment and 0 for an empty space.

The first operation you need is a transpose which is effectively a 90 degree clockwise rotation:

B T(B b)
    B b' <- new B[b.columns, b.rows]

    for i = 0 to b.rows - 1
        for j = 0 to b.columns -1
            b'[j,i] <- b[i, j]

    return b'

The second operation is mirroring a block about the y-axis:

B Y(B b)
    B b' <- new B[b.rows, B.columns]

    for i = 0 to b.rows - 1
        for j = 0 to b.columns - 1
            b'[i,j] <- b[i, b.columns - 1 - j]

    return b'

And finally, mirroring a block about the x-axis:

B X(B b)
    B b' <- new B[b.rows, B.columns]

    for i = 0 to b.rows - 1
        for j = 0 to b.columns - 1
            b'[i,j] <- b[b.rows - 1 - i, j]

    return b'

Given a block $B$ in a neutral position, a 90 degree clockwise rotation is $T(B)$, 180 is $T(X(T(B)))$, 270 is $Y(T(B))$ and 360 is simply $B$. counter clockwise rotations are just 360 minus the complementing clockwise rotations.

Also, as @StevenStadnicki pointed out in the comments, you can hard code much of this and you will find that if you are replicating the original Tetris, you'll encounter more that requires a hard coded solution than not.

share|cite|improve this answer
Okay, but that's how I started off doing it (see here:, but it seemed pretty hairy to have to calculate each rotation for each block for either left or right rotations... unless I'm doing it wrong? – Growler Apr 18 '13 at 14:23
ahh... I finally understand why you were mirroring it... Once you transpose it... you have your 0 degree and 90 degree rotation... then you can mirror both of those to get your missing 2 rotations (180 and 270 degree). Clever... haha – Growler Apr 18 '13 at 15:32
Is my graph above the correct depiction of what you've said? – Growler Apr 18 '13 at 17:37
Yes, your graph is correct. I was mistaken however when I said the transpose would rotate clockwise, as implemented, it will rotate counter clockwise. Also note that $T(X(T(B)))$ can be simplified to just $X(B)$. So the final clockwise rotation of 0, 90, 180, 270 degree is $B$, $Y(T(B))$, $X(T)$, $T(B)$ respectively. – GEL Apr 19 '13 at 13:52
Okay I'll give that a try. Thanks! – Growler Apr 19 '13 at 13:54

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.