I have a function for mapping a value to between -180 and 180:
min_ = -180
max_ = 180
range_ = (max_ - min_)
def min_max_map(value):
return (value max_) % range_ - range_/2
print(min_max_map(200)) # -160
print(min_max_map(200 360)) # -160 again
However when I want to shift over the minimum and maximum bounds the answer is wrong:
min_ = -180 - 360
max_ = 180 - 360
range_ = (max_ - min_)
def min_max_map(value):
return (value max_) % range_ - range_/2
print(min_max_map(200)) # -160 rather than -160 - 360 = -520
I can't work out how to fix my formula. How can I generalize my formula to map any minimum and maximum values?
CodePudding user response:
def min_max_map(value):
return (value - min_) % range_ min_
CodePudding user response:
Why not just use the python built-in min
and max
functions? Something like:
return min( 180, max( -180, value ) )