chiark / gitweb /
quacks-ingredients: wip counts
[reprap-play.git] / quacks-ingredients-counts
index 0affe2efe4eee8892bd45228c0fbaa23c969cd29..870970a9c718d296b1aca93b2a98ac8780ae34b1 100755 (executable)
@@ -1,6 +1,9 @@
 #!/usr/bin/perl -w
 
+use strict;
+
 use Data::Dumper;
+use POSIX;
 
 $_=<DATA>; chomp or die;
 our @names = split /\t/, $_;
@@ -10,12 +13,47 @@ our %count;
 foreach my $nspots (qw(1 2 3 4)) {
     $_=<DATA>; chomp or die;
     my @l = split /\t/, $_;
-    foreach my $i (0..$#names) { $count{$names[$i]}{$nspots} = $l[$i] // 0; }
+    foreach my $i (0..$#names) { $count{$names[$i]}{$nspots} = $l[$i] || 0; }
 }
 
 $_ = Dumper(\%count);
 s{^}{// }mg;
-print or die $!;
+
+our $name;
+
+sub wrtoplevel () {
+    print "module $name(){ ////toplevel\n";
+    my $cs = $count{$name};
+    my $total = 0; $total += $_ foreach values %$cs;
+    my $rowsz = ceil(sqrt($total));
+    my $nrows = ceil($total / $rowsz);
+    my $ix = 0;
+    print "// total=$total, rowsz=$rowsz, nrows=$nrows\n";
+    foreach my $ndots (sort keys %$cs) {
+       my $c = $cs->{$ndots};
+       print <<END;
+  union(){
+    \$ndots = $ndots;
+END
+       while ($c--) {
+           my $xy = sprintf "[ %5.1f, %5.1f ]",
+               $ix / $nrows - 0.5 * $rowsz,
+               $ix % $nrows - 0.5 * $nrows;
+           print "    translate(token_pitch * $xy) Token_L();\n";
+           $ix++;
+       }
+       print <<END;
+  };
+END
+    }
+    print "}\n";
+}
+
+foreach $name (sort keys %count) {
+    wrtoplevel();
+}
+
+STDOUT->error and die $!;
 
 __DATA__
 White  Green   Blue    Red     Yellow  Purple  Black   Orange