Draws shapes (currently only squares) to PBM files
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
1.6 KiB

  1. #!/usr/bin/env perl
  2. use strict;
  3. use warnings;
  4. use POSIX;
  5. use feature 'say';
  6. sub printBorderH {
  7. my ($size, $fh, $b) = @_;
  8. for (my $i=0; $i < $b; $i++) {
  9. print $fh "0";
  10. for (my $i=0; $i < $size - 2; $i++) {
  11. print $fh " 1";
  12. }
  13. say $fh " 0";
  14. }
  15. }
  16. sub help {
  17. say "DrawShape: Draws shapes (currently only squares) to a Portable BitMap (PBM) file\n";
  18. say "Help:";
  19. say "Size must be more than 10\n";
  20. say "How to draw a shape:";
  21. say "drawshape size filename\n";
  22. say "Example:";
  23. say "drawshape 60 shape0.pbm";
  24. }
  25. sub borderV {
  26. my $b = $_[0];
  27. return "1 " x $b;
  28. }
  29. if ($#ARGV == -1) {
  30. help();
  31. } elsif ($#ARGV > 0) {
  32. my ($size, $filename) = @ARGV;
  33. if ($size < 10) {
  34. say "Size must be more than 10";
  35. exit 1;
  36. }
  37. my $b = ceil($size / 10);
  38. open(my $fh, '>', $filename) or die "Could not open file '$filename' $!";
  39. # Prints the PBM header
  40. say $fh "P1\n$size $size";
  41. # Prints the first line of 0s
  42. for (my $i=0; $i < $size - 1; $i++) {
  43. print $fh "0 ";
  44. }
  45. say $fh "0";
  46. printBorderH($size, $fh, $b);
  47. # Prints the main square body
  48. for (my $j=0; $j < $size - ($b * 2) - 2; $j++) {
  49. print $fh "0 " . borderV($b);
  50. for (my $i=0; $i < $size - ($b * 2) - 2; $i++) {
  51. print $fh "0 ";
  52. }
  53. say $fh borderV($b) . "0";
  54. }
  55. printBorderH($size, $fh, $b);
  56. # Prints the last line
  57. for (my $i=0; $i < $size - 1; $i++) {
  58. print $fh "0 ";
  59. }
  60. say $fh "0";
  61. close $fh;
  62. }