-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
utils.go
105 lines (89 loc) · 1.91 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package asciigraph
import (
"fmt"
"log"
"math"
"os"
"os/exec"
"runtime"
)
func minMaxFloat64Slice(v []float64) (min, max float64) {
min = math.Inf(1)
max = math.Inf(-1)
if len(v) == 0 {
panic("Empty slice")
}
for _, e := range v {
if e < min {
min = e
}
if e > max {
max = e
}
}
return
}
func round(input float64) float64 {
if math.IsNaN(input) {
return math.NaN()
}
sign := 1.0
if input < 0 {
sign = -1
input *= -1
}
_, decimal := math.Modf(input)
var rounded float64
if decimal >= 0.5 {
rounded = math.Ceil(input)
} else {
rounded = math.Floor(input)
}
return rounded * sign
}
func linearInterpolate(before, after, atPoint float64) float64 {
return before + (after-before)*atPoint
}
func interpolateArray(data []float64, fitCount int) []float64 {
var interpolatedData []float64
springFactor := float64(len(data)-1) / float64(fitCount-1)
interpolatedData = append(interpolatedData, data[0])
for i := 1; i < fitCount-1; i++ {
spring := float64(i) * springFactor
before := math.Floor(spring)
after := math.Ceil(spring)
atPoint := spring - before
interpolatedData = append(interpolatedData, linearInterpolate(data[int(before)], data[int(after)], atPoint))
}
interpolatedData = append(interpolatedData, data[len(data)-1])
return interpolatedData
}
// clear terminal screen
var Clear func()
func init() {
platform := runtime.GOOS
if platform == "windows" {
Clear = func() {
cmd := exec.Command("cmd", "/c", "cls")
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
log.Fatal(err)
}
}
} else {
Clear = func() {
fmt.Print("\033[2J\033[H")
}
}
}
func calculateHeight(interval float64) int {
if interval >= 1 {
return int(interval)
}
scaleFactor := math.Pow(10, math.Floor(math.Log10(interval)))
scaledDelta := interval / scaleFactor
if scaledDelta < 2 {
return int(math.Ceil(scaledDelta))
}
return int(math.Floor(scaledDelta))
}