Ernest's code for Drawing Complex Functions
FIRST SLAVE CODE FOLLOWS
<?php // PHP slave script drawing complex functions
// create a blank image 200x200
$image = imagecreate(200, 200);
// background color white
$white = imagecolorallocate($image, 255, 255, 255);
imagefilledrectangle($image, 0, 0, 200, 200, $white);
// --- FIRST FUNCTION (blue starburst) ---
$blue = imagecolorallocate($image, 0, 0, 255);
$center = 100;
$PI = 2 * pi();
$a = 1;
$b = 1;
$m = 12;
$n1 = 5;
$n2 = 6;
$n3 = 48;
// Draw blue pattern
for ($f = 0; $f <= $PI; $f += 0.0001) {
$r = pow((pow(abs(cos($m * $f / 4) / $a), $n2) +
pow(abs(sin($m * $f / 4) / $b), $n3)), -(1 / $n1));
$x = $center + $r * cos($f) * 35;
$y = $center + $r * sin($f) * 35;
imagesetpixel($image, $x, $y, $blue);
}
// --- SECOND FUNCTION (red overlay curve) ---
$red = imagecolorallocate($image, 255, 0, 0);
$a = 1;
$b = 1;
$m = 8;
$n1 = 5;
$n2 = 6;
$n3 = 24;
// Draw red pattern over blue
for ($f = 0; $f <= $PI; $f += 0.0001) {
$r = pow((pow(abs(cos($m * $f / 4) / $a), $n2) +
pow(abs(sin($m * $f / 4) / $b), $n3)), -(1 / $n1));
$x = $center + $r * cos($f) * 35;
$y = $center + $r * sin($f) * 35;
imagesetpixel($image, $x, $y, $red);
}
// Output PNG
header("Content-type: image/png");
imagepng($image);
imagedestroy($image);
?>
SECOND SLAVE CODE FOLLOWS
<?php // PHP slave script drawing a fractal
// create a blank 200x200 image
$image = imagecreate(200, 200);
// background white
$white = imagecolorallocate($image, 255, 255, 255);
imagefilledrectangle($image, 0, 0, 200, 200, $white);
// red pixels for fractal
$red = imagecolorallocate($image, 255, 0, 0);
// starting point
$x = 200;
$y = 200;
// triangle corners
$corners = [];
$corners[0] = ['x' => 100, 'y' => 10];
$corners[1] = ['x' => 0, 'y' => 190];
$corners[2] = ['x' => 200, 'y' => 190];
// Chaos Game algorithm (Sierpinski Triangle)
for ($i = 0; $i < 100000; $i++) {
imagesetpixel($image, round($x), round($y), $red);
$a = rand(0, 2);
$x = ($x + $corners[$a]['x']) / 2;
$y = ($y + $corners[$a]['y']) / 2;
}
header("Content-Type: image/png");
imagepng($image);
imagedestroy($image);
?>