forked from daleathan/ProgrammingInPython3-MarkSummerfield
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShapeAlt_ans.py
More file actions
executable file
·301 lines (230 loc) · 6.59 KB
/
ShapeAlt_ans.py
File metadata and controls
executable file
·301 lines (230 loc) · 6.59 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/usr/bin/env python3
# Copyright (c) 2008-11 Qtrac Ltd. All rights reserved.
# This program or module is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version. It is provided for educational
# purposes and is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
"""
This module provides the Point and Circle classes.
>>> point = Point()
>>> point
Point(0, 0)
>>> point.x = 12
>>> str(point)
'(12, 0)'
>>> a = Point(3, 4)
>>> b = Point(3, 4)
>>> a == b
True
>>> a == point
False
>>> a != point
True
>>> circle = Circle(2)
>>> circle
Circle(2, 0, 0)
>>> circle.radius = 3
>>> circle.x = 12
>>> circle
Circle(3, 12, 0)
>>> a = Circle(4, 5, 6)
>>> b = Circle(4, 5, 6)
>>> a == b
True
>>> a == circle
False
>>> a != circle
True
"""
import math
class Point:
def __init__(self, x=0, y=0):
"""A 2D cartesian coordinate
>>> point = Point()
>>> point
Point(0, 0)
"""
self.x = x
self.y = y
@property
def distance_from_origin(self):
"""The distance of the point from the origin
>>> point = Point(3, 4)
>>> point.distance_from_origin
5.0
"""
return math.hypot(self.x, self.y)
def __add__(self, other):
"""Returns a new Point whose coordinate are the sum of this
one's and the other one's
>>> p = Point(2, 4)
>>> q = p + Point(3, 5)
>>> q
Point(5, 9)
"""
return Point(self.x + other.x, self.y + other.y)
def __iadd__(self, other):
"""Returns this Point with its coordinate set to the sum of this
one's and the other one's
>>> p = Point(2, 4)
>>> p += Point(3, 5)
>>> p
Point(5, 9)
"""
self.x += other.x
self.y += other.y
return self
def __sub__(self, other):
"""Returns a new Point whose coordinate are the difference of this
one's and the other one's
>>> p = Point(2, 4)
>>> q = p - Point(3, 5)
>>> q
Point(-1, -1)
"""
return Point(self.x - other.x, self.y - other.y)
def __isub__(self, other):
"""Returns this Point with its coordinate set to the difference
of this one's and the other one's
>>> p = Point(2, 4)
>>> p -= Point(3, 5)
>>> p
Point(-1, -1)
"""
self.x -= other.x
self.y -= other.y
return self
def __mul__(self, other):
"""Returns a new Point whose coordinate is this one's multiplied
by the other number
>>> p = Point(2, 4)
>>> q = p * 3
>>> q
Point(6, 12)
"""
return Point(self.x * other, self.y * other)
def __imul__(self, other):
"""Returns this Point with its coordinate set to this one's
multiplied by the other number
>>> p = Point(2, 4)
>>> p *= 3
>>> p
Point(6, 12)
"""
self.x *= other
self.y *= other
return self
def __truediv__(self, other):
"""Returns a new Point whose coordinate is this one's divided
by the other number
>>> p = Point(2, 4)
>>> q = p / 2
>>> q
Point(1.0, 2.0)
"""
return Point(self.x / other, self.y / other)
def __itruediv__(self, other):
"""Returns this Point with its coordinate set to this one's
divided by the other number
>>> p = Point(2, 4)
>>> p /= 2
>>> p
Point(1.0, 2.0)
"""
self.x /= other
self.y /= other
return self
def __floordiv__(self, other):
"""Returns a new Point whose coordinate is this one's floor
divided by the other number
>>> p = Point(2, 4)
>>> q = p // 2
>>> q
Point(1, 2)
"""
return Point(self.x // other, self.y // other)
def __ifloordiv__(self, other):
"""Returns this Point with its coordinate set to this one's
floor divided by the other number
>>> p = Point(2, 4)
>>> p //= 2
>>> p
Point(1, 2)
"""
self.x //= other
self.y //= other
return self
def __eq__(self, other):
return self.x == other.x and self.y == other.y
def __repr__(self):
return ("{0.__class__.__name__}({0.x!r}, {0.y!r})".format(
self))
def __str__(self):
return "({0.x!r}, {0.y!r})".format(self)
class Circle(Point):
def __init__(self, radius, x=0, y=0):
"""A Circle
>>> circle = Circle(2)
>>> circle
Circle(2, 0, 0)
"""
super().__init__(x, y)
self.radius = radius
@property
def area(self):
"""The circle's area
>>> circle = Circle(3)
>>> a = circle.area
>>> int(a)
28
"""
return math.pi * (self.radius ** 2)
@property
def edge_distance_from_origin(self):
"""The distance of the circle's edge from the origin
>>> circle = Circle(2, 3, 4)
>>> circle.edge_distance_from_origin
3.0
"""
return abs(self.distance_from_origin - self.radius)
@property
def circumference(self):
"""The circle's circumference
>>> circle = Circle(3)
>>> d = circle.circumference
>>> int(d)
18
"""
return 2 * math.pi * self.radius
@property
def radius(self):
"""
The circle's radius
>>> circle = Circle(-2)
Traceback (most recent call last):
...
AssertionError: radius must be nonzero and non-negative
>>> circle = Circle(4)
>>> circle.radius = -1
Traceback (most recent call last):
...
AssertionError: radius must be nonzero and non-negative
>>> circle.radius = 6
"""
return self.__radius
@radius.setter
def radius(self, radius):
assert radius > 0, "radius must be nonzero and non-negative"
self.__radius = radius
def __eq__(self, other):
return self.radius == other.radius and super().__eq__(other)
def __repr__(self):
return ("{0.__class__.__name__}({0.radius!r}, {0.x!r}, "
"{0.y!r})".format(self))
if __name__ == "__main__":
import doctest
doctest.testmod()