I wanted to created a rotation function for rotating unsigned int arrays. After browsing for some good sources, I came up with the following code :
Code:
void RotationFunction( unsigned int * array, int Width, int Height, int angle)
{ //calculations of the new Width and Height
long x1 = ( -Height * fp16sin[angle] );
long y1 = ( Height * fp16cos[angle] );
long x2 = ( Width * fp16cos[angle] ) - ( Height * fp16sin[angle] );
long y2 = ( Height * fp16cos[angle] ) + (Width * fp16sin[angle] );
long x3 = ( Width * fp16cos[angle] );
long y3 = ( Width * fp16sin[angle] );
long minx = min(0,min(x1,min(x2,x3 ) ) );
long miny = min(0,min(y1,min(y2,y3 ) ) );
long maxx = max(x1,max(x2,x3) );
long maxy = max(y1,max(y2,y3) );
//Created as a global variable just for this prototype test
NewWidth = ( maxx - minx )>>16;
NewHeight =( maxy - miny )>>16;
unsigned int * ptr = (unsigned int *) calloc(NewWidth * NewHeight, sizeof(unsigned int) );
if( ptr != NULL )
{
int srotx = Width >>15;
int sroty = Height >>15;
int dx = NewWidth >> 1;
int dy = NewHeight >> 1;
/*translate from the differenct between the roatation of the center of the new size with
with the center of the previous size in 16by16 fixed point */
srotx -= (int) (dx * fp16cos[angle] + dy * fp16sin[angle]);
sroty -= (int) (dx * fp16sin[angle] - dy * fp16cos[angle]);
int newx, newy, rotx, roty;
for(int y = 0; y < NewHeight; y++)
{ // Gets the rotated new coordinates
rotx = srotx = srotx - fp16sin[angle];
roty = sroty = sroty + fp16cos[angle];
for(int x = 0; x < NewWidth; x++)
{
//converts from fixed point
newx = rotx >> 16;
newy = roty >> 16;
//checks if rotated coordinates are out of boundary
if((newx >= 0 && newx < NewWidth) && (newy >= 0 && newy < NewHeight))
ptr[ (y*NewWidth)+ x ] = array[ (newy *Width) + newx];
else
{ptr[ (y*NewWidth)+ x ] = 0;} // feels up any gaps
rotx += fp16cos[angle];
roty += fp16sin[angle];
}
}
array = ptr;
}//end of if
}
The code does run without any error, but doesn't show any numbers for certain angles (not including the ones more then 256). Also, I believe the output is wrong, but i could be mistaken. Any suggestions or tips will be deeply appreciated... ^_^