-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathEX12.8.py
176 lines (135 loc) · 5.21 KB
/
EX12.8.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# 12.8 (Tkinter: two circles intersect?) Using the Circle2D class you defined in Exercise
# 8.18, write a program that enables the user to specify the location and size of
# two circles and displays whether the circles intersect, as shown in Figure 12.20.
# Enable the user to point the mouse inside a circle and drag it. As a circle is being
# dragged, the program updates the circle’s center coordinates and its radius in the
# text fields.
import math
from tkinter import *
class Circle2D:
def __init__(self, x=0, y=0, radius=0):
self.__x__ = x
self.__y__ = y
self.__r__ = radius
def getX(self):
return self.__x__
def getY(self):
return self.__y__
def getRadius(self):
return self.__r__
def setX(self, x):
self.__x__ = x
def setY(self, y):
self.__y__ = y
def setRadius(self, radius):
self.__r__ = radius
def getArea(self):
return math.pi * self.getRadius() ** 2
def getPerimeter(self):
return math.pi * 2 * self.getRadius()
def containsPoint(self, x, y):
dis = math.sqrt((self.getX() - x) ** 2 + (self.getY() - y) ** 2)
return True if dis <= self.getRadius() else False
def contains(self, circle):
dis = math.sqrt((self.getX() - circle.getX()) ** 2 + (self.getY() - circle.getY()) ** 2)
return True if self.getRadius() >= dis + circle.getRadius() else False
def overlaps(self, circle):
dis = math.sqrt((self.getX() - circle.getX()) ** 2 + (self.getY() - circle.getY()) ** 2)
return True if dis <= self.getRadius() + circle.getRadius() else False
def __contains__(self, circle):
return circle.contains(self)
def __eq__(self, circle):
return True if self.getRadius() == circle.getRadius() else False
def __ne__(self, circle):
return True if self.getRadius() != circle.getRadius() else False
def __cmp__(self, circle):
if self.getRadius() > circle.getRadius():
return 1
elif self.getRadius() < circle.getRadius():
return -1
else:
return 0
def __lt__(self, circle):
return self.getRadius() < circle.getRadius()
def __gt__(self, circle):
return self.getRadius() > circle.getRadius()
def __ge__(self, circle):
return self.getRadius() >= circle.getRadius()
def __le__(self, circle):
return self.getRadius() <= circle.getRadius()
def displayCircle(c, text):
canvas.delete(text)
canvas.create_oval(c.getX() - c.getRadius(),
c.getY() - c.getRadius(), c.getX() + c.getRadius(), c.getY() + c.getRadius(), tags=text)
canvas.create_text(c.getX(), c.getY(), text=text, tags=text)
def mouseMoved(event):
if c1.containsPoint(event.x, event.y):
c1.setX(event.x)
c1.setY(event.y)
displayCircle(c1, "c1")
c1x.set(c1.getX())
c1y.set(c1.getY())
c1Radius.set(c1.getRadius())
if c1.overlaps(c2):
label["text"] = "Two circles intersect"
else:
label["text"] = "Two circles don't intersect"
elif c2.containsPoint(event.x, event.y):
c2.setX(event.x)
c2.setY(event.y)
displayCircle(c2, "c2")
c2x.set(c2.getX())
c2y.set(c2.getY())
c2Radius.set(c2.getRadius())
if c1.overlaps(c2):
label["text"] = "Two circles intersect"
else:
label["text"] = "Two circles don't intersect"
def redraw():
c1 = Circle2D(c1x.get(), c1y.get(), c1Radius.get())
c2 = Circle2D(c2x.get(), c2y.get(), c2Radius.get())
displayCircle(c1, "c1")
displayCircle(c2, "c2")
window = Tk() # Create a window
window.title("Two Circles") # Set title
width = 250
height = 100
label = Label(window, text="Two circles intersect")
label.pack()
canvas = Canvas(window, width=width, height=height)
canvas.pack()
canvas.bind("<B1-Motion>", mouseMoved)
c1 = Circle2D(10, 10, 30)
c2 = Circle2D(30, 40, 20)
displayCircle(c1, "c1")
displayCircle(c2, "c2")
frame1 = Frame(window)
frame1.pack()
Label(frame1, text="C1 Center x:").grid(row=1, column=1)
Label(frame1, text="C1 Center y:").grid(row=2, column=1)
Label(frame1, text="C1 Radius:").grid(row=3, column=1)
c1x = DoubleVar()
c1x.set(c1.getX())
Entry(frame1, width=5, justify=RIGHT, textvariable=c1x).grid(row=1, column=2)
c1y = DoubleVar()
c1y.set(c1.getY())
Entry(frame1, width=5, justify=RIGHT, textvariable=c1y).grid(row=2, column=2)
c1Radius = DoubleVar()
c1Radius.set(c1.getRadius())
Entry(frame1, width=5, justify=RIGHT, textvariable=c1Radius).grid(row=3, column=2)
Label(frame1, text="C2 Center x:").grid(row=1, column=3)
Label(frame1, text="C2 Center y:").grid(row=2, column=3)
Label(frame1, text="C2 Radius:").grid(row=3, column=3)
c2x = DoubleVar()
c2x.set(c2.getX())
Entry(frame1, width=5, justify=RIGHT, textvariable=c2x).grid(row=1, column=4)
c2y = DoubleVar()
c2y.set(c2.getY())
Entry(frame1, width=5, justify=RIGHT, textvariable=c2y).grid(row=2, column=4)
c2Radius = DoubleVar()
c2Radius.set(c2.getRadius())
Entry(frame1, width=5, justify=RIGHT, textvariable=c2Radius).grid(row=3, column=4)
frame2 = Frame(window)
frame2.pack()
Button(frame2, text="Redraw Circles", command=redraw).pack()
window.mainloop() # Create an event loop