PHP Classes
Icontem

File: Mandelbrot.class.php


  Search   All class groups All class groups   Latest entries Latest entries   Top 10 charts Top 10 charts   Newsletter Newsletter   Blog Blog   Forums Forums   Help FAQ Help FAQ  
  Login   Register  
Recommend this page to a friend! ReTweet ReTweet Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of V. Yanson  >  rsMandelbrot  >  Mandelbrot.class.php  
File: Mandelbrot.class.php
Role: Class source
Content type: text/plain
Description: class itself
Class: rsMandelbrot
Output Mandelbrot fractals
 

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 $c1;
            
$c2 $c2 255 255 $c2$c2 $c2 $c2;
            
$c3 $c3 255 255 $c3$c3 $c3 $c3;
            
            
$this->palette[$i] = imagecolorallocate($imground($c1), round($c2), round($c3));
            
$this->palette[($this->maxiter 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 $old_x $fy $y;
                    
$m++;
                }
                
                
$color = ($m == $this->maxiter 1) ? $this->palette[$m % ($this->maxiter 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");
        }
    }
}
 
?>

 
  Advertise on this site Advertise on this site   Site map Site map   Statistics Statistics   Site tips Site tips   Privacy policy Privacy policy   Contact Contact  

For more information send a message to :
info at phpclasses dot org.
Copyright (c) Icontem 1999-2009 PHP Classes - PHP Class Scripts
  PHP Book Reviews - Reviews of books and other products