github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/geo/geomfn/coord.go (about) 1 // Copyright 2020 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package geomfn 12 13 import ( 14 "math" 15 16 "github.com/twpayne/go-geom" 17 ) 18 19 // coordAdd adds two coordinates and returns a new result. 20 func coordAdd(a geom.Coord, b geom.Coord) geom.Coord { 21 return geom.Coord{a.X() + b.X(), a.Y() + b.Y()} 22 } 23 24 // coordSub subtracts two coordinates and returns a new result. 25 func coordSub(a geom.Coord, b geom.Coord) geom.Coord { 26 return geom.Coord{a.X() - b.X(), a.Y() - b.Y()} 27 } 28 29 // coordMul multiplies a coord by a scalar and returns the new result. 30 func coordMul(a geom.Coord, s float64) geom.Coord { 31 return geom.Coord{a.X() * s, a.Y() * s} 32 } 33 34 // coordDot returns the dot product of two coords if the coord was a vector. 35 func coordDot(a geom.Coord, b geom.Coord) float64 { 36 return a.X()*b.X() + a.Y()*b.Y() 37 } 38 39 // coordNorm2 returns the normalization^2 of a coordinate if the coord was a vector. 40 func coordNorm2(c geom.Coord) float64 { 41 return coordDot(c, c) 42 } 43 44 // coordNorm returns the normalization of a coordinate if the coord was a vector. 45 func coordNorm(c geom.Coord) float64 { 46 return math.Sqrt(coordNorm2(c)) 47 } 48 49 // coordEqual returns whether two coordinates are equal. 50 func coordEqual(a geom.Coord, b geom.Coord) bool { 51 return a.X() == b.X() && a.Y() == b.Y() 52 }