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

interface

uses
  BaseVariation, XFormMan;

type
  TVariationGlynnSim2 = class(TBaseVariation)
  private
    radius, thickness, contrast, pow,
	  Phi1, Phi2, Phi10, Phi20,
	  gamma, delta, abspow: double;
    procedure Circle(var x: double; var y: 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;

{ TVariationGlynnSim2 }

///////////////////////////////////////////////////////////////////////////////
procedure TVariationGlynnSim2.Prepare;
begin
	Phi10 := Phi1 * Pi / 180;
	Phi20 := Phi2 * Pi / 180;
	delta := Phi20 - Phi10;
	gamma := thickness * (2 * radius + thickness) / (radius + thickness);
	abspow := abs(pow);	
end;

procedure TVariationGlynnSim2.Circle(var x: double; var y: double);
var r, Phi, sPhi, cPhi: double;
begin
	Randomize;
	r := radius + thickness - gamma * random;
	Phi := Phi10 + delta * random;
	SinCos(Phi, sPhi, cPhi);
	x := r * cPhi;
	y := r * sPhi;
end;

procedure TVariationGlynnSim2.CalcFunction;
var r, x, y, px, py, Alpha: double;
begin
	x := FTx^;  y := FTy^;
	r := hypot(x, y);
	Alpha := radius / r;

	if (r < radius) then
	begin
		Circle(px, py);
		FPx^ := FPx^ + vvar * px;
		FPy^ := FPy^ + vvar * py;
	end else
		if (random > contrast * power(Alpha, abspow)) then
		begin
			FPx^ := FPx^ + vvar * x;
			FPy^ := FPy^ + vvar * y;
		end else
		begin
			FPx^ := FPx^ + vvar * sqr(Alpha) * x;
			FPy^ := FPy^ + vvar * sqr(Alpha) * y;		
		end;
end;

///////////////////////////////////////////////////////////////////////////////
constructor TVariationGlynnSim2.Create;
begin
  radius := 1;
  thickness := 0.1;
  contrast := 0.5;
  pow := 1.5;
  Phi1 := 0;
  Phi2 := 360;
end;

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

///////////////////////////////////////////////////////////////////////////////
class function TVariationGlynnSim2.GetName: string;
begin
  Result := 'GlynnSim2';
end;

///////////////////////////////////////////////////////////////////////////////
function TVariationGlynnSim2.GetVariableNameAt(const Index: integer): string;
begin
  case Index Of
  0: Result := 'GlynnSim2_radius';
  1: Result := 'GlynnSim2_thickness';
  2: Result := 'GlynnSim2_contrast';
  3: Result := 'GlynnSim2_pow';
  4: Result := 'GlynnSim2_Phi1';
  5: Result := 'GlynnSim2_Phi2';  
  else
    Result := '';
  end
end;

///////////////////////////////////////////////////////////////////////////////
function TVariationGlynnSim2.SetVariable(const Name: string; var value: double): boolean;
begin
  Result := False;
  if Name = 'GlynnSim2_radius' then begin
    radius := Value;
    Result := True;
  end else if Name = 'GlynnSim2_thickness' then begin
	  if Value < 0 then Value := 0;
	  if Value > 1 then Value := 1;
    thickness := Value;
    Result := True;
  end else if Name = 'GlynnSim2_contrast' then begin
 	  if Value < 0 then Value := 0;
	  if Value > 1 then Value := 1;
    contrast := Value;
    Result := True;
  end else if Name = 'GlynnSim2_pow' then begin
    pow := Value;
    Result := True;
  end else if Name = 'GlynnSim2_Phi1' then begin
    Phi1 := Value;
    Result := True;
  end else if Name = 'GlynnSim2_Phi2' then begin
    Phi2 := Value;
    Result := True;
  end
end;

function TVariationGlynnSim2.ResetVariable(const Name: string): boolean;
begin
  Result := False;
 if Name = 'GlynnSim2_radius' then begin
    radius:= 1;
    Result := True;
  end else if Name = 'GlynnSim2_thickness' then begin
    thickness := 0.1;
    Result := True;
  end else if Name = 'GlynnSim2_contrast' then begin
    contrast := 0.5;
    Result := True;
  end else if Name = 'GlynnSim2_pow' then begin
    pow := 1.5;
    Result := True;	
  end else if Name = 'GlynnSim2_Phi1' then begin
    Phi1 := 0;
    Result := True;		
  end else if Name = 'GlynnSim2_Phi2' then begin
    Phi2 := 360;
    Result := True;
  end
end;

///////////////////////////////////////////////////////////////////////////////
function TVariationGlynnSim2.GetNrVariables: integer;
begin
  Result := 6
end;

///////////////////////////////////////////////////////////////////////////////
function TVariationGlynnSim2.GetVariable(const Name: string; var value: double): boolean;
begin
  Result := False;
  if Name = 'GlynnSim2_radius' then begin
    Value := radius;
    Result := True;
  end else if Name = 'GlynnSim2_thickness' then begin
    Value := thickness;
    Result := True;
  end else if Name = 'GlynnSim2_contrast' then begin
    Value := contrast;
    Result := True;
  end else if Name = 'GlynnSim2_pow' then begin
    Value := pow;
    Result := True;
  end else if Name = 'GlynnSim2_Phi1' then begin
    Value := Phi1;
    Result := True;
  end else if Name = 'GlynnSim2_Phi2' then begin
    Value := Phi2;
    Result := True;
  end
end;

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