-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender.py
200 lines (171 loc) · 6.94 KB
/
render.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
from typing import List, Tuple
import svgwrite
from enum import Enum, auto
import yaml
from direction import Direction
class Side(Enum):
TOP = auto()
BOTTOM = auto()
LEFT = auto()
RIGHT = auto()
class RenderGrid:
def __init__(self, settings_filename: str):
with open(settings_filename) as file:
self.settings = yaml.full_load(file)
self.drawing = svgwrite.Drawing(size=tuple(self.settings['board_size']))
# self.drawing.add(self.drawing.rect(size=('100%', '100%'),background={'fill':'pink'}))
self.count = self.settings['size']
self.spacing = self.settings['spacing']
self.offset = self.settings['offset']
self.x_text_offset = self.settings['x_text_offset']
self.y_text_offset = self.settings['y_text_offset']
self.circle_radius = self.settings['circle_radius']
def center(self, row: int, column: int) -> Tuple[int, int]:
x = self.offset + int((column - 0.5) * self.spacing)
y = self.offset + int((row - 0.5) * self.spacing)
return x, y
def draw_cells(self):
for i in range(self.count + 1):
self.drawing.add(
self.drawing.line(
(self.offset + i * self.spacing, self.offset),
(self.offset + i * self.spacing, self.offset + self.spacing * 9),
**self.settings['cell_line']
)
)
self.drawing.add(
self.drawing.line(
(self.offset, self.offset + i * self.spacing),
(self.offset + self.spacing * 9, self.offset + i * self.spacing),
**self.settings['cell_line']
)
)
def shade_cell(self, row: int, col: int, colour: str):
self.drawing.add(
self.drawing.rect(
(self.offset + (row - 1) * self.spacing, self.offset + (col - 1) * self.spacing),
(self.spacing, self.spacing),
fill=colour
)
)
def draw_border(self):
self.drawing.add(
self.drawing.line(
(self.offset, self.offset),
(self.offset, self.offset + self.spacing * self.count),
**self.settings['border_line']
)
)
self.drawing.add(
self.drawing.line(
(self.offset, self.offset),
(self.offset + self.spacing * self.count, self.offset),
**self.settings['border_line']
)
)
self.drawing.add(
self.drawing.line(
(self.offset + self.spacing * self.count, self.offset),
(self.offset + self.spacing * self.count, self.offset + self.spacing * self.count),
**self.settings['border_line']
)
)
self.drawing.add(
self.drawing.line(
(self.offset, self.offset + self.spacing * self.count),
(self.offset + self.spacing * self.count, self.offset + self.spacing * self.count),
**self.settings['border_line']
)
)
def draw_boxes(self):
for i in range(3, self.count, 3):
self.drawing.add(
self.drawing.line(
(self.offset + i * self.spacing, self.offset),
(self.offset + i * self.spacing, self.offset + self.spacing * 9),
**self.settings['box_line']
)
)
self.drawing.add(
self.drawing.line(
(self.offset, self.offset + i * self.spacing),
(self.offset + self.spacing * 9, self.offset + i * self.spacing),
**self.settings['box_line']
)
)
def draw_line(self, r1: int, c1: int, r2: int, c2: int, category: str):
self.drawing.add(self.drawing.line(self.center(r1, c1),self.center(r2, c2), **self.settings[category]))
def draw_diagonals(self):
self.drawing.add(
self.drawing.line(
(self.offset, self.offset),
(self.offset + self.spacing * self.count, self.offset + self.spacing * self.count),
**self.settings['diagonal_line']
)
)
self.drawing.add(
self.drawing.line(
(self.offset, self.offset + self.spacing * self.count),
(self.offset + self.spacing * self.count, self.offset),
**self.settings['diagonal_line']
)
)
def draw_number(self, row: int, col: int, value: int, known: bool):
x = self.offset + int((col - 1) * self.spacing) + self.x_text_offset
y = self.offset + int(row * self.spacing) + self.y_text_offset
self.drawing.add(
self.drawing.text(text=str(value), insert=(x, y),
**self.settings['number_text']['known' if known else 'unknown'])
)
def draw_circle(self, row: int, column: int, category: str) -> None:
x, y = self.center(row, column)
self.drawing.add(
self.drawing.circle(center=(x, y), r=self.circle_radius, **(self.settings[category]))
)
def draw_arrow(self, r1: int, c1: int, r2: int, c2: int) -> None:
direction = Direction.create(r1, c1, r2, c2)
print(direction.name)
x1, y1 = self.center(r1, c1)
x2, y2 = self.center(r2, c2)
self.drawing.add(self.drawing.line((x1, y1), (x2, y2), **self.settings['arrow_line']))
self.draw_circle(r1, c1)
(dx1, dy1), (dx2, dy2) = direction.arrow(20)
self.drawing.add(self.drawing.line((x2, y2), (x2 + dx1, y2 + dy1), **self.settings['arrow_line']))
self.drawing.add(self.drawing.line((x2, y2), (x2 + dx2, y2 + dy2), **self.settings['arrow_line']))
def draw_outside_number(self, row: int, column: int, value: int) -> None:
pass
def draw_thermometer(self, cells: List) -> None:
pass
def draw_shaded_cell(self, row: int, column: int, colour) -> None:
pass
def draw_region_outline(self, cells: List[Tuple[int, int]]) -> None:
pass
def draw_small_total(self, row: int, column: int, value: int) -> None:
pass
def draw_x(self, r1: int, c1: int, r2: int, c2: int) -> None:
pass
def draw_v(self, r1: int, c1: int, r2: int, c2: int) -> None:
pass
def draw_node_circle(r1: int, c1: int, r2: int, c2: int, values: List[int]) -> None:
pass
def draw_dot(r1: int, c1: int, r2: int, c2: int, black: bool) -> None:
pass
def write(self):
return self.drawing.save(pretty=True)
#
# rg = RenderGrid("drawing.yaml")
# rg.draw_cells()
# rg.draw_boxes()
# rg.draw_border()
# rg.draw_diagonals()
# rg.draw_number(5, 5, 5)
# rg.draw_circle(1, 1)
# rg.draw_arrow(5,5,3,3)
# rg.draw_arrow(5,5,7,3)
# rg.draw_arrow(5,5,3,7)
# rg.draw_arrow(5,5,7,7)
# rg.draw_arrow(5,5,5,3)
# rg.draw_arrow(5,5,7,5)
# rg.draw_arrow(5,5,5,7)
# rg.draw_arrow(5,5,3,5)
# rg.write()