Janilabo

Janilabo | TPAFromEllipse() & TPAFromCircle2() [SCAR Divi]

Jul 18th, 2013
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 3.60 KB | None | 0 0
  1. {==============================================================================]
  2.   Explanation: Generates a TPA containing all coordinates within the given ellipse,
  3.                defined by the center point and radius. Coordinates are stored row-by-row.
  4. [==============================================================================}
  5. function TPAFromEllipse(center: TPoint; XRadius, YRadius: Extended): TPointArray;
  6. var
  7.   r, x, y, rx, ry: Integer;
  8.   b: TBox;
  9. begin
  10.   if ((XRadius > 0) and (YRadius > 0)) then
  11.   begin                              
  12.     rx := Round(XRadius);
  13.     ry := Round(YRadius);
  14.     b := IntToBox((center.X - rx), (center.Y - ry), (center.X + rx), (center.Y + ry));
  15.     SetLength(Result, Integer(((b.X2 - b.X1) + 1) * ((b.Y2 - b.Y1) + 1)));
  16.     for y := b.Y1 to b.Y2 do
  17.       for x := b.X1 to b.X2 do
  18.         if (Sqr((x - center.X) / XRadius) + Sqr((y - center.Y) / YRadius) <= 1) then
  19.         begin
  20.           Result[r] := Point(x, y);
  21.           Inc(r);
  22.         end;
  23.   end;
  24.   SetLength(Result, r);
  25. end;
  26.  
  27. {==============================================================================]
  28.   Explanation: Returns true if point is inside the given ellipse,
  29.                defined by the center point and radius.
  30. [==============================================================================}
  31. function InEllipse(pt, center: TPoint; XRadius, YRadius: Extended): Boolean;
  32. begin
  33.   Result := (Sqr((pt.X - center.X) / XRadius) + Sqr((pt.Y - center.Y) / YRadius) <= 1);
  34. end;
  35.  
  36. {==============================================================================]
  37.   Explanation: Generates a TPA containing all coordinates within the given circle,
  38.                defined by the center point and radius. Coordinates are stored row-by-row.
  39. [==============================================================================}
  40. function TPAFromCircle2(center: TPoint; radius: Extended): TPointArray;
  41. var
  42.   r, x, y, v: Integer;
  43.   b: TBox;
  44. begin
  45.   if (radius > 0) then
  46.   begin                              
  47.     v := Round(radius);
  48.     b := IntToBox((center.X - v), (center.Y - v), (center.X + v), (center.Y + v));
  49.     SetLength(Result, Integer(((b.X2 - b.X1) + 1) * ((b.Y2 - b.Y1) + 1)));
  50.     for y := b.Y1 to b.Y2 do
  51.       for x := b.X1 to b.X2 do
  52.         if (Sqr((x - center.X) / radius) + Sqr((y - center.Y) / radius) <= 1) then
  53.         begin
  54.           Result[r] := Point(x, y);
  55.           Inc(r);
  56.         end;
  57.   end;
  58.   SetLength(Result, r);
  59. end;
  60.  
  61. {==============================================================================]
  62.   Explanation: Returns true if point is inside the given circle,
  63.                defined by the center point and radius.
  64. [==============================================================================}
  65. function InCircle2(pt, center: TPoint; radius: Extended): Boolean;
  66. begin
  67.   Result := (Sqr((pt.X - center.X) / radius) + Sqr((pt.Y - center.Y) / radius) <= 1);
  68. end;
  69.  
  70. var
  71.   bmp: TSCARBitmap;
  72.  
  73. begin
  74.   bmp := TSCARBitmap.Create('');
  75.   bmp.SetSize(500, 500);        
  76.   WriteLn(TPAToStr(TPAFromCircle(Point(50, 50), 5)));
  77.   WriteLn(TPAToStr(TPAFromCircle2(Point(50, 50), 5)));
  78.   bmp.SetPixels(TPAFromCircle2(Point(50, 50), 25.5), clRed);
  79.   bmp.SetPixels(TPAFromCircle2(Point(250, 250), 50.5), clBlue);
  80.   bmp.SetPixels(TPAFromEllipse(Point(100, 250), 20.5, 10.5), clGreen);
  81.   bmp.SetPixels(TPAFromEllipse(Point(350, 100), 40.55, 15.444), clYellow);
  82.   bmp.SetPixels(TPAFromEllipse(Point(450, 350), 15, 45), clWhite);
  83.   bmp.SetPixels(TPAFromEllipse(Point(60, 400), 35, 45), clGray);
  84.   bmp.SetPixels(TPAFromEllipse(Point(270, 420), 30, 30), clBronze);
  85.   DebugBitmap(bmp);
  86.   bmp.Free;
  87. end.
Advertisement
Add Comment
Please, Sign In to add comment