{
     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 varJulian2DC;

interface

uses
  BaseVariation, XFormMan;

const
  j2power = 'julian2dc_power';
  j2dist = 'julian2dc_dist';
  j2col = 'julian2dc_col';
  j2a = 'julian2dc_a';
  j2b = 'julian2dc_b';
  j2c = 'julian2dc_c';
  j2d = 'julian2dc_d';
  j2e = 'julian2dc_e';
  j2f = 'julian2dc_f';

  jeps: double = 1E-5;
    
type
  TVariationJulian2DC = class(TBaseVariation)
  private
    jdist, jcol, jpower, ja, jb, jc, jd, je, jf: double;
   cN, pi2, jps, absN, den: double;
   psn: smallint;

  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;

{ TVariationJulian2DC }

//////////////////////////////////////////
procedure TVariationJulian2DC.Prepare;
begin
  absN := abs(jpower);
  if (jpower > 0) then psn := 1 else psn := -1;
  den :=  jpower + psn * jeps;
  if (absN < 1) then
     jps := 1 / den
  else jps := jpower;
  cN := jdist / jps / 2;
  pi2 := pi * 2;
end;

//////////////////////////////////////////
procedure TVariationJulian2DC.CalcFunction;
var
  x, y, r, angle, sina, cosa: double;
  ra: integer;
begin
    x := ja * FTx^ + jb * FTy^ + je;
    y := jc * FTx^ + jd * FTy^ + jf;

    ra := Trunc(random * (absN - jeps));
    angle := (arctan2(y, x) + pi2 * ra)/ den;
    r := vvar * Math.power(sqr(x) + sqr(y), cN);

    SinCos(angle, sina, cosa);
	  FPx^ := FPx^ + r * cosa;
    FPy^ := FPy^ + r * sina;
    color^ := fmod(abs(r * jcol + (angle / pi2) * (1 - jcol)), 1.0);
end;

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

constructor TVariationJulian2DC.Create;
var beta, sinb, cosb: double;
begin
	beta := pi / RandomRange(1, 6);
	SinCos(beta, sinb, cosb);
  jpower := 2 + random(5);
  jdist := 1;
  jcol := random;
  ja := cosb;
  jb := sinb;
  jc := -sinb;
  jd := cosb;
  je := 2 * random - 1;
  jf := 2 * random - 1;
end;

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

///////////////////////////////////////////////////////////////////////////////
class function TVariationJulian2DC.GetName: string;
begin
  Result := 'julian2dc';
end;

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
function TVariationJulian2DC.GetVariableNameAt(const Index: integer): string;
begin
  case Index of
    0: Result := j2power;
    1: Result := j2dist;
    2: Result := j2col;
	  3: Result := j2a;
	  4: Result := j2b;
	  5: Result := j2c;
	  6: Result := j2d;
	  7: Result := j2e;
	  8: Result := j2f;
  else
    Result := '';
  end
end;

///////////////////////////////////////////////////////////////////////////////
function TVariationJulian2DC.SetVariable(const Name: string; var value: double): boolean;
begin
  Result := False;
  if Name = j2power then begin
    if abs(Value) < jeps then Value := jeps;
    jpower := Value;
    Result := True;
  end else if Name = j2dist then begin
    jdist := value;
    Result := True;
  end else if Name = j2col then begin    
	jcol := Value;
    Result := True;
  end else if Name = j2a then begin
	ja := Value;
    Result := True;
  end else if Name = j2b then begin
	jb := Value;
    Result := True;
  end else if Name = j2c then begin
	jc := Value;
    Result := True;
  end else if Name = j2d then begin
	jd := Value;
    Result := True;
  end else if Name = j2e then begin
	je := Value;
    Result := True;
  end else if Name = j2f then begin
	jf := Value;
    Result := True;
  end;
end;

function TVariationJulian2DC.ResetVariable(const Name: string): boolean;
begin
  Result := False;
  if Name = j2power then begin
	jpower := 2;
    Result := True;
  end else if Name = j2dist then begin
    jdist := 1;
    Result := True;
  end else if Name = j2col then begin    
	jcol := 0;
    Result := True;
  end else if Name = j2a then begin
	ja := 1;
    Result := True;
  end else if Name = j2b then begin
	jb := 0;
    Result := True;
  end else if Name = j2c then begin
	jc := 0;
    Result := True;
  end else if Name = j2d then begin
	jd := 1;
    Result := True;
  end else if Name = j2e then begin
	je := 0;
    Result := True;
  end else if Name = j2f then begin
	jf := 0;
    Result := True;
  end;
end;

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

function TVariationJulian2DC.GetNrVariables: integer;
begin
  Result := 9;
end;

///////////////////////////////////////////////////////////////////////////////
function TVariationJulian2DC.GetVariable(const Name: string; var value: double): boolean;
begin
  Result := False;
  if Name = j2power then begin
    Value := jpower; 
    Result := True;
  end else if Name = j2dist then begin
    Value := jdist;
    Result := True;
  end else if Name = j2col then begin    
	Value := jcol;
    Result := True;
  end else if Name = j2a then begin
	Value := ja;
    Result := True;
  end else if Name = j2b then begin
	Value := jb;
    Result := True;
  end else if Name = j2c then begin
	Value := jc;
    Result := True;
  end else if Name = j2d then begin
	Value := jd;
    Result := True;
  end else if Name = j2e then begin
	Value := je;
    Result := True;
  end else if Name = j2f then begin
	Value := jf;
    Result := True;
  end;
end;

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