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

unit varCosine;

interface
uses
  BaseVariation, XFormMan;

type
  TVariationCosine = class(TBaseVariation)
  private
    old: byte;
	procedure CalcPlugin;
  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 CalcFunction; override;
	  procedure GetCalcFunction(var f: TCalcFunction); override;
  end;


implementation

uses
  Math;
////////////////////////
procedure TVariationCosine.GetCalcFunction(var f: TCalcFunction);
begin
	if old = 1 then f := CalcFunction   // Apo 2.09 version
	else f := CalcPlugin;  // cosine.dll
end;

procedure TVariationCosine.CalcFunction;
var
  sinx, cosx, sinhy, coshy: double;
begin
  SinCos(FTx^ * PI, sinx, cosx);
  if FTy^ = 0 then
    // sinh(0) = 0, cosh(0) = 1
    FPx^ := FPx^ + vvar * cosx
  else begin
	  SinhCosh(FTy^, sinhy, coshy);
    FPx^ := FPx^ + vvar * cosx * coshy;
    FPy^ := FPy^ - vvar * sinx * sinhy;
  end;
end;

procedure TVariationCosine.CalcPlugin;
var
  sinx, cosx, sinhy, coshy: double;
begin
  SinCos(FTx^ * PI, sinx, cosx);
  if FTy^ = 0 then
    // sinh(0) = 0, cosh(0) = 1
    FPx^ := FPx^ + vvar * cosx
  else begin
	  SinhCosh(FTy^, sinhy, coshy);
    FPx^ := FPx^ + vvar * cosx * coshy;
    FPy^ := FPy^ + vvar * sinx * sinhy;
  end;
end;

constructor TVariationCosine.Create;
begin
  old := 1;
end;

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

class function TVariationCosine.GetName: string;
begin
   Result := 'cosine';
end;

function TVariationCosine.GetNrVariables: integer;
begin
   Result := 1;
end;

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

function TVariationCosine.GetVariableNameAt(const Index: integer): string;
begin
  case Index of
    0: Result := 'cosine_old';
  else
    Result := '';
  end;
end;

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

function TVariationCosine.ResetVariable(const Name: string): boolean;
begin
  Result := False;
  if Name = 'cosine_old' then begin
    old := 1;
    Result := True;
  end;
end;

//////////////////////////////
initialization
  RegisterVariation(TVariationClassLoader.Create(TVariationCosine), false, false);

end.
