Mirror

How to paint an arc on a TCanvas (Views: 101)


Problem/Question/Abstract:

How to paint an arc on a TCanvas

Answer:

procedure PlotArc(const Canvas: TCanvas; const Center: TPoint; const Radius: Integer;
  const StartAngle: Single; const StopAngle: Single);

  function GetPositionForAngle(const Angle: Single): TPoint;
  var
    CosAngle: Extended;
    SinAngle: Extended;
  begin
    SinCos(DegToRad(Angle), SinAngle, CosAngle);
    Result.X := Round(Center.X + Radius * SinAngle);
    Result.Y := Round(Center.Y - Radius * CosAngle);
  end;

var
  Index: Integer;
begin
  with GetPositionForAngle(StartAngle) do
    Canvas.MoveTo(X, Y);
  for Index := Ceil(StartAngle) to Floor(StopAngle) do
    with GetPositionForAngle(Index) do
      Canvas.LineTo(X, Y);
  with GetPositionForAngle(StopAngle) do
    Canvas.LineTo(X, Y);
end;

<< Back to main page