# Is it possible to rotate an array?

### » Sat Jul 30, 2011 4:32 am

In my level editor you can select any amount of tiles from a tileset by drawing over them. The data is saved to an array and loaded each time I place some tiles on the grid. I know how to set the angle of the individual tiles, but I want to be able to rotate groups of tiles - angle and position, just like rotating tetrominoes in tetris. To do this I'd need to rotate the array itself by 90 degree increments. Any idea how I'd do that?
B
254
S
32
G
13
Posts: 1,796
Reputation: 19,272

### » Sat Jul 30, 2011 4:57 am

Should be possible using two identical sized array objects and a nested for loop. Exact syntax would depend on the size of your arrays, and obviously these arrays could only rotate in 90 degree increments.

It would look something like this for a 90 degree rotation clockwise:

[code]
int CurrentRow = 0;
int CurrentColumn = HIGHY;

for ( int y = 0;y<=HIGHY;y++ )
{
for ( int x = 0;x<=HIGHX;x++ )
{
CurrentRow++;
DestinationArray[CurrentRow][CurrentColumn] = SourceArray[x][y];
}
CurrentRow = 0;
CurrentColumn--;
}
[/code]

It's in C++, but it should be easily convertible to event structure.Arsonide2011-07-30 05:06:25
B
5
S
2
G
5
Posts: 221
Reputation: 2,272

### » Sat Jul 30, 2011 5:33 am

<- is retarded when it comes to code.

Any chance you can write that out in events or pseudo code for me? I'd really appreciate it.
B
254
S
32
G
13
Posts: 1,796
Reputation: 19,272

### » Sat Jul 30, 2011 6:30 am

That may not be exactly correct, as some things in Construct are base 0 and some things are base 1, but it's close. If it has any problems they will be along the outer borders of the array, along index 0 and along the maximums. Rotating other directions should be a similar process with some +'s and -'s changed around.Arsonide2011-07-30 06:32:38
B
5
S
2
G
5
Posts: 221
Reputation: 2,272

### » Sat Jul 30, 2011 10:58 am

I'm sorry, but this is not correct. Apart from the 1-based index issue, that code would mirror vertically on the horizontal axis. But Tokinsom wants to rotate in 90 degree steps.

Here is an example. Click on the sprites to color them then use the buttons to rotate clockwise or counterclockwise. Of course, you would need to integrate offsets if you want to rotate only a part of the array.

Feel free to ask, if it troubles you.

http://www.mediafire.com/file/mz0cv4sm5x7bt5j/rotatingarray.cap

EDIT: Ups, there was an redundant event, causing the array to be calculated two times. New link.tulamide2011-07-30 12:17:22
B
24
S
8
G
10
Posts: 1,821
Reputation: 8,289

### » Sat Jul 30, 2011 2:19 pm

I realize you just need 90 degrees rotations, but for added effect, you can use any angle doing a matrix transformation using S.xoffrot(,,,) from "S". I'll try to do an example later.
Btw I think "S" has a built in function to rotate its arrays as well.
B
178
S
50
G
206
Posts: 8,685
Reputation: 127,713

### » Sat Jul 30, 2011 8:17 pm

[QUOTE=newt]Btw I think "S" has a built in function to rotate its arrays as well.[/QUOTE]

I thought something did, but I couldn't remember what. I think it was S.
B
5
S
2
G
5
Posts: 221
Reputation: 2,272

### » Sat Jul 30, 2011 8:29 pm

Agh it seems the 'S' Plugin is the answer to all my questions but I'm still hesitating to learn it ^^; Plus I'd basically have to start over on my level editor..and all the "snippets" in the S tutorials are obfuscated due to old quote boxes not working on the new forums!

Anyway, thanks for all the help guys. Tulamide, I'll try out your example in a bit!Tokinsom2011-07-30 20:31:20
B
254
S
32
G
13
Posts: 1,796
Reputation: 19,272

### » Sat Jul 30, 2011 9:12 pm

[QUOTE=Arsonide] [QUOTE=newt]Btw I think "S" has a built in function to rotate its arrays as well.[/QUOTE]

I thought something did, but I couldn't remember what. I think it was S.[/QUOTE]

Again, I'm sorry, but this is not quite right. The function indeed is called rotate array, but it shifts a 1-dimensional array to the right or to the left (aka the equivalent to moving, not angular rotation), so

ABCD

rotated by 1 gets

DABC

or rotated by -1 gets

BCDA

Also, when using any free rotional functions, wether from 's' or just sin/cos, you can't make sure you will always keep all values on the grid (because of the rounding, that needs to be applied), leading to overwriting or doubling values.
B
24
S
8
G
10
Posts: 1,821
Reputation: 8,289

### » Sat Jul 30, 2011 10:39 pm

[QUOTE=tulamide] [QUOTE=Arsonide] [QUOTE=newt]Btw I think "S" has a built in function to rotate its arrays as well.[/QUOTE]

I thought something did, but I couldn't remember what. I think it was S.[/QUOTE]

Again, I'm sorry, but this is not quite right. The function indeed is called rotate array, but it shifts a 1-dimensional array to the right or to the left (aka the equivalent to moving, not angular rotation), so

ABCD

rotated by 1 gets

DABC

or rotated by -1 gets

BCDA[/QUOTE]

Ahh well thats ok. Thats the only type of array I can do in S :P

[QUOTE=tulamide]Also, when using any free rotional functions, wether from 's' or just sin/cos, you can't make sure you will always keep all values on the grid (because of the rounding, that needs to be applied), leading to overwriting or doubling values.
[/QUOTE]
True, but once it gets to 90 degrees it should line back up, shouldn't it?
B
178
S
50
G
206
Posts: 8,685
Reputation: 127,713

Next