| 1 |
package chart |
| 2 |
|
| 3 |
import "math" |
| 4 |
|
| 5 |
const ( |
| 6 |
_pi = math.Pi |
| 7 |
_2pi = 2 * math.Pi |
| 8 |
_3pi4 = (3 * math.Pi) / 4.0 |
| 9 |
_4pi3 = (4 * math.Pi) / 3.0 |
| 10 |
_3pi2 = (3 * math.Pi) / 2.0 |
| 11 |
_5pi4 = (5 * math.Pi) / 4.0 |
| 12 |
_7pi4 = (7 * math.Pi) / 4.0 |
| 13 |
_pi2 = math.Pi / 2.0 |
| 14 |
_pi4 = math.Pi / 4.0 |
| 15 |
_d2r = (math.Pi / 180.0) |
| 16 |
_r2d = (180.0 / math.Pi) |
| 17 |
) |
| 18 |
|
| 19 |
|
| 20 |
func MinMax(values ...float64) (min, max float64) { |
| 21 |
if len(values) == 0 { |
| 22 |
return |
| 23 |
} |
| 24 |
|
| 25 |
max = values[0] |
| 26 |
min = values[0] |
| 27 |
var value float64 |
| 28 |
for index := 1; index < len(values); index++ { |
| 29 |
value = values[index] |
| 30 |
if value < min { |
| 31 |
min = value |
| 32 |
} |
| 33 |
if value > max { |
| 34 |
max = value |
| 35 |
} |
| 36 |
} |
| 37 |
return |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
func MinInt(values ...int) (min int) { |
| 42 |
if len(values) == 0 { |
| 43 |
return |
| 44 |
} |
| 45 |
|
| 46 |
min = values[0] |
| 47 |
var value int |
| 48 |
for index := 1; index < len(values); index++ { |
| 49 |
value = values[index] |
| 50 |
if value < min { |
| 51 |
min = value |
| 52 |
} |
| 53 |
} |
| 54 |
return |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
func MaxInt(values ...int) (max int) { |
| 59 |
if len(values) == 0 { |
| 60 |
return |
| 61 |
} |
| 62 |
|
| 63 |
max = values[0] |
| 64 |
var value int |
| 65 |
for index := 1; index < len(values); index++ { |
| 66 |
value = values[index] |
| 67 |
if value > max { |
| 68 |
max = value |
| 69 |
} |
| 70 |
} |
| 71 |
return |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
func AbsInt(value int) int { |
| 76 |
if value < 0 { |
| 77 |
return -value |
| 78 |
} |
| 79 |
return value |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
func DegreesToRadians(degrees float64) float64 { |
| 84 |
return degrees * _d2r |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
func RadiansToDegrees(value float64) float64 { |
| 89 |
return math.Mod(value, _2pi) * _r2d |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
func PercentToRadians(pct float64) float64 { |
| 94 |
return DegreesToRadians(360.0 * pct) |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
func RadianAdd(base, delta float64) float64 { |
| 99 |
value := base + delta |
| 100 |
if value > _2pi { |
| 101 |
return math.Mod(value, _2pi) |
| 102 |
} else if value < 0 { |
| 103 |
return math.Mod(_2pi+value, _2pi) |
| 104 |
} |
| 105 |
return value |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
func DegreesAdd(baseDegrees, deltaDegrees float64) float64 { |
| 110 |
value := baseDegrees + deltaDegrees |
| 111 |
if value > _2pi { |
| 112 |
return math.Mod(value, 360.0) |
| 113 |
} else if value < 0 { |
| 114 |
return math.Mod(360.0+value, 360.0) |
| 115 |
} |
| 116 |
return value |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
func DegreesToCompass(deg float64) float64 { |
| 121 |
return DegreesAdd(deg, -90.0) |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
func CirclePoint(cx, cy int, radius, thetaRadians float64) (x, y int) { |
| 127 |
x = cx + int(radius*math.Sin(thetaRadians)) |
| 128 |
y = cy - int(radius*math.Cos(thetaRadians)) |
| 129 |
return |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
func RotateCoordinate(cx, cy, x, y int, thetaRadians float64) (rx, ry int) { |
| 134 |
tempX, tempY := float64(x-cx), float64(y-cy) |
| 135 |
rotatedX := tempX*math.Cos(thetaRadians) - tempY*math.Sin(thetaRadians) |
| 136 |
rotatedY := tempX*math.Sin(thetaRadians) + tempY*math.Cos(thetaRadians) |
| 137 |
rx = int(rotatedX) + cx |
| 138 |
ry = int(rotatedY) + cy |
| 139 |
return |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
func RoundUp(value, roundTo float64) float64 { |
| 144 |
if roundTo < 0.000000000000001 { |
| 145 |
return value |
| 146 |
} |
| 147 |
d1 := math.Ceil(value / roundTo) |
| 148 |
return d1 * roundTo |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
func RoundDown(value, roundTo float64) float64 { |
| 153 |
if roundTo < 0.000000000000001 { |
| 154 |
return value |
| 155 |
} |
| 156 |
d1 := math.Floor(value / roundTo) |
| 157 |
return d1 * roundTo |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
func Normalize(values ...float64) []float64 { |
| 164 |
var total float64 |
| 165 |
for _, v := range values { |
| 166 |
total += v |
| 167 |
} |
| 168 |
output := make([]float64, len(values)) |
| 169 |
for x, v := range values { |
| 170 |
output[x] = RoundDown(v/total, 0.0001) |
| 171 |
} |
| 172 |
return output |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
func Mean(values ...float64) float64 { |
| 177 |
return Sum(values...) / float64(len(values)) |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
func MeanInt(values ...int) int { |
| 182 |
return SumInt(values...) / len(values) |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
func Sum(values ...float64) float64 { |
| 187 |
var total float64 |
| 188 |
for _, v := range values { |
| 189 |
total += v |
| 190 |
} |
| 191 |
return total |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
func SumInt(values ...int) int { |
| 196 |
var total int |
| 197 |
for _, v := range values { |
| 198 |
total += v |
| 199 |
} |
| 200 |
return total |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
func PercentDifference(v1, v2 float64) float64 { |
| 206 |
if v1 == 0 { |
| 207 |
return 0 |
| 208 |
} |
| 209 |
return (v2 - v1) / v1 |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
func GetRoundToForDelta(delta float64) float64 { |
| 214 |
startingDeltaBound := math.Pow(10.0, 10.0) |
| 215 |
for cursor := startingDeltaBound; cursor > 0; cursor /= 10.0 { |
| 216 |
if delta > cursor { |
| 217 |
return cursor / 10.0 |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
return 0.0 |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
func RoundPlaces(input float64, places int) (rounded float64) { |
| 226 |
if math.IsNaN(input) { |
| 227 |
return 0.0 |
| 228 |
} |
| 229 |
|
| 230 |
sign := 1.0 |
| 231 |
if input < 0 { |
| 232 |
sign = -1 |
| 233 |
input *= -1 |
| 234 |
} |
| 235 |
|
| 236 |
precision := math.Pow(10, float64(places)) |
| 237 |
digit := input * precision |
| 238 |
_, decimal := math.Modf(digit) |
| 239 |
|
| 240 |
if decimal >= 0.5 { |
| 241 |
rounded = math.Ceil(digit) |
| 242 |
} else { |
| 243 |
rounded = math.Floor(digit) |
| 244 |
} |
| 245 |
|
| 246 |
return rounded / precision * sign |
| 247 |
} |
| 248 |
|
| 249 |
func f64i(value float64) int { |
| 250 |
r := RoundPlaces(value, 0) |
| 251 |
return int(r) |
| 252 |
} |