Advertisement
WarPie90

MaxBoxInRectangle

Feb 6th, 2018
406
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.21 KB | None | 0 0
  1. program new;
  2. {$I SRL/OSR.simba}
  3. {$I RSWalker/Walker.simba}
  4.  
  5. function MinBoxInRotated(B: TBox; angle: Double): TBox;
  6. var
  7.   sinA,cosA,ss,ls,wr,hr: Double;
  8.   W,H: Int32;
  9. begin
  10.   W := B.x2-B.x1+1;
  11.   H := B.y2-B.y1+1;
  12.   ls := W;
  13.   ss := H;
  14.   if w < h then Swap(ls,ss);
  15.  
  16.   sinA := Abs(Sin(angle));
  17.   cosA := Abs(Cos(angle));
  18.   if (ss <= 2.0*sinA*cosA*ls) or (abs(sinA-cosA) < 0.00001) then
  19.   begin
  20.     wr := (0.5*ss)/sinA;
  21.     hr := (0.5*ss)/cosA;
  22.     if (w < h) then Swap(wr,hr);
  23.   end else
  24.   begin
  25.     wr := (W*cosA - H*sinA) / (Sqr(cosA) - Sqr(sinA));
  26.     hr := (H*cosA - W*sinA) / (Sqr(cosA) - Sqr(sinA));
  27.   end;
  28.  
  29.   with B.Middle() do
  30.     Result := [Ceil(X-wr/2), Ceil(Y-hr/2), Trunc(X+wr/2), Trunc(Y+hr/2)];
  31. end;
  32.  
  33. var
  34.   BMP: TMufasaBitmap;
  35.   B,B1: TBox;
  36.   R: TRectangle;
  37.   TPA: TPointArray;
  38.   a: Double;
  39. begin
  40.   BMP.Init(client.GetMBitmaps);
  41.   BMP.SetSize(500,500);
  42.   BMP.Debug();
  43.   B1 := [100,100,420,300];
  44.  
  45.   while True do
  46.   begin
  47.     a += 0.02;
  48.     R := B1.ToRectangle.Rotate(a);
  49.     B := MinBoxInRotated(B1, a);
  50.     //WriteLn(B);
  51.  
  52.     BMP.DrawBox(B, $00FF00);
  53.     BMP.DrawRect(R, 255);
  54.     BMP.UpdateDebugImage();
  55.     BMP.Clear();
  56.   end;
  57.  
  58.   BMP.Debug();
  59.   BMP.Free();
  60. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement