102 Triangle containment.pl 834 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #!/usr/bin/perl
  2. # Author: Daniel "Trizen" Șuteu
  3. # Date: 28 August 2016
  4. # License: GPLv3
  5. # Website: https://github.com/trizen
  6. # https://projecteuler.net/problem=102
  7. use 5.010;
  8. use strict;
  9. use warnings;
  10. require Imager;
  11. my $count = 0;
  12. my $green = Imager::Color->new(0, 255, 0);
  13. while (<>) {
  14. chomp;
  15. my @triangles = split(/,/);
  16. my @x = @triangles[0,2,4];
  17. my @y = @triangles[1,3,5];
  18. my ($width, $height) = (2000, 2000);
  19. my $img = Imager->new(xsize => $width, ysize => $height);
  20. $img->polygon(
  21. points => [
  22. [$x[0]+$width/2, $y[0]+$height/2],
  23. [$x[1]+$width/2, $y[1]+$height/2],
  24. [$x[2]+$width/2, $y[2]+$height/2],
  25. ],
  26. color => $green,
  27. );
  28. ($img->getpixel(x => $width/2, y => $height/2)->rgba)[1] == 255
  29. && ++$count;
  30. }
  31. say $count;