-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path1_no_caching.py
69 lines (51 loc) · 1.76 KB
/
1_no_caching.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
class Point:
def __init__(self, x, y):
self.y = y
self.x = x
def draw_point(p):
print('.', end='')
# ^^ you are given this
# vv you are working with this
class Line:
def __init__(self, start, end):
self.end = end
self.start = start
class Rectangle(list):
""" Represented as a list of lines. """
def __init__(self, x, y, width, height):
super().__init__()
self.append(Line(Point(x, y), Point(x + width, y)))
self.append(Line(Point(x + width, y), Point(x + width, y + height)))
self.append(Line(Point(x, y), Point(x, y + height)))
self.append(Line(Point(x, y + height), Point(x + width, y + height)))
class LineToPointAdapter(list):
count = 0
def __init__(self, line):
self.count += 1
print(f'{self.count}: Generating points for line '
f'[{line.start.x},{line.start.y}]→'
f'[{line.end.x},{line.end.y}]')
left = min(line.start.x, line.end.x)
right = max(line.start.x, line.end.x)
top = min(line.start.y, line.end.y)
bottom = min(line.start.y, line.end.y)
if right - left == 0:
for y in range(top, bottom):
self.append(Point(left, y))
elif line.end.y - line.start.y == 0:
for x in range(left, right):
self.append(Point(x, top))
def draw(rcs):
print("\n\n--- Drawing some stuff ---\n")
for rc in rcs:
for line in rc:
adapter = LineToPointAdapter(line)
for p in adapter:
draw_point(p)
if __name__ == '__main__':
rs = [
Rectangle(1, 1, 10, 10),
Rectangle(3, 3, 6, 6)
]
draw(rs)
draw(rs)