-
Notifications
You must be signed in to change notification settings - Fork 2
/
objects.py
109 lines (95 loc) · 3.8 KB
/
objects.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
import numpy as np
class Rectangle(object):
"""Rectangle class"""
def __init__(self, context, x, y, w, h,
fill_rgb=(1, 1, 1),
stroke=False,
line_width=5.0,
gradient=None,
rot_angle=0):
self.context = context
self.x = x
self.y = y
self.x_center = x + w / 2
self.y_center = y + h / 2
self.w = w
self.h = h
self.fill_rgb = fill_rgb
self.stroke = stroke
self.line_width = line_width
self.gradient = gradient
self.rot_angle = rot_angle
def draw(self):
self.context.save()
if self.gradient is None:
self.context.set_source_rgb(self.fill_rgb[0], self.fill_rgb[1], self.fill_rgb[2])
else:
self.context.set_source(self.gradient)
self.context.set_line_width(self.line_width)
self.context.translate(self.x_center, self.y_center)
self.context.rotate(self.rot_angle)
self.context.translate(-self.w / 2, -self.h / 2)
self.context.rectangle(0, 0, self.w, self.h)
if self.stroke:
self.context.stroke_preserve()
self.context.fill()
self.context.restore()
class Circle(object):
"""Circle class"""
def __init__(self, context, x, y, r, fill_rgb=(1, 1, 1), stroke=False, line_width=5.0, gradient=None, scaling=None):
self.context = context
self.x = x
self.y = y
self.r = r
self.fill_rgb = fill_rgb
self.stroke = stroke
self.line_width = line_width
self.gradient = gradient
self.scaling = scaling
def draw(self):
self.context.save()
if self.gradient is None:
self.context.set_source_rgb(self.fill_rgb[0], self.fill_rgb[1], self.fill_rgb[2])
else:
self.context.set_source(self.gradient)
self.context.set_line_width(self.line_width)
if self.scaling is not None:
self.context.translate(self.x, self.y)
self.context.scale(self.scaling[0], self.scaling[1])
self.context.arc(0, 0, self.r, 0, 2 * np.pi) ## (x, y, r, theta_start, theta_end)
else:
self.context.arc(self.x, self.y, self.r, 0, 2 * np.pi) ## (x, y, r, theta_start, theta_end)
if self.stroke:
self.context.stroke_preserve()
self.context.fill()
self.context.restore()
class Arrow(object):
"""Arrow class"""
def __init__(self, context, x, y, length, angle, fill_rgb=(1, 1, 1), stroke=False, line_width=1.0, stroke_rgb=(0, 0, 0), gradient=None, scaling=None):
self.context = context
self.x = x
self.y = y
self.length = length
self.angle = angle
self.points = [(x, y)]
self.points.append((x + self.length * np.cos(self.angle), y - self.length * np.sin(self.angle)))
self.points.append((x + (self.length * 0.9) * np.cos(self.angle + 0.20), y - (self.length * 0.9) * np.sin(self.angle + 0.20)))
self.points.append((x + (self.length * 0.9) * np.cos(self.angle - 0.20), y - (self.length * 0.9) * np.sin(self.angle - 0.20)))
self.fill_rgb = fill_rgb
self.stroke = stroke
self.line_width = line_width
self.stroke_rgb = stroke_rgb
self.gradient = gradient
self.scaling = scaling
def draw(self):
self.context.save()
self.context.set_line_width(self.line_width)
self.context.set_source_rgb(*self.stroke_rgb)
self.context.move_to(*self.points[0])
self.context.line_to(*self.points[1])
self.context.line_to(*self.points[2])
self.context.move_to(*self.points[1])
self.context.line_to(*self.points[3])
self.context.move_to(0, 0)
self.context.stroke_preserve()
self.context.restore()