|
| 1 | +// This code is taken from the android-maps-utils 3.10.0 repository and its import statements have been adjusted. |
| 2 | +// The original repository from Google-Maven (maven.google.com) can't be used, because it requires Android. |
| 3 | + |
| 4 | +/* |
| 5 | + * Copyright 2013 Google Inc. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package android_maps_utils_3_10_0; |
| 21 | + |
| 22 | +import static java.lang.Math.*; |
| 23 | + |
| 24 | +/** |
| 25 | + * Utility functions that are used my both PolyUtil and SphericalUtil. |
| 26 | + */ |
| 27 | +class MathUtil { |
| 28 | + /** |
| 29 | + * The earth's radius, in meters. |
| 30 | + * Mean radius as defined by IUGG. |
| 31 | + */ |
| 32 | + static final double EARTH_RADIUS = 6371009; |
| 33 | + |
| 34 | + /** |
| 35 | + * Restrict x to the range [low, high]. |
| 36 | + */ |
| 37 | + static double clamp(double x, double low, double high) { |
| 38 | + return x < low ? low : (x > high ? high : x); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Wraps the given value into the inclusive-exclusive interval between min and max. |
| 43 | + * |
| 44 | + * @param n The value to wrap. |
| 45 | + * @param min The minimum. |
| 46 | + * @param max The maximum. |
| 47 | + */ |
| 48 | + static double wrap(double n, double min, double max) { |
| 49 | + return (n >= min && n < max) ? n : (mod(n - min, max - min) + min); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Returns the non-negative remainder of x / m. |
| 54 | + * |
| 55 | + * @param x The operand. |
| 56 | + * @param m The modulus. |
| 57 | + */ |
| 58 | + static double mod(double x, double m) { |
| 59 | + return ((x % m) + m) % m; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Returns mercator Y corresponding to latitude. |
| 64 | + * See http://en.wikipedia.org/wiki/Mercator_projection . |
| 65 | + */ |
| 66 | + static double mercator(double lat) { |
| 67 | + return log(tan(lat * 0.5 + PI / 4)); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Returns latitude from mercator Y. |
| 72 | + */ |
| 73 | + static double inverseMercator(double y) { |
| 74 | + return 2 * atan(exp(y)) - PI / 2; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Returns haversine(angle-in-radians). |
| 79 | + * hav(x) == (1 - cos(x)) / 2 == sin(x / 2)^2. |
| 80 | + */ |
| 81 | + static double hav(double x) { |
| 82 | + double sinHalf = sin(x * 0.5); |
| 83 | + return sinHalf * sinHalf; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Computes inverse haversine. Has good numerical stability around 0. |
| 88 | + * arcHav(x) == acos(1 - 2 * x) == 2 * asin(sqrt(x)). |
| 89 | + * The argument must be in [0, 1], and the result is positive. |
| 90 | + */ |
| 91 | + static double arcHav(double x) { |
| 92 | + return 2 * asin(sqrt(x)); |
| 93 | + } |
| 94 | + |
| 95 | + // Given h==hav(x), returns sin(abs(x)). |
| 96 | + static double sinFromHav(double h) { |
| 97 | + return 2 * sqrt(h * (1 - h)); |
| 98 | + } |
| 99 | + |
| 100 | + // Returns hav(asin(x)). |
| 101 | + static double havFromSin(double x) { |
| 102 | + double x2 = x * x; |
| 103 | + return x2 / (1 + sqrt(1 - x2)) * .5; |
| 104 | + } |
| 105 | + |
| 106 | + // Returns sin(arcHav(x) + arcHav(y)). |
| 107 | + static double sinSumFromHav(double x, double y) { |
| 108 | + double a = sqrt(x * (1 - x)); |
| 109 | + double b = sqrt(y * (1 - y)); |
| 110 | + return 2 * (a + b - 2 * (a * y + b * x)); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Returns hav() of distance from (lat1, lng1) to (lat2, lng2) on the unit sphere. |
| 115 | + */ |
| 116 | + static double havDistance(double lat1, double lat2, double dLng) { |
| 117 | + return hav(lat1 - lat2) + hav(dLng) * cos(lat1) * cos(lat2); |
| 118 | + } |
| 119 | +} |
0 commit comments