1 // Copyright 2018 Google Inc. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package s2 16 17 import ( 18 "math" 19 "testing" 20 ) 21 22 func TestPolylineMeasuresGreatCircles(t *testing.T) { 23 // Construct random great circles and divide them randomly into segments. 24 // Then make sure that the length and centroid are correct. Note that 25 // because of the way the centroid is computed, it does not matter how 26 // we split the great circle into segments. 27 for iter := 0; iter < 100; iter++ { 28 // Choose a coordinate frame for the great circle. 29 f := randomFrame() 30 x := f.row(0) 31 y := f.row(1) 32 33 var line []Point 34 for theta := 0.0; theta < 2*math.Pi; theta += math.Pow(randomFloat64(), 10) { 35 line = append(line, Point{x.Mul(math.Cos(theta)).Add(y.Mul(math.Sin(theta)))}) 36 } 37 38 // Close the circle. 39 line = append(line, line[0]) 40 41 length := polylineLength(line) 42 if got, want := math.Abs(length.Radians()-2*math.Pi), 2e-14; got > want { 43 t.Errorf("polylineLength(%v) = %v, want < %v", line, got, want) 44 } 45 46 centroid := polylineCentroid(line) 47 if got, want := centroid.Norm(), 2e-14; got > want { 48 t.Errorf("%v.Norm() = %v, want < %v", centroid, got, want) 49 } 50 } 51 } 52