Skip to content

Instantly share code, notes, and snippets.

@webstory
Last active March 30, 2018 01:13
Show Gist options
  • Save webstory/4bd1eb2234bd4330100961856d9dbf42 to your computer and use it in GitHub Desktop.
Save webstory/4bd1eb2234bd4330100961856d9dbf42 to your computer and use it in GitHub Desktop.
/*
* 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.
*/
public final class MathUtils {
/**
* Normalize an angle in a 2π wide interval around a center value.
* <p>This method has three main uses:</p>
* <ul>
* <li>normalize an angle between 0 and 2&pi;:<br>
* {@code a = MathUtils.normalizeAngle(a, FastMath.PI);}</li>
* <li>normalize an angle between -&pi; and +&pi;<br>
* {@code a = MathUtils.normalizeAngle(a, 0.0);}</li>
* <li>compute the angle between two defining angular positions:<br>
* {@code angle = MathUtils.normalizeAngle(end, start) - start;}</li>
* </ul>
* <p>Note that due to numerical accuracy and since &pi; cannot be represented
* exactly, the result interval is <em>closed</em>, it cannot be half-closed
* as would be more satisfactory in a purely mathematical view.</p>
* @param a angle to normalize
* @param center center of the desired 2&pi; interval for the result
* @return a-2k&pi; with integer k and center-&pi; &lt;= a-2k&pi; &lt;= center+&pi;
* @since 1.2
*/
public static double normalizeAngle(double a, double center) {
double r = Math.toRadians(lon);
double norm_r = r - (2 * Math.PI) * Math.floor((r + Math.PI - Math.toRadians(center)) / (2 * Math.PI));
return Math.toDegrees(norm_r);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment