#!/usr/bin/perl

sub escapes {
	my ($cr, $ci, $it) = @_;
    my $zr = 0.0;
    my $zi = 0.0;
    for ($i = 0; $i < $it; ++$i) {
        # z <- z^2 + c
        my $zrtmp = $zr*$zr - $zi*$zi + $cr; 
        $zi = 2*$zr*$zi + $ci;
        $zr = $zrtmp;
        if (($zr*$zr + $zi*$zi) > 4) {
            return " ";
         }   
    }
    return "X";
}
     
sub mandel {
	my ( $xmin, $xmax, $xstep, $ymin, $ymax, $ystep, $iterations ) = @_;
	for (my $yc = 0; $yc < $ystep; $yc++) { 
		my $y = $yc*($ymax-$ymin)/$ystep + $ymin;
		for (my $xc = 0; $xc < $xstep; $xc++) {
			my $x = $xc*($xmax-$xmin)/$xstep + $xmin;
			print escapes($x, $y, $iterations);
		}
		print "\n";
	}  
}

mandel(-2.0, 1.0, 256, -1.0, 1.0, 256, 100000);

