Solved

How to rotate an image in C++

Posted on 1999-01-25
3
384 Views
Last Modified: 2008-03-10
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
Comment
Question by:Alec417
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 32

Expert Comment

by:jhance
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

by:
viktornet earned 80 total points
ID: 1185126
Here is a function that will do it...

double rad(double degree)
{
  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;
      CosX = cos(rad(Theta));
      SinY = sin(rad(Theta));
      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

by:chensu
ID: 1185127
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question