-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConvexHull.py
50 lines (39 loc) · 1.01 KB
/
ConvexHull.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
"""
Draw the Convex Hull of Given Points
SEE: https://www.designcoding.net/convex-hull-with-rhino-python/
"""
import rhinoscriptsyntax as rs
def main(points, plane, close_hull=True):
if not points:
return False
if not plane:
return False
plane_x = 0
plane_y = 1
current = min(points)
start = current
hull = []
while current:
origin = current
current = points[0]
for candidate in points:
if (current[plane_x] - origin[plane_x]) * (candidate[plane_y] - origin[plane_y]) - (current[plane_y] - origin[plane_y]) * (candidate[plane_x] - origin[plane_x]) < 0:
current = candidate
hull.append(current)
if current == start:
break
if close_hull:
hull.append(hull[0])
return hull
def rhino():
points = rs.GetPointCoordinates("Select points", preselect=True)
if not points:
return
plane = rs.ViewCPlane()
if not plane:
return
hull = main(points, plane, close_hull=True)
if hull:
rs.AddPolyline(hull)
if __name__ == '__main__':
rhino()