Version: 1.0
Type: Full Script
Category: Games
License: GNU General Public License
Description: This class generates Mandelbrot fractals
<?php // mandelbrot generation class v1.0 // (c) copyright 2006 positive blue <[email protected]> // some rights reserved class Mandelbrot { // thats where all step coordinates are stored public $coArray = array(); public $maxiter = 256; // picture width and height public $width = 800; public $height = 600; // palette and colors public $palette = array(); public $colors = 64; // number of colors public $set_colors = false; // steps config private $steps; // this function loads coordinates from the file public function loadConfigFile($filename) { if (file_exists($filename)) { $co = file($filename); // loop through each line and add coordinates foreach ($co as $n => $line) { // explode if (!empty($line)) $this->coArray[] = explode(" ", trim($line)); } $this->steps = count($this->coArray); return $this->steps; } else { // could not load file return false; } } // prepare image object private function initImage() { return imagecreatetruecolor($this->width, $this->height); } // this allocates palette in image object private function createPalette($img) { for ($i = 0; $i != $this->colors; $i++) { if (is_array($this->set_colors)) { eval('$c1 = $i' . $this->set_colors[0] . ';'); eval('$c2 = $i' . $this->set_colors[1] . ';'); eval('$c3 = $i' . $this->set_colors[2] . ';'); } else { $c1 = $i * 5; $c2 = $i + 3; $c3 = $i + 6; } $c1 = $c1 > 255 ? 255 : $c1; $c1 = $c1 < 1 ? 1 : $c1; $c2 = $c2 > 255 ? 255 : $c2; $c2 = $c2 < 1 ? 1 : $c2; $c3 = $c3 > 255 ? 255 : $c3; $c3 = $c3 < 1 ? 1 : $c3; $this->palette[$i] = imagecolorallocate($img, round($c1), round($c2), round($c3)); $this->palette[($this->maxiter / 2 + 1) - $i] = $this->palette[$i]; } return $palette; } // this function draws a step public function doStep($pic, $step = 0) { // set our vars $xmin = (float) $this->coArray[$step][0]; $xmax = (float) $this->coArray[$step][1]; $ymin = (float) $this->coArray[$step][2]; $ymax = (float) $this->coArray[$step][3]; $dx = ($xmax - $xmin) / $this->width; $dy = ($ymax - $ymin) / $this->height; $py = 0; $y = $ymin; while ($py < $this->height) { $px = 0; $x = $xmin; $py++; while ($px < $this->width) { $px++; $fx = 0; $fy = 0; $m = 0; while ((($fx * $fx + $fy * $fy) < 4) && ($m < $this->maxiter)) { $old_x = $fx; $fx = ($fx * $fx) - ($fy * $fy) + $x; $fy = 2 * $old_x * $fy + $y; $m++; } $color = ($m == $this->maxiter + 1) ? 0 : $this->palette[$m % ($this->maxiter / 2 + 1)]; imagesetpixel($pic, $px, $py, $color); $x += $dx; } $y += $dy; } } public function run($output_dir = '.') { for($i = 0; $i < count($this->coArray); $i++) { $img = $this->initImage(); $this->createPalette(& $img); $this->doStep(& $img, $i); imagepng($img, $output_dir . '/img' . ($i + 1) . ".png"); } } } set_time_limit(3600); $m = new Mandelbrot(); $m->width = 320; $m->height = 240; $m->maxiter = 64; $m->set_colors = array('*2', '*2', '*3'); $m->colors = 120; //$m->loadConfigFile("input.txt"); $m->coArray = array( 0 => array(-1, 1, -1, 1) ); $m->run('output'); ?>