2 * Copyright © 2014 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 #include <ao_distance.h>
22 ao_dist(int32_t a, int32_t b)
27 return (uint32_t) ((int64_t) d * 111198 / 10000000);
31 ao_lat_dist(int32_t lat_a, int32_t lat_b)
33 return ao_dist(lat_a, lat_b);
36 static const uint8_t cos_table[] = {
95 ao_lon_dist(int32_t lon_a, int32_t lon_b)
97 uint8_t c = cos_table[lon_a >> 24];
100 /* check if it's shorter to go the other way around */
101 if (lon_a < lon_b - 1800000000)
103 lon_dist = ao_dist(lon_a, lon_b);
105 if (lon_dist & 0x7f800000)
106 lon_dist = (lon_dist >> 8) * c;
108 lon_dist = (lon_dist * (int16_t) c) >> 8;
113 static uint32_t sqr(uint32_t x) { return x * x; }
116 ao_distance(int32_t lat_a, int32_t lon_a, int32_t lat_b, int32_t lon_b)
118 uint32_t lat_dist = ao_lat_dist(lat_a, lat_b);
119 uint32_t lon_dist = ao_lon_dist(lon_a, lon_b);
121 return ao_sqrt (sqr(lat_dist) + sqr(lon_dist));