Advertisement
rsidwell

NestedCircles.java

Feb 20th, 2025
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.88 KB | None | 0 0
  1. /*
  2.   JWildfire - an image and animation processor written in Java
  3.   Copyright (C) 1995-2011 Andreas Maschke
  4.  
  5.   This is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser
  6.   General Public License as published by the Free Software Foundation; either version 2.1 of the
  7.   License, or (at your option) any later version.
  8.  
  9.   This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
  10.   even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11.   Lesser General Public License for more details.
  12.  
  13.   You should have received a copy of the GNU Lesser General Public License along with this software;
  14.   if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  15.   02110-1301 USA, or see the FSF site: http://www.fsf.org.
  16. */
  17. package org.jwildfire.create.tina.variation;
  18.  
  19. import org.jwildfire.create.tina.base.Layer;
  20. import org.jwildfire.create.tina.base.XForm;
  21. import org.jwildfire.create.tina.base.XYZPoint;
  22.  
  23. import static org.jwildfire.base.mathlib.MathLib.*;
  24.  
  25. public class NestedCirclesFunc extends VariationFunc {
  26.   private static final long serialVersionUID = 1L;
  27.  
  28.   private static final String PARAM_LEVELS = "levels";
  29.  
  30.   private static final String[] paramNames = {PARAM_LEVELS};
  31.  
  32.   private int levels = 6;
  33.  
  34.   @Override
  35.   public void transform(FlameTransformationContext pContext, XForm pXForm, XYZPoint pAffineTP, XYZPoint pVarTP, double pAmount) {
  36.     int level = (int) (sqrt(pContext.random()) * levels);
  37.     int n = 1 << level;
  38.     double radius = pAmount / n;
  39.     double x = topLeftx + radius / sqrt(2.0) + ((int) pContext.random(n)) * radius * sqrt(2.0);
  40.     double y = topLefty + radius / sqrt(2.0) + ((int) pContext.random(n)) * radius * sqrt(2.0);
  41.     double theta = pContext.random() * M_2PI;
  42.     pVarTP.x += radius * cos(theta) + x;
  43.     pVarTP.y += radius * sin(theta) + y;
  44.  
  45.     if (pContext.isPreserveZCoordinate()) {
  46.       pVarTP.z += pAmount * pAffineTP.z;
  47.     }
  48.  
  49.   }
  50.  
  51.   @Override
  52.   public String[] getParameterNames() {
  53.     return paramNames;
  54.   }
  55.  
  56.   @Override
  57.   public Object[] getParameterValues() {
  58.     return new Object[]{levels};
  59.   }
  60.  
  61.   @Override
  62.   public void setParameter(String pName, double pValue) {
  63.     if (PARAM_LEVELS.equalsIgnoreCase(pName))
  64.       levels = (int) limitVal(pValue, 1, 10);
  65.     else
  66.       throw new IllegalArgumentException(pName);
  67.   }
  68.  
  69.   @Override
  70.   public String getName() {
  71.     return "nested_circles";
  72.   }
  73.  
  74.   private double topLeftx, topLefty;
  75.  
  76.   @Override
  77.   public void init(FlameTransformationContext pContext, Layer pLayer, XForm pXForm, double pAmount) {
  78.     topLeftx = topLefty = -pAmount / sqrt(2.0);
  79.   }
  80.  
  81.   @Override
  82.   public VariationFuncType[] getVariationTypes() {
  83.     return new VariationFuncType[]{VariationFuncType.VARTYPE_BASE_SHAPE};
  84.   }
  85.  
  86. }
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement