Advertisement

Bitmap Rotation

Started by December 02, 1999 06:14 PM
2 comments, last by Josh Neta 25 years ago
The reason most books cover rotating it in your graphics package is because rotating images in real-time is both low-quality and time consuming. I would recommend you do that as well: the algorithym for rotating is not that hard, basically is entails creating a rotation matrix, inverting it, then scanning across the destination (rotated) bitmap and using the inverting rotation to figure out what pixel(s) from the source should be placed there. The most generally useful system for using rotated images that I can tell you is this: Pre-calculate the image rotated at some (likely 5) degree increments in the upper right quadrant (quadrant I (1) for math people). Then, in-game, you can quickly (and easily, most people can figure out how to do it on their own) rotate it 90, 180, or 270 degrees to gain the rest of the orientations you need.

- Splat

ok... when the hell did we get all these new little icon thingies to put next to the posts?

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!

Advertisement
Does anyone know an algo for rotating bitmaps? Every book i have either trys getting around it by saying rotate the images in your modeling/drawing packages or they just kinda glazes over it. And yes i know "use d3d" or "use GL", sorry but no that wont do (in other words don't suggest it)

Thanks
Josh

Gees, I'm sorry about not being more specific. I had just responded to the same question a day ago so I just summarized to process.

- Splat

This topic is closed to new replies.

Advertisement