util.go (2652B)
1 // Copyright 2017 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 "github.com/golang/geo/s1" 18 19 // roundAngle returns the value rounded to nearest as an int32. 20 // This does not match C++ exactly for the case of x.5. 21 func roundAngle(val s1.Angle) int32 { 22 if val < 0 { 23 return int32(val - 0.5) 24 } 25 return int32(val + 0.5) 26 } 27 28 // minAngle returns the smallest of the given values. 29 func minAngle(x s1.Angle, others ...s1.Angle) s1.Angle { 30 min := x 31 for _, y := range others { 32 if y < min { 33 min = y 34 } 35 } 36 return min 37 } 38 39 // maxAngle returns the largest of the given values. 40 func maxAngle(x s1.Angle, others ...s1.Angle) s1.Angle { 41 max := x 42 for _, y := range others { 43 if y > max { 44 max = y 45 } 46 } 47 return max 48 } 49 50 // minChordAngle returns the smallest of the given values. 51 func minChordAngle(x s1.ChordAngle, others ...s1.ChordAngle) s1.ChordAngle { 52 min := x 53 for _, y := range others { 54 if y < min { 55 min = y 56 } 57 } 58 return min 59 } 60 61 // maxChordAngle returns the largest of the given values. 62 func maxChordAngle(x s1.ChordAngle, others ...s1.ChordAngle) s1.ChordAngle { 63 max := x 64 for _, y := range others { 65 if y > max { 66 max = y 67 } 68 } 69 return max 70 } 71 72 // minFloat64 returns the smallest of the given values. 73 func minFloat64(x float64, others ...float64) float64 { 74 min := x 75 for _, y := range others { 76 if y < min { 77 min = y 78 } 79 } 80 return min 81 } 82 83 // maxFloat64 returns the largest of the given values. 84 func maxFloat64(x float64, others ...float64) float64 { 85 max := x 86 for _, y := range others { 87 if y > max { 88 max = y 89 } 90 } 91 return max 92 } 93 94 // minInt returns the smallest of the given values. 95 func minInt(x int, others ...int) int { 96 min := x 97 for _, y := range others { 98 if y < min { 99 min = y 100 } 101 } 102 return min 103 } 104 105 // maxInt returns the largest of the given values. 106 func maxInt(x int, others ...int) int { 107 max := x 108 for _, y := range others { 109 if y > max { 110 max = y 111 } 112 } 113 return max 114 } 115 116 // clampInt returns the number closest to x within the range min..max. 117 func clampInt(x, min, max int) int { 118 if x < min { 119 return min 120 } 121 if x > max { 122 return max 123 } 124 return x 125 }