I'm dealing with objects on 15x15 chunks in my game.

I'd like the chunks to rotate, for example, rotation version 0 is the original position of 2,5;

(EDIT: Oops, rotation 2 and 3 names need switching in the image) enter image description here

How can I get the other positions in the simplest and quickest way? It only needs to rotate in 90 degrees increments, having 4 version (so 0, 90, 180, 270)

  • \$\begingroup\$ Simply drawing it like you have with concrete numbers is already halfway to figuring the formula out yourself. From there you need to identify how one number changes from one rotation to the next. For example: Based on the image you can see in rotation 0 the red square is 5 positions from the top (y), but in rotation 1 it's 5 positions from the right (x). So you need to define the new x in terms of the old y. Putting this in more mathematical terms, we go from y=5 to x=14-5 or x_new=14-y_old. Do something similar for y_new and double-check it and you have the answer. \$\endgroup\$ Commented Oct 29, 2020 at 10:14
  • \$\begingroup\$ Note that it can be an XY problem, depending on the complexity of your operations on elements and what you want to do with them, it can be much easier to adapt the loop that displays the grid rather than changing each element's coordinates every time. for example if for(x = 0; x <= n - 1; x++){ for(y = 0; y <= n - 1; y++){ plot(x,y) } } displays Rotation 0, then for(y = n - 1; y >= 0; y--){ for(x = 0; x <= n - 1; x++){ plot(x,y) } } will be Rotation 1. (Basically you'll notice the changes to the loops are similar to what you get in the accepted answer) \$\endgroup\$
    – Kaddath
    Commented Oct 29, 2020 at 15:16

3 Answers 3


If horizontal axis is x, vertical is y, and chunk size is n, then clockwise rotation can be performed as follows:

(x2, y2) = (n - 1 - y1, x1)

and counter-clockwise rotation would be:

(x2, y2) = (y1, n - 1 - x1)
  • 2
    \$\begingroup\$ For completion sake you may want to include the function for rotation counter clockwise. \$\endgroup\$ Commented Oct 29, 2020 at 8:46

If you number the grid from the middle as (0,0), rather than the edge, rotating becomes as simple as

(x2, y2) = (-y1, x1)

You can still provide the grid to your player with conventional numbering, of course.


If you use Weckar's suggestion to number the grid with (0,0) in the center, this is specialized case of the general rotation formula. You could get any rotation using these equations

new_x = old_x * cos(angle) - old_y * sin(angle)
new_y = old_x * sin(angle) + old_y * cos(angle)

In your case, angle can only be 0, 90, 180, and 270, which means the sins and cosines can only produce values of -1, 0, and +1.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .