I am working on rotating some images for a simple little game. Currently, I have working code that will rotate an image 90, 180, or 270 degrees. When I use this same code to rotate an image, say 45 degrees, the image gets distorted and cut off. Example:
For the rotation, I'm using the formulas:
DestinationX = Cos(Degrees) * (OriginalX - OriginX) - Sin(Degrees) * (OriginalY - OriginY)
DestinationY = Sin(Degrees) * (OriginalX - OriginX) + Cos(Degrees) * (OriginalY - OriginY)
I just loop through every pixel in the original image and redraw it on a new image. My code for this is:
Image is holding the original picture, and pict is holding the rotated image as it is being assembled. This is in REALbasic.
If there is a better algorithm for rotating pictures, I'd be extremely interested in knowing it. Any insight into this would be greatly appreciated.
For the rotation, I'm using the formulas:
DestinationX = Cos(Degrees) * (OriginalX - OriginX) - Sin(Degrees) * (OriginalY - OriginY)
DestinationY = Sin(Degrees) * (OriginalX - OriginX) + Cos(Degrees) * (OriginalY - OriginY)
I just loop through every pixel in the original image and redraw it on a new image. My code for this is:
Code Select
For i=-1 To Image.Width
For n=-1 To Image.Height
pnt.X = i
pnt.Y = n
rpnt = RotatePoint(pnt,originpnt,Amount)
If rpnt.X > maxx Then
maxx = rpnt.X
End if
If rpnt.Y > maxy Then
maxy = rpnt.Y
End If
pict.RGBSurface.Pixel(rpnt.X,rpnt.Y) = Image.RGBSurface.Pixel(i,n)
Next
Next
final = NewPicture(maxx,maxy,32)
final.Graphics.DrawPicture pict,0,0
Image is holding the original picture, and pict is holding the rotated image as it is being assembled. This is in REALbasic.
If there is a better algorithm for rotating pictures, I'd be extremely interested in knowing it. Any insight into this would be greatly appreciated.