NUMBERS-41: Utility class (syntactic sugar).
Project: http://git-wip-us.apache.org/repos/asf/commons-numbers/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-numbers/commit/04e92c6e Tree: http://git-wip-us.apache.org/repos/asf/commons-numbers/tree/04e92c6e Diff: http://git-wip-us.apache.org/repos/asf/commons-numbers/diff/04e92c6e Branch: refs/heads/master Commit: 04e92c6ebf0eac14514b381e750beaaf9257eac1 Parents: 2b955e3 Author: Gilles Sadowski <gil...@harfang.homelinux.org> Authored: Tue May 30 15:10:37 2017 +0200 Committer: Gilles Sadowski <gil...@harfang.homelinux.org> Committed: Tue May 30 15:10:37 2017 +0200 ---------------------------------------------------------------------- .../numbers/angle/PlaneAngleRadians.java | 41 ++++++++++++++++++++ .../numbers/angle/PlaneAngleRadiansTest.java | 35 +++++++++++++++++ 2 files changed, 76 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/04e92c6e/commons-numbers-angle/src/main/java/org/apache/commons/numbers/angle/PlaneAngleRadians.java ---------------------------------------------------------------------- diff --git a/commons-numbers-angle/src/main/java/org/apache/commons/numbers/angle/PlaneAngleRadians.java b/commons-numbers-angle/src/main/java/org/apache/commons/numbers/angle/PlaneAngleRadians.java new file mode 100644 index 0000000..0dd6b0a --- /dev/null +++ b/commons-numbers-angle/src/main/java/org/apache/commons/numbers/angle/PlaneAngleRadians.java @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.numbers.angle; + +/** + * Utility class where all {@code double} values are assumed to be in + * radians. + * + * @see PlaneAngle + */ +public class PlaneAngleRadians { + /** + * Normalize an angle in an interval of size 2π around a + * center value. + * + * @param angle Value to be normalized. + * @param center Center of the desired interval for the result. + * @return {@code a - 2 * k} with integer {@code k} such that + * {@code center - pi <= a - 2 * k * pi <= center + pi}. + */ + public static double normalize(double angle, + double center) { + final PlaneAngle a = PlaneAngle.ofRadians(angle); + final PlaneAngle c = PlaneAngle.ofRadians(center); + return a.normalize(c).toRadians(); + } +} http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/04e92c6e/commons-numbers-angle/src/test/java/org/apache/commons/numbers/angle/PlaneAngleRadiansTest.java ---------------------------------------------------------------------- diff --git a/commons-numbers-angle/src/test/java/org/apache/commons/numbers/angle/PlaneAngleRadiansTest.java b/commons-numbers-angle/src/test/java/org/apache/commons/numbers/angle/PlaneAngleRadiansTest.java new file mode 100644 index 0000000..1244915 --- /dev/null +++ b/commons-numbers-angle/src/test/java/org/apache/commons/numbers/angle/PlaneAngleRadiansTest.java @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law + * or agreed to in writing, software distributed under the License is + * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the specific language + * governing permissions and limitations under the License. + */ +package org.apache.commons.numbers.angle; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Test cases for the {@link PlaneAngleRadians} class. + */ +public class PlaneAngleRadiansTest { + @Test + public void testNormalize() { + for (double a = -15.0; a <= 15.0; a += 0.1) { + for (double b = -15.0; b <= 15.0; b += 0.2) { + final double c = PlaneAngleRadians.normalize(a, b); + Assert.assertTrue((b - Math.PI) <= c); + Assert.assertTrue(c <= (b + Math.PI)); + double twoK = Math.rint((a - c) / Math.PI); + Assert.assertEquals(c, a - twoK * Math.PI, 1e-14); + } + } + } +}