{  Apophysis AV "Phoenix Edition" Copyright (C) 2021 Alice V. Koryagina }

unit varCircleCrop;

interface

uses
  BaseVariation, XFormMan;

type
  TVariationCircleCrop = class(TBaseVariation)

  const
    sx : string = 'circlecrop_x';
    sy : string = 'circlecrop_y';
    sradius : string = 'circlecrop_radius';
    szero : string = 'circlecrop_zero';
    sarea : string = 'circlecrop_scatter_area';
    srewrite: string = 'circlecrop_rewrite_xy';  // AV

  private
    x0, y0, radius, scatter_area, ca: double;
    zero, rewrite: byte;
    resetpoint: boolean;
  public
    constructor Create;

    class function GetName: string; override;
    class function GetInstance: TBaseVariation; override;

    function GetNrVariables: integer; override;
    function GetVariableNameAt(const Index: integer): string; override;

    function SetVariable(const Name: string; var value: double): boolean; override;
    function GetVariable(const Name: string; var value: double): boolean; override;
	  function ResetVariable(const Name: string): boolean; override;

    procedure Prepare; override;
    procedure CalcFunction; override;
  end;


implementation
uses
  Math;

{ TVariationCircleCrop }

//////////////////////////////////////////

procedure TVariationCircleCrop.Prepare;
begin
  ca := max(-1.0, min(scatter_area, 1.0));
  resetpoint := (rewrite = 1);
end;

procedure TVariationCircleCrop.CalcFunction;
var
    x, y, rad, ang, rdc, sn, cn: double;
begin
  x := FTx^ - x0;
  y := FTy^ - y0;
  rad := Hypot(x, y);

  if resetpoint then
  begin
    FTx^ := x;
    FTy^ := y;
  end;

  if (rad > radius) then
  begin
    if (zero = 1) then
    begin
      if resetpoint then
      begin
        FPx^ := 0;
        FPy^ := 0;
      end else
      begin
        FPx^ := FPx^;
        FPy^ := FPy^;
      end;
    end else
    begin
      ang := arctan2(y, x);
      SinCos(ang, sn, cn);
      rdc := radius + (random * 0.5 * ca);

      FPx^ := FPx^ + vvar * rdc * cn + x0;
      FPy^ := FPy^ + vvar * rdc * sn + y0;
    end;
  end else
  begin
      FPx^ := FPx^ + vvar * x + x0;
      FPy^ := FPy^ + vvar * y + y0;
  end;
  FPz^ := FPz^ + vvar * FTz^;
end;

constructor TVariationCircleCrop.Create;
begin
  x0 := 0;
  y0 := 0;
  radius := 1;
  scatter_area := 0;
  zero := 0;
  rewrite := 1;
end;

class function TVariationCircleCrop.GetInstance: TBaseVariation;
begin
  Result := TVariationCircleCrop.Create;
end;

class function TVariationCircleCrop.GetName: string;
begin
  Result := 'circlecrop';
end;

function TVariationCircleCrop.GetNrVariables: integer;
begin
  Result := 6;
end;

function TVariationCircleCrop.GetVariable(const Name: string;
  var value: double): boolean;
begin
  Result := False;
  if Name = sx then begin
    Value := x0;
    Result := True;
  end
  else if Name = sy then begin
    Value := y0;
    Result := True;
  end
  else if Name = sradius then begin
    Value := radius;
    Result := True;
  end
  else if Name = sarea then begin
    Value := scatter_area;
    Result := True;
  end
  else if Name = szero then begin
    Value := zero;
    Result := True;
  end else if Name = srewrite then begin
    Value := rewrite;
    Result := True;
  end;
end;

function TVariationCircleCrop.GetVariableNameAt(const Index: integer): string;
begin
  case Index Of
	  0: Result := sradius;
    1: Result := sx;
    2: Result := sy;
    3: Result := sarea;
    4: Result := szero;
    5: Result := srewrite;
  else
    Result := '';
  end;
end;

function TVariationCircleCrop.ResetVariable(const Name: string): boolean;
begin
Result := False;
  if Name = sx then begin
    x0 := 0;
    Result := True;
  end
  else if Name = sy then begin
    y0 := 0;
    Result := True;
  end
  else if Name = sradius then begin
    radius := 1;
    Result := True;
  end
  else if Name = sarea then begin
    scatter_area := 0;
    Result := True;
  end
  else if Name = szero then begin
    zero := 0;
    Result := True;
  end else if Name = srewrite then begin
    rewrite := 1;
    Result := True;
  end;
end;

function TVariationCircleCrop.SetVariable(const Name: string;
  var value: double): boolean;
begin
Result := False;
  if Name = sx then begin
    x0 := Value;
    Result := True;
  end
  else if Name = sy then begin
    y0 := Value;
    Result := True;
  end
  else if Name = sradius then begin
    radius := Value;
    Result := True;
  end
  else if Name = sarea then begin
    scatter_area := Value;
    Result := True;
  end
  else if Name = szero then begin
    if Value < 0 then Value := 0;
    if Value > 1 then Value := 1;
    zero := Round(Value);
    Result := True;
  end else if Name = srewrite then begin
    if Value < 0 then Value := 0;
    if Value > 1 then Value := 1;
    rewrite := Round(Value);
    Result := True;
  end;
end;
///////////////////////////////////////////////////////////////////////////////
initialization
  RegisterVariation(TVariationClassLoader.Create(TVariationCircleCrop), true, false);

end.
