where : ibrtses delphi

Delphi - rotations in 2D

disclaimer

the source code of this page may not appear correctly in certain browsers
due to special characters. Have a look at the source of this HTML page
with notepad instead

A rotation of a point around the origin is done as:

|xnew|    |cos(phi)  sin(phi)|   |xold|
|ynew| := |-sin(phi) cos(phi)| * |yold|

or :

xnew:=xold*cos(phi)+yold*sin(phi)
ynew:=-xold*sin(phi)+yold*cos(phi)

the rotation normally is counter clock wise for increasing 
phi, due to the Y axis starting at top left, the rotation
now is clockwise. The rotation angle phi is measured in radians.

For a rotation around an arbitrary point, the coordinate 
system has to be shifted :

Be the rotation center (x0,y0) :

xnew:=(xold-x0)*cos(phi)+(yold-y0)*sin(phi)+ x0
ynew:=-(xold-x0)*sin(phi)+(yold-y0)*cos(phi)+ y0


notes

Rotation of a graph is done by rotating the vectors.
An image is rotated by rotating the pixels :
Scan the new image : for x, for y.
Assign the (x,y)pixel the color found in the old image at 
the rotated position. Should the rotated position be outside 
the old image, fill black or white.

centerx:=image.width div 2;
centery:=image.height div 2;
for x:=0 to image.width-1 do begin
  for y:=0 to image.height-1 do begin
    oldx:=(x-centerx)*cos(-phi)+(y-centery)*sin(-phi)+ centerx;
    oldy:=-(x-centerx)*sin(-phi)+(ycentery)*cos(-phi)+ centery;
    if outsideimage(oldx,oldy) then color:=clwhite
    else color:=oldimage.canvas.pixels[olx,oldy];
    image.canvas.pixels[x,y]:=color;
  end;
end;

Using the pixel property is the slowest possible approach.
Use the scanlines to read and write the image, and rotate
the array where the pixels are in RAM.




Feedback is welcome





sponsored links




Delphi
home

last updated: 23.june.99

Copyright (99,2000) Ing.Büro R.Tschaggelar