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

unit varSecant;

interface

uses
  BaseVariation, XFormMan;
             
type
  TVariationSecant = class(TBaseVariation)
  private
    old: byte;
	  procedure CalcSecant2;
  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;

    procedure CalcFunction; override;
	procedure GetCalcFunction(var f: TCalcFunction); override;
  end;

implementation

uses
  Math;

{ TVariationSecant }

///////////////////////////////////////////////////////////////////////////////
constructor TVariationSecant.Create;
begin
  old := 0;
end;

///////////////////////////////////////////////////////////////////////////////
procedure TVariationSecant.GetCalcFunction(var f: TCalcFunction);
begin
	if old = 1 then f := CalcFunction
	else f := CalcSecant2;
end;

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

procedure TVariationSecant.CalcFunction;
var
  r: double;
begin
  r := sqrt(sqr(FTx^) + sqr(FTy^)) * vvar;
  r := cos(r);
  if (r = 0) then exit;
  FPx^ := FPx^ + FTx^ * vvar;
  FPy^ := FPy^ + 1 / r;
end;

procedure TVariationSecant.CalcSecant2;
var
  r: double;
begin
  r := sqrt(sqr(FTx^) + sqr(FTy^)) * vvar;
  r := cos(r);
  if (r = 0) then exit;
  FPx^ := FPx^ + FTx^ * vvar;
  if (r < 0) then
	  FPy^ := FPy^ + (1 / r + 1) * vvar
  else	
	  FPy^ := FPy^ + (1 / r - 1) * vvar;
end;

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

///////////////////////////////////////////////////////////////////////////////
class function TVariationSecant.GetName: string;
begin
  Result := 'secant2';
end;

///////////////////////////////////////////////////////////////////////////////
function TVariationSecant.GetVariableNameAt(const Index: integer): string;
begin
  case Index Of
  0: Result := 'secant2_old';
  else
    Result := '';
  end;
end;

///////////////////////////////////////////////////////////////////////////////
function TVariationSecant.SetVariable(const Name: string; var value: double): boolean;
begin
  Result := False;
  if Name = 'secant2_old' then
  begin
    if Value < 0 then Value := 0;
	if Value > 1 then Value := 1;
    old := Round(value);
   Result := True;
  end;
end;

///////////////////////////////////////////////////////////////////////////////
function TVariationSecant.GetNrVariables: integer;
begin
  Result := 1;
end;

///////////////////////////////////////////////////////////////////////////////
function TVariationSecant.GetVariable(const Name: string; var value: double): boolean;
begin
  Result := False;
  if Name = 'secant2_old' then
  begin
    Value := old;
    Result := True;
  end;
end;

///////////////////////////////////////////////////////////////////////////////
initialization
  RegisterVariation(TVariationClassLoader.Create(TVariationSecant), false, false);
end.
