Recent

Author Topic: [SOLVED] Rotate Rectangle Corners Around Origin Help  (Read 1139 times)

JasonLugg

  • Jr. Member
  • **
  • Posts: 67
[SOLVED] Rotate Rectangle Corners Around Origin Help
« 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:https://gamedev.stackexchange.com/questions/86755/how-to-calculate-corner-positions-marks-of-a-rotated-tilted-rectangle) 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);
  11.  
  12.    //Translate back from origin
  13.    TmpPoint.x:=Round(Rx-Ox);
  14.    TmpPoint.y:=Round(Ry-Oy);
  15.  
  16.    //Rotate Point
  17.    NewPoint.x:=Round(TmpPoint.x * cosine - TmpPoint.y * sine);
  18.    NewPoint.y:=Round(TmpPoint.x * sine + TmpPoint.y * cosine);
  19.  
  20.    //Translate Back To origin
  21.    TmpPoint.x:=Round(NewPoint.x + Ox);
  22.    TmpPoint.y:=Round(NewPoint.y + Oy);
  23.  
  24.    Result:=TmpPoint;
  25.  
  26. end;
  27.  

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?


[SOLUTION]
Do not Round, use singles works smoothly with Angle=0.02!
« Last Edit: April 10, 2018, 05:09:25 pm by JasonLugg »