#!/usr/bin/env perl use strict; use warnings; use Imager; my $width = 320 * 10; my $height = 240 * 10; my $image = Imager->new( xsize => $width, ysize => $height ); my $black = Imager::Color->new( 0, 0, 0 ); my @palette; my $iterations = 20; for ( 1 .. $iterations ) { 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 ) { my $re_c = ( $x - 3 * $width / 4 ) / ( $width / 3 ); my $im_c = ( $y - $height / 2 ) / ( $width / 3 ); my ( $re_z, $im_z, $color, $test, $div ) = (0) x 5; while ( !$test ) { my $old_re_z = $re_z; $re_z = $re_z * $re_z - $im_z * $im_z + $re_c; $im_z = 2 * $old_re_z * $im_z + $im_c; ++$color; ( $test, $div ) = ( 1, 1 ) if $re_z * $re_z + $im_z * $im_z > 4; ( $test, $div ) = ( 1, 0 ) if $color == $iterations; } ($div) ? $image->setpixel( x => $x, y => $y, color => $palette[$color] ) : $image->setpixel( x => $x, y => $y, color => $black ); } } my $file = qq(mandelbrot.bmp); $image->write( file => $file );