forked from AllenDowney/ThinkPython
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathShape.py
64 lines (46 loc) · 1.7 KB
/
Shape.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
"""Code for use with Think Python by Allen Downey.
Copyright 2010 Allen B. Downey
Distributed under the GNU General Public License at gnu.org/licenses/gpl.html.
"""
import math
class Location(object):
"""Represents a point in 2-D space."""
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def distance(self, other):
"""Computes the distance from this location to other."""
dx = self.x - other.x
dy = self.y - other.y
return math.sqrt(dx*dx + dy*dy)
def isLeft(self, other): return self.x < other.x
def isRight(self, other): return self.x > other.x
def isAbove(self, other): return self.y > other.y
def isBelow(self, other): return self.y < other.y
class Shape(object):
"""The parent class that all shapes inherit from."""
class Circle(Shape):
def __init__(self, center, radius):
self.center = center
self.radius = radius
def contains(self, loc):
"""Returns True if Location loc is inside this circle
(including the boundary)."""
return loc.distance(self.center) <= self.radius
class Rectangle(Shape):
def __init__(self, topLeft, botRight):
self.topLeft = topLeft
self.botRight = botRight
def contains(self, loc):
"""return True if Location loc is inside this rectangle
(including the boundary)"""
if loc.isLeft(self.topLeft): return False
if loc.isRight(self.botRight): return False
if loc.isAbove(self.topLeft): return False
if loc.isBelow(self.botRight): return False
return True
center = Location(100, 200)
circle = Circle(center, 75)
loc = Location(150, 150)
bool = circle.contains(loc)
print bool