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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20 #include <ao_distance.h>
23 ao_dist(int32_t a, int32_t b)
29 return (uint32_t) ((int64_t) d * 111198 / 10000000);
33 ao_lat_dist(int32_t lat_a, int32_t lat_b)
35 return ao_dist(lat_a, lat_b);
38 static const uint8_t cos_table[] = {
97 ao_lon_dist(int32_t lon_a, int32_t lon_b)
99 uint8_t c = cos_table[lon_a >> 24];
102 /* check if it's shorter to go the other way around */
103 if ((lon_a >> 1) < (lon_b >> 1) - (1800000000 >> 1))
105 lon_dist = ao_dist(lon_a, lon_b);
107 if (lon_dist & 0x7f800000)
108 lon_dist = (lon_dist >> 8) * c;
110 lon_dist = (lon_dist * (int16_t) c) >> 8;
115 static uint32_t sqr(uint32_t x) { return x * x; }
118 ao_distance(int32_t lat_a, int32_t lon_a, int32_t lat_b, int32_t lon_b)
120 uint32_t lat_dist = ao_lat_dist(lat_a, lat_b);
121 uint32_t lon_dist = ao_lon_dist(lon_a, lon_b);
123 return ao_sqrt (sqr(lat_dist) + sqr(lon_dist));