circular_triangle.pl 728 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/usr/bin/perl
  2. # Author: Daniel "Trizen" Șuteu
  3. # License: GPLv3
  4. # Date: 08 June 2015
  5. # https://github.com/trizen
  6. #
  7. ## Generate a circular triangle based on triangular numbers.
  8. #
  9. use 5.010;
  10. use strict;
  11. use warnings;
  12. use GD::Simple;
  13. my $from = 0;
  14. my $step = 1;
  15. my $max = 3_000_000;
  16. my $limit = int(sqrt($max));
  17. # create a new image
  18. my $img = GD::Simple->new($limit * 6, $limit * 6);
  19. # move to right
  20. $img->moveTo($limit * 2.75, $limit * 1.75);
  21. my $j = 1;
  22. foreach my $i (1 .. $limit) {
  23. for my $n ($j .. $i**2) {
  24. $img->line(1);
  25. $img->turn(($from + $i) * (($i - $from) / $step + 1) / 2);
  26. ++$j;
  27. }
  28. ++$i;
  29. }
  30. open my $fh, '>:raw', "circular_triangle.png";
  31. print $fh $img->png;
  32. close $fh;