• Status: Solved
• Priority: Medium
• Security: Public
• Views: 429

# How to rotate an image in C++

How would one rotate an image in Visual C++, specify the point around which it is rotated, and as a bonus find out where in the newly rotated image a pixel from the origional image was? (Ex. find out where in the new image the bottom-leftmost pixel from the origional image is.)
0
Alec417
1 Solution

Commented:
Are you looking to do general purpose rotation or limited?  By limited, I mean orthogonal only perhaps.  If so, there are many shortcuts that speed things up by avoiding solving trigonometric (i.e. VERY slow) equations for each point.  If general purpose, you just perform a angular transformation on each point using the origin of the rotation as the base point.  Surely you covered this in grade-school mathematics....

BTW, this wouldn't be homework, would it???
0

Commented:
Here is a function that will do it...

{
return (degree * 3.1415 / 180);
}

int round(double num)
{
return int(num + 0.5);
}

void RotateImage(Image* OrigImg, Image* NewImg, int Theta)
{
int CenterX, CenterY;
double CosX, SinY;
int x, y, nx, ny;
NewImg->Width = OrigImg->Width;
NewImg->Height = OrigImg->Height;
CenterX = OrigImg->Width >> 1;
CenterY = OrigImg->Height >> 1;
for (int i = 0; i < OrigImg->Height; i++) {
for (int j = 0; j < OrigImg->Width; j++) {
x = j - CenterX;
y = i - CenterY;
nx = CenterX + round(x*CosX - y*SinY);
ny = CenterY + round(x*SinY + Y*CosX);
NewImg->Pixels[j][i] = OrigImg->Pixels[nx][ny];
}
}
}

It should look something like this...

-Viktor
--Ivanov
0

Commented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.