#!/usr/bin/env perl use strict; use warnings; use Imager; my $width = 1280; my $height = 1024; my $image = Imager->new( xsize => $width, ysize => $height ); my @palette = Imager::Color->new( 0, 0, 0 ); for ( 1 .. 20 ) { my ( $r, $g, $b ) = map { int rand 255 } 1 .. 3; push @palette, Imager::Color->new( $r, $g, $b ); } for my $x ( 0 .. $width - 1 ) { for my $y ( 0 .. $height - 1 ) { $image->setpixel( x => $x, y => $y, color => $palette[ mandelbrot( $x, $y ) ] ); } } my $file = qq(mandelbrot.bmp); $image->write( file => $file ); sub mandelbrot { my ( $x, $y ) = @_; my $re_c = ( $x - 3 * $width / 4 ) / ( $width / 3 ); my $im_c = ( $y - $height / 2 ) / ( $width / 3 ); my ( $re_z, $im_z, $iteration ) = (0) x 3; while (1) { my $temp = $re_z; $re_z = $re_z * $re_z - $im_z * $im_z + $re_c; $im_z = 2 * $temp * $im_z + $im_c; ++$iteration; last if $re_z * $re_z + $im_z * $im_z > 4; $iteration = 0, last if $iteration == 20; } return $iteration; } __END__