Skip to content

Commit fb24d6e

Browse files
committed
finished ex3
1 parent 416e961 commit fb24d6e

File tree

3 files changed

+47
-31
lines changed

3 files changed

+47
-31
lines changed

gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/part1/src/ex*/*.pyc

part1/src/ex3_ex4/main.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import numpy as np
44
import matplotlib.pyplot as plt
55
import time
6-
RANGE_XY = 10000
6+
RANGE_XY = 1000000
77
def parse_input():
88
points = []
99
ch = raw_input("To autogenerate write ->AUTO<- else just type something: ")
@@ -31,10 +31,10 @@ def main():
3131
if plane.check_points() == True:
3232
print("***Found 2 Points with same x, Algorithm ends***")
3333
return
34-
choice = raw_input("Type (1) for Incremental / Type (2) for Gift Wrap")
34+
choice = raw_input("Type (1) for Incremental / Type (2) for Gift Wrap: ")
3535
while True:
3636
if choice == "1":
37-
print(plane.incremental_convex_hull())
37+
plane.incremental_convex_hull()
3838
break
3939
elif choice == "2":
4040
plane.gift_wrap_convex_hull()

part1/src/ex3_ex4/plane.py

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ def check_points(self):
3636
curr += 1
3737
return False
3838

39-
def ort(self, pt1 , pt2, r):
39+
#if we start from left to right
40+
def ort_min_max(self, pt1 , pt2, r):
4041
v = ((pt2.get_y()-pt1.get_y())*(r.get_x()-pt2.get_x())
4142
-(pt2.get_x()-pt1.get_x())*(r.get_y()-pt2.get_y()))
4243
if v == 0:
@@ -46,10 +47,13 @@ def ort(self, pt1 , pt2, r):
4647
else:
4748
return CCW
4849

49-
def cross(self, pt1 , pt2, r):
50+
#if we start from right to left
51+
def ort_max_min(self, pt1 , pt2, r):
5052
v = ((pt2.get_x()-pt1.get_x())*(r.get_y()-pt2.get_y())
5153
-(pt2.get_y()-pt1.get_y())*(r.get_x()-pt2.get_x()))
52-
if v <= 0:
54+
if v == 0:
55+
return COLINEAR
56+
elif v < 0:
5357
return CW
5458
else:
5559
return CCW
@@ -86,41 +90,52 @@ def sort_function(self,point):
8690
return point.get_x()
8791

8892
def incremental_convex_hull(self):
89-
lowerHull = []
90-
upperHull = []
93+
lower_hull = []
94+
upper_hull = []
9195

92-
if len(self.points) == 0:
96+
if len(self.points) <= 1:
9397
return self.points
9498

95-
#sorting list based on x coord
96-
#self.print_plane()
97-
self.points = sorted(self.points,key=self.sort_function)
98-
#self.print_plane()
99+
#sorting list based on x coord {max -> min}
100+
self.points = sorted(self.points,key = self.sort_function, reverse = True)
99101
N = len(self.points)
100102

101103
# culculating lower hull #
102104
for idx in range(N):
103-
#print idx
104-
while len(lowerHull) >= 2 and self.cross(lowerHull[-2], lowerHull[-1], self.points[idx]) == CW:
105-
lowerHull.pop()
106-
lowerHull.append(self.points[idx])
105+
while True:
106+
curr_len = len(lower_hull)
107+
if (curr_len >= 2):
108+
ort = self.ort_max_min(lower_hull[-2], lower_hull[-1], self.points[idx])
109+
if ort == CW:
110+
lower_hull.pop()
111+
elif ort == CCW:
112+
break
113+
else:
114+
print ("COLINEAR points in the lower_hull")
115+
else:
116+
break
117+
lower_hull.append(self.points[idx])
107118

108119
# culculating upper hull #
120+
self.points.reverse()
109121
for idx in range(N):
110-
self.points.reverse()
111-
#self.print_plane()
112-
while len(upperHull) >= 2 and \
113-
(self.ort(lowerHull[-2], lowerHull[-1], self.points[idx]) == CCW or \
114-
self.ort(lowerHull[-2], lowerHull[-1], self.points[idx]) == COLINEAR):
115-
upperHull.pop()
116-
upperHull.append(self.points[idx])
117-
118-
#pop common point
119-
lowerHull.pop()
120-
hull = lowerHull + upperHull
121-
#self.display_hull(lowerHull)
122-
#self.display_hull(upperHull)
122+
while True:
123+
curr_len = len(upper_hull)
124+
if (curr_len >= 2):
125+
ort = self.ort_max_min(upper_hull[-2], upper_hull[-1], self.points[idx])
126+
if ort == CW:
127+
upper_hull.pop()
128+
elif ort == CCW:
129+
break
130+
else:
131+
print ("COLINEAR points in the lower_hull")
132+
else:
133+
break
134+
upper_hull.append(self.points[idx])
135+
136+
hull = lower_hull + upper_hull
123137
self.display_hull(hull)
138+
return
124139

125140

126141
def gift_wrap_convex_hull(self):
@@ -134,7 +149,7 @@ def gift_wrap_convex_hull(self):
134149
curr_pt = (cr_pt+1)%total_points
135150
#check for orientation of all other Points
136151
for i in range(0, total_points):
137-
ort = self.ort(self.points[cr_pt], self.points[i], self.points[curr_pt])
152+
ort = self.ort_min_max(self.points[cr_pt], self.points[i], self.points[curr_pt])
138153
if ort == CCW:
139154
curr_pt = i
140155
cr_pt = curr_pt

0 commit comments

Comments
 (0)