-
Notifications
You must be signed in to change notification settings - Fork 8
/
context.py
88 lines (61 loc) · 2.23 KB
/
context.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
from collections import namedtuple
# Convenience class that allows indexing as well as x and y attribute access
XYPoint = namedtuple("XYPoint", ["x", "y"])
# BGR color values
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (255, 0, 0)
GREEN = (0, 255, 0)
RED = (0, 0, 255)
CYAN = (255, 255, 0)
MAGENTA = (255, 0, 255)
YELLOW = (0, 255, 255)
class Shape:
"""Base class for shapes to draw on the landscape."""
def __init__(self, x, y, color):
self.center = XYPoint(x, y)
self.color = color
class Circle(Shape):
def __init__(self, x, y, radius, color, thickness):
super().__init__(x, y, color)
self.radius = radius
self.thickness = thickness
class Rectangle(Shape):
def __init__(self, x, y, width, height, rotation, color):
super().__init__(x, y, color)
self.width = width
self.height = height
self.rotation = rotation
class Text(Shape):
def __init__(self, x, y, content, color, size):
super().__init__(x, y, color)
self.content = str(content)
self.size = size
class Image(Shape):
def __init__(self, filepath, x, y, width, height):
super().__init__(x, y, color=BLACK)
self.width = width
self.height = height
self.filepath = filepath
class DrawingContext:
"""Context with all information to project a drawing onto the landscape.
Args:
width (int): width of the drawing
height (int): height of the drawing
Attributes:
width (int): width of the drawing
height (int): height of the drawing
shapes (list<Shape>): shapes to draw
"""
def __init__(self, width, height):
self.width = width
self.height = height
self.shapes = []
def rect(self, x, y, width, height, rotation=0, color=WHITE):
self.shapes.append(Rectangle(x, y, width, height, rotation, color))
def circle(self, x, y, radius, color=WHITE, thickness=-1):
self.shapes.append(Circle(x, y, radius, color, thickness))
def text(self, x, y, content, color=WHITE, size=2):
self.shapes.append(Text(x, y, content, color, size))
def image(self, filepath, x, y, width, height):
self.shapes.append(Image(filepath, x, y, width, height))