- Splat
Bitmap Rotation
anyway. splat's answer, while correct, probably left you hanging-- the answer is good and valid, but only helps if you already know how to do this.
so, i'll give you the really long detailed version...
1. start with your source bitmap.
it'll have a width(SrcWidth) and a height(SrcHeight) this rectangular image should be the smallest rectangle into which your image(which is likely not rectangular or only moderately so)
2. Pick the center of mass(SrcCenterX,SrcCenterY)
usually, this will be the center of the image, but you may want to use a different point, depending on the shape of the object.
3. calculate the distances from (SrcCenterX,SrcCenterY) to the (0,0) and (SrcWidth-1,SrcHeight-1) corners.
as a refresher, the distance calculation is
sqrt((x1-x2)*(x1-x2)+(y1-y1)*(y1-y2))
after these are calculated, pick the largest one, call it SrcRadius
4. create the destination bitmap
the destination bitmap will have a width and height of SrcRadius*2. this will ensure that any angle of the image will fit in the destination bitmap.
5. pick the number of images you will need.
commonly, you can get away with 16, 32, or 64 rotations, and in addition, if you will be rotating images by 90 degrees at runtime, then you only need 1/4 of the images.
so, the total number of angles in 360 degrees call AngleCount
6. start to loop through the angles
//if generating all rotated angle
for(int angle=0;angle<AngleCount;++angle)
//if generating 1/4 of them
for(int angle=0;angle<AngleCount/4;++angle)
7. doing the actual rotation
since we will be using sin() and cos(), we need to convert our angle into radians...
double Radians=(2*3.14159265358979323846/(double)AngleCount)*(double)angle;
now, we scan convert.
we will take the destination coordinates for each point in the destination bitmap, rotate it backwards to get the corresponding coordinate in the source, check that it is within the range of the source bitmap, and copy the pixel.
double dstx2,dsty2,srcx,srcy;
for(int dsty=0;dsty<SrcRadius*2;++dsty)
{
for(int dstx=0;dstx<SrcRadius*2;++dstx)
{
//convert to doubles, translate so that they are based on the center of the destination image
dstx2=(double)(dstx-SrcRadius);
dsty2=(double)(dsty-SrcRadius);
srcx=cos(-Radians)*dstx2-sin(-Radians)*dsty2+(double)SrcCenterX;
srcy=sin(-Radians)*dstx2+cos(-Radians)*dsty2+(double)SrcCenterY;
//here you should check to see if srcx and srcy are in range of the source bitmap
//here you should read the pixel from the source bitmap at position (srcx,srcy)
//here you should write the pixel on the destination bitmap at position(dstx,dsty)
}
}
i left out the range checking because i figure you probably know how to do it already
also, i left out the actual reading and writing of the pixel, since i do not know what you will be using to accomplish it.
hopefully, this will help. seeing actual code usually does.
its not really all that complicated. doesnt need d3d or opengl, or even matrices(although a rotation matrix would do the same job)
Get off my lawn!
Thanks
Josh