function snowflake_fractal ($length, $level, $color, $image, $x, $y, $orientation
{
if ($image==='') {
// initialize
$image = image_create_alpha$length+2, round$length*4/3*sindeg2rad(60)))+2);
$x = $length+1;
$y = round$length/3*sindeg2rad(60)))+1;
$orientation = -90;
$r = hexdecsubstr$color, 0, 2));
$g = hexdecsubstr$color, 2, 2));
$b = hexdecsubstr$color, 4, 2));
$color = imagecolorallocate$image, $r, $g, $b);
$start = true;
} else {
$start = false;
}
if ($level==0) {
// all drawing is done at depth 0
$x2 = ($length)*sindeg2rad$orientation))+$x
$y2 = ($length)*cosdeg2rad$orientation))+$y
imageline$image, round$x), round$y), round$x2), round$y2), $color);
// keep track of the image, current location, and the direction we're facing
return array$image, $x2, $y2, $orientation);
} else {
// draw a side (Koch curve)
list$image, $x, $y, $orientation) = snowflake_fractal$length/3, $level-1, $color, $image, $x, $y, $orientation);
$orientation = ($orientation-60) % 360;
list$image, $x, $y, $orientation) = snowflake_fractal$length/3, $level-1, $color, $image, $x, $y, $orientation);
$orientation = ($orientation+120) % 360;
list$image, $x, $y, $orientation) = snowflake_fractal$length/3, $level-1, $color, $image, $x, $y, $orientation);
$orientation = ($orientation-60) % 360;
list$image, $x, $y, $orientation) = snowflake_fractal$length/3, $level-1, $color, $image, $x, $y, $orientation);
// draw the other two sides
if ($start) {
$orientation = ($orientation+120) % 360;
list$image, $x, $y, $orientation) = snowflake_fractal$length, $level, $color, $image, $x, $y, $orientation);
$orientation = ($orientation+120) % 360;
list$image, $x, $y, $orientation) = snowflake_fractal$length, $level, $color, $image, $x, $y, $orientation);
return $image
} else {
// keep track of the image, current location, and the direction we're facing
return array$image, $x, $y, $orientation);
}
}
}