-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHull.py
More file actions
215 lines (154 loc) · 5.53 KB
/
Hull.py
File metadata and controls
215 lines (154 loc) · 5.53 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
# File: Hull.py
# Description:
# Student Name: Sashi Ayyalasomayajula
# Student UT EID: sa55465
# Partner Name:
# Partner UT EID:
# Course Name: CS 313E
# Unique Number:
# Date Created:
# Date Last Modified:
import sys
import math
class Point (object):
# constructor
def __init__(self, x = 0, y = 0):
self.x = x
self.y = y
# get the distance to another Point object
def dist (self, other):
return math.hypot (self.x - other.x, self.y - other.y)
# string representation of a Point
def __str__ (self):
return '(' + str(self.x) + ', ' + str(self.y) + ')'
# equality tests of two Points
def __eq__ (self, other):
tol = 1.0e-8
return ((abs(self.x - other.x) < tol) and (abs(self.y - other.y) < tol))
def __ne__ (self, other):
tol = 1.0e-8
return ((abs(self.x - other.x) >= tol) or (abs(self.y - other.y) >= tol))
def __lt__ (self, other):
tol = 1.0e-8
if (abs(self.x - other.x) < tol):
if (abs(self.y - other.y) < tol):
return False
else:
return (self.y < other.y)
return (self.x < other.x)
def __le__ (self, other):
tol = 1.0e-8
if (abs(self.x - other.x) < tol):
if (abs(self.y - other.y) < tol):
return True
else:
return (self.y <= other.y)
return (self.x <= other.x)
def __gt__ (self, other):
tol = 1.0e-8
if (abs(self.x - other.x) < tol):
if (abs(self.y - other.y) < tol):
return False
else:
return (self.y > other.y)
return (self.x > other.x)
def __ge__ (self, other):
tol = 1.0e-8
if (abs(self.x - other.x) < tol):
if (abs(self.y - other.y) < tol):
return True
else:
return (self.y >= other.y)
return (self.x >= other.x)
# Input: p, q, r are Point objects
# Output: compute the determinant and return the value
def det (p, q, r):
det = (q.x * r.y) - (r.x * q.y) + (r.x * p.y) - (p.x * r.y) + (p.x * q.y) - (q.x * p.y)
return det
# Input: sorted_points is a sorted list of Point objects
# Output: computes the convex hull of a sorted list of Point objects
# convex hull is a list of Point objects starting at the
# extreme left point and going clockwise in order
# returns the convex hull
def convex_hull (sorted_points):
upper_hull = []
#append first two points of sorted list to upper hull
upper_hull.append(sorted_points[0])
upper_hull.append(sorted_points[1])
#adds rest of the points to upper_hull, but extracts specific points that are right turns relative to the previous point added
for i in range(2, len(sorted_points)):
upper_hull.append(sorted_points[i])
while(len(upper_hull) >= 3) and (det(upper_hull[-3], upper_hull[-2], upper_hull[-1]) >= 0): #extracts point if is making a right turn
upper_hull.pop(-2)
lower_hull = []
#append last two points of sorted list to lower hull
lower_hull.append(sorted_points[-1])
lower_hull.append(sorted_points[-2])
# similar functioning loop to upper hull but in reverse order realtive to sorted points list
for j in range(len(sorted_points)-3, -1, - 1):
lower_hull.append(sorted_points[j])
while(len(lower_hull) >= 3) and det(lower_hull[-3], lower_hull[-2], lower_hull[-1]) >= 0:
lower_hull.pop(-2)
Cvex_hull = [] #empty list to store final points that make up the polygon
# gets rid of two duplicate points at the beginnign and end of lower hull
lower_hull.pop(len(lower_hull)-1)
lower_hull.pop(0)
Cvex_hull = upper_hull # appends upper hull to conver hull
for point in lower_hull:
Cvex_hull.append(point) # appends lower hull to conver hull
return Cvex_hull
# Input: convex_poly is a list of Point objects that define the
# vertices of a convex polygon in order
# Output: computes and returns the area of a convex polygon
def area_poly(convex_poly):
d = 0
for i in range(len(convex_poly)-1): # goes through points in polygon
#sets a and b as two consecutive points
a = convex_poly[i]
b = convex_poly[i+1]
d += (a.x*b.y)
d -= (a.y*b.x)
# adds det of two points to d
d += convex_poly[-1].x*convex_poly[0].y
d -= convex_poly[-1].y*convex_poly[0].x
# computes area
A = 0.5 * math.fabs(d)
return A
# Input: no input
# Output: a string denoting all test cases have passed
def test_cases():
# write your own test cases
return "all test cases passed"
def main():
# create an empty list of Point objects
points_list = []
# read number of points
line = sys.stdin.readline()
line = line.strip()
num_points = int (line)
# read data from standard input
for i in range (num_points):
line = sys.stdin.readline()
line = line.strip()
line = line.split()
x = int (line[0])
y = int (line[1])
points_list.append (Point (x, y))
# sort the list according to x-coordinates
sorted_points = sorted(points_list)
# get the convex hull
Cvex_hull = []
Cvex_hull = convex_hull(sorted_points)
# run your test cases
# print your results to standard output
# print the convex hull
print('Convex Hull')
for point in Cvex_hull:
print(tuple([point.x, point.y]))
print()
# get the area of the convex hull
ACvex_hull = area_poly(Cvex_hull)
# print the area of the convex hull
print("Area of Convex Hull = " + str(ACvex_hull))
if __name__ == "__main__":
main()