Advertisement
snicker02

bad code 3

Jun 13th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.40 KB | None | 0 0
  1. /*
  2. JWildfire - an image and animation processor written in Java
  3. Copyright (C) 1995-2011 Andreas Maschke
  4. This is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser
  5. General Public License as published by the Free Software Foundation; either version 2.1 of the
  6. License, or (at your option) any later version.
  7.  
  8. This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
  9. even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License along with this software;
  12. if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  13. 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  14. */
  15. package org.jwildfire.create.tina.variation;
  16.  
  17. import org.jwildfire.create.tina.base.XForm;
  18. import org.jwildfire.create.tina.base.XYZPoint;
  19.  
  20. import static org.jwildfire.base.mathlib.MathLib.M_PI;
  21. import static java.lang.Math.abs;
  22. import static java.lang.Math.*;
  23. import static org.jwildfire.base.mathlib.MathLib.*;
  24.  
  25. public class TruchetHexCropFunc extends VariationFunc {
  26. private static final long serialVersionUID = 1L;
  27.  
  28.  
  29. private static final String PARAM_N = "n";
  30. private static final String PARAM_FLIPX = "flipx";
  31. private static final String PARAM_FLIPY = "flipy";
  32. private static final String PARAM_SPREADX = "spreadx";
  33. private static final String PARAM_SPREADY = "spready";
  34. private static final String PARAM_SEED = "seed";
  35.  
  36. private static final String[] paramNames = {PARAM_N, PARAM_FLIPX,, PARAM_FLIPY PARAM_SPREADX, PARAM_SPREADY, PARAM_SEED};
  37.  
  38. private int n = 3;
  39. private int flipx = 1;
  40. private int flipy = 1;
  41. private double spreadx = 1.0;
  42. private double spready = 1.0;
  43. private int seed = 0;
  44.  
  45. private final static double M_SQRT3_2 = 0.86602540378443864676372317075249;
  46. private final static double M_SQRT3 = 1.7320508075688772935274463415059;
  47.  
  48. @Override
  49. public void transform(FlameTransformationContext pContext, XForm pXForm, XYZPoint pAffineTP, XYZPoint pVarTP, double pAmount) {
  50. /* truchet_hex_crop from Tatyana Zabanova converted by Brad Stefanov and Whittaker Courtney */
  51.  
  52. //round
  53. double rx = floor(log(pContext.random()) * (pContext.random() < 0.5 ? spreadx : -spreadx));
  54. double rz = floor(log(pContext.random()) * (pContext.random() < 0.5 ? spready : -spready));
  55.  
  56. double FX_h = M_SQRT3 * rx + M_SQRT3_2 * (pContext.random() < 0.5 ? rz : -rz);
  57. double FY_h = 1.5 * rz;
  58.  
  59. //bool add = true;
  60.  
  61. if (seed == 1){
  62. if ((int(rx) % 2 == 0) & (int(rz) % 2 == 0)) {
  63. add = false;
  64. }
  65. }
  66. if (seed >= 2){
  67. double hash_f = sin(FX_h * 12.9898 + FY_h * 78.233 + double(seed)) * 43758.5453;
  68. hash_f = hash_f - floor(hash_f);
  69. if (hash_f < 0.5) {
  70. add = false; //pi/3
  71. }
  72. }
  73. double 3n = 3.0 * VAR(truchet_hex_fill_n);
  74. double 1_3n = 1.0 / 3n;
  75. //normalization constant for radius
  76. double s1 = exp(M_PI *1_3n);
  77. double k_factor = 2.0 / (s1 + 1.0 / s1);
  78. //exponential to make a tiled circle
  79. double rangle = floor(pContext.random() * 3n) * M_2PI *1_3n;
  80. double y_aux = flipy ? (add ? pAffineTP.y : -pAffineTP.y) : pAffineTP.y);
  81. double x_aux = flipx ? (add ? pAffineTP.x : -pAffineTP.x) : pAffineTP.x);
  82. double FY = y_aux *1_3n;
  83. double FX = x_aux *1_3n;
  84. double sn = sin(FY * M_PI + rangle);
  85. double cs = cos(FY * M_PI + rangle);
  86. //fsincos(FY * M_PI + rangle, &sn, &cs);
  87. //scale down the circle by k_factor so the edges match when we tile it
  88. double a = 1 + FX * M_PI; //
  89. //double a = k_factor * exp(FX * M_PI); // Angular polar dimension
  90. FX = a * cs;
  91. FY = a * sn;
  92. //split
  93.  
  94. double A = atan2(FY, FX);
  95. if (A < 0) A += M_2PI;
  96. A = floor(1.5 * A * M_1_PI);
  97. double ang = (M_PI + A * M_2PI) / 3.0;
  98. double sn2 = sin(ang);
  99. double cs2 = cos(ang);
  100. //fsincos(ang, &sn2, &cs2);
  101. double FX_new = FX - cs2 * 2.0;
  102. double FY_new = FY - sn2 * 2.0;
  103. //rotate by 30 to fit the hex
  104. if (add) {
  105. FX = M_SQRT3_2 * FX_new - 0.5 * FY_new;
  106. FY = 0.5 * FX_new + M_SQRT3_2 * FY_new;
  107. } else {
  108. FX = M_SQRT3_2 * FX_new + 0.5 * FY_new;
  109. FY = -0.5 * FX_new + M_SQRT3_2 * FY_new;
  110. }
  111.  
  112. pVarTP.x += pAmount * (FX * 0.5 + FX_h);
  113. pVarTP.y += pAmount * (FY * 0.5 + FY_h);
  114.  
  115. }
  116.  
  117. @Override
  118. public String[] getParameterNames() {
  119. return paramNames;
  120. }
  121.  
  122. @Override
  123. public Object[] getParameterValues() {
  124. return new Object[]{n, flipx, flipy, spreadx, spready, seed};
  125. }
  126.  
  127. @Override
  128. public void setParameter(String pName, double pValue) {
  129. if (PARAM_N.equalsIgnoreCase(pName))
  130. n = (int) pValue;
  131. else if (PARAM_FLIPX.equalsIgnoreCase(pName))
  132. flipx = (int) limitVal(pValue, 0, 1);
  133. else if (PARAM_FLIPy.equalsIgnoreCase(pName))
  134. flipy = (int) limitVal(pValue, 0, 1);
  135. else if (PARAM_SPREADX.equalsIgnoreCase(pName))
  136. spreadx = pValue;
  137. else if (PARAM_SPREADY.equalsIgnoreCase(pName))
  138. spready = pValue;
  139. else if (PARAM_SEED.equalsIgnoreCase(pName))
  140. seed = (int) pValue;
  141. else
  142. throw new IllegalArgumentException(pName);
  143. }
  144.  
  145. @Override
  146. public String getName() {
  147. return "truchet_hex_crop";
  148. }
  149.  
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement