# Rotation of indices of a 2d array by 90 degrees

I know how to rotate an entire 2d array by 90 degrees around the center(My 2d array lengths are always odd numbers), but I need to find an algorithm that rotates specific indices of a 2d array of known length. For example I know that the 2d array is a 17 by 17 grid and I want the method to rotate the indices [4][5] around the center by 90 degrees and return the new indices as two separate ints(y,x); Please point me in the right direction or if your feeling charitable I would very much appreciate some bits of code - preferably in java. Thanks!

-
If you know how to rotate a whole array, you need to know how to rotate individual points. Am I missing something? –  Peter Lawrey Aug 21 '12 at 18:57
@PeterLawrey yes, you are. Rotating a whole (square) array can be done with simple operations on entire rows or columns. –  Alnitak Aug 21 '12 at 18:59
@Alnitak and to do that you have to move each point, point by point. –  Peter Lawrey Aug 21 '12 at 19:07
@PeterLawrey kind of - conceptually doing the whole array is probably simpler. I'm working on it. –  Alnitak Aug 21 '12 at 19:11
@PeterLawrey OK, I worked it out. The whole array based solution leads to a more natural solution than one based on normal geometric transformations, but they do (of course) degenerate to the same formulae. –  Alnitak Aug 21 '12 at 19:32

Assuming cartesian coordinates (i.e. `x` points right, and `y` points up) and that your coordinates are in the form `array[y][x]` the center [cx, cy] of your 17x17 grid is [8, 8].

Calculate the offset [dx, dy] of your point [px, py] being [4, 5] from there, i.e. [-4, -3]

For a clockwise rotation, the new location will be [cx - dy, cy + dx]

If your array uses the Y axis pointing "downwards" then you will need to reverse some of the signs in the formulae.

For a non-geometric solution, consider that the element [0][16] needs to get mapped to [16][16], and [0][0] mapped to [0][16]. i.e. the first row maps to the last column, the second row maps to the second last column, etc.

If `n` is one less than the size of the grid (i.e. 16) that just means that point `[y][x]` will map to `[x][n - y]`

In theory, the geometric solution should provide the same answer - here's the equivalence:

``````n = 17 - 1;

c = n / 2;

dx = x - c;
dy = y - c;

nx = c - dy = c - (y - c) = 2 * c - y = n - y
ny = c + dx = c + (x - c) = x
``````

-
I was just typing another question addressing this post, but the last edit answered it :). Ill implement this and give you feedback/accept answer in a few. Thanks so much for your input! –  John Aug 21 '12 at 19:47
excellent, works very nicely :) thanks again! –  John Aug 21 '12 at 20:33

If you have a square array with `N` elements in each row/column a 90deg turn anti-/counter-clockwise sends `(x,y)` to `(N+1-y,x)` doesn't it ?

That is, if like me, you think that the top-left element in a square array is `(1,1)` and row numbers increase down and column numbers to the right. I guess someone who counts from `0` will have to adjust the formula somewhat.

-

The point in Cartesian space `x,y` rotated 90 degrees counterclockwise maps to `-y,x`.

An array with N columns and M rows would map to an array of M columns and N rows. The new "x" index will be non-positive, and will be made zero-based by adding M:

`a[x][y]` maps to `a[M-y][x]`

-
only if you're using cartesian coordinates and rotating around the origin. –  Alnitak Aug 21 '12 at 19:10
Wasn't that what the OP asked for? –  John Aug 21 '12 at 19:12
Try rotating `a[1][0]` to `a[0][-1]` ;) –  Peter Lawrey Aug 21 '12 at 19:13
Rotate ccw 90 degrees three times: 1,0 -> 0,1 -> -1,0 -> 0,-1, or cw once: 1,0 -> 0,-1. I guess I'm completely missing the point of the question? –  John Aug 21 '12 at 19:18
Oh ... you still need the indices to be valid, correct? Not just the location of the points in cartesian space? –  John Aug 21 '12 at 19:21
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0}