PHP Classes

File: Mandelbrot.class.php

Recommend this page to a friend!
  Classes of V. Yanson   rsMandelbrot   Mandelbrot.class.php   Download  
File: Mandelbrot.class.php
Role: Class source
Content type: text/plain
Description: class itself
Class: rsMandelbrot
Output Mandelbrot fractals
Author: By
Last change: moved example to separate file
Date: 17 years ago
Size: 3,185 bytes
 

Contents

Class file image Download
<?php
 
// mandelbrot generation class v1.0
// (c) copyright 2006 positive blue <info@positiveblue.com>
// 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");
        }
    }
}
 
?>