Rotating graphics (held in an array)

Started by
3 comments, last by Peon 21 years, 9 months ago
Just wondering... is this even possible? I''m writing a pixel level sprite editor (don''t ask ) for my next game, and want the ability to rotate an image, so that I won''t have to do it by hand. First a bit of background on the program. The graphics data is stored in three equally sized arrays -- red, green, and blue. I want to be able to rotate to say... 30 degrees or something, and then store the resulting "rotated array" in another array. I know that when I rotate something, it will no longer fit in the same size array (meaning, I''ll have to store it in a bigger array) but that''s not an issue. Here''s what I''ve been thinking so far: - I would take the current column position of the array, multiply by the cosine of the angle, and store that into the new array - Then, I would take the current row position of the array, multiply by the sin of the angle, and then store that. The only problem is, the cos/sin functions will return a float, which cannot be used a subscript. I haven''t tried this yet, but would it work if I simply typecasted to an int and lost the float precision? (This is assuming what I said up there makes any sense at all; it might be totally off ) Anyone know how this would be done? I''ve thought and thought and thought about how I could do this, but I can''t seem to get it right. I''m not sure this can be done, but seeing as how it can be done with bitmaps and such, I''m guessing there is SOME way to do it. Suggestions appreciated
Peon
Advertisement
As bitmaps are arrays of bytes you certainly can.


For a 90 degree rotation:

NewArray[Height-Y][X] = OldArray[X][Y];

For other degrees:

Translate to Origin Point P

NewX = X Cos & + Y Sin &; //I may have them the wrong way round
NewY = Y Cos & - X Sin &; // & is the angle

Translate Back from Origin P

There is a newsgroup clipping on it in the articles and resources section.

,Jay
I explained how I did rotations HERE.

Note, the code has been updated since to lock and unlock the surfaces. Even though it was done in system memory, I learned it was wise to always take this approach.

( Note: This question comes up on a regular basis, someone should write an article. )

Guy
quote:( Note: This question comes up on a regular basis, someone should write an article. )


Sorry about that; I usually do a search first, but I was kind of in a hurry, so I just posted the topic instead.

Anyway, thanks for the help; I appreciate it
Peon
The formulae I gave can be used back-to-front to get the source pixels for a given destination, this means no missing pixels and no overdraw.

You can also use Breshams algorithm to generate a line from two rotated end points, this is discussed in the clipping I mentioned, in theory very quick.

,Jay

This topic is closed to new replies.

Advertisement