Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* Timing */
- set_time_limit(0);
- function microtime_float() {
- list($usec, $sec) = explode(" ", microtime());
- return ((float)$usec + (float)$sec);
- }
- $time_start = microtime_float();
- /* Lil Mandlebrot Set Fractal Generator */
- define ('IMAGE_WIDTH', 200);
- define ('IMAGE_HEIGHT', 200);
- define ('X_MIN', -1.5);
- define ('X_MAX', 0.5);
- define ('Y_MIN', -1);
- define ('Y_MAX', 1);
- define ('MAX_ITERATIONS', 200); // if you change this to a number bigger than 200, make sure you create a new palette.png
- header("Content-type: image/png");
- $image = imagecreate(IMAGE_WIDTH, IMAGE_HEIGHT);
- // Load the palette to find colours
- $colours = array();
- $colours['inside'] = imagecolorallocate($image, 0, 0, 0);
- $palette = imagecreatefrompng("palette.png");
- for ($i=0; $i<imagesx($palette); $i++) {
- $rgb = imagecolorat($palette, $i, 0);
- $colours[$i] = imagecolorallocate($image, ($rgb >> 16) & 0xFF, ($rgb >> 8) & 0xFF, $rgb & 0xFF);
- }
- for ($i=0; $i<IMAGE_WIDTH; $i++) {
- for ($j=0; $j<IMAGE_HEIGHT; $j++) {
- // What values of x and y does this pixel represent?
- $x = X_MIN+$i*((X_MAX - X_MIN) / (IMAGE_WIDTH-1));
- $y = Y_MIN+$j*((Y_MAX - Y_MIN) / (IMAGE_HEIGHT-1));
- // C=x+yi
- $iteration = 0;
- $z = array(0,0); // The initial value of z is 0+0i
- $magnitude = 0;
- // Optimization: Store x^2 and y^2 so we don't have to keep calculating it
- $x2 = 0;
- $y2 = 0;
- // If the |z| > 2 ever, then the sequence will tend to infinity so we can exit the loop
- while ($iteration <= MAX_ITERATIONS && $x2+y2 <= 4) {
- // In order to get the next term in the sequence
- // we need to square the previous number and then add
- // the original complex number
- //
- // z(n+1) = z(n)^2+c
- $k = $z;
- // z is a complex number. When we square a complex number in the form x+yi:
- // New real component: x^2-y^2
- // New imaginary component: 2*real*imaginary
- $z[0] = $x2 - $y2;
- $z[1] = 2 * $k[0] * $k[1];
- // Add Complex Number
- $z[0] = $z[0] + $x;
- $z[1] = $z[1] + $y;
- $x2 = pow($z[0], 2);
- $y2 = pow($z[1], 2);
- ++$iteration;
- }
- if ($iteration == MAX_ITERATIONS) {
- imagesetpixel($image, $i, $j, $colors['inside']);
- } else {
- imagesetpixel($image, $i, $j, $colours[$iteration]);
- }
- }
- }
- $time_end = microtime_float();
- header ("X-Exec-Time: ".($time_end - $time_start));
- imagepng($image);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement