-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgraphics.py
147 lines (114 loc) · 4.21 KB
/
graphics.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
import os
if 'TK_SILENCE_DEPRECATION' not in os.environ:
os.environ['TK_SILENCE_DEPRECATION']='1'
from tkinter import Tk,Canvas,Label
import sys
def extent(points):
"""
Computes the extent of a list of (x,y) points
:param points: a list of (x,y) points
:return: the extent as xmin, ymin, xmax, ymax
"""
xmax = -sys.maxsize - 1
ymax = -sys.maxsize - 1
ymin = sys.maxsize
xmin = sys.maxsize
for p in points:
x, y = p
if x < xmin:
xmin = x
if x > xmax:
xmax = x
if y < ymin:
ymin = y
if y > ymax:
ymax = y
return xmin, ymin, xmax, ymax
class Graphics:
"""
Wraps tk graphics primitives to make graphics concepts easier for beginners.
"""
def __init__(self, w, h):
self.tk = Tk()
self.width = w
self.height = h
self.canvas = Canvas(self.tk, width=self.width, height=self.height)
self.canvas.pack()
def circle(self, center, radius, **kwargs):
x, y = center
self.checkbounds([(x - radius, y - radius), (x + radius, y + radius)])
return self.canvas.create_oval(x - radius, y - radius, x + radius, y + radius, kwargs)
def show_grid(self, size=50, color='black'):
w = self.width # Get current width of canvas
h = self.height # Get current height of canvas
# Creates all vertical lines at intevals of 100
for i in range(0, w + size, size):
self.line((i, 0), (i, h), fill=color, tag='grid_line')
label = Label(self.canvas, text=str(i),fg='red')
x_off = 0
if i > 0 :
x_off = 15
if i == w:
print("w....")
x_off = 25
label.place(x =i - x_off, y = 0 )
# Creates all horizontal lines at intevals of 100
for i in range(0, h + size, size):
label = Label(self.canvas, text=str(i),fg='red')
y_off = 0
if i > 0 :
y_off = 15
if i == h :
y_off = 20
label.place(x = 0, y = i - y_off)
self.line((0, i), (w, i), fill=color, tag='grid_line')
def hide_grid(self):
self.canvas.delete('grid_line')
def rectangle(self, topleft, bottomright, **kwargs):
self.checkbounds([topleft, bottomright])
xmin, ymin, xmax, ymax = extent([topleft, bottomright])
return self.canvas.create_rectangle(xmin, ymin, xmax, ymax, kwargs)
def polygon(self, points, **kwargs):
self.checkbounds(points)
return self.canvas.create_polygon(points, kwargs)
def line(self, a, b, **kwargs):
self.checkbounds([a, b])
xmin, ymin, xmax, ymax = extent([a, b])
return self.canvas.create_line(xmin, ymin, xmax, ymax, kwargs)
def coords(self, obj, *args):
points = self.canvas.coords(obj,*args)
coords = []
for i in range(0, len(points), 2):
coords.append((points[i], points[i + 1]))
return coords
def move(self, obj, delta):
self.canvas.move(obj, delta[0], delta[1])
def update(self, after=20):
self.canvas.update()
self.canvas.after(after)
def show(self):
self.tk.mainloop()
def checkbounds(self, points):
xmin, ymin, xmax, ymax = extent(points);
if xmax > self.width:
print(
"WARNING:The shape extends beyond the canvas: x=%d is greater than canvas width %d" % (
xmax, self.width))
if xmin < 0:
print("WARNING: The minimum boundary x=%d cannot be less than 0" % xmin)
if ymax > self.height:
print(
"WARNING:The shape extends beyond the canvas: y=%d is greater than window height %d " % (
ymax, self.height))
if ymin < 0:
print("WARNING: The minimum boundary y=%d cannot be less than 0" % ymin)
class Group:
def __init__(self, graphics):
self.graphics = graphics
self.objects = []
def add(self, obj):
self.objects.append(obj)
def move(self, delta):
for obj in self.objects:
self.graphics.move(obj, delta)
self.graphics.update()