-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.py
134 lines (121 loc) · 3.5 KB
/
Utils.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
import csv
import random
import math
from math import pi
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def write_test_results(results):
"""
appends test results to .csv file
:param results: list of test results
:return: None
"""
with open(f'test_results.csv', "a") as f:
writer = csv.writer(f, delimiter=',')
writer.writerow(results)
return
def create_point(low,high):
"""
Randomly generate values to create Point object
:param low: lowest possible value
:param high: highest possible value
:return: Point object
"""
range = high-low
return Point(low+random.random()*range,low+random.random()*range)
def print_pretty(list):
"""
:param list: list of points to print
:return: None
"""
for point in list:
print(f"({point.x},{point.y})")
def polar_angle(p1, p2):
y = p1.y - p2.y
x = p1.x - p2.x
ret = math.degrees(math.atan2(y,x))
return ret
def distance(origin,p):
"""
:return: distance between point origin and point p
"""
AC = abs(origin.x - p.x)
BC = abs(origin.y - p.y)
AB = math.sqrt(pow(AC,2) + pow(BC,2))
return AB
def is_left_turn(hull, p1, p2):
"""
:return: True if p2 is left of line segment (hull,p1)
"""
m = ((p2.y-hull.y) * (p1.x - hull.x))
n = ((p1.y - hull.y) * (p2.x - hull.x))
if(m == n):
# if points are collinear select furthest point
if distance(hull, p1) >= distance(hull,p2):
return True
else:
return False
if(m < n):
return True
else:
return False
def get_lowest_point(point_list):
"""
:param list: of points
:return: lowest point on the coordinate system
furthest left if there is a tie
"""
lowest_p = point_list[0]
for p in point_list:
if p.y < lowest_p.y:
lowest_p = p
if p.y == lowest_p.y:
if p.x < lowest_p.x:
lowest_p = p
return lowest_p
def make_random_list(length):
"""
creates a list of points with a random distribution
:param length: length of desired list
:return: generated list
"""
list = []
for i in range(0,length):
list.append(create_point(0, 800))
return list
def make_maxhull_list(length, center=(0,0), radius=500):
"""
creates a list of points with min points on the hull
and all other points within the hull.
Math formula based on gist.github.com/danleyb2/ce6d2b82b1556f7bb7dc3c5d2bccb2fc
:param length: length of desired list
:return: generated list
"""
center_x, center_y = center
list = []
for i in range(0,length):
x = center_x + math.cos(2*pi/length*i)*radius
y = center_y + math.sin(2*pi/length*i)*radius
list.append(Point(x,y))
return list
def make_minhull_list(length, center=(0,0),height=500):
"""
My hacky way in create a triangle with points within in
:param length: size of list to create
:param center: center coordinate to build right angle
:param height: height of triangle to create
:return:
"""
center_x, center_y = center
a = Point(center_x, center_y)
b = Point(center_x, center_y + height)
c = Point(center_y + height, center_x)
list = [a,b,c]
for i in range(0,length):
p = create_point(center_x+1, center_y+height-1)
while(polar_angle(c, b) >= polar_angle(c, p)):
p = create_point(center_x+1, center_y+height-1)
list.append(p)
return list