Last active
March 30, 2018 01:13
-
-
Save webstory/4bd1eb2234bd4330100961856d9dbf42 to your computer and use it in GitHub Desktop.
Normalize Angle (from https://github.com/apache/commons-math/blob/53ec46ba272e23c0c96ada42f26f4e70e96f3115/src/main/java/org/apache/commons/math4/util/MathUtils.java#L107 )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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π:<br> | |
* {@code a = MathUtils.normalizeAngle(a, FastMath.PI);}</li> | |
* <li>normalize an angle between -π and +π<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 π 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π interval for the result | |
* @return a-2kπ with integer k and center-π <= a-2kπ <= center+π | |
* @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