Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {==============================================================================]
- Explanation: Generates a TPA containing all coordinates within the given ellipse,
- defined by the center point and radius. Coordinates are stored row-by-row.
- [==============================================================================}
- function TPAFromEllipse(center: TPoint; XRadius, YRadius: Extended): TPointArray;
- var
- r, x, y, rx, ry: Integer;
- b: TBox;
- begin
- if ((XRadius > 0) and (YRadius > 0)) then
- begin
- rx := Round(XRadius);
- ry := Round(YRadius);
- b := IntToBox((center.X - rx), (center.Y - ry), (center.X + rx), (center.Y + ry));
- SetLength(Result, Integer(((b.X2 - b.X1) + 1) * ((b.Y2 - b.Y1) + 1)));
- for y := b.Y1 to b.Y2 do
- for x := b.X1 to b.X2 do
- if (Sqr((x - center.X) / XRadius) + Sqr((y - center.Y) / YRadius) <= 1) then
- begin
- Result[r] := Point(x, y);
- Inc(r);
- end;
- end;
- SetLength(Result, r);
- end;
- {==============================================================================]
- Explanation: Returns true if point is inside the given ellipse,
- defined by the center point and radius.
- [==============================================================================}
- function InEllipse(pt, center: TPoint; XRadius, YRadius: Extended): Boolean;
- begin
- Result := (Sqr((pt.X - center.X) / XRadius) + Sqr((pt.Y - center.Y) / YRadius) <= 1);
- end;
- {==============================================================================]
- Explanation: Generates a TPA containing all coordinates within the given circle,
- defined by the center point and radius. Coordinates are stored row-by-row.
- [==============================================================================}
- function TPAFromCircle2(center: TPoint; radius: Extended): TPointArray;
- var
- r, x, y, v: Integer;
- b: TBox;
- begin
- if (radius > 0) then
- begin
- v := Round(radius);
- b := IntToBox((center.X - v), (center.Y - v), (center.X + v), (center.Y + v));
- SetLength(Result, Integer(((b.X2 - b.X1) + 1) * ((b.Y2 - b.Y1) + 1)));
- for y := b.Y1 to b.Y2 do
- for x := b.X1 to b.X2 do
- if (Sqr((x - center.X) / radius) + Sqr((y - center.Y) / radius) <= 1) then
- begin
- Result[r] := Point(x, y);
- Inc(r);
- end;
- end;
- SetLength(Result, r);
- end;
- {==============================================================================]
- Explanation: Returns true if point is inside the given circle,
- defined by the center point and radius.
- [==============================================================================}
- function InCircle2(pt, center: TPoint; radius: Extended): Boolean;
- begin
- Result := (Sqr((pt.X - center.X) / radius) + Sqr((pt.Y - center.Y) / radius) <= 1);
- end;
- var
- bmp: TSCARBitmap;
- begin
- bmp := TSCARBitmap.Create('');
- bmp.SetSize(500, 500);
- WriteLn(TPAToStr(TPAFromCircle(Point(50, 50), 5)));
- WriteLn(TPAToStr(TPAFromCircle2(Point(50, 50), 5)));
- bmp.SetPixels(TPAFromCircle2(Point(50, 50), 25.5), clRed);
- bmp.SetPixels(TPAFromCircle2(Point(250, 250), 50.5), clBlue);
- bmp.SetPixels(TPAFromEllipse(Point(100, 250), 20.5, 10.5), clGreen);
- bmp.SetPixels(TPAFromEllipse(Point(350, 100), 40.55, 15.444), clYellow);
- bmp.SetPixels(TPAFromEllipse(Point(450, 350), 15, 45), clWhite);
- bmp.SetPixels(TPAFromEllipse(Point(60, 400), 35, 45), clGray);
- bmp.SetPixels(TPAFromEllipse(Point(270, 420), 30, 30), clBronze);
- DebugBitmap(bmp);
- bmp.Free;
- end.
Advertisement
Add Comment
Please, Sign In to add comment