2D array manipulation

Oct 17, 2013 at 7:11am
I would like to do array manipulation like the following example.

Rotation 90 degree
a b
c d

becomes

c a
d b

Mathematically, just swap the x and y coordinate and put a - sign with x coordinate like 1,1--> -1,1.
But in terms of array, there is no negative index and starts from 0 so how can i deal with this operation?
Also the operation is accumulated like if i rotate it 4 times then the output is the same as the initial one.
Any hints or help would be appreciated.
Last edited on Oct 17, 2013 at 7:12am
Oct 17, 2013 at 7:36am

Mathematically, just swap the x and y coordinate

"Swap x and y" is not "mathematically". Mathematically it is multiplication by rotation matrix. And I am not sure this helps you in rotating big rectangular matrix.

However for arrays things are simpler. Imagine array of size 100*100. Write down coordinates of its corner elements. Then write which coordinates these elements will have after rotation.

This should give you satisfactory clue.
Last edited on Oct 17, 2013 at 7:37am
Oct 17, 2013 at 10:44am
Umm.....i still cannot get what you mean
The corner x and y coordinate originally and after clockwise rotation are
0,0-->0,99
0,99-->99,99
up to this part, swapping and the y coordinate+99
99,99-->99,0
99,0-->0,0
but here swapping and the y coordinate -99

Also in the inner part like array[20][60], follow those equation will output a value which is over 99 or under 0


Can i do in this way?
1.swapping
2.x coordinate - 99
3.x coordinate* -1
Last edited on Oct 17, 2013 at 10:54am
Oct 17, 2013 at 10:56am
Notice that column { a, c, e, g } converts into row { g, e, c, a }. 'c' is the second character. In the result 'c' is second from end.

Does i convert to "ith from end" by adding or subtracting 99?
Oct 17, 2013 at 11:40am

Can i do in this way?

You can simply flip array around horizontal axis and then around main diagonal. But this takes two operations.


0,0-->0,99
0,99-->99,99
up to this part, swapping and the y coordinate+99
99,99-->99,0
99,0-->0,0
but here swapping and the y coordinate -99

You are close, though you misscalculate something.

Try represent this as:

xNew = A*xOld + B*yOld + U
yNew = C*xOld + D*yOld + V

where A, B, C, D should be chosen among 1, 0 or -1. Make your numbers fill these formulas correctly. You should get something really simple.

http://en.wikipedia.org/wiki/Rotation_matrix
Topic archived. No new replies allowed.