koch_snowflakes.pl 621 B

123456789101112131415161718192021222324252627
  1. #!/usr/bin/perl
  2. # Draw Koch snowflakes as concentric rings, using Math::PlanePath.
  3. # See also:
  4. # https://en.wikipedia.org/wiki/Koch_snowflake
  5. # https://metacpan.org/pod/Math::PlanePath::KochSnowflakes
  6. use 5.010;
  7. use strict;
  8. use warnings;
  9. use Math::PlanePath::KochSnowflakes;
  10. my $path = Math::PlanePath::KochSnowflakes->new;
  11. use Imager;
  12. my $img = Imager->new(xsize => 1000, ysize => 1000);
  13. my $red = Imager::Color->new('#ff0000');
  14. foreach my $n (1 .. 100000) {
  15. my ($x, $y) = $path->n_to_xy($n);
  16. $img->setpixel(x => 500 + $x, y => 500 + $y, color => $red);
  17. }
  18. $img->write(file => 'Koch_snowflakes.png');