#!/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();
####
#!/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();
####
#!/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();
####
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long 'GetOptions';
use Imager;
sub usage {
print <<'USAGE';
Usage: mandelbrot.pl [options]
Options:
--width, -w Image width in pixels (default 800)
--height, -h Image height in pixels (default 600)
--iterations, -i Max iterations per pixel (default 500)
--centerx, --cx Center X coordinate in complex plane (default -0.75)
--centery, --cy Center Y coordinate in complex plane (default 0.0)
--zoom, -z Zoom factor (default 1.0). Larger -> zoom in
--output, -o Output filename (default mandelbrot.png)
--help Show this help
Examples:
perl mandelbrot.pl -w 1200 -h 900 -i 1000 -cx -0.743643887037151 -cy 0.13182590420533 -z 200 -o zoom.png
USAGE
exit;
}
# 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.0; # Zoom level (smaller = more zoomed in)
my $outfile = 'mandelbrot.png';
my $help = 0;
GetOptions(
'width|w=i' => \$width,
'height|h=i' => \$height,
'iterations|i=i' => \$max_iterations,
'centerx|cx=f' => \$center_x,
'centery|cy=f' => \$center_y,
'zoom|z=f' => \$zoom,
'output|o=s' => \$outfile,
'help' => \$help,
) or usage();
usage() if $help;
# 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 => $outfile)
or die "Cannot save image: " . $img->errstr;
print "Mandelbrot set rendered to $outfile\n";
}
# Main execution
render_mandelbrot();