-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandcon.py
More file actions
executable file
·52 lines (46 loc) · 1.33 KB
/
Copy pathrandcon.py
File metadata and controls
executable file
·52 lines (46 loc) · 1.33 KB
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
#!/usr/bin/python3
# Generate a random convex polygon using Valtr's algorithm
import sys
import math
import random
def random_partition(a):
b = list()
c = list()
for i in range(len(a)):
if random.randrange(2):
b.append(a[i])
else:
c.append(a[i])
return b, c
def get_deltas(n):
de = [random.random() for _ in range(n)]
de.sort()
dep, dem = random_partition(de[1:-1])
dem.reverse()
des = [de[0]] + dep + [de[-1]] + dem + [de[0]]
deltas = [des[i] - des[i-1] for i in range(1, len(des))]
return deltas, (de[0], de[-1])
def get_xyq(n):
x, (a1, a2) = get_deltas(n)
y, (b1, b2) = get_deltas(n)
random.shuffle(y)
vectors = [(x[i], y[i]) for i in range(n)]
vectors.sort(key=lambda v: math.atan2(v[1], v[0]))
points = [(0, 0)]
for v in vectors:
points.append((points[-1][0] + v[0], points[-1][1] + v[1]))
xmin = min([p[0] for p in points])
ymin = min([p[1] for p in points])
dx = a1 - xmin
dy = b1 - ymin
points = [(p[0]+dx, p[1]+dy) for p in points]
return points
import matplotlib.pyplot as plt
if __name__ == "__main__":
n = 20
if len(sys.argv) == 2:
n = int(sys.argv[1])
points = get_xyq(n)
plt.plot([p[0] for p in points], [p[1] for p in points])
plt.ylabel('some numbers')
plt.show()