rotating a 3x3 array

BlitzMax Forums/BlitzMax Programming/rotating a 3x3 array

Ghost Dancer(Posted 2007) [#1]
I have a 3x3 array which holds various templates for some level data:

block[3, 3]


The array just holds integers to represent wall, floor etc. which are copied into my level map.

Before I copy the data into my level, I want to rotate it in increments of 90 degrees. I'm pretty sure there is a simple way of doing this but my maths is a little rusty.

Does anyone know how to do this?


rockford(Posted 2007) [#2]
I did this for a 15x15 grid array in my Guru Logic Remake.

I had two arrays - one that held the data (map[]), and the other I used to actually rotate and display it (cmap[]).

This is just a small portion of the code. There are probably a lot more efficient ways of doing it (bit-shifting? etc.) but it worked for me, first time.

Every time you rotate by 90 degrees, update the "ROTATE" variable (+ or -).

 
 For x=0 To 15
 For y=0 To 15

  If rotated=0
   cmap[x,y]=map[x,y]
  EndIf
  
  If rotated=1
	cmap[x,y]=map[y,15-x]
  EndIf

  If rotated=2
	cmap[x,y]=map[15-x,15-y]
  EndIf

  If rotated=3
	cmap[x,y]=map[15-y,x]
  EndIf

 Next
 Next



Torrente(Posted 2007) [#3]
This should work, although I don't have blitz with me to test.

n:int = 3 'dimension of square array

for x:int = 0 to n-1
   for y:int = 0 to n-1
      newArray[n-1-y, x] = block[x, y]
   next
next




Ghost Dancer(Posted 2007) [#4]
Thanks to you both - that's exactly what I was after :)