Skip to content

Commit 6ea6baa

Browse files
committed
fixed some bugs
1 parent 1963f26 commit 6ea6baa

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

cyaron/polygon.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ def area(self):
3535
ans = ans / 2
3636
return ans
3737

38+
#generate a convex hull with n points
39+
#it's possible to have even edges
3840
@staticmethod
3941
def convex_hull(n, **kwargs):
4042
# fx, fy are functions which map [0,1] to int or float
@@ -45,7 +47,7 @@ def convex_hull(n, **kwargs):
4547
while len(result) < n:
4648
points = []
4749
# about 10 points will be randomized
48-
randomize_prob = sz / 10 + 1
50+
randomize_prob = int(sz / 10) + 1
4951
if randomize_prob < 10:
5052
randomize_prob = 10
5153
for i in range(0, sz):
@@ -86,6 +88,7 @@ def convex_hull(n, **kwargs):
8688
break
8789
st.pop()
8890
st.append(points[i])
91+
st.pop()
8992
result = st
9093
sz = int(sz * 1.7) + 3 # if failed, increase size and try again
9194
ids = [i for i in range(0, len(result))]
@@ -101,6 +104,9 @@ def convex_hull(n, **kwargs):
101104
def __conquer(points):
102105
if len(points) <= 2:
103106
return points
107+
if len(points) == 3:
108+
(points[1],points[2])=(points[2],points[1])
109+
return points
104110
divide_id = random.randint(2, len(points) - 1)
105111
divide_point1 = points[divide_id]
106112
divide_k = random.uniform(0.01, 0.99)

cyaron/tests/polygon_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from cyaron import Polygon,Vector
33
class TestPolygon(unittest.TestCase):
44
def test_convex_hull(self):
5-
hull = Polygon.convex_hull(300, fx=lambda x:int(x*1000), fy=lambda x:int(x*1000))
5+
hull = Polygon.convex_hull(300, fx=lambda x:int(x*100000), fy=lambda x:int(x*100000))
66
points = hull.points
77
points = sorted(points)
88
# unique
@@ -33,6 +33,7 @@ def test_convex_hull(self):
3333
break
3434
st.pop()
3535
st.append(points[i])
36+
st.pop()
3637
self.assertEqual(len(st), len(hull.points))
3738
def test_perimeter_area(self):
3839
poly = Polygon([[0,0],[0,1],[1,1],[1,0]])

examples/test_geometry.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python
22
from cyaron import *
33
print "random convex hull of size 300:"
4-
hull=Polygon.convex_hull(300, fx=lambda x:int(x*1000), fy=lambda x:int(x*1000))
4+
hull=Polygon.convex_hull(300, fx=lambda x:int(x*100000), fy=lambda x:int(x*100000))
55
print hull
66
print "perimeter:"
77
print hull.perimeter()

0 commit comments

Comments
 (0)