chiark / gitweb /
quacks-ingredients: wip counts
[reprap-play.git] / quacks-ingredients-counts
1 #!/usr/bin/perl -w
2
3 use strict;
4
5 use Data::Dumper;
6 use POSIX;
7
8 $_=<DATA>; chomp or die;
9 our @names = split /\t/, $_;
10
11 our %count;
12
13 foreach my $nspots (qw(1 2 3 4)) {
14     $_=<DATA>; chomp or die;
15     my @l = split /\t/, $_;
16     foreach my $i (0..$#names) { $count{$names[$i]}{$nspots} = $l[$i] || 0; }
17 }
18
19 $_ = Dumper(\%count);
20 s{^}{// }mg;
21
22 our $name;
23
24 sub wrtoplevel () {
25     print "module $name(){ ////toplevel\n";
26     my $cs = $count{$name};
27     my $total = 0; $total += $_ foreach values %$cs;
28     my $rowsz = ceil(sqrt($total));
29     my $nrows = ceil($total / $rowsz);
30     my $ix = 0;
31     print "// total=$total, rowsz=$rowsz, nrows=$nrows\n";
32     foreach my $nspots (sort keys %$cs) {
33         my $c = $cs->{$nspots};
34         print <<END;
35   union(){
36     Frame(\$phase, token_pitch * [ $rowsz + 0.5, $nrows ]);
37     \$nspots = $nspots;
38 END
39         while ($c--) {
40             my $xy = sprintf "[ %5.1f, %5.1f ]",
41                 int($ix / $nrows) - 0.5 * ($rowsz-1),
42                 $ix % $nrows - 0.5 * ($nrows-1);
43             print "    translate(token_pitch * $xy) Token_L();\n";
44             $ix++;
45         }
46         print <<END;
47   };
48 END
49     }
50     print "}\n";
51 }
52
53 foreach $name (sort keys %count) {
54     wrtoplevel();
55 }
56
57 STDOUT->error and die $!;
58
59 __DATA__
60 White   Green   Blue    Red     Yellow  Purple  Black   Orange
61 20      15      14      12      13      15      18      20
62 8       10      10      8       6
63 4
64         13      10      10      10