{
     Apophysis Copyright (C) 2001-2004 Mark Townsend
     Apophysis Copyright (C) 2005-2006 Ronald Hordijk, Piotr Borys, Peter Sdobnov
     Apophysis Copyright (C) 2007-2008 Piotr Borys, Peter Sdobnov
     
     Apophysis "3D hack" Copyright (C) 2007-2008 Peter Sdobnov
     Apophysis "7X" Copyright (C) 2009-2010 Georg Kiehne
	 Apophysis AV "Phoenix Edition" Copyright (C) 2021 Alice V. Koryagina

     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
     the Free Software Foundation; either version 2 of the License, or
     (at your option) any later version.

     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
}

unit varHypertile;

interface

uses
  BaseVariation, XFormMan;

type
  TVariationHypertile = class(TBaseVariation)
  private
    hypertile_p, hypertile_q, hypertile_n: integer;
	re, im: double;
  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;

///////////////////////////////////////////////////////////////////////////////
procedure TVariationHypertile.Prepare;
var 	
	t, pa, qa, r: double;
begin
	pa := PI2 / hypertile_p;
  qa := PI2 / hypertile_q;
	t := cos(pa);
	r := (1 - t) / (t + cos(qa)) + 1;
	if (r > 0) then
        r := 1 / sqrt(r)
    else
        r := 1;
	t := hypertile_n * pa;
	SinCos(t, pa, qa);
	re := r * qa;
	im := r * pa;
end;

procedure TVariationHypertile.CalcFunction;
var 	
	u, v, s, t, vr: double;
begin
  u := FTx^ + re;
  v := FTy^ - im;

  s := re * FTx^ - im * FTy^ + 1;
  t := re * FTy^ + im * FTx^;

  vr := vvar / (sqr(s) + sqr(t));

  FPx^ := FPx^ + vr * (u * s + v * t);
  FPy^ := FPy^ + vr * (v * s - u * t);
  // FPz^ := FPz^ + VVAR * FTz^;
end;

///////////////////////////////////////////////////////////////////////////////
constructor TVariationHypertile.Create;
begin
  hypertile_p := 3; 
  hypertile_q := 7;
  hypertile_n := 0; 
end;

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

///////////////////////////////////////////////////////////////////////////////
class function TVariationHypertile.GetName: string;
begin
  Result := 'hypertile';
end;

///////////////////////////////////////////////////////////////////////////////
function TVariationHypertile.GetVariableNameAt(const Index: integer): string;
begin
  case Index Of
  0: Result := 'hypertile_p';
  1: Result := 'hypertile_q';
  2: Result := 'hypertile_n';
  else
    Result := '';
  end
end;

///////////////////////////////////////////////////////////////////////////////
function TVariationHypertile.SetVariable(const Name: string; var value: double): boolean;
begin
  Result := False;
  if Name = 'hypertile_p' then begin
    if Value < 3 then Value := 3;
	hypertile_p := Round(Value);
    Result := True;
  end else if Name = 'hypertile_q' then begin
    if Value < 3 then Value := 3;
    hypertile_q := Round(Value);
    Result := True;
  end else if Name = 'hypertile_n' then begin
    hypertile_n := Round(Value);
    Result := True;
  end;
end;

function TVariationHypertile.ResetVariable(const Name: string): boolean;
begin
  Result := False;
  if Name = 'hypertile_p' then begin
    hypertile_p := 3;
    Result := True;
  end else if Name = 'hypertile_q' then begin
    hypertile_q := 7;
    Result := True;
  end else if Name = 'hypertile_n' then begin
    hypertile_n := 0;
    Result := True;
  end;
end;

///////////////////////////////////////////////////////////////////////////////
function TVariationHypertile.GetNrVariables: integer;
begin
  Result := 3;
end;

///////////////////////////////////////////////////////////////////////////////
function TVariationHypertile.GetVariable(const Name: string; var value: double): boolean;
begin
  Result := False;
  if Name = 'hypertile_p' then begin
    Value := hypertile_p;
    Result := True;
  end else if Name = 'hypertile_q' then begin
    Value := hypertile_q;
    Result := True;
  end else if Name = 'hypertile_n' then begin
    Value := hypertile_n;
    Result := True;
  end;
end;

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