Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. <?php
  2. /* Timing */
  3. set_time_limit(0);
  4. function microtime_float() {
  5. list($usec, $sec) = explode(" ", microtime());
  6. return ((float)$usec + (float)$sec);
  7. }
  8.  
  9. $time_start = microtime_float();
  10.  
  11. /* Lil Mandlebrot Set Fractal Generator */
  12. define ('IMAGE_WIDTH', 200);
  13. define ('IMAGE_HEIGHT', 200);
  14.  
  15. define ('X_MIN', -1.5);
  16. define ('X_MAX', 0.5);
  17. define ('Y_MIN', -1);
  18. define ('Y_MAX', 1);
  19.  
  20. define ('MAX_ITERATIONS', 200); // if you change this to a number bigger than 200, make sure you create a new palette.png
  21.  
  22. header("Content-type: image/png");
  23. $image = imagecreate(IMAGE_WIDTH, IMAGE_HEIGHT);
  24.  
  25. // Load the palette to find colours
  26. $colours = array();
  27. $colours['inside'] = imagecolorallocate($image, 0, 0, 0);
  28.  
  29. $palette = imagecreatefrompng("palette.png");
  30. for ($i=0; $i<imagesx($palette); $i++) {
  31. $rgb = imagecolorat($palette, $i, 0);
  32. $colours[$i] = imagecolorallocate($image, ($rgb >> 16) & 0xFF, ($rgb >> 8) & 0xFF, $rgb & 0xFF);
  33. }
  34.  
  35. for ($i=0; $i<IMAGE_WIDTH; $i++) {
  36. for ($j=0; $j<IMAGE_HEIGHT; $j++) {
  37. // What values of x and y does this pixel represent?
  38. $x = X_MIN+$i*((X_MAX - X_MIN) / (IMAGE_WIDTH-1));
  39. $y = Y_MIN+$j*((Y_MAX - Y_MIN) / (IMAGE_HEIGHT-1));
  40.  
  41. // C=x+yi
  42.  
  43. $iteration = 0;
  44. $z = array(0,0); // The initial value of z is 0+0i
  45. $magnitude = 0;
  46.  
  47. // Optimization: Store x^2 and y^2 so we don't have to keep calculating it
  48. $x2 = 0;
  49. $y2 = 0;
  50.  
  51. // If the |z| > 2 ever, then the sequence will tend to infinity so we can exit the loop
  52. while ($iteration <= MAX_ITERATIONS && $x2+y2 <= 4) {
  53. // In order to get the next term in the sequence
  54. // we need to square the previous number and then add
  55. // the original complex number
  56. //
  57. // z(n+1) = z(n)^2+c
  58.  
  59. $k = $z;
  60.  
  61. // z is a complex number. When we square a complex number in the form x+yi:
  62. // New real component: x^2-y^2
  63. // New imaginary component: 2*real*imaginary
  64. $z[0] = $x2 - $y2;
  65. $z[1] = 2 * $k[0] * $k[1];
  66.  
  67. // Add Complex Number
  68. $z[0] = $z[0] + $x;
  69. $z[1] = $z[1] + $y;
  70.  
  71. $x2 = pow($z[0], 2);
  72. $y2 = pow($z[1], 2);
  73.  
  74. ++$iteration;
  75. }
  76.  
  77. if ($iteration == MAX_ITERATIONS) {
  78. imagesetpixel($image, $i, $j, $colors['inside']);
  79. } else {
  80. imagesetpixel($image, $i, $j, $colours[$iteration]);
  81. }
  82.  
  83. }
  84. }
  85.  
  86. $time_end = microtime_float();
  87. header ("X-Exec-Time: ".($time_end - $time_start));
  88.  
  89. imagepng($image);
  90. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement