Skip to main content
gamedev.net
🔒 Locked

Rotating a image

Started by Metric Sep 26, 2002 at 2:30 PM 4 replies 350+ views
Original Post
Metric
Metric
ok I have tried and tried to figure out how to rotate a image in a 2d array I can get it to rotate but there are holes/missing pixels
Metric
Metric
could you post a little sample code I am having trouble following what you are saying.
Metric
Metric
pls I really need help. somebody pls help me pls?
Yann L
Yann L
It's really easy, if you only want to restrict it to 2D.

In your original algorithm, you took each source pixel, applied a 2D rotation formula to find it's new position, and draw it there. Because of inaccuracies and the discrete nature of pixel graphics, you'll get holes/overlapping pixels.

Just turn the approach around: for every pixel in the destination image, rotate it *backwards* (using the same equation as you did before, just with a negative angle) and find it's position in the source image. Then copy this source pixel to the destination one.

Pseudocode:

for( y=0; y<height; y++ ) {
for( x=0; x<width; x++ ) {

// x and y are destination coordinates, so rotate them
// backwards into the source image
RotatePixel(x, y, -angle, &source_x, &source_y);

// source_x/y now hold the position of the destination
// pixel in the source image. Now, just copy it:
Destination_array[x][y] = Source_array[source_x, source_y];

}
}


As said, RotatePixel() is the same equation as you currently use. The only difference is the negative angle.

/ Yann

[edited by - Yann L on September 27, 2002 2:44:00 PM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.