/*------------------------------*/
|
/*
|
Titre : Créer des images fractales
|
|
Date édition : 15 Fev 2019
|
Date mise a jour : 31 Oct 2019
|
|
Rapport de la maj:
|
- fonctionnement du code vérifié
|
Date mise a jour : 23 Fev 2026
|
|
Rapport de la maj:
|
- fonctionnement du code vérifié
|
- ajout d'une démo
|
- refactoring du code en PHP 8
|
*/
|
/*------------------------------*/
|
function generateMandelbrot(
|
int $width = 800,
|
int $height = 600,
|
float $xMin = -2.5,
|
float $xMax = 1.0,
|
float $yMin = -1.25,
|
float $yMax = 1.25,
|
int $maxIterations = 100,
|
string $outputFile = 'mandelbrot.png'
|
): void {
|
$image = imagecreatetruecolor($width, $height);
|
|
$colors = array_map(
|
fn(int $i) => imagecolorallocate(
|
$image,
|
(int)(255 * ($i / $maxIterations) ** 0.5),
|
(int)(255 * ($i / $maxIterations) ** 0.3),
|
(int)(255 * (1 - $i / $maxIterations))
|
),
|
range(0, $maxIterations - 1)
|
);
|
$black = imagecolorallocate($image, 0, 0, 0);
|
|
for ($px = 0; $px < $width; $px++) {
|
for ($py = 0; $py < $height; $py++) {
|
$c_re = $xMin + ($px / $width) * ($xMax - $xMin);
|
$c_im = $yMin + ($py / $height) * ($yMax - $yMin);
|
|
$z_re = 0.0;
|
$z_im = 0.0;
|
$iteration = 0;
|
|
while ($z_re * $z_re + $z_im * $z_im <= 4.0 && $iteration <
|
$maxIterations) {
|
$tmp = $z_re * $z_re - $z_im * $z_im + $c_re;
|
$z_im = 2.0 * $z_re * $z_im + $c_im;
|
$z_re = $tmp;
|
$iteration++;
|
}
|
|
$color = ($iteration === $maxIterations) ? $black : $colors[
|
$iteration];
|
imagesetpixel($image, $px, $py, $color);
|
}
|
}
|
|
imagepng($image, $outputFile);
|
echo "Image générée : {$outputFile}\n";
|
|
imagepng($image, $outputFile);
|
|
if (PHP_VERSION_ID < 80500) {
|
imagedestroy($image);
|
}
|
}
|
|
| ?> |