polyline_measures.go (1915B)
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 // This file defines various measures for polylines on the sphere. These are 18 // low-level methods that work directly with arrays of Points. They are used to 19 // implement the methods in various other measures files. 20 21 import ( 22 "github.com/golang/geo/r3" 23 "github.com/golang/geo/s1" 24 ) 25 26 // polylineLength returns the length of the given Polyline. 27 // It returns 0 for polylines with fewer than two vertices. 28 func polylineLength(p []Point) s1.Angle { 29 var length s1.Angle 30 31 for i := 1; i < len(p); i++ { 32 length += p[i-1].Distance(p[i]) 33 } 34 return length 35 } 36 37 // polylineCentroid returns the true centroid of the polyline multiplied by the 38 // length of the polyline. The result is not unit length, so you may wish to 39 // normalize it. 40 // 41 // Scaling by the Polyline length makes it easy to compute the centroid 42 // of several Polylines (by simply adding up their centroids). 43 // 44 // Note that for degenerate Polylines (e.g., AA) this returns Point(0, 0, 0). 45 // (This answer is correct; the result of this function is a line integral over 46 // the polyline, whose value is always zero if the polyline is degenerate.) 47 func polylineCentroid(p []Point) Point { 48 var centroid r3.Vector 49 for i := 1; i < len(p); i++ { 50 centroid = centroid.Add(EdgeTrueCentroid(p[i-1], p[i]).Vector) 51 } 52 return Point{centroid} 53 }