mandelbrot_set.pl 469 B

12345678910111213141516171819202122232425
  1. #!/usr/bin/perl
  2. # ASCII generation of the Mandelbrot set.
  3. # See also:
  4. # https://en.wikipedia.org/wiki/Mandelbrot_set
  5. use lib qw(../lib);
  6. use Math::AnyNum qw(i);
  7. sub mandelbrot {
  8. my ($z, $c) = @_[0, 0];
  9. for (1 .. 20) {
  10. $z = $z * $z + $c;
  11. return $_ if abs($z) > 2;
  12. }
  13. }
  14. for (my $y = 1 ; $y >= -1 ; $y -= 0.05) {
  15. for (my $x = -2 ; $x <= 0.5 ; $x += 0.0315) {
  16. print mandelbrot($x + $y * i) ? ' ' : '#';
  17. }
  18. print "\n";
  19. }