-
Notifications
You must be signed in to change notification settings - Fork 0
/
points_generator.py
135 lines (116 loc) · 3.75 KB
/
points_generator.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import random
#Returns size random collinear 2D points in range [min,max]
#if collinear_ratio > 0 then this ratio of size points will
#be collinear. If dix_x_coords=True then all points will
#have different x coordinate. If cannot generate size points
#based on min, max and parameters returns []
def gen_2D_points(min=0, max=1000, size=10, dif_x_coords = False, collinear_ratio=0):
points = []
if collinear_ratio == 0:
total_range = max-min+1
total_pos_comb = total_range ** 2
if dif_x_coords:
total_pos_comb = total_range
if size > total_pos_comb:
return []
x_coords = []
while len(points) < size:
x = random.randint(min, max)
y = random.randint(min, max)
p = [x,y]
if p not in points:
if not dif_x_coords:
points.append(p)
elif dif_x_coords and p[0] not in x_coords:
x_coords.append(p[0])
points.append(p)
else:
points = gen_2D_coll_points(min, max, size, collinear_ratio)
return points
#Returns size random collinear 2D points in range [min,max]
#The collinear points will either have same x coord, or same
#y coord or be of the form (x,x)
#If cannot generate size points based on min, max returns []
def gen_only_collinear(min=0,max=1000,size=10):
if max-min+1 < size:
return []
coll_type = random.randint(0,2)
points = []
if coll_type == 0:
while len(points) < size:
x = random.randint(min, max)
p = [x,x]
if p not in points:
points.append(p)
else:
a = random.randint(min, max)
while len(points) < size:
b = random.randint(min, max)
if coll_type == 1: #same x coords
p = [a, b]
else: #same y coords
p = [b, a]
if p not in points:
points.append(p)
return points
#Returns size random 2D points in range [min,max] with
#the collinear_ratio of size being collinear.
#If cannot generate size points based on min, max returns []
def gen_2D_coll_points(min=0, max=1000, size=10, collinear_ratio=0.5):
coll_size = collinear_ratio*size
points = gen_only_collinear(min, max, coll_size)
total_range = max-min+1
pos_points = total_range ** 2 - coll_size
if points == [] or pos_points < size-coll_size:
return []
while len(points) < size:
x = random.randint(min,max)
y = random.randint(min,max)
p = [x,y]
if p not in points:
points.append([x, y])
return points
#Returns random size 3D points with x,y,z coordinates
#in range min,max. If cannot generate size points
#based on min, max returns []
def gen_3D_points(min=0, max=100, size=10):
total_range = max-min+1
total_pos_comb = total_range ** 3
if size > total_pos_comb:
return []
points = []
while len(points) < size:
x = random.randint(min, max)
y = random.randint(min, max)
z = random.randint(min, max)
p = [x,y,z]
if p not in points:
points.append(p)
return points
#returns [[min_x, min_y], [max_x, max_y]] bounding box
#where min_x, max_x are random in range of min and max x of points
#and min_y, max_y are random in range of min and max y of points
def box_generator(points):
xx = [p[0] for p in points]
yy = [p[1] for p in points]
min_x = min(xx)
min_y = min(yy)
max_x = max(xx)
max_y = max(yy)
x1 = random.randint(min_x, max_x)
x2 = random.randint(min_x, max_x)
while x2 == x1:
x2 = random.randint(min_x, max_x)
y1 = random.randint(min_y, max_y)
y2 = random.randint(min_y, max_y)
while y2 == y1:
y2 = random.randint(min_y, max_y)
if x2 < x1:
tmp = x2
x2 = x1
x1 = tmp
if y2 < y1:
tmp = y2
y2 = y1
y1 = tmp
return [[x1,y1], [x2,y2]]