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

unit varHeart;

interface
uses
  BaseVariation, XFormMan;

type
  TVariationHeart = class(TBaseVariation)
  private
    use3D: byte;
	  procedure Calc3D;
  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;
  
////////////////////////
procedure TVariationHeart.GetCalcFunction(var f: TCalcFunction);
begin
	if use3D <> 0 then
		f := Calc3D
	else f := CalcFunction;
end;

procedure TVariationHeart.CalcFunction;
var
  FAngle, r, sinr, cosr: double;
begin
  FAngle := arctan2(FTx^, FTy^);
  r := sqrt(sqr(FTx^) + sqr(FTy^));
  Sincos(r * FAngle, sinr, cosr);
  r := r * vvar;
  FPx^ := FPx^ + r * sinr;
  FPy^ := FPy^ - r * cosr;
end;

procedure TVariationHeart.Calc3D;
var
  FAngle, r, sinr, cosr: double;
begin
  FAngle := arctan2(FTx^, FTy^);
  r := sqrt(sqr(FTx^) + sqr(FTy^));
  Sincos(r * FAngle, sinr, cosr);
  r := r * vvar;
  FPx^ := FPx^ + r * sinr;
  FPy^ := FPy^ - r * cosr;
  FAngle := arctan2(FTz^, FTy^);
  r := sqrt(sqr(FTx^) + sqr(FTz^));
  cosr := cos(r * FAngle);
  FPz^ := FPz^ - vvar * r * cosr;
end;

constructor TVariationHeart.Create;
begin
  inherited Create;
  use3D := 0;
end;

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

class function TVariationHeart.GetName: string;
begin
   Result := 'heart';
end;

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

function TVariationHeart.GetNrVariables: integer;
begin
  Result := 1;
end;
///////////////////////////////////////////////////////////////////////////////
function TVariationHeart.SetVariable(const Name: string; var value: double): boolean;
begin
  Result := False;
  if Name = 'heart_use3D' then begin
    if (Value > 1) then Value := 1;
    if (Value < 0) then Value := 0;
    use3D := Round(Value);
    Result := True;
  end;
end;

function TVariationHeart.GetVariable(const Name: string; var value: double): boolean;
begin
  Result := False;
  if Name = 'heart_use3D' then begin
    Value := use3D;
    Result := True;
  end;
end;

//////////////////////////////
initialization
  RegisterVariation(TVariationClassLoader.Create(TVariationHeart), true, false);

end.
