-
Notifications
You must be signed in to change notification settings - Fork 0
/
geometry.py
141 lines (106 loc) · 3.57 KB
/
geometry.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
# GEO1000 - Assignment 3
# Authors:
# Studentnumbers:
import math
# __all__ leaves out _test method and only makes
# the classes available for "from geometry import *":
__all__ = ["Point", "Circle", "Rectangle"]
class Point(object):
def __init__(self, x, y):
"""Constructor.
Takes the x and y coordinates to define the Point instance.
"""
self.x = float(x)
self.y = float(y)
def __str__(self):
"""Returns WKT String "POINT (x y)".
"""
pass
def intersects(self, other):
"""Checks whether other shape has any interaction with
interior or boundary of self shape. Uses type based dispatch.
other - Point, Circle or Rectangle
returns - True / False
"""
pass
def distance(self, other):
"""Returns cartesian distance between self and other Point
"""
pass
class Circle(object):
def __init__(self, center, radius):
"""Constructor.
Takes the center point and radius defining the Circle.
"""
assert radius > 0
assert isinstance(center, Point)
self.center = center
self.radius = float(radius)
def __str__(self):
"""Returns WKT str, discretizing the boundary of the circle
into straight line segments
"""
N = 400
step = 2 * math.pi / N
pts = []
for i in range(N):
pts.append(Point(self.center.x + math.cos(i * step) * self.radius,
self.center.y + math.sin(i * step) * self.radius))
pts.append(pts[0])
coordinates = ["{0} {1}".format(pt.x, pt.y) for pt in pts]
coordinates = ", ".join(coordinates)
return "POLYGON (({0}))".format(coordinates)
def intersects(self, other):
"""Checks whether other shape has any interaction with
interior or boundary of self shape. Uses type based dispatch.
other - Point, Circle or Rectangle
Returns - True / False
"""
pass
class Rectangle(object):
def __init__(self, pt_ll, pt_ur):
"""Constructor.
Takes the lower left and upper right point defining the Rectangle.
"""
assert isinstance(pt_ll, Point)
assert isinstance(pt_ur, Point)
self.ll = pt_ll
self.ur = pt_ur
def __str__(self):
"""Returns WKT String "POLYGON ((x0 y0, x1 y1, ..., x0 y0))"
"""
pass
def intersects(self, other):
"""Checks whether other shape has any interaction with
interior or boundary of self shape. Uses type based dispatch.
other - Point, Circle or Rectangle
Returns - True / False
"""
pass
def width(self):
"""Returns the width of the Rectangle.
Returns - float
"""
pass
def height(self):
"""Returns the height of the Rectangle.
Returns - float
"""
pass
def _test():
"""Test whether your implementation of all methods works correctly.
"""
pt0 = Point(0, 0)
pt1 = Point(0, 0)
pt2 = Point(10, 10)
assert pt0.intersects(pt1)
assert pt1.intersects(pt0)
asse rt not pt0.intersects(pt2)
assert not pt2.intersects(pt0)
c = Circle(Point(-1, -1), 1)
r = Rectangle(Point(0,0), Point(10,10))
assert not c.intersects(r)
# Extend this method to be sure that you test all intersects methods!
# Read Section 16.5 of the book if you have never seen the assert statement
if __name__ == "__main__":
_test()