-
Notifications
You must be signed in to change notification settings - Fork 4
/
hexgrid.pl
executable file
·60 lines (46 loc) · 1.56 KB
/
hexgrid.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
#!/usr/bin/perl
use strict;
use warnings;
use GD;
my @hexpoints = (
[-1, 0],
[-0.5, sqrt(3) / 2],
[0.5, sqrt(3) / 2],
[1, 0],
[0.5, -sqrt(3) / 2],
[-0.5, -sqrt(3) / 2],
);
my ($size, $fname) = @ARGV;
my $short = $size / 2 / 2;
my $long = $short * 2 / sqrt(3);
my $off = 2 * $short;
my $radius = $long * 0.9;
my $height = $short * 4;
my $width = int(3 * $long);
my $alpha = 120;
my $im = new GD::Image($width, $height, 1);
$im->alphaBlending(0);
$im->saveAlpha(1);
my $color_fg = $im->colorAllocateAlpha(255,255,255, 127);
my $color_bg = $im->colorAllocateAlpha(0,0,0, $alpha);
#$im->setAntiAliased($white);
$im->filledRectangle(0, 0, $width, $height, $color_bg);
sub drawhex{
my ($im, $x, $y, $r, $color) = @_;
my $poly = new GD::Polygon;
for my $point (@hexpoints){
$poly->addPt($x + $r * $point->[0], $y + $r * $point->[1]);
}
$im->filledPolygon($poly, $color);
}
drawhex($im, $width / 2, $height / 2, $radius, $color_fg);
drawhex($im, $width / 2, $height / 2 - $off, $radius, $color_fg);
drawhex($im, $width / 2, $height / 2 + $off, $radius, $color_fg);
drawhex($im, $width / 2 + $off * sqrt(3) / 2, $height / 2 + $off / 2, $radius, $color_fg);
drawhex($im, $width / 2 - $off * sqrt(3) / 2, $height / 2 + $off / 2, $radius, $color_fg);
drawhex($im, $width / 2 + $off * sqrt(3) / 2, $height / 2 - $off / 2, $radius, $color_fg);
drawhex($im, $width / 2 - $off * sqrt(3) / 2, $height / 2 - $off / 2, $radius, $color_fg);
print "Writing $width x $height hextile to $fname\n";
open OUT, '>', $fname;
binmode OUT;
print OUT $im->png;