-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquickhull3d.py
61 lines (40 loc) · 1.61 KB
/
quickhull3d.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
from scipy.spatial import ConvexHull
import sys
import time
import random
import matplotlib.pyplot as plt
class Point:
def __init__(self, x = None, y = None, z = None):
self.x = x
self.y = y
self.z = z
def print(self):
print(float(self.x),",", float(self.y), ",",float(self.z))
def main(argv):
convex_hull = []
if len(argv)!=2:
print("Wrong Arguments!")
return -1
points_number = int(argv[1])
points = [Point(random.uniform(0, 1), random.uniform(0, 1), random.uniform(0, 1)) for _ in range(points_number)]
start_time = time.time()
convex_hull = ConvexHull([(point.x, point.y, point.z) for point in points])
end_time = time.time()
print("-------------")
print("Convex Hull:")
print("-------------")
for i in convex_hull.vertices:
points[i].print()
print("-------------")
elapsed_time = end_time - start_time
print("Elapsed Time:", elapsed_time, "seconds")
fig = plt.figure()
ch = fig.add_subplot(111, projection='3d')
ch.scatter([point.x for point in points], [point.y for point in points], [point.z for point in points], c='b', marker='o', label='Points')
ch.scatter([points[i].x for i in convex_hull.vertices], [points[i].y for i in convex_hull.vertices], [points[i].z for i in convex_hull.vertices], c='r', marker='o', label='Hull Vertices')
for simplex in convex_hull.simplices:
ch.plot([points[i].x for i in simplex], [points[i].y for i in simplex], [points[i].z for i in simplex], 'k-')
plt.legend()
plt.show()
if __name__ == '__main__':
main(sys.argv)