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)
28 return (uint32_t) ((int64_t) d * 111198 / 10000000);
32 ao_lat_dist(int32_t lat_a, int32_t lat_b)
34 return ao_dist(lat_a, lat_b);
37 static const uint8_t cos_table[] = {
96 ao_lon_dist(int32_t lon_a, int32_t lon_b)
98 uint8_t c = cos_table[lon_a >> 24];
101 /* check if it's shorter to go the other way around */
102 if ((lon_a >> 1) < (lon_b >> 1) - (1800000000 >> 1))
104 lon_dist = ao_dist(lon_a, lon_b);
106 if (lon_dist & 0x7f800000)
107 lon_dist = (lon_dist >> 8) * c;
109 lon_dist = (lon_dist * (int16_t) c) >> 8;
114 static uint32_t sqr(uint32_t x) { return x * x; }
117 ao_distance(int32_t lat_a, int32_t lon_a, int32_t lat_b, int32_t lon_b)
119 uint32_t lat_dist = ao_lat_dist(lat_a, lat_b);
120 uint32_t lon_dist = ao_lon_dist(lon_a, lon_b);
122 return ao_sqrt (sqr(lat_dist) + sqr(lon_dist));