<?php
/*
PM tetrastar fractal generator (C) MC Loonee Dan (Dan Stowell).
This is open source code under the terms of the LGPL Licence.
(v3 - Added ability to specify size of the first tetrastar, and the colours)
(v2 - Not specifying a variable causes it to be randomised)
Variables to be supplied:
$iter = number of iterations
$size = dimensions of [square] image
$ratio = Ratio of size between successive levels
$rotate = Rotation (in degrees) between each iteration - multiples of 60 make symmetrical fractal
$firstsize = Size of the first tetrastar. This is in pixels and represents the "radius" -
the distance from the centre to each of the three points
$cola, $colb, $colc = The fill colours for the tetrastars. Specify these as hex values,
without the hash sign in front: eg cola=ff0000&colb=0099ff&colc=ff00ff
*/
mt_srand ((double)microtime()*1000000);
// Ensure variables are initialised, using random numbers if they ain't
if(!isset($iter) || !(intval($iter) > 0))
$iter=mt_rand(3,8);
$iter = intval($iter);
if(!isset($size) || !(intval($size) > 0))
$size=200;
$size=intval($size);
if(!isset($ratio))
$ratio = mt_rand(200,850)/1000;
settype($ratio,'double');
if(!isset($rotate))
$rotate = mt_rand(0,360);
else
settype($rotate,'double');
if(!isset($firstsize))
$firstsize = $size/3;
$firstsize = intval($firstsize);
// Initialise the image and its colours
$image = imagecreate($size,$size);
$fg = ImageColorAllocate($image, 0, 0, 0);
if(isset($cola) && strlen($cola)==6)
{
$colar = hexdec(substr($cola, 0, 2));
$colag = hexdec(substr($cola, 2, 2));
$colab = hexdec(substr($cola, 4, 2));
$cola = ImageColorAllocate($image, $colar, $colag, $colab);
}
else
$cola = ImageColorAllocate($image, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
if(isset($colb) && strlen($colb)==6)
{
$colbr = hexdec(substr($colb, 0, 2));
$colbg = hexdec(substr($colb, 2, 2));
$colbb = hexdec(substr($colb, 4, 2));
$colb = ImageColorAllocate($image, $colbr, $colbg, $colbb);
}
else
$colb = ImageColorAllocate($image, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
if(isset($colc) && strlen($colc)==6)
{
$colcr = hexdec(substr($colc, 0, 2));
$colcg = hexdec(substr($colc, 2, 2));
$colcb = hexdec(substr($colc, 4, 2));
$colc = ImageColorAllocate($image, $colcr, $colcg, $colcb);
}
else
$colc = ImageColorAllocate($image, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
if(isset($bg) && strlen($bg)==6)
{
$bgr = hexdec(substr($bg, 0, 2));
$bgg = hexdec(substr($bg, 2, 2));
$bgb = hexdec(substr($bg, 4, 2));
$bg = ImageColorAllocate($image, $bgr, $bgg, $bgb);
}
else
$bg = ImageColorAllocate($image, 247, 247, 247);
imagefilledrectangle($image, 0, 0, $size, $size, $bg); // Colour in the background
// The recursive function which will calc & draw tetrastars
function tetrastar($centrex,$centrey,$length,$iteration)
{
global $image,$iter,$cola,$colb,$colc,$rotate,$ratio,$bg,$fg;
$xa = $centrex + ($length * sin(deg2rad($rotate*$iteration + 60)));
$ya = $centrey + ($length * cos(deg2rad($rotate*$iteration + 60)));
$xb = $centrex + ($length * sin(deg2rad($rotate*$iteration + 180)));
$yb = $centrey + ($length * cos(deg2rad($rotate*$iteration + 180)));
$xc = $centrex + ($length * sin(deg2rad($rotate*$iteration + 300)));
$yc = $centrey + ($length * cos(deg2rad($rotate*$iteration + 300)));
if($iteration<$iter)
{
// Call function for each of the sub-tetrastars
tetrastar($xa,$ya,$length*$ratio,$iteration+1);
tetrastar($xb,$yb,$length*$ratio,$iteration+1);
tetrastar($xc,$yc,$length*$ratio,$iteration+1);
}
// Draw self
imagefilledpolygon($image,array(intval($centrex),intval($centrey),intval($xa),intval($ya),intval($xb),intval($yb)),3,$cola);
imagefilledpolygon($image,array(intval($centrex),intval($centrey),intval($xb),intval($yb),intval($xc),intval($yc)),3,$colb);
imagefilledpolygon($image,array(intval($centrex),intval($centrey),intval($xc),intval($yc),intval($xa),intval($ya)),3,$colc);
imagepolygon($image,array(intval($xa),intval($ya),intval($xb),intval($yb),intval($xc),intval($yc)),3,$fg);
}
// Set the darn thing going
tetrastar($size/2,$size/2,$firstsize,0);
header("content-type: image/jpeg");
imagejpeg($image);
?>