-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFunctions.py
56 lines (42 loc) · 1.37 KB
/
Functions.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
import time
import math
from datetime import datetime
def curtime():
"""
Returns current time in microsecond
"""
from datetime import datetime
dt = datetime.now()
return dt.microsecond
def func_per_second(fn, *args):
"""
Runs a function every second
Parameters:
fn -> Function object
"""
starttime = curtime()
while True:
fn(*args)
time.sleep((1000.0 - ((curtime() - starttime) % 1000.0)) / 1000)
def get_distance(x1, y1, x2, y2):
"""
Returns distance two points
"""
return math.sqrt(pow(abs(x1-x2), 2) + pow(abs(y1-y2), 2))
def get_dict_distance(pos_dict1, pos_dict2):
"""
Returns distance two positions as dictionaries of format {'x': x_value, 'y': y_value}
"""
return get_distance(pos_dict1['x'], pos_dict1['y'], pos_dict2['x'], pos_dict2['y'])
def check_within_range(x1, y1, range1, x2, y2):
"""
Returns boolean whether (x2, y2) is in range of (x1, y1)
"""
return pow(abs(x1-x2), 2) + pow(abs(y1-y2), 2) <= pow(range1, 2)
def check_dict_within_range(pos_dict1, range1, pos_dict2):
"""
Returns boolean whether pos_dict2 is in range of pos_dict1
where the two dictionaries are of format {'x': x_value, 'y': y_value}
and represent positions
"""
return check_within_range(pos_dict1['x'], pos_dict1['y'], range1, pos_dict2['x'], pos_dict2['y'])