1 /*
2 * Copyright (C) 2012 Alberto Irurueta Carro (alberto@irurueta.com)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package com.irurueta.geometry;
17
18 /**
19 * Helper class containing commonly used methods related to geometry.
20 */
21 public class Utils {
22 /**
23 * Amount to add/subtract to an angle in degrees to make a half turn.
24 */
25 public static final double HALF_TURN_DEGREES = 180.0;
26
27 /**
28 * Amount to add/subtract to an angle in radians to make a half turn.
29 */
30 public static final double HALF_TURN_RADIANS = Math.PI;
31
32 /**
33 * Constructor.
34 * Prevents instantiation.
35 */
36 private Utils() { }
37
38 /**
39 * Converts provided value from radians to degrees.
40 * @param radians Angle in radians.
41 * @return Converted angle in degrees.
42 */
43 public static double convertToDegrees(final double radians) {
44 return radians / Math.PI * 180.0;
45 }
46
47 /**
48 * Converts provided value from degrees to radians.
49 * @param degrees Angle in degrees.
50 * @return Converted angle in radians.
51 */
52 public static double convertToRadians(final double degrees) {
53 return degrees / 180.0 * Math.PI;
54 }
55
56 /**
57 * Normalizes an angle between the range -180...180 by adding or subtracting
58 * the required amount of turns.
59 * @param angle angle to be normalized expressed in degrees.
60 * @return the same angle but normalized.
61 */
62 public static double normalizeAngleDegrees(final double angle) {
63 return normalizeAngle(angle, HALF_TURN_DEGREES);
64 }
65
66 /**
67 * Normalizes an angle between the range -pi...pi by adding or subtracting
68 * the required amount of turns.
69 * @param angle angle to be normalized expressed in degrees.
70 * @return the same angle but normalized.
71 */
72 public static double normalizeAngleRadians(final double angle) {
73 return normalizeAngle(angle, HALF_TURN_RADIANS);
74 }
75
76 /**
77 * Normalizes an angle between the range -pi...pi (or -180...180) by adding
78 * or subtracting the required amount of turns.
79 * This method can work with angles expressed either on degrees or radians
80 * depending on the units used for the definition of a half turn.
81 * @param angle angle to be normalized.
82 * @param halfTurn the definition of a half turn (either in degrees or
83 * radians).
84 * @return the same angle but normalized.
85 */
86 private static double normalizeAngle(final double angle, final double halfTurn) {
87 var result = angle;
88 final var fullTurn = 2.0 * halfTurn;
89
90 if (result <= -halfTurn) {
91 var nTurns = (int) Math.ceil(-result / fullTurn);
92 result += nTurns * fullTurn;
93 if (result > halfTurn) {
94 result -= fullTurn;
95 }
96 }
97 if (result > halfTurn) {
98 var nTurns = (int) Math.ceil(result / fullTurn);
99 result -= nTurns * fullTurn;
100 if(result <= -halfTurn){
101 result += fullTurn;
102 }
103 }
104 return result;
105 }
106 }