forked from fogleman/fauxgl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
94 lines (82 loc) · 1.49 KB
/
util.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
package fauxgl
import (
"image"
_ "image/jpeg"
"image/png"
"math"
"os"
"strconv"
)
func Radians(degrees float64) float64 {
return degrees * math.Pi / 180
}
func Degrees(radians float64) float64 {
return radians * 180 / math.Pi
}
func LatLngToXYZ(lat, lng float64) Vector {
lat, lng = Radians(lat), Radians(lng)
x := math.Cos(lat) * math.Cos(lng)
y := math.Cos(lat) * math.Sin(lng)
z := math.Sin(lat)
return Vector{x, y, z}
}
func LoadImage(path string) (image.Image, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
im, _, err := image.Decode(file)
return im, err
}
func SavePNG(path string, im image.Image) error {
file, err := os.Create(path)
if err != nil {
return err
}
defer file.Close()
return png.Encode(file, im)
}
func ParseFloats(items []string) []float64 {
result := make([]float64, len(items))
for i, item := range items {
f, _ := strconv.ParseFloat(item, 64)
result[i] = f
}
return result
}
func Clamp(x, lo, hi float64) float64 {
if x < lo {
return lo
}
if x > hi {
return hi
}
return x
}
func ClampInt(x, lo, hi int) int {
if x < lo {
return lo
}
if x > hi {
return hi
}
return x
}
func AbsInt(x int) int {
if x < 0 {
return -x
}
return x
}
func Round(a float64) int {
if a < 0 {
return int(math.Ceil(a - 0.5))
} else {
return int(math.Floor(a + 0.5))
}
}
func RoundPlaces(a float64, places int) float64 {
shift := math.Pow(10, float64(places))
return float64(Round(a*shift)) / shift
}