generated from cubao/pybind11-rdp
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest.py
51 lines (43 loc) · 1.48 KB
/
test.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
import matplotlib.pyplot as plt
import numpy as np
from scipy.spatial import ConvexHull
from concave_hull import ( # noqa: F401
concave_hull,
concave_hull_indexes,
convex_hull_indexes,
)
points = []
c = np.array([250, 250])
for x in np.arange(100, 400, 5 * np.pi):
for y in np.arange(100, 400, 5 * np.pi):
if x > c[0] and y > c[1]:
continue
r = np.linalg.norm(c - [x, y])
if r > 150:
continue
points.append([x, y])
points = np.array(points)
convex_hull = ConvexHull(points[:, :2]) # it's already N-by-2, I'm just emphasizing
# https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.ConvexHull.html
plt.plot(points[:, 0], points[:, 1], "o")
for simplex in convex_hull.simplices:
continue
plt.plot(points[simplex, 0], points[simplex, 1], "g-", alpha=0.5)
hull = convex_hull_indexes(points[:, :2])
for i, j in zip(hull[:-1], hull[1:]): # noqa: B905
plt.plot(
[points[i, 0], points[j, 0]], [points[i, 1], points[j, 1]], "g-", alpha=0.5
)
idxes = concave_hull_indexes(
points[:, :2],
length_threshold=50,
# convex_hull_indexes=convex_hull.vertices.astype(np.int32),
)
# you can get coordinates by `points[idxes]`
# assert np.all(points[idxes] == concave_hull(points, length_threshold=50))
for f, t in zip(idxes[:-1], idxes[1:]): # noqa
seg = points[[f, t]]
plt.plot(seg[:, 0], seg[:, 1], "r-", alpha=0.5)
plt.gca().set_aspect("equal")
# plt.savefig("docs/hull.png")
plt.show()