{ Apophysis AV "Phoenix Edition" Copyright (C) 2021 Alice V. Koryagina } unit varPreCircleCrop; interface uses BaseVariation, XFormMan; type TVariationPreCircleCrop = class(TBaseVariation) const sx : string = 'pre_circlecrop_x'; sy : string = 'pre_circlecrop_y'; sradius : string = 'pre_circlecrop_radius'; szero : string = 'pre_circlecrop_zero'; sarea : string = 'pre_circlecrop_scatter_area'; private x0, y0, radius, scatter_area, ca: double; zero: byte; 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; { TVariationPreCircleCrop } ////////////////////////////////////////// procedure TVariationPreCircleCrop.Prepare; begin ca := max(-1.0, min(scatter_area, 1.0)); end; procedure TVariationPreCircleCrop.CalcFunction; var x, y, rad, ang, rdc, sn, cn: double; begin x := FTx^ - x0; y := FTy^ - y0; rad := Hypot(x, y); if (rad > radius) then begin if (zero = 1) then begin FTx^ := 0; FTy^ := 0; end else begin ang := arctan2(y, x); SinCos(ang, sn, cn); rdc := radius + (random * 0.5 * ca); FTx^ := vvar * rdc * cn + x0; FTy^ := vvar * rdc * sn + y0; end; end else begin FTx^ := vvar * x + x0; FTy^ := vvar * y + y0; end; end; constructor TVariationPreCircleCrop.Create; begin x0 := 0; y0 := 0; radius := 1; scatter_area := 0; zero := 0; end; class function TVariationPreCircleCrop.GetInstance: TBaseVariation; begin Result := TVariationPreCircleCrop.Create; end; class function TVariationPreCircleCrop.GetName: string; begin Result := 'pre_circlecrop'; end; function TVariationPreCircleCrop.GetNrVariables: integer; begin Result := 5; end; function TVariationPreCircleCrop.GetVariable(const Name: string; var value: double): boolean; begin Result := False; if Name = sx then begin Value := x0; Result := True; end else if Name = sy then begin Value := y0; Result := True; end else if Name = sradius then begin Value := radius; Result := True; end else if Name = sarea then begin Value := scatter_area; Result := True; end else if Name = szero then begin Value := zero; Result := True; end; end; function TVariationPreCircleCrop.GetVariableNameAt(const Index: integer): string; begin case Index Of 0: Result := sradius; 1: Result := sx; 2: Result := sy; 3: Result := sarea; 4: Result := szero; else Result := ''; end; end; function TVariationPreCircleCrop.ResetVariable(const Name: string): boolean; begin Result := False; if Name = sx then begin x0 := 0; Result := True; end else if Name = sy then begin y0 := 0; Result := True; end else if Name = sradius then begin radius := 1; Result := True; end else if Name = sarea then begin scatter_area := 0; Result := True; end else if Name = szero then begin zero := 0; Result := True; end; end; function TVariationPreCircleCrop.SetVariable(const Name: string; var value: double): boolean; begin Result := False; if Name = sx then begin x0 := Value; Result := True; end else if Name = sy then begin y0 := Value; Result := True; end else if Name = sradius then begin radius := Value; Result := True; end else if Name = sarea then begin scatter_area := Value; Result := True; end else if Name = szero then begin if Value < 0 then Value := 0; if Value > 1 then Value := 1; zero := Round(Value); Result := True; end; end; /////////////////////////////////////////////////////////////////////////////// initialization RegisterVariation(TVariationClassLoader.Create(TVariationPreCircleCrop), false, false); end.