-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPolygon_perimeter_calc.py
59 lines (45 loc) · 1.66 KB
/
Polygon_perimeter_calc.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
from tkinter import *
from collections import namedtuple
from sympy.geometry import *
class sketch_window(object):
def __init__(self):
self.root = Tk()
self.reset_button = Button(self.root, text='reset', command=self.reset)
self.reset_button.grid(row=0, column=0)
self.c = Canvas(self.root, bg='black', width=600, height=600)
self.c.grid(row=1, columnspan=3)
self.setup()
self.root.mainloop()
def setup(self):
self.line_width = 1
self.color = 'white'
self.c.bind('<Button-1>', self.click)
self.c.bind('<Double-Button-1>', self.close_polygon)
self.points = []
self.polygon = None
def reset(self):
self.setup()
self.c.delete("all")
def click(self, event):
if self.polygon:
self.reset()
else:
pt = Point(event.x, event.y)
self.points.append(pt)
if len(self.points) > 1:
self.draw_line(*self.points[-2:])
def close_polygon(self, event):
self.click(event)
self.draw_line(self.points[0], self.points[-1])
self.polygon = Polygon(*self.points)
self.show_perimeter()
def draw_line(self, p1, p2):
self.c.create_line(*p1, *p2, width=self.line_width, fill=self.color)
def show_perimeter(self):
perimeter = self.polygon.perimeter.evalf()
area = abs(self.polygon.area.evalf())
msg = 'Perimeter: {:.2f}'.format(perimeter)
msg += '\n Area: {:.2f}'.format(area)
self.c.create_text(*self.polygon.centroid.evalf(), text=msg, fill='white')
if __name__ == '__main__':
sketch_window()