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

interface

uses
  BaseVariation, XFormMan;

type
  TVariationCirclize = class(TBaseVariation)
  private
    PI_4, hole: 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;

	procedure Prepare; override;
    procedure CalcFunction; override;
  end;

implementation

uses
  Math;

///////////////////////////////////////////////////////////////////////////////
procedure TVariationCirclize.Prepare;
begin
  PI_4 := PI * 0.25;
end;

procedure TVariationCirclize.CalcFunction;
var
  absx, absy, side, perimeter, r, sina, cosa: double;
begin

  absx := abs(FTx^); 
  absy := abs(FTy^);  

  if (absx >= absy) then
  begin
    if (FTx^ >= absy) then
      perimeter := absx + FTy^
    else perimeter := 5.0 * absx - FTy^;
    side := absx;
  end else
  begin
    if (FTy^ >= absx) then
      perimeter := 3.0 * absy - FTx^
    else perimeter := 7.0 * absy + FTx^;
    side := absy;
  end;

  r :=  VVAR * (side + hole);
  SinCos(PI_4 * perimeter / side - PI_4, sina, cosa);

  FPx^ := FPx^ + r * cosa;
  FPy^ := FPy^ + r * sina;
  //FPz^ := FPz^ + vvar * FTz^;
end;


///////////////////////////////////////////////////////////////////////////////
constructor TVariationCirclize.Create;
begin
	hole := 0;
end;

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

///////////////////////////////////////////////////////////////////////////////
class function TVariationCirclize.GetName: string;
begin
  Result := 'circlize2';
end;

///////////////////////////////////////////////////////////////////////////////
function TVariationCirclize.GetVariableNameAt(const Index: integer): string;
begin
  case Index Of
	0: Result := 'circlize2_hole';
  else
    Result := '';
  end;  
end;

///////////////////////////////////////////////////////////////////////////////
function TVariationCirclize.SetVariable(const Name: string; var value: double): boolean;
begin
  Result := False;
  if Name = 'circlize2_hole' then begin
    hole := Value;
    Result := True;
  end;
end;

///////////////////////////////////////////////////////////////////////////////
function TVariationCirclize.GetNrVariables: integer;
begin
  Result := 1;
end;

///////////////////////////////////////////////////////////////////////////////
function TVariationCirclize.GetVariable(const Name: string; var value: double): boolean;
begin
  Result := False;
  if Name = 'circlize2_hole' then begin
    Value := hole;
    Result := True;
   end;
end;

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