PDA

View Full Version : 2D Matrix Rotation



Art
- 15th February 2010, 03:07
Hi Guys,
Does anyone have a 2D matrix rotation algo they'd like to share?
I'd like to rotate all bits in a byte array so:

byte0: 00001110
byte1: 00001111
byte2: 00001111
byte3: 11111111
byte4: 11111111
byte5: 00001111
byte6: 00001111
byte7: 00001110

becomes:

byte0: 01111110
byte1: 11111111
byte2: 11111111
byte3: 11111111
byte4: 00011000
byte5: 00011000
byte6: 00011000
byte7: 00011000

I can do this if I have a list of coordinates like this:
x_new = y_old
y_new = 1 - (x_old - (8 - 2))
Do this for every coordinate (64 times) and the result is what I want.
Art.

Kamikaze47
- 15th February 2010, 06:53
off the top of my head this is what i came up with (untested):


array var byte[8]
array_rotated var byte[8]
bit_num var byte
byte_num var byte
temp var byte
result var byte

for byte_num=0 to 7
for bit_num=0 to 7
temp=array[7-bit_num]
temp=temp>>byte_num
result=result<<1
result.0=temp.0
next bit_num
array_rotated[byte_num]=result
next byte_num


*edit* fixed some mistakes
*edit* fixed one last mistake

Art
- 15th February 2010, 11:35
I did come up with a way using a very verbose drawn out method that referenced every bit,
and copies one array to the other, but this looks much neater :) I'll give it a try, thanks!

Kamikaze47
- 15th February 2010, 17:12
If it works, you can repay me by sending me some of those DSE component racks you were bragging about on OCAU. Hehe j/k.

Art
- 15th February 2010, 22:16
It's a small world :)

There was a little more to it than bragging.
I was trying to find interested people to get together to possibly buy the next lot in a few months.
But I also try to show off a bit ;)
Cheers, Art.

Kamikaze47
- 16th February 2010, 03:29
*wonders if any of the Perth DSE stores are willing to flog theirs off too*

:)

Ioannis
- 16th February 2010, 08:44
Nice routine. How about an angle turn and not exactly 90 degrees?

I mean, to rotate an object live?

Ioannis

Art
- 16th February 2010, 12:27
Nice routine. How about an angle turn and not exactly 90 degrees?

I mean, to rotate an object live?

Ioannis

Then you'd be needing this:
http://www.picbasic.co.uk/forum/showthread.php?t=10528
For the trig functions.

Ioannis
- 16th February 2010, 12:34
Ooh, yes. I think I would.

Thanks a lot.
Ioannis