forked from chriskim06/drawille-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
75 lines (65 loc) · 1.44 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
package drawille
import (
"fmt"
"image"
"math"
)
func getMinMaxFloat64From2dSlice(slices [][]float64) (float64, float64) {
var max float64
min := math.Inf(1)
for _, slice := range slices {
for _, val := range slice {
if val > max {
max = val
}
if val < min {
min = val
}
}
}
return min, max
}
func ColorString(s string, c Color) string {
if c == Default {
return s
}
return fmt.Sprintf("%s%s%s", c, s, reset)
}
func line(p0, p1 image.Point) []image.Point {
points := []image.Point{}
leftPoint, rightPoint := p0, p1
if leftPoint.X > rightPoint.X {
leftPoint, rightPoint = rightPoint, leftPoint
}
xDistance := absInt(leftPoint.X - rightPoint.X)
yDistance := absInt(leftPoint.Y - rightPoint.Y)
slope := float64(yDistance) / float64(xDistance)
slopeSign := 1
if rightPoint.Y < leftPoint.Y {
slopeSign = -1
}
targetYCoordinate := float64(leftPoint.Y)
currentYCoordinate := leftPoint.Y
for i := leftPoint.X; i < rightPoint.X; i++ {
points = append(points, image.Pt(i, currentYCoordinate))
targetYCoordinate += (slope * float64(slopeSign))
for currentYCoordinate != int(targetYCoordinate) {
points = append(points, image.Pt(i, currentYCoordinate))
currentYCoordinate += slopeSign
}
}
return points
}
func absInt(x int) int {
if x >= 0 {
return x
}
return -x
}
func repeatRune(r rune, amount int) []rune {
runes := []rune{}
for i := 0; i < amount; i++ {
runes = append(runes, r)
}
return runes
}