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

interface

uses
  BaseVariation, XFormMan;

type
  TVariationGlynnSim1 = class(TBaseVariation)
  private
    radius, radius1, thickness, contrast, pow,
	  Phi1, abspow, x1, y1: double;
    procedure FilledCircle(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;

{ TVariationGlynnSim1 }

///////////////////////////////////////////////////////////////////////////////
procedure TVariationGlynnSim1.Prepare;
var sPhi1, cPhi1: double;
begin
	SinCos(Phi1 * Pi / 180, sPhi1, cPhi1);
	x1 := radius * cPhi1;
	y1 := radius * sPhi1;
	abspow := abs(pow);	
end;

procedure TVariationGlynnSim1.FilledCircle(var x: double; var y: double);
var r, Phi, sPhi, cPhi: double;
begin
	Randomize;
	r := radius1 * (thickness + (1 - thickness) * random);
	Phi := 2 * Pi * random;
	SinCos(Phi, sPhi, cPhi);
	x := r * cPhi + x1;
	y := r * sPhi + y1;
end;

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

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

///////////////////////////////////////////////////////////////////////////////
constructor TVariationGlynnSim1.Create;
begin
  radius := 1;
  radius1 := 0.1;
  thickness := 0.1;
  contrast := 0.5;
  pow := 1.5;
  Phi1 := 0;
end;

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

///////////////////////////////////////////////////////////////////////////////
class function TVariationGlynnSim1.GetName: string;
begin
  Result := 'GlynnSim1';
end;

///////////////////////////////////////////////////////////////////////////////
function TVariationGlynnSim1.GetVariableNameAt(const Index: integer): string;
begin
  case Index Of
  0: Result := 'GlynnSim1_radius';
  1: Result := 'GlynnSim1_radius1';   
  2: Result := 'GlynnSim1_Phi1';  
  3: Result := 'GlynnSim1_thickness';  
  4: Result := 'GlynnSim1_pow';
  5: Result := 'GlynnSim1_contrast';
  else
    Result := '';
  end
end;

///////////////////////////////////////////////////////////////////////////////
function TVariationGlynnSim1.SetVariable(const Name: string; var value: double): boolean;
begin
  Result := False;
  if Name = 'GlynnSim1_radius' then begin
    radius := Value;
    Result := True;
  end else if Name = 'GlynnSim1_thickness' then begin
	if Value < 0 then Value := 0;
	if Value > 1 then Value := 1;
    thickness := Value;
    Result := True;
  end else if Name = 'GlynnSim1_contrast' then begin
 	if Value < 0 then Value := 0;
	if Value > 1 then Value := 1;
    contrast := Value;
    Result := True;
  end else if Name = 'GlynnSim1_pow' then begin
    pow := Value;
    Result := True;
  end else if Name = 'GlynnSim1_Phi1' then begin
    Phi1 := Value;
    Result := True;
  end else if Name = 'GlynnSim1_radius1' then begin
    radius1 := Value;
    Result := True;
  end
end;

function TVariationGlynnSim1.ResetVariable(const Name: string): boolean;
begin
  Result := False;
 if Name = 'GlynnSim1_radius' then begin
    radius:= 1;
    Result := True;
  end else if Name = 'GlynnSim1_thickness' then begin
    thickness := 0.1;
    Result := True;
  end else if Name = 'GlynnSim1_contrast' then begin
    contrast := 0.5;
    Result := True;
  end else if Name = 'GlynnSim1_pow' then begin
    pow := 1.5;
    Result := True;	
  end else if Name = 'GlynnSim1_Phi1' then begin
    Phi1 := 0;
    Result := True;		
  end else if Name = 'GlynnSim1_radius1' then begin
    radius1 := 0.1;
    Result := True;
  end
end;

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

///////////////////////////////////////////////////////////////////////////////
function TVariationGlynnSim1.GetVariable(const Name: string; var value: double): boolean;
begin
  Result := False;
  if Name = 'GlynnSim1_radius' then begin
    Value := radius;
    Result := True;
  end else if Name = 'GlynnSim1_thickness' then begin
    Value := thickness;
    Result := True;
  end else if Name = 'GlynnSim1_contrast' then begin
    Value := contrast;
    Result := True;
  end else if Name = 'GlynnSim1_pow' then begin
    Value := pow;
    Result := True;
  end else if Name = 'GlynnSim1_Phi1' then begin
    Value := Phi1;
    Result := True;
  end else if Name = 'GlynnSim1_radius1' then begin
    Value := radius1;
    Result := True;
  end
end;

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