-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathvis.pl
96 lines (88 loc) · 3.11 KB
/
vis.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/perl -lw
use strict;
use JSON;
use List::Util qw(min max);
# Fixup the almost-json.
my $data = join '', <>;
$data = "[$data]";
$data =~ s/,\s*(\]|\})/$1/gsm;
my $boards = decode_json $data;
my $sz = 32;
my $hsz = $sz / 2;
my %prev;
sub draw_board {
my $board = shift;
my $height = $board->{height} * $sz;
my $width = $board->{width} * $sz;
my $hwidth = $width / 2;
if ($board->{type}) {
print "<text x='$hwidth' y='18' text-anchor='middle' fill='black' class='text'>$board->{type}</text>";
}
if (exists $board->{iter}) {
print "<text x='5' y='18' text-anchor='left' fill='black' class='text'>Iteration: $board->{iter}</text>";
}
if (exists $board->{score}) {
print "<text x='$hwidth' y='18' text-anchor='middle' fill='black' class='text'>Score: $board->{score}</text>";
}
print "<g transform='translate(5, 21)'>";
print "<rect x='0' y='0' height='$height' width='$width' stroke='black' stroke-width='2' fill='white'/>";
for my $line (@{$board->{lines}}) {
my $x = ($line->{c} + 0.5) * $sz;
my $y = ($line->{r} + 0.85) * $sz;
print "<text x='$x' y='$y' text-anchor='middle' fill='black' class='number'>$line->{value}</text>";
if (exists $line->{hz}) {
my $minx = $line->{minc} * $sz + $hsz;
my $miny = $line->{minr} * $sz + $hsz;
my $maxx = $line->{maxc} * $sz + $hsz;
my $maxy = $line->{maxr} * $sz + $hsz;
if ($line->{hz}) {
$minx -= $sz * 0.4;
$maxx += $sz * 0.4;
} else {
$miny -= $sz * 0.4;
$maxy += $sz * 0.4;
}
my $key = "$x $y";
my $val = "$minx $miny $maxx $maxy";
my $width = 5;
my $color = '#f00';
my $dash = '';
if (!defined $prev{$key} or $prev{$key} ne $val) {
$dash = '3,3';
$prev{$key} = $val;
}
if ($line->{value} == max($line->{maxr} - $line->{minr},
$line->{maxc} - $line->{minc}) + 1) {
$color = '#4c4';
}
print "<line x1='$minx' y1='$miny' x2='$maxx' y2='$maxy' stroke-width='$width' stroke='$color' stroke-dasharray='$dash' stroke-opacity='0.75'/>";
}
}
for my $dot (@{$board->{dots}}) {
my $x = $dot->{c} * $sz + $hsz;
my $y = $dot->{r} * $sz + $hsz;
print "<circle r='3' cx='$x' cy='$y' height='$height' fill='black'/>";
}
print "</g>";
return ($width, $height + 16);
}
print qq(<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<style>
.number { font-size: 28px; }
.text { font-size: 16px; }
</style>
<rect width="100%" height="100%" fill="white"></rect>
);
my $x_offset = 5;
my $y_offset = 5;
for my $board (@{$boards}) {
print "<g transform='translate($x_offset, $y_offset)'>";
my ($w, $h) = draw_board $board;
$x_offset += 5 + $w;
if ($x_offset > 1600) {
$x_offset = 5;
$y_offset += 16 + $h;
}
print "</g>";
}
print qq(</svg>);