Programming => Graphics and Multimedia => Graphics => Topic started by: JasonLugg on April 10, 2018, 04:52:09 pm

Title: [SOLVED] Rotate Rectangle Corners Around Origin Help
Post by: JasonLugg on April 10, 2018, 04:52:09 pm
Hi All and once again Thx in advance!

I have a function (grabbed from Stack Exchange here: which does pretty much what I wish. It rotates a point around an origin.

This is the function:
Code: Pascal  [Select]
  1. Function TForm1.CalcRotatedPosition(Rx: single; Ry: single; Ox: single;
  2.                         Oy: single; Angle: single): TPoint;
  3. var
  4.    cosine:single;
  5.    sine:single;
  6.    TmpPoint: TPoint;
  7.    NewPoint: TPoint;
  8. begin
  9.    cosine:=cos(Angle);
  10.    sine:=sin(Angle);
  12.    //Translate back from origin
  13.    TmpPoint.x:=Round(Rx-Ox);
  14.    TmpPoint.y:=Round(Ry-Oy);
  16.    //Rotate Point
  17.    NewPoint.x:=Round(TmpPoint.x * cosine - TmpPoint.y * sine);
  18.    NewPoint.y:=Round(TmpPoint.x * sine + TmpPoint.y * cosine);
  20.    //Translate Back To origin
  21.    TmpPoint.x:=Round(NewPoint.x + Ox);
  22.    TmpPoint.y:=Round(NewPoint.y + Oy);
  24.    Result:=TmpPoint;
  26. end;

If I use an angle below 1.0 the corners oscillate slightly in and out (not wanted!).

However, at angle:=1.0 it rotates too fast (Using BGRAVirtualScreen). I would like to slow it down but maybe my math is not good enough to work out how!

I have tried hard coding a delay but this does not work (movement too jerky).

What can I do?

Do not Round, use singles works smoothly with Angle=0.02!