-
Notifications
You must be signed in to change notification settings - Fork 4
/
goboard.py
executable file
·149 lines (121 loc) · 4.34 KB
/
goboard.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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/python2
import argparse
import collections
import svgwrite
import sys
DefaultSizing = {
'width':424.2,
'length':454.5,
'width_spacing':22,
'length_spacing':23.7,
'linewidth':1,
'star_diameter':4,
'unit':'mm'
}
Sizes = collections.defaultdict(lambda: dict(DefaultSizing))
Sizes["Standard 19x19"].update({
'lines':19,
'star_points':((3,3), (3,9), (3, 15), (9, 3), (9, 9), (9, 15), (15, 3), (15, 9), (15, 15))
})
Sizes["Standard 13x13"].update({
'lines':13,
'star_points':((3, 3), (3, 9), (6,6), (9, 3), (9, 9)),
})
Sizes["Standard 9x9"].update({
'lines':9,
'star_points':((2, 2), (2,6), (5, 5), (6,2), (6,6)),
})
class GoBoard(object):
def __init__(self,
width, length, width_spacing, length_spacing, linewidth,
star_diameter, star_points, lines, unit):
"""Saves the parameters we're going to use to draw the board"""
self.width = width
self.length = length
self.width_spacing = width_spacing
self.length_spacing = length_spacing
self.linewidth = linewidth
self.star_diameter = star_diameter
self.star_points = star_points
self.lines = lines
self.unit = unit
self.drawing = None
def draw(self, draw_border = True):
"""draws the go board using current dimentions"""
drawing = svgwrite.Drawing(size=(
"%f%s" % (self.width, self.unit),
"%f%s" % (self.length, self.unit)),
viewBox = ('0 0 %d %d' % (self.width, self.length)))
self.drawing = drawing
# create an initial rectangle around the drawing
stroke = "black" if draw_border else "white"
border = drawing.rect(size=(self.width, self.length), stroke = stroke, stroke_width = 1.3, fill = 'white')
drawing.add(border)
# how much room from the edge to the first horizontal line?
total_y_space = (self.lines - 1) * (self.linewidth + self.length_spacing)
y_margin = (self.length - total_y_space) / 2
total_x_space = (self.lines - 1) * (self.linewidth + self.width_spacing)
x_margin = (self.width - total_x_space) / 2
start = (x_margin, y_margin)
self.draw_lines(start, self.lines, 'across', self.linewidth, total_x_space, self.length_spacing)
self.draw_lines(start, self.lines, 'down', self.linewidth, total_y_space, self.width_spacing)
self.draw_starpoints(start)
def draw_starpoint(self, pos):
star = self.drawing.circle(pos, r=self.star_diameter)
star['fill'] = 'black'
self.drawing.add(star)
def draw_starpoints(self, grid_start):
"""Draws the star points"""
wspace = self.linewidth + self.width_spacing
lspace = self.linewidth + self.length_spacing
for point in self.star_points:
center = (grid_start[0] + point[0] * wspace, grid_start[1] + point[1] * lspace)
self.draw_starpoint(center)
def draw_lines(self, start, count, direction, width, length, spacing):
"""Draws a bunch of lines
start is a tuple of two values
direction should be 'down' or 'across'
"""
start = list(start)
if len(start) != 2:
raise ValueError("start must be a tuple of 2 values")
for i in xrange(count):
end = list(start)
if direction == 'down':
end[1] += length
else:
end[0] += length
line = self.drawing.line(start=start, end=end)
line['stroke'] = 'black'
line['stroke-width'] = width
self.drawing.add(line)
if direction == 'down':
start[0] += spacing + width
else:
start[1] += spacing + width
def write(self, filename):
"""writes the svg rendering into a file"""
if not self.drawing:
self.draw()
self.drawing.saveas(filename)
def tostring(self):
if self.drawing:
return self.drawing.tostring()
else:
return ""
def main():
parser = argparse.ArgumentParser(
description="Generate an SVG file defining a go board",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("--size", default="Standard 19x19", choices=Sizes.keys(),
help="Size of go board to generate")
parser.add_argument("-o", "--output", default="goboard.svg",
help="Output filename")
parser.add_argument("-b", "--border", default=False, action="store_true",
help="Draw a border around the entire image?")
opts = parser.parse_args()
b = GoBoard(**Sizes[opts.size])
b.draw(opts.border)
b.write(opts.output)
if __name__ == "__main__":
sys.exit(main())