github.com/15mga/kiwi@v0.0.2-0.20240324021231-b95d5c3ac751/util/math.go (about) 1 package util 2 3 import "math" 4 5 const ( 6 Rad2Deg float32 = 180 / math.Pi 7 Deg2Rad float32 = math.Pi / 180 8 HalfPi float32 = math.Pi / 2 9 Eps float32 = 1e-5 10 ) 11 12 func RadianToDegree(radian float32) float32 { 13 return radian * Rad2Deg 14 } 15 16 func DegreeToRadian(angle float32) float32 { 17 return angle * Deg2Rad 18 } 19 20 func Round(d float32, bit int32) float32 { 21 var v = math.Pow(10, float64(bit)) 22 return float32(math.Round(float64(d)*v) / v) 23 } 24 25 func Ceil(d float32, bit int32) float32 { 26 var v = math.Pow(10, float64(bit)) 27 return float32(math.Ceil(float64(d)*v) / v) 28 } 29 30 func Floor(d float32, bit int32) float32 { 31 var v = math.Pow(10, float64(bit)) 32 return float32(math.Floor(float64(d)*v) / v) 33 } 34 35 func ClampInt32(min, max, v int32) int32 { 36 if v < min { 37 return min 38 } else if v > max { 39 return max 40 } 41 return v 42 } 43 44 func Clamp(min, max, v float32) float32 { 45 if v < min { 46 return min 47 } else if v > max { 48 return max 49 } 50 return v 51 } 52 53 func FloorInt32(floor, v int32) (r, o int32) { 54 if v < floor { 55 r = floor 56 o = floor - v 57 } else { 58 r = v 59 } 60 return 61 } 62 63 func CeilInt32(ceil, v int32) (r, o int32) { 64 if v > ceil { 65 r = ceil 66 o = v - ceil 67 } else { 68 r = v 69 } 70 return 71 } 72 73 func MinInt64(a, b int64) int64 { 74 if a < b { 75 return a 76 } 77 return b 78 } 79 80 func MaxInt64(a, b int64) int64 { 81 if a < b { 82 return b 83 } 84 return a 85 } 86 87 func MinInt(a, b int) int { 88 if a < b { 89 return a 90 } 91 return b 92 } 93 94 func MinInt32(a, b int32) int32 { 95 if a < b { 96 return a 97 } 98 return b 99 } 100 101 func MinUint32(a, b uint32) uint32 { 102 if a < b { 103 return a 104 } 105 return b 106 } 107 108 func MaxInt(a, b int) int { 109 if a < b { 110 return b 111 } 112 return a 113 } 114 115 func MaxInt32(a, b int32) int32 { 116 if a < b { 117 return b 118 } 119 return a 120 } 121 122 func MaxUint32(a, b uint32) uint32 { 123 if a < b { 124 return b 125 } 126 return a 127 } 128 129 func Min(l, r float32) float32 { 130 if l > r { 131 return r 132 } else { 133 return l 134 } 135 } 136 137 func Max(l, r float32) float32 { 138 if l > r { 139 return l 140 } else { 141 return r 142 } 143 } 144 145 func Abs(d float32) float32 { 146 return math.Float32frombits(math.Float32bits(d) &^ (1 << 31)) 147 } 148 149 func IsPowerOfTwo(mask int32) bool { 150 return (mask & (mask - 1)) == 0 151 } 152 153 func Sqrt(v float32) float32 { 154 return float32(math.Sqrt(float64(v))) 155 } 156 157 func NextPowerOfTwo(v int) int { 158 v -= 1 159 v |= v >> 16 160 v |= v >> 8 161 v |= v >> 4 162 v |= v >> 2 163 v |= v >> 1 164 return v + 1 165 } 166 167 func NextCap(required, current, slow int) (int, bool) { 168 if required <= current { 169 return current, false 170 } 171 if current < slow { 172 return NextPowerOfTwo(required), true 173 } 174 for current < required { 175 current = current << 1 176 } 177 return current, true 178 } 179 180 func Lerp(from, to, t float32) float32 { 181 return to*t + from*(1.0-t) 182 } 183 184 func CmpZero(v float32) int { 185 if Abs(v) <= Eps { 186 return 0 187 } 188 if v < 0 { 189 return -1 190 } 191 return 1 192 } 193 194 func Cmp(v1, v2 float32) int { 195 return CmpZero(v1 - v2) 196 } 197 198 func Equal(v1, v2 float32) bool { 199 return Abs(v1-v2) < Eps 200 } 201 202 func ClampFloat(v, min, max float32) float32 { 203 if v < min { 204 return min 205 } else if v > max { 206 return max 207 } 208 return v 209 } 210 211 func LerpFloat(v1, v2, v float32) float32 { 212 return v1 + (v2-v1)*v 213 } 214 215 func LerpInt(v1, v2, v, t int) int { 216 return v1 + (v2-v1)*v/t 217 }