Simple linear algebra will provide you the equations needed to rotate any given point p
by some angle
about the origin:
new_p.x = p.x * cos(angle) - p.y * sin(angle)
new_p.y = p.x * sin(angle) + p.y * cos(angle)
To instead rotate around some point other than the origin, say the point q
, you first translate your point so as to shift the coordinate frame and make q
the origin:
p.x = p.x - q.x
p.y = p.y - q.y
Then you perform the rotation as before:
new_p.x = p.x * cos(angle) - p.y * sin(angle)
new_p.y = p.x * sin(angle) + p.y * cos(angle)
Then you "undo" the translation:
new_p.x = new_p.x + q.x
new_p.y = new_p.y + q.y
Given a region (typically a rectangular one) of pixels that you want to rotate, and a location you want to rotate about (often the center of that region), you can apply the above process to every pixel in that region to find it's new, rotated location and use PutPixel
or whatever equivalent to write the appropriate color value from the original location.
The typical way to do this is to represent the transformations as transformation matrices, which will allow you to easily compose the "translate, rotate, translate-back" idiom into a single matrix you can apply to many points.
Do note that you should perform the rotation into a temporary buffer of some sort first and only write back to the original bitmap until you've completed the computation of the new pixel locations and colors. If you do the operation directly onto the final bitmap, and your angle of rotation is small enough, you may end up with a "smeared" image because you wrote some of the earlier pixels over some of the later pixels and lost the original data of those later pixels.