Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

# How to rotate an image in C++

Posted on 1999-01-25
Medium Priority
408 Views
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
Question by:Alec417

LVL 32

Expert Comment

ID: 1185125
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

LVL 10

Accepted Solution

viktornet earned 240 total points
ID: 1185126
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

LVL 23

Expert Comment

ID: 1185127
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base classâ€¦
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article locâ€¦
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
###### Suggested Courses
Course of the Month10 days, 16 hours left to enroll