php source code of fractal contest at apress.com
on request of some users i post here the source code of my script, which won the fractal code contest in scripting languages at apress.com.
thats the plain source code:
<?php
$path = dirname(__FILE__);
@mkdir($path . "/output");
$input = explode("n",file_get_Contents($path . "/sampleinput.txt"));
$i = 1;
$scale=1;
$Cols=800; $Lines=600;
$MaxIter=32;
$img = ImageCreate($Cols*$scale,$Lines*$scale);
for($n=0; $n<$MaxIter; $n++)
{
$pitch = (int)($n*256/$MaxIter);
$c[$n] = ImageColorAllocate($img, 256-$pitch,$pitch,256-$pitch);
}
foreach($input as $line)
{
ImageFilledRectangle($img, 0, 0, $Cols*$scale, $Lines*$scale, $c[0]);
$line = trim($line);
if ($line != "")
{
list($MinRe, $MaxRe, $MinIm, $MaxIm) = explode(chr(32), $line);
$y=0;
for($Im=$MinIm;$Im<=$MaxIm;$Im+=($MaxIm-$MinIm)/$Lines)
{
set_time_limit(10);
$x=0;
for($Re=$MinRe;$Re<=$MaxRe;$Re+=($MaxRe-$MinRe)/$Cols)
{
$zr=$Re; $zi=$Im;
for($n=0;$n<$MaxIter;$n++)
{
$a=$zr*$zr; $b=$zi*$zi;
if($a+$b>4.0) { break; }
$zi=2*$zr*$zi+$Im; $zr=$a-$b+$Re;
}
ImageFilledRectangle($img, $x*$scale, $y*$scale, ($x+1)*$scale-1, ($y+1)*$scale-1, $c[$n]);
++$x;
}
++$y;
}
imagejpeg($img, $path . "/output/" . $i . ".jpg", 100);
}
$i++;
}
?>
here is the script, that generates the code:
<?php
$source = file_get_contents(dirname(__FILE__) . "/run.php");
$clean = array();
$clean[] = chr(9);
$clean[] = chr(13);
$clean[] = chr(10);
// $clean[] = chr(32);
$rep = array();
$rep["$input"] = "$_in";
$rep["$pitch"] = "$_p";
$rep["$MaxIter"] = "$_Mi";
$rep["$MinRe"] = "$_Mir";
$rep["$MaxRe"] = "$_Mxr";
$rep["$MinIm"] = "$_MiI";
$rep["$MaxIm"] = "$_MxI";
$rep["$scale"] = "$_s";
$rep["$Cols"] = "$_C";
$rep["$Lines"] = "$_L";
$rep["$line"] = "$_l";
$rep["$img"] = "$_i";
$rep["$path"] = "$_P";
$rep["$Re"] = "$_R";
foreach($clean as $c)
{
$source = str_replace($c, "", $source);
}
foreach($rep as $c=>$w)
{
$source = str_replace($c, $w, $source);
}
echo $source;
?>
thats the generated source code, one-line-code:
<?php $_P = dirname(__FILE__);@mkdir($_P . "/output");$_in = explode("n",file_get_Contents($_P . "/sampleinput.txt"));$i = 1;$_s=1;$_C=800; $_L=600;$
_Mi=32;$_i = ImageCreate($_C*$_s,$_L*$_s);for($n=0; $n<$_Mi; $n++){$_p = (int)($n*256/$_Mi);$c[$n] = ImageColorAllocate($_i, 256-$_p,$_p,256-$_p);}for
each($_in as $_l){ImageFilledRectangle($_i, 0, 0, $_C*$_s, $_L*$_s, $c[0]);$_l = trim($_l);if ($_l != ""){list($_Mir, $_Mxr, $_MiI, $_MxI) = explode(c
hr(32), $_l);$y=0;for($Im=$_MiI;$Im<=$_MxI;$Im+=($_MxI-$_MiI)/$_L){ set_time_limit(10);$x=0;for($_R=$_Mir;$_R<=$_Mxr;$_R+=($_Mxr-$_Mir)/$_C){$zr=$_R;
$zi=$Im;for($n=0;$n<$_Mi;$n++){$a=$zr*$zr; $b=$zi*$zi;if($a+$b>4.0) { break; }$zi=2*$zr*$zi+$Im; $zr=$a-$b+$_R;}ImageFilledRectangle($_i, $x*$_s, $y*$
_s, ($x+1)*$_s-1, ($y+1)*$_s-1, $c[$n]);++$x;}++$y;}imagejpeg($_i, $_P . "/output/" . $i . ".jpg", 100);}$i++;}?>
have phun.
Comment(s) »
» Leave a comment
- Your E-mail address is never displayed. If you enter it, it will only be visible to the blog author
- The line and paragraph breaks automatically
Student in an university in France, i would like to know what deals with the file 'sampleinput.txt' (its content)in your source code ( $input = explode("n",file_get_Contents($path . "/sampleinput.txt") ). In fact, i will have to generate a fractal of Mandelbrot.
I'm looking forward hearing from you soon
Comment by nguyen— 2006/03/11 @ 11:56 AM — (Reply)