Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

 
Advanced search

1075929 Posts in 44152 Topics- by 36119 Members - Latest Member: Royalhandstudios

December 29, 2014, 04:01:15 PM
TIGSource ForumsDeveloperTechnical (Moderators: Glaiel-Gamer, ThemsAllTook)Rotating a non-square 2D Array
Pages: [1]
Print
Author Topic: Rotating a non-square 2D Array  (Read 386 times)
Mattivc
Level 0
***

Level 21 Viking Programmer


View Profile WWW Email
« on: November 14, 2012, 03:38:22 PM »

Ok, so i have a 2D array of data. Well, actually is a 1D array for technical reasons. But it its being used as a 2D by doing myArray[xPos+yPos*width].

What i am trying to is creating a method for allowing me to rotate the array 90, 180 or 270 degrees.

So for example:
[01][02][03]
[04][05][06]
[07][08][09]
[10][11][12]

Would become:
[10][07][04][01]
[11][08][05][02]
[12][09][06][03]

Doing this is trivial for a square array, problem is i need it to work for non-square array as well. I have been trying to figure it for myself and searching for a solution for a while now without luck. Any of you geniuses who got a solution or can point me in the right direction?

Also, as this is really a 1D array being used it shouldn't be absolutely necessary to create a new array, and copy the data to it. As it would be using a real 2D, because the width and height changes. And instead just move around the data in the existing array. Al tough that is just a bonus to settle my optimization OCD and not absolutely necessary for me to solve my problem.
Logged
Polly
Level 4
****


View Profile
« Reply #1 on: November 14, 2012, 04:48:38 PM »

Add a rotation parameter to your array interface so you can simply use a different address calculation for each of 4 angles.

Code:
switch(rotation)
{
  case   0: return myArray[x+y*w];
  case  90: return myArray[(h-x-1)*w+y];
  case 180: return myArray[w-x-1+(h-y-1)*w];
  case 270: return myArray[x*w+w-y-1];
}
Logged
Mattivc
Level 0
***

Level 21 Viking Programmer


View Profile WWW Email
« Reply #2 on: November 16, 2012, 02:55:18 AM »

This works fine for square arrays, but i need to have it work for non-square arrays as well.
Logged
powly
Level 3
***



View Profile WWW
« Reply #3 on: November 16, 2012, 03:18:58 AM »

His version does - it just uses w and h differently for different angles.
Logged
Polly
Level 4
****


View Profile
« Reply #4 on: November 16, 2012, 03:33:39 AM »

His version does - it just uses w and h differently for different angles.

Exactly Smiley
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic