Browse Source

improved angle-reduction funcs

Darren Ranalli 23 years ago
parent
commit
95166f8d59
1 changed files with 24 additions and 7 deletions
  1. 24 7
      direct/src/showbase/PythonUtil.py

+ 24 - 7
direct/src/showbase/PythonUtil.py

@@ -383,17 +383,34 @@ def replace(list, old, new, all=0):
 
 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
-    assumed that src is between -180. and 180.
-    Example: (shortest-dest-angle 50. -170.) --> 190.
+    given a src and destination angle, returns an equivalent src angle
+    that is within [-180..180) of dest
+    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):
     # The function above didn't seem to do what I wanted. So I hacked