- May 6, 2004
- 6,285
- 1
- 0
Not sure if this is the appropriate forum to post this, but couldnt find anywhere more fitting. Here it goes:
void RotateImage(QDisplay &avatar, QDisplay &obstacle, int &startX, int&startY, int &angle)
{
int centerX;
int centerY;
int coordX;
int coordY;
int coordNew;
double dArray[AVATAR_WIDTH*AVATAR_HEIGHT];
double tArray[AVATAR_WIDTH*AVATAR_HEIGHT];
unsigned char* inputArray;
unsigned char temp[AVATAR_WIDTH*AVATAR_HEIGHT];
centerX = AVATAR_WIDTH/2;
centerY = AVATAR_HEIGHT/2;
inputArray = avatar.ImageData();
for(int i=0; i<AVATAR_WIDTH*AVATAR_HEIGHT; ++i)
{
coordX = i%AVATAR_WIDTH;
coordY = i/AVATAR_HEIGHT;
dArray[ i ] = sqrt(pow((coordY-centerY),2)
+pow((coordX-centerX),2));
tArray[ i ] = atan2( (coordY-centerY), (coordX-centerX) );
temp[ i ] = inputArray[ i ];
}
for(int a=1; a<=angle; ++a)
{
for(int i=0; i<AVATAR_WIDTH*AVATAR_HEIGHT; ++i)
{
if(dArray[ i ] <= AVATAR_WIDTH/2-1)
{
coordNew = round(centerY + sin(tArray[ i ]-a*ONE_DEGREE)*dArray[ i ]) * AVATAR_WIDTH
+round(centerX + cos(tArray[ i ]-a*ONE_DEGREE)*dArray[ i ]);
inputArray[coordNew] = temp[ i ];
}
}
InsertAvatar(avatar, obstacle, startX, startY);
}
}
instead each time, without the for loop to revert back to the original.
However, that results in a static image that never rotates, as ONEDEGREE is such a tiny number that wont make appreciable difference when rounded up. Also, without reverting back, errors induced by rounding up compounds up, leading to a not-so-recognizable end result after a few degrees of rotation. Any suggestions
TIA!
edit : had to replace [ I ] with [ 1 ] everywhere due to the obvious reason
I am not asking anyone to write code for me, please dont do that. Just hoping to get some hints on improving the algorithm.
edit2: I managed to get rid of some for loops, should look much better now
void RotateImage(QDisplay &avatar, QDisplay &obstacle, int &startX, int&startY, int &angle)
{
int centerX;
int centerY;
int coordX;
int coordY;
int coordNew;
double dArray[AVATAR_WIDTH*AVATAR_HEIGHT];
double tArray[AVATAR_WIDTH*AVATAR_HEIGHT];
unsigned char* inputArray;
unsigned char temp[AVATAR_WIDTH*AVATAR_HEIGHT];
centerX = AVATAR_WIDTH/2;
centerY = AVATAR_HEIGHT/2;
inputArray = avatar.ImageData();
for(int i=0; i<AVATAR_WIDTH*AVATAR_HEIGHT; ++i)
{
coordX = i%AVATAR_WIDTH;
coordY = i/AVATAR_HEIGHT;
dArray[ i ] = sqrt(pow((coordY-centerY),2)
+pow((coordX-centerX),2));
tArray[ i ] = atan2( (coordY-centerY), (coordX-centerX) );
temp[ i ] = inputArray[ i ];
}
for(int a=1; a<=angle; ++a)
{
for(int i=0; i<AVATAR_WIDTH*AVATAR_HEIGHT; ++i)
{
if(dArray[ i ] <= AVATAR_WIDTH/2-1)
{
coordNew = round(centerY + sin(tArray[ i ]-a*ONE_DEGREE)*dArray[ i ]) * AVATAR_WIDTH
+round(centerX + cos(tArray[ i ]-a*ONE_DEGREE)*dArray[ i ]);
inputArray[coordNew] = temp[ i ];
}
}
InsertAvatar(avatar, obstacle, startX, startY);
}
}
instead each time, without the for loop to revert back to the original.
However, that results in a static image that never rotates, as ONEDEGREE is such a tiny number that wont make appreciable difference when rounded up. Also, without reverting back, errors induced by rounding up compounds up, leading to a not-so-recognizable end result after a few degrees of rotation. Any suggestions
edit : had to replace [ I ] with [ 1 ] everywhere due to the obvious reason
edit2: I managed to get rid of some for loops, should look much better now