unit dsqrsector;

interface

procedure SqrSector(Angle, Len: Single;
                    out X, Y: Single);
//
//  0 <= Angle <= 2 * pi
//

implementation

function Sing(Value, Len: Single): Single; inline;
begin
  if Value < 0 then begin
    Exit(- Len);
  end else
    Exit(+ Len);
end;

function SqrLine(Angle, Mult: Single): Single;
// [-pi/4 .. pi/4] -> [-Mult .. Mult]
begin
  Angle := Sqr(1 / Cos(Angle)) - 1; // use Angle as a temporary variable
  if Angle < 0 then
    Angle := 0; // oops?..
  Result := Mult * Sqrt(Angle);
end;

procedure SqrSector(Angle, Len: Single;
                    out X, Y: Single);
begin
  if Angle < 3 * pi / 4 then begin
    if Angle < 1 * pi / 4 then begin
      X := SqrLine(Angle, Len);
      Y := -Len;
    end else begin
      Angle := Angle - 2 * pi / 4;
      X := Len;
      Y := SqrLine(Angle, Sing(Angle, Len));
    end;
  end else if Angle < 5 * pi / 4 then begin
    Angle := - (Angle - 4 * pi / 4);
    X := SqrLine(Angle, Sing(Angle, Len));
    Y := Len;
  end else if Angle < 7 * pi / 4 then begin
    Angle := - (Angle - 6 * pi / 4);
    X := -Len;
    Y := SqrLine(Angle, Sing(Angle, Len));
  end else begin // >= 7 * pi / 4
    Angle := Angle - 8 * pi / 4;
    X := SqrLine(Angle, - Len);
    Y := -Len;
  end;
end;

end.
