improved angle-reduction funcs

This commit is contained in:
Darren Ranalli 2002-11-01 01:01:09 +00:00
parent 7e17841ed3
commit 95166f8d59

View File

@ -383,17 +383,34 @@ def replace(list, old, new, all=0):
def reduceAngle(deg): def reduceAngle(deg):
""" """
Reduces an angle (in degrees) to a value between -180. and 180. Reduces an angle (in degrees) to a value in [-180..180)
""" """
return (math.fmod((deg + 180.0), 360.0) - 180.0) return (((deg + 180.) % 360.) - 180.)
def shortestDestAngle(src, dest): def fitSrcAngle2Dest(src, dest):
""" """
Returns a version of dest that is numerically closest to src. It is given a src and destination angle, returns an equivalent src angle
assumed that src is between -180. and 180. that is within [-180..180) of dest
Example: (shortest-dest-angle 50. -170.) --> 190. examples:
fitSrcAngle2Dest(30,60) == 30
fitSrcAngle2Dest(60,30) == 60
fitSrcAngle2Dest(0,180) == 0
fitSrcAngle2Dest(-1,180) == 359
fitSrcAngle2Dest(-180,180) == 180
""" """
return (src + (reduceAngle(dest - src))) return dest + reduceAngle(src - dest)
def fitDestAngle2Src(src, dest):
"""
given a src and destination angle, returns an equivalent dest angle
that is within [-180..180) of src
examples:
fitDestAngle2Src(30,60) == 60
fitDestAngle2Src(60,30) == 30
fitDestAngle2Src(0,180) == -180
fitDestAngle2Src(1,180) == 180
"""
return src + (reduceAngle(dest - src))
def closestDestAngle2(src, dest): def closestDestAngle2(src, dest):
# The function above didn't seem to do what I wanted. So I hacked # The function above didn't seem to do what I wanted. So I hacked