'How to rotate a bidimensional byte array?
I have the need to rotate the data in a bidimensional square byte matrix in any degree (0~359)
My matrix is composed by a 16x16 byte square like this:
[00][00][00][00][00][00][00][00][00][00][00][00][00][00][00][00]
[00][00][00][00][00][00][00][00][00][00][00][00][00][00][00][00]
[00][00][00][00][00][00][00][00][00][00][00][00][00][00][00][00]
[00][00][00][00][00][00][00][00][00][00][00][00][00][00][00][00]
[00][00][00][00][00][00][00][00][00][00][00][00][00][00][00][00]
[00][00][00][00][00][00][7F][7F][7F][7F][7F][7F][7F][7F][00][00]
[00][00][00][00][00][00][7F][7F][7F][7F][7F][7F][7F][7F][00][00]
[00][00][00][00][00][00][7F][7F][7F][7F][7F][7F][7F][7F][00][00]
[00][00][00][00][00][00][7F][7F][7F][7F][7F][7F][7F][7F][00][00]
[00][00][00][00][00][00][7F][7F][7F][7F][7F][7F][7F][7F][00][00]
[00][00][00][00][00][00][00][00][7F][7F][00][00][00][00][00][00]
[00][00][00][00][00][00][00][00][00][00][00][00][00][00][00][00]
[00][00][00][00][00][00][00][00][00][00][00][00][00][00][00][00]
[00][00][00][00][00][00][00][00][00][00][00][00][00][00][00][00]
[00][00][00][00][00][00][00][00][00][00][00][00][00][00][00][00]
[00][00][00][00][00][00][00][00][00][00][00][00][00][00][00][00]
This matrix represents a collision map of an item in a game. The numbers in the matrix represents the height of each position in the game world to be used to determine collision with other objects in the game.
I need to be able to rotate this collision map (the matrix data), as the object in the game can be rotated.
Does anyone have any idea how I could accomplish this?
Thank you very much!
Solution 1:[1]
I was so amazed by Norman's answer that I had to share my badly written Python code here just to show how the calculation works well!
from math import sin,cos,floor
import os,time
height = 16
width = 16
angle = 0
# Because python is weird sometimes - https://stackoverflow.com/questions/2397141/how-to-initialize-a-two-dimensional-array-in-python
tmp= [ [0]*16 for i in range(16)]
#My bitmap is actually a sprite pattern
bitmap=[
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0],
[0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0],
[0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0],
[0,0,1,0,0,0,0,1,1,0,0,0,0,1,0,0],
[0,0,1,0,0,0,1,1,1,1,0,0,0,1,0,0],
[0,0,1,0,0,1,1,0,0,1,1,0,0,1,0,0],
[0,0,1,0,0,1,0,0,0,0,1,0,0,1,0,0],
[0,0,1,0,0,0,0,1,1,0,0,0,0,1,0,0],
[0,0,1,0,0,0,0,1,1,0,0,0,0,1,0,0],
[0,0,1,0,0,0,0,1,1,0,0,0,0,1,0,0],
[0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0],
[0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
]
while(angle < 6.2):
os.system('clear') #replace clear with cls if OS == Windows
for x in range (0,height):
for y in range(0,width):
rx = cos(-angle) * x + sin(-angle) * y - (+0.5 - (0.5 * height)) * ( 1 - cos(-angle) - sin(-angle))
ry = -sin(-angle) * x + cos(-angle) * y - (+0.5 - (0.5 * width)) * ( 1 - cos(-angle) + sin(-angle))
if ( round(rx) >= 0 and round(rx) < width and round(ry) >= 0 and round(ry) < height):
tmp[x][y]=bitmap[round(rx)][round(ry)]
else:
tmp[x][y]=0
print("\r\n",end='')
for x in range (0,height):
for y in range(0,width):
if (tmp[x][y]) == 0:
print (".",end='')
else:
print ("1",end='')
print("\r\n", end='')
print(angle)
angle = angle + 0.1
time.sleep(.1)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |