#!/usr/bin/perl use strict; use warnings; use Imager; # Mandelbrot Set Renderer sub render_mandelbrot { # Image parameters my $width = 800; my $height = 600; my $max_iterations = 100; # Create Imager object my $img = Imager->new( xsize => $width, ysize => $height, channels => 3 ); # Mandelbrot set rendering parameters my $x_min = -2.0; my $x_max = 1.0; my $y_min = -1.5; my $y_max = 1.5; # Render each pixel for my $px (0 .. $width - 1) { for my $py (0 .. $height - 1) { # Map pixel coordinates to complex plane my $x0 = $x_min + ($x_max - $x_min) * $px / $width; my $y0 = $y_min + ($y_max - $y_min) * $py / $height; # Mandelbrot calculation my $x = 0; my $y = 0; my $iteration = 0; while ($x*$x + $y*$y <= 4 && $iteration < $max_iterations) { my $xtemp = $x*$x - $y*$y + $x0; $y = 2*$x*$y + $y0; $x = $xtemp; $iteration++; } # Color the pixel based on iterations my $color; if ($iteration == $max_iterations) { # Inside the Mandelbrot set (black) $color = Imager::Color->new(0, 0, 0); } else { # Outside the set - create a color gradient my $r = int(($iteration * 5) % 255); my $g = int(($iteration * 7) % 255); my $b = int(($iteration * 11) % 255); $color = Imager::Color->new($r, $g, $b); } # Set the pixel color $img->setpixel(x => $px, y => $py, color => $color); } } # Save the image $img->write(file => 'mandelbrot.png') or die "Cannot save image: " . $img->errstr; print "Mandelbrot set rendered to mandelbrot.png\n"; } # Run the renderer render_mandelbrot();