#!/usr/bin/perl use strict; use warnings; use Term::ANSIColor; # Mandelbrot Set Renderer # Configuration parameters my $width = 80; # Width of the output my $height = 40; # Height of the output my $max_iterations = 100; # Maximum iteration count # Mandelbrot set calculation function sub mandelbrot { my ($c_real, $c_imag) = @_; my $z_real = 0; my $z_imag = 0; for (my $i = 0; $i < $max_iterations; $i++) { # Mandelbrot iteration: z = z^2 + c my $new_real = $z_real * $z_real - $z_imag * $z_imag + $c_real; my $new_imag = 2 * $z_real * $z_imag + $c_imag; $z_real = $new_real; $z_imag = $new_imag; # If magnitude exceeds 2, it's not in the set return $i if ($z_real * $z_real + $z_imag * $z_imag > 4); } return $max_iterations; } # Render the Mandelbrot set sub render_mandelbrot { for my $y (0 .. $height - 1) { my $line = ''; for my $x (0 .. $width - 1) { # Map pixel coordinates to complex plane my $real = ($x / $width) * 4 - 2; # X-axis from -2 to 2 my $imag = ($y / $height) * 4 - 2; # Y-axis from -2 to 2 my $iterations = mandelbrot($real, $imag); # Color/character selection based on iterations my $char; if ($iterations == $max_iterations) { $char = colored('#', 'black'); # Points inside the set } else { # Gradient coloring based on iteration count my $color = _get_color($iterations); $char = colored('*', $color); } $line .= $char; } print "$line\n"; } } # Color selection function sub _get_color { my ($iterations) = @_; my @colors = ( 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'bright_red', 'bright_green' ); return $colors[$iterations % scalar(@colors)]; } # Main execution print "Mandelbrot Set Visualization\n"; render_mandelbrot();