Advertisement
rsidwell

spirograph3D.java

Jun 19th, 2017
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.70 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 static org.jwildfire.base.mathlib.MathLib.cos;
  20. import static org.jwildfire.base.mathlib.MathLib.sin;
  21.  
  22. import org.jwildfire.base.Tools;
  23.  
  24. import static org.jwildfire.base.mathlib.MathLib.fmod;
  25. import static org.jwildfire.base.mathlib.MathLib.M_2PI;
  26.  
  27. import org.jwildfire.create.tina.base.XForm;
  28. import org.jwildfire.create.tina.base.XYZPoint;
  29.  
  30. public class Spirograph3DFunc extends VariationFunc {
  31.   private static final long serialVersionUID = 1L;
  32.   private static final String PARAM_A = "a";
  33.   private static final String PARAM_B = "b";
  34.   private static final String PARAM_C = "c";
  35.   private static final String PARAM_TMIN = "tmin";
  36.   private static final String PARAM_TMAX = "tmax";
  37.   private static final String PARAM_WIDTH = "width";
  38.   private static final String PARAM_MODE = "mode";
  39.   private static final String PARAM_DIRECT_COLOR = "direct_color";
  40.  
  41.   private static final String[] paramNames = { PARAM_A, PARAM_B, PARAM_C, PARAM_TMIN, PARAM_TMAX, PARAM_WIDTH, PARAM_MODE, PARAM_DIRECT_COLOR };
  42.  
  43.   private double a = 1.0;
  44.   private double b = -0.3;
  45.   private double c = 0.4;
  46.   private double tmin = 0.0;
  47.   private double tmax = 1000.0;
  48.   private double width = 0.0;
  49.   private int mode = 0;
  50.   private int direct_color = 0;
  51.  
  52.   @Override
  53.   public void transform(FlameTransformationContext pContext, XForm pXForm, XYZPoint pAffineTP, XYZPoint pVarTP, double pAmount) {
  54.     double t = (tmax - tmin) * pContext.random() + tmin;
  55.     double w1, w2, w3;
  56.     switch(mode) {
  57.     case 0:
  58.         w1 = w2 = w3 = width * pContext.random() - width/2;
  59.         break;
  60.     case 1:
  61.         w1 = width * pContext.random() - width/2;
  62.         w2 = w1 * sin(36*t + M_2PI/3);
  63.         w3 = w1 * sin(36*t + 2*M_2PI/3);
  64.         w1 = w1 * sin(36*t);
  65.         break;
  66.     case 2:
  67.         w1 = width * pContext.random() - width/2;
  68.         w2 = width * pContext.random() - width/2;
  69.         w3 = width * pContext.random() - width/2;
  70.         break;
  71.     case 3:
  72.         w1 = width * (pContext.random() + pContext.random() + pContext.random() + pContext.random() - 2)/2;
  73.         w2 = width * (pContext.random() + pContext.random() + pContext.random() + pContext.random() - 2)/2;
  74.         w3 = width * (pContext.random() + pContext.random() + pContext.random() + pContext.random() - 2)/2;
  75.         break;
  76.     case 4:
  77.         w1 = (pContext.random()<0.5) ? width : -width;
  78.         w2 = w3 = 0;
  79.         break;
  80.     default:
  81.         w1 = w2 = w3 = 0;
  82.     }
  83.    
  84.     double x1 = (a + b) * cos(t) - c * cos((a + b) / b * t);
  85.     double y1 = (a + b) * sin(t) - c * sin((a + b) / b * t);
  86.     double z1 = c * sin((a + b) / b * t);
  87.     pVarTP.x += pAmount * (x1 + w1);
  88.     pVarTP.y += pAmount * (y1 + w2);
  89.     pVarTP.z += pAmount * (z1 + w3);
  90.     if (direct_color != 0) {
  91.         pVarTP.color = fmod(t/M_2PI,1);
  92.     }
  93.   }
  94.  
  95.   @Override
  96.   public String[] getParameterNames() {
  97.     return paramNames;
  98.   }
  99.  
  100.   @Override
  101.   public Object[] getParameterValues() {
  102.     return new Object[] { a, b, c, tmin, tmax, width, mode, direct_color };
  103.   }
  104.  
  105.   @Override
  106.   public void setParameter(String pName, double pValue) {
  107.     if (PARAM_A.equalsIgnoreCase(pName))
  108.       a = pValue;
  109.     else if (PARAM_B.equalsIgnoreCase(pName))
  110.       b = pValue;
  111.     else if (PARAM_C.equalsIgnoreCase(pName))
  112.       c = pValue;
  113.     else if (PARAM_TMIN.equalsIgnoreCase(pName))
  114.       tmin = pValue;
  115.     else if (PARAM_TMAX.equalsIgnoreCase(pName))
  116.       tmax = pValue;
  117.     else if (PARAM_WIDTH.equalsIgnoreCase(pName))
  118.       width = pValue;
  119.     else if (PARAM_MODE.equalsIgnoreCase(pName))
  120.       mode = limitIntVal(Tools.FTOI(pValue),0,4);
  121.     else if (PARAM_DIRECT_COLOR.equalsIgnoreCase(pName))
  122.       direct_color = limitIntVal(Tools.FTOI(pValue), 0, 1);
  123.     else
  124.       throw new IllegalArgumentException(pName);
  125.   }
  126.  
  127.   @Override
  128.   public String getName() {
  129.     return "spirograph3D";
  130.   }
  131.  
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement