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!
Share
Assuming cartesian coordinates (i.e.
xpoints right, andypoints up) and that your coordinates are in the formarray[y][x]the center [cx, cy] of your 17×17 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
nis 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: