viernes, 28 de febrero de 2014

Los Fractales y la Teoría del Caos

Script en PHP para generar el fractal Mandlebrot


Generated Fractal

El siguiente script permite generar el fractal de Mandlebrot. Descargado desde la pagina : http://khlo.co.uk/stuff/fractals/

<?php 


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', 100); 
define ('IMAGE_HEIGHT', 100); 

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 
        $x2 = 0; 
        $y2 = 0; 
         
        // If the |z| > 2 ever, then the sequence will tend to infinity so 
        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  
            // 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); 
?>

Nota : se requiere la siguiente paleta de imagen que deberá ser guardada en la carpeta donde esta el script.