matrix_visual.pl 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/usr/bin/perl
  2. # Author: Daniel "Trizen" Șuteu
  3. # License: GPLv3
  4. # Date: 13 August 2016
  5. # Website: https://github.com/trizen
  6. # Display a matrix as a rectangle packed with circles.
  7. # Brighter circles represent larger numerical values,
  8. # while dimmer circles represent smaller numerical values.
  9. use 5.010;
  10. use strict;
  11. use warnings;
  12. use Imager;
  13. use List::MoreUtils qw(minmax);
  14. my @matrix = (
  15. [131, 673, 234, 103, 18],
  16. [201, 96, 342, 965, 150],
  17. [630, 803, 746, 422, 111],
  18. [537, 699, 497, 121, 956],
  19. [805, 732, 524, 37, 331],
  20. );
  21. #<<<
  22. # Reading a matrix from the standard input.
  23. #~ @matrix = ();
  24. #~ while(<>) {
  25. #~ chomp;
  26. #~ push @matrix, [split(/,/, $_)];
  27. #~ }
  28. #>>>
  29. my $max_color = 2**16 - 1;
  30. my $scale_factor = 10;
  31. my $radius = $scale_factor / atan2(0, -'inf');
  32. my $space = $radius / 2;
  33. my $img = Imager->new(
  34. xsize => @{$matrix[0]} * $scale_factor,
  35. ysize => @matrix * $scale_factor,
  36. channels => 3,
  37. );
  38. my ($min, $max) = minmax(map { @$_ } @matrix);
  39. foreach my $i (0 .. $#matrix) {
  40. my $row = $matrix[$i];
  41. foreach my $j (0 .. $#{$row}) {
  42. my $cell = $row->[$j];
  43. my $value = int($max_color / ($max - $min) * ($cell - $min));
  44. my $color = Imager::Color->new(sprintf("#%06x", $value));
  45. $img->circle(
  46. r => $radius,
  47. x => int($j * $scale_factor + $radius + $space),
  48. y => int($i * $scale_factor + $radius + $space),
  49. color => $color,
  50. );
  51. }
  52. }
  53. $img->write(file => 'matrix_circle.png');