Advertisement
Janilabo

TPACircle

Feb 26th, 2015
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 2.10 KB | None | 0 0
  1. function TPACircle(center: TPoint; radius: Extended): TPointArray;
  2. var
  3.   l, i, j, r: Integer;
  4. begin
  5.   r := Floor(radius);
  6.   SetLength(Result, Trunc((((r * 2) + 1) * ((r * 2) + 1)) * 0.8));
  7.   l := 0;
  8.   Result[0] := center;
  9.   for i := 1 to r do
  10.   begin
  11.     Result[(l + 1)] := Point((center.X - i), center.Y);
  12.     Result[(l + 2)] := Point((center.X + i), center.Y);
  13.     Result[(l + 3)] := Point(center.X, (center.Y - i));
  14.     Result[(l + 4)] := Point(center.X, (center.Y + i));
  15.     l := (l + 4);
  16.     for j := 1 to r do
  17.       if (Sqr((j / radius)) + Sqr((i / radius)) <= 1) then
  18.       begin
  19.         Result[(l + 1)] := Point((center.X - j), (center.Y - i));
  20.         Result[(l + 2)] := Point((center.X - j), (center.Y + i));
  21.         Result[(l + 3)] := Point((center.X + j), (center.Y - i));
  22.         Result[(l + 4)] := Point((center.X + j), (center.Y + i));
  23.         l := (l + 4);
  24.       end else
  25.         Break;
  26.   end;
  27.   SetLength(Result, (l + 1));
  28. end;
  29.  
  30. function TPACircleFilled(const C:TPoint; Rad:Integer): TPointArray;
  31. var
  32.   x, y, i: Integer;
  33.   sqrad: single;
  34.   B: TBox;
  35. begin
  36.   sqrad := Trunc(Sqr(Rad+0.5));
  37.   B := IntToBox(C.x-Rad, C.y-Rad, C.x+Rad, C.y+Rad);
  38.   SetLength(Result, (B.x2-B.x1+1)*(B.y2-B.y1+1));
  39.   i := 0;
  40.   for y:=B.y1 to B.y2 do
  41.     for x:=B.x1 to B.x2 do
  42.       if Sqr(x-c.x) + Sqr(y-c.y) < SqRad then
  43.       begin
  44.         Result[i] := Point(x,y);
  45.         i := i+1;
  46.       end;
  47.   SetLength(Result, i);
  48. end;
  49.  
  50. var
  51.   t, i, b: Integer;
  52.   a: TPointArray;
  53.  
  54. begin
  55.   ClearDebug;
  56.   b := CreateBitmap(1000, 500);
  57.   DisplayDebugImgWindow(1000, 500);
  58.   DrawTPABitmap(b, TPACircle(Point(250, 250), 50.5), 16777215);
  59.   DrawTPABitmap(b, TPACircleFilled(Point(750, 250), 50), 255);
  60.   DrawBitmapDebugImg(b);
  61.   for i := 0 to 9 do
  62.   begin
  63.     t := GetSystemTime;
  64.     a := TPACircle(Point(100, 100), 2000.5);
  65.     WriteLn(GetSystemTime - t, ' ms. for TPACircle. [', Length(a), ']');
  66.     SetLength(a, 0);
  67.     t := GetSystemTime;
  68.     a := TPACircleFilled(Point(100, 100), 2000);
  69.     WriteLn(GetSystemTime - t, ' ms. for TPACircleFilled. [', Length(a), ']');
  70.     SetLength(a, 0);
  71.   end;
  72. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement