From f26a72c26f58b902b5db8484297bc710d219e4ed Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Tue, 12 Feb 2013 14:24:04 -0800 Subject: [PATCH] Change new mirrorAngle to mirrorAngleOrigin, improved javadocs. The old mirrorAngle mirrors around the "y-axis" (PI/2 or -PI/2 as appropriate) which is necessary for other code (particularly angularDifference) to work correctly. The new mirrorAngleOrigin mirrors around the "x-axis" (zero). --- src/main/java/pythagoras/f/MathUtil.java | 13 +++++++++- src/test/java/pythagoras/f/MathUtilTest.java | 25 +++++++++++++++++--- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/main/java/pythagoras/f/MathUtil.java b/src/main/java/pythagoras/f/MathUtil.java index 133df38..37ca460 100644 --- a/src/main/java/pythagoras/f/MathUtil.java +++ b/src/main/java/pythagoras/f/MathUtil.java @@ -166,9 +166,20 @@ public class MathUtil } /** - * Returns the mirror angle of the specified angle (assumed to be in [-pi, +pi]). + * Returns the mirror angle of the specified angle (assumed to be in [-pi, +pi]). The angle is + * mirrored around the PI/2 if it is positive, and -PI/2 if it is negative. One can visualize + * this as mirroring around the "y-axis". */ public static float mirrorAngle (float a) { + return (a > 0f ? FloatMath.PI : -FloatMath.PI) - a; + } + + /** + * Returns the mirror angle of the specified angle (assumed to be in [-pi, +pi]). The angle is + * mirrored around the origin (zero degrees). One can visualize this as mirroring around the + * "x-axis". + */ + public static float mirrorAngleOrigin (float a) { return a + (a > 0f ? -FloatMath.PI : FloatMath.PI); } diff --git a/src/test/java/pythagoras/f/MathUtilTest.java b/src/test/java/pythagoras/f/MathUtilTest.java index 5111d8a..569e76f 100644 --- a/src/test/java/pythagoras/f/MathUtilTest.java +++ b/src/test/java/pythagoras/f/MathUtilTest.java @@ -12,8 +12,27 @@ import static org.junit.Assert.*; */ public class MathUtilTest { - @Test public void testMirrorAngle() { - assertEquals(-MathUtil.HALF_PI, MathUtil.mirrorAngle(MathUtil.HALF_PI), MathUtil.EPSILON); - assertEquals(MathUtil.HALF_PI, MathUtil.mirrorAngle(-MathUtil.HALF_PI), MathUtil.EPSILON); + public static final float PI = FloatMath.PI; + public static final float PI2 = FloatMath.PI/2; + public static final float PI4 = FloatMath.PI/4; + public static final float PI8 = FloatMath.PI/8; + + @Test public void testLerpa() { + assertEquals(MathUtil.lerpa(PI4, -PI4, 0.25f), PI8, MathUtil.EPSILON); + assertEquals(MathUtil.lerpa(PI4, -PI4, 0.75f), -PI8, MathUtil.EPSILON); + assertEquals(MathUtil.lerpa(-PI4, PI4, 0.25f), -PI8, MathUtil.EPSILON); + assertEquals(MathUtil.lerpa(-PI4, PI4, 0.75f), PI8, MathUtil.EPSILON); + // make sure we lerp the shortest route around the circle + assertEquals(MathUtil.lerpa(3*PI4, PI4, 0.5f), PI2, MathUtil.EPSILON); + assertEquals(MathUtil.lerpa(PI4, 3*PI4, 0.5f), PI2, MathUtil.EPSILON); + assertEquals(MathUtil.lerpa(-3*PI4, -PI4, 0.5f), -PI2, MathUtil.EPSILON); + assertEquals(MathUtil.lerpa(-PI4, -3*PI4, 0.5f), -PI2, MathUtil.EPSILON); + + assertEquals(MathUtil.lerpa(3*PI4, -3*PI4, 0.5f), -PI, MathUtil.EPSILON); + } + + @Test public void testMirrorAngleOrigin() { + assertEquals(-PI2, MathUtil.mirrorAngleOrigin(PI2), MathUtil.EPSILON); + assertEquals(PI2, MathUtil.mirrorAngleOrigin(-PI2), MathUtil.EPSILON); } }