-
Notifications
You must be signed in to change notification settings - Fork 0
/
globy.py
99 lines (75 loc) · 1.99 KB
/
globy.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
# globy.py
import sys
import math
import random
# constants
window_width = 1280
window_height = 720
goal_x = window_width - 50
goal_y = window_height / 2
goal_radius = 25
goal_left = goal_x - goal_radius
goal_right = goal_x + goal_radius
goal_bottom = goal_y + goal_radius
goal_top = goal_y - goal_radius
start_x = 40
start_y = window_height / 2
# map list index
def map_to_index(x1, y1, x2, y2):
return (int(x2 / x1), int(y2 / y1))
# radians conversion
def get_radians(degrees):
return degrees * (math.pi/180)
# degrees conversion
def get_degrees(radians):
return radians * (180/math.pi)
# integer tuple
def int_tuple(a, b):
return (int(a), int(b))
# quadrant of point
def get_quadrant(x, y):
if x < 0:
if y < 0:
return 3
else:
return 2
else:
if y < 0:
return 4
else:
return 1
# euclidean distance
def euclidean_distance(x1, y1, x2, y2):
return math.sqrt( (math.pow(y2 - y1, 2) + math.pow(x2 - x1, 2)) )
# scale distance [0, 1]
def scale_distance(min_distance, max_distance, distance):
return (distance - min_distance) / max_distance
# vector [ 2D ]
class Vector:
def __init__(self, x, y):
self.x = x
self.y = y
def __eq__(self, other):
return ((self.x == other.x) and (self.y == other.y))
def __str__(self):
return "Vector({}, {})".format(self.x, self.y)
def __add__(self, other):
x = self.x + other.x
y = self.y + other.y
return Vector(x, y)
def __mul__(self, scalar):
x = self.x * scalar
y = self.y * scalar
return Vector(x, y)
def __rmul__(self, scalar):
x = self.x * scalar
y = self.y * scalar
return Vector(x, y)
def __truediv__(self, scalar):
x = self.x / scalar
y = self.x / scalar
return Vector(x, y)
def __floordiv__(self, scalar):
x = self.x // scalar
y = self.y // scalar
return Vector(x, y)