-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
88 lines (71 loc) · 2.02 KB
/
helpers.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
"""
Provides various helper functions.
"""
import math
def remap( x, oMin, oMax, nMin, nMax ): # thanks stackoverflow.com/a/15537393
#range check
if oMin == oMax:
print("Warning: Zero input range")
return None
if nMin == nMax:
print("Warning: Zero output range")
return None
#check reversed input range
reverseInput = False
oldMin = min( oMin, oMax )
oldMax = max( oMin, oMax )
if not oldMin == oMin:
reverseInput = True
#check reversed output range
reverseOutput = False
newMin = min( nMin, nMax )
newMax = max( nMin, nMax )
if not newMin == nMin :
reverseOutput = True
portion = (x-oldMin)*(newMax-newMin)/(oldMax-oldMin)
if reverseInput:
portion = (oldMax-x)*(newMax-newMin)/(oldMax-oldMin)
result = portion + newMin
if reverseOutput:
result = newMax - portion
return result
'''
Given a value and power, this raises the value
to the power, then negates it if the value was
originally negative.
'''
def raiseKeepSign(value, power):
newValue = value ** power
if (value < 0 and newValue > 0):
return newValue * -1
else:
return value ** power
'''
Given a Cartesian x,y position, this 'snaps'
it to the nearest angle, in degrees (the
number of snappable angles is determined by
`divisions`). Intended to be used with joystick
values.
'''
def snap(divisions, x, y):
if (x == 0):
return 0
result = round(math.atan2(y, x) / (2 * math.pi / divisions) + divisions, 0) % divisions
return result * (360 / divisions)
'''
Maps a value onto a sin curve. Made for
the driving fuctions.
'''
def curve(value):
return math.sin(value ** 2) * math.pi/2.6
'''
Rotates a vector in Caresian space.
'''
def rotateVector(x, y, angle):
angle = math.radians(angle)
cosA = math.cos(angle)
sinA = math.sin(angle)
return (x * cosA - y * sinA), (x * sinA + y * cosA)
def normalizeAngle(angle):
'''Normalize angle to [-180,180]'''
return ((angle + 180) % 360) - 180.0