Create your blog and photo album with postbit
Create your blog and photo album

Create new post

Content:

Upload a picture:
Tags (keywords separated by comma)

Save Cancel
perl:   Followers: 2 ; Following: 2

Explore perl's photo albums:
Photos from posts (2)
Perl images (1)

Creating the Mandelbrot Set with Perl6



Perl 6 script to create the Mandelbrot fractal.

See also the JavaScript version of this fractal generator: http://js.do/rsiqueira/mandelbrot

 

use v6;
 
# Description: Generates the Mandelbrot Set fractal using Perl 6
# For the Benchmarks to test Perl6 speed / performance in comparison with other programming languages.
#
# Created by Rodrigo Siqueira (rsiqueira (at) gmail)
# Creation date: 2015-12-27
 
# Usage (will output a PBM image file to stdout):
# /c/rakudo/bin/perl6.bat mandelbrot.p6 > /tmp/mandelbrot.pbm
 
 
my Int $MAXITER = 50;
 
my Rat $xmin = -1.5;
my Rat $ymin = -1.0;
 
my Int $w = 1000;
my Int $h = 1000;
 
my Rat $invN = 2/$w;
 
print "P4\n$w $h\n"; # PBM image header
 
 
for (0..$h-1) -> $y {
 
  my Rat $y_coord = $y * $invN + $ymin;
 
  my @v;
 
  x:
  for 0..$w-1 -> $x {
 
    my Rat $x_coord = $x * $invN + $xmin;
    my Complex $C = $x_coord + $y_coord\i;
 
    my Complex $z_prev = 0 + 0i;
 
    for (0..$MAXITER) { # Iterate
 
      my Complex $z = $z_prev * $z_prev + $C;
 
      if ($z.abs > 4) { # Outer part of the Mandelbrot Set
        print '.';
        push @v, 0;
        next x;
      }
 
      $z_prev = $z;
    }
 
    print 'o'; # Inner part of the Mandelbrot Set
    push @v, 1;
 
  }
 
  print "\n";
 
  # print pack 'B*', pack 'C*', @v;
 
if (0) {
  my $bits = '';
  for (1..@v) -> $n {
    $bits = $bits ~ @v[$n-1];
    if ($n && !($n % 8)) {
      my $buf = Buf.new( :2($bits) );
      $*OUT.write($buf);
      $bits = '';
    }
  }
  if ($bits.chars>0) { # Trailing last bytes with less than 8 bits.
    my $fill_tail = '0' x (8 - $bits.chars) ; # add trailing zeros
    my $bits_complete = $bits ~ $fill_tail;
    my $buf = Buf.new( :2($bits_complete) );
    $*OUT.write($buf);
  }
}
 
 
}
 
### END ###
 
This is the text file (70x70 pixels size) generated by the script above. It is the ASCII art version of the Mandelbrot set:
 
......................................................................
......................................................................
......................................................................
......................................................................
......................................................................
................................................oo....................
...............................................ooo....................
.............................................oooooo...................
.............................................ooooooo..................
.............................................ooooooo..................
.............................................oooooooo.................
.............................................oooooo...................
........................................o.o..o.ooo..oo................
..................................o.....ooooooooooooooo..o............
..................................oo..oooooooooooooooooooo............
..................................ooo.oooooooooooooooooooooo.oooo.....
..................................oooooooooooooooooooooooooo.ooo......
...................................oooooooooooooooooooooooooooooo.....
................................oooooooooooooooooooooooooooooooo......
.............................oo.oooooooooooooooooooooooooooooooo......
..............................oooooooooooooooooooooooooooooooooooo....
..............................oooooooooooooooooooooooooooooooooo......
..............................ooooooooooooooooooooooooooooooooooo...o.
.............................ooooooooooooooooooooooooooooooooooooooo..
.............................oooooooooooooooooooooooooooooooooooooo...
............oo...o..........oooooooooooooooooooooooooooooooooooooo....
............oo...oooo.......oooooooooooooooooooooooooooooooooooooo....
............oooooooooooo....ooooooooooooooooooooooooooooooooooooooo...
............oooooooooooo...ooooooooooooooooooooooooooooooooooooooo....
..........ooooooooooooooo..ooooooooooooooooooooooooooooooooooooooo....
..........ooooooooooooooo..oooooooooooooooooooooooooooooooooooooooo...
.........ooooooooooooooooo.ooooooooooooooooooooooooooooooooooooooo....
.........ooooooooooooooooo.oooooooooooooooooooooooooooooooooooooo.....
......ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo.....
.....ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo......
oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo........
.....ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo......
......ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo.....
.........ooooooooooooooooo.oooooooooooooooooooooooooooooooooooooo.....
.........ooooooooooooooooo.ooooooooooooooooooooooooooooooooooooooo....
..........ooooooooooooooo..oooooooooooooooooooooooooooooooooooooooo...
..........ooooooooooooooo..ooooooooooooooooooooooooooooooooooooooo....
............oooooooooooo...ooooooooooooooooooooooooooooooooooooooo....
............oooooooooooo....ooooooooooooooooooooooooooooooooooooooo...
............oo...oooo.......oooooooooooooooooooooooooooooooooooooo....
............oo...o..........oooooooooooooooooooooooooooooooooooooo....
.............................oooooooooooooooooooooooooooooooooooooo...
.............................ooooooooooooooooooooooooooooooooooooooo..
..............................ooooooooooooooooooooooooooooooooooo...o.
..............................oooooooooooooooooooooooooooooooooo......
..............................oooooooooooooooooooooooooooooooooooo....
.............................oo.oooooooooooooooooooooooooooooooo......
................................oooooooooooooooooooooooooooooooo......
...................................oooooooooooooooooooooooooooooo.....
..................................oooooooooooooooooooooooooo.ooo......
..................................ooo.oooooooooooooooooooooo.oooo.....
..................................oo..oooooooooooooooooooo............
..................................o.....ooooooooooooooo..o............
........................................o.o..o.ooo..oo................
.............................................oooooo...................
.............................................oooooooo.................
.............................................ooooooo..................
.............................................ooooooo..................
.............................................oooooo...................
...............................................ooo....................
................................................oo....................
......................................................................
......................................................................
......................................................................
......................................................................

 

Post by perl (2015-12-30 17:56)

Post your comment:

Name: Email: Site:




| Explore users | New posts | Create your blog | Create your photo album |
| About Postbit | Our blog | Terms of use | Contact Postbit |


Copyright © 2017 - postbit.com