barnsley_fern_fractal.pl 889 B

123456789101112131415161718192021222324252627282930313233
  1. #!/usr/bin/perl
  2. # Author: Daniel "Trizen" Șuteu
  3. # License: GPLv3
  4. # Date: 20 March 2016
  5. # Website: https://github.com/trizen
  6. # Perl implementation of the Barnsley fern fractal.
  7. # See: https://en.wikipedia.org/wiki/Barnsley_fern
  8. use Imager;
  9. my $w = 640;
  10. my $h = 640;
  11. my $img = Imager->new(xsize => $w, ysize => $h, channels => 3);
  12. my $green = Imager::Color->new('#00FF00');
  13. my ($x, $y) = (0, 0);
  14. foreach (1 .. 1e5) {
  15. my $r = rand(100);
  16. ($x, $y) =
  17. $r <= 1 ? ( 0.00 * $x - 0.00 * $y, 0.00 * $x + 0.16 * $y + 0.00) :
  18. $r <= 8 ? ( 0.20 * $x - 0.26 * $y, 0.23 * $x + 0.22 * $y + 1.60) :
  19. $r <= 15 ? (-0.15 * $x + 0.28 * $y, 0.26 * $x + 0.24 * $y + 0.44) :
  20. ( 0.85 * $x + 0.04 * $y, -0.04 * $x + 0.85 * $y + 1.60) ;
  21. $img->setpixel(x => $w / 2 + $x * 60, y => $y * 60, color => $green);
  22. }
  23. $img->flip(dir => 'v');
  24. $img->write(file => 'barnsleyFern.png');