#!/usr/bin/perl use strict; use warnings; use Imager; # Mandelbrot Set Renderer sub render_mandelbrot { # Configurable parameters my $width = 800; my $height = 600; my $max_iterations = 200; # Center and zoom parameters my $center_x = -0.5; # X-coordinate of image center my $center_y = 0.0; # Y-coordinate of image center my $zoom = 1.5; # Zoom level (smaller = more zoomed in) # Create Imager object my $img = Imager->new( xsize => $width, ysize => $height, channels => 3 ); # Calculate complex plane boundaries my $x_min = $center_x - (2.0 * $zoom); my $x_max = $center_x + (2.0 * $zoom); my $y_min = $center_y - (1.5 * $zoom); my $y_max = $center_y + (1.5 * $zoom); # 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 set 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 mapping my $color; if ($iteration == $max_iterations) { # Inside the Mandelbrot set (black) $color = Imager::Color->new(0, 0, 0); } else { # Smooth color gradient my $t = $iteration / $max_iterations; my $r = int(9 * (1 - $t) * $t * $t * $t * 255); my $g = int(15 * (1 - $t) * (1 - $t) * $t * $t * 255); my $b = int(8.5 * (1 - $t) * (1 - $t) * (1 - $t) * $t * 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"; } # Main execution render_mandelbrot();