-
Notifications
You must be signed in to change notification settings - Fork 0
/
havers2_test.go
182 lines (172 loc) · 4.11 KB
/
havers2_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package havers2_test
import (
"math"
"testing"
"github.com/loraxipam/havers2"
"github.com/golang/geo/s2"
)
// Great circle calculations are really only valid for about four sig figs
// for Earth, anyway. Up this, (or should that be, "decrease this"?)
// for real spheres.
const ppm = 0.0001
var tests = []struct {
p havers2.Coord
q havers2.Coord
outMi float64
outKm float64
outRa float64
town string
}{
{
havers2.Coord{Lat: -22.55, Lon: -43.12}, // Rio de Janeiro, Brazil
havers2.Coord{Lat: 13.45, Lon: 100.28}, // Bangkok, Thailand
9958,
16026,
2.515463,
"Rio to Bangkok",
},
{
havers2.Coord{Lat: -20.10, Lon: 57.30}, // Port Louis, Mauritius
havers2.Coord{Lat: 0.57, Lon: 100.21}, // Padang, Indonesia
3234,
5205,
0.817067,
"Mauritius to Indonesia",
},
{
havers2.Coord{Lat: 51.45, Lon: -1.15}, // Oxford, United Kingdom
havers2.Coord{Lat: 41.54, Lon: 12.27}, // Vatican, City Vatican City
933,
1501,
0.235736,
"Oxford to the Vatican",
},
{
// These are antipodes
havers2.Coord{Lat: 32.30, Lon: -64.77}, // Bermuda
havers2.Coord{Lat: -32.30, Lon: 115.23}, // Perth, Australia
12436, // earthRadiusMi * pi
20015, // earthRadiusKm * pi
3.14159, // pi
"Bermuda to Perth",
},
{
havers2.Coord{Lat: -22.34, Lon: 17.05}, // Windhoek, Namibia
havers2.Coord{Lat: 51.56, Lon: 4.29}, // Rotterdam, Netherlands
5164,
8311,
1.304548,
"Namibia to the Netherlands",
},
{
havers2.Coord{Lat: -63.24, Lon: -56.59}, // Esperanza, Argentina
havers2.Coord{Lat: -8.50, Lon: 13.14}, // Luanda, Angola
5069,
8157,
1.280482,
"Argentina to Angola",
},
{
havers2.Coord{Lat: 90.00, Lon: 0.00}, // North Pole
havers2.Coord{Lat: 48.51, Lon: 2.21}, // Paris, France
2866,
4613,
0.724137,
"Santa to Paris",
},
{
havers2.Coord{Lat: -90.00, Lon: 0.00}, // South Pole
havers2.Coord{Lat: 48.51, Lon: 2.21}, // Paris, France
9570,
15401,
2.417456,
"Penguins to Paris",
},
{
havers2.Coord{Lat: 45.04, Lon: 7.42}, // Turin, Italy
havers2.Coord{Lat: 3.09, Lon: 101.42}, // Kuala Lumpur, Malaysia
6262,
10078,
1.581873,
"Turin to Malaysia",
},
{
havers2.Coord{Lat: 45.71, Lon: -122.43}, // Hockinson, Washington, USA
havers2.Coord{Lat: 29.13, Lon: -80.96}, // Wilbur-by-the-Sea, Florida, USA
2510,
4040,
0.634270,
"Washington to Florida",
},
}
// TestHaversineDistance makes sure that the several great circle distance
// functions work properly
func TestHaversineDistance(t *testing.T) {
for _, input := range tests {
// init the points
input.p.Calc()
input.q.Calc()
mi, _ := math.Modf(havers2.DistanceMi(input.p, input.q))
km, _ := math.Modf(havers2.DistanceKm(input.p, input.q))
if input.outMi != mi || input.outKm != km {
t.Errorf("fail: want %v %v -> %v %v got %g %g %s",
input.p,
input.q,
input.outMi,
input.outKm,
mi,
km,
input.town,
)
}
}
}
// TestIntAngle makes sure the internal angle calculation result is within tolerance
func TestIntAngle(t *testing.T) {
for _, input := range tests {
// init the points
input.p.Calc()
input.q.Calc()
iAngle := havers2.IntAngle(input.p, input.q)
delta := math.Abs(input.outRa-iAngle) / input.outRa
if delta > ppm {
t.Errorf("fail: want %v %v -> %v got %.6f, %.1f ppm %s",
input.p,
input.q,
input.outRa,
iAngle,
delta*1000000,
input.town,
)
}
}
}
// TestPoleToPole makes sure the sum of internal angles of the north pole, a
// point and the south pole adds up to 180 degrees (pi radians)
func TestPoleToPole(t *testing.T) {
var ll s2.LatLng
var p s2.Point
reindeer := havers2.Coord{90, 0, ll, p}
penguins := havers2.Coord{-90, 0, ll, p}
reindeer.Calc()
penguins.Calc()
for _, input := range tests {
// init the points
input.p.Calc()
input.q.Calc()
nAngle := havers2.IntAngle(input.q, reindeer)
sAngle := havers2.IntAngle(input.q, penguins)
tAngle := nAngle + sAngle
delta := math.Abs(tAngle-math.Pi) / math.Pi
if delta > ppm {
t.Errorf("fail: want %v -> %-.5f + %-.5f = Pi got %-.5f, %.1f ppm %s",
input.q,
nAngle,
sAngle,
tAngle,
delta*1000000,
input.town,
)
}
}
}