-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhexplot.py
63 lines (48 loc) · 1.6 KB
/
hexplot.py
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
import svgwrite
import svgwrite.shapes
import svgwrite.image
import svgwrite.pattern
from svgwrite import cm, mm
import json
import math
with open('test.json') as file:
data = json.load(file)
def hex_coords(x=1, y=1):
R = 50
r = math.sqrt(3) / 2 * R
x = (2 * r) * x + r + (y % 2) * r
y = (1.5 * R) * y + R
return [(int(x), int(y)) for (x, y) in [
(x + 0, y + R),
(x + r, y + R/2),
(x + r, y - R/2),
(x + 0, y - R),
(x - r, y - R/2),
(x - r, y + R/2),
]]
def draw(name):
dwg = svgwrite.Drawing(filename=name, debug=True)
images = set(v['image'] for v in data['hexes'].values())
image_map = {}
# <pattern id="pattern1" height="100%" width="100%" patternContentUnits="objectBoundingBox">
# <image height="1" width="1" preserveAspectRatio="none" xlink:href="file:///Users/jhollocombe/Documents/hexmap/tex_Water.jpg" />
# </pattern>
n = 1
for image in images:
pattern = svgwrite.pattern.Pattern(size=("100%", "100%"))
pattern["id"] = "pattern" + str(n)
pattern["patternContentUnits"] = "objectBoundingBox"
img = svgwrite.image.Image(image, size=(1, 1))
img.stretch()
pattern.add(img)
dwg.defs.add(pattern)
image_map[image] = pattern["id"]
n += 1
for v in data['hexes'].values():
hex = svgwrite.shapes.Polygon(points=hex_coords(v['q'], v['r']))
hex["style"] = "stroke: #999DA3;"
hex["fill"] = "url(#%s)" % image_map[v['image']]
dwg.add(hex)
dwg.save(pretty=True)
if __name__ == '__main__':
draw('test.svg')