[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: image rotation





Ken Dunn wrote:

> > Actually, this will probably be more useful, from the
> > comp.graphics.algorithms FAQ (you'll need to understand
> > matrix math in order to make sense of it):
> I'd looked at that FAQ before :-) Wouldn't know matrix
> math if leap up and bit me, looks like I'll have to do
> some reading up on the subject.

A 2D matrix is really just a turbo-shorthand notation which
tells you what to replace the normal increments with.
Normally, you'd start at position (0, 0) (the upperleft)
and increment X with 1, Y with 0 as you move to the
right, and X with 0, Y with 1 as you move down.
The matrix tells you that if you want to rotate the
image with an angle of R radians, you should
increment X with cos (R), Y with -sin (R) everytime
you step to the right, and X with sin (R), Y with cos (R)
everytime you step down :

float OriginalXPos = 0;
float OriginalYPos = 0;
for (long Y = 0; Y < ImageHeight; Y++)
{
  float CurrentXPos = OriginalXPos;
  float CurrentYPos = OriginalYPos;
  for (long X = 0; X < ImageWidth; X++)
  {
      TargetPixel [Y][X] = Image [CurrentYPos][CurrentXPos];
      CurrentXPos += cos (Angle);
      CurrentYPos += -sin (Angle);
  }
  OriginalXPos += sin (Angle);
  OriginalYPos += cos (Angle);
}

The trick is obviously to do it efficiently, and to take care
of out-of-bounds pixels.

The "matrix trick" from the FAQ is the math way of saying
that you can also do this using skewing, which is easier for hardware,
and easier for anti-aliasing (avoiding pixelation)

Bert
--

-=<..alle bugjes zwemmen in het water..>=-