-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
131 lines (109 loc) · 2.7 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package maprenderer
import (
"fmt"
"image"
"image/color"
"github.com/minetest-go/types"
)
func GetIsometricImageSize(size *types.Pos, cube_len int) (int, int) {
width := (size.X() * cube_len / 2) +
(size.Z() * cube_len / 2)
height := (size.X() * cube_len / 4) +
(size.Y() * cube_len / 2) +
(size.Z() * cube_len / 4)
return width, height
}
func GetIsoCenterCubeOffset(size *types.Pos, cube_len int) (int, int) {
x := (size.Z() * cube_len / 2) -
(cube_len / 2)
y := (size.X() * cube_len / 4) +
(size.Y() * cube_len / 2) +
(size.Z() * cube_len / 4) -
cube_len
return x, y
}
func GetIsoCubePosition(center_x, center_y, cube_len int, pos *types.Pos) (int, int) {
x := center_x -
(pos.Z() * cube_len / 2) +
(pos.X() * cube_len / 2)
y := center_y -
(pos.X() * cube_len / 4) -
(pos.Y() * cube_len / 2) -
(pos.Z() * cube_len / 4)
return x, y
}
func DrawIsoCube(img *image.RGBA, cube_len, x_offset, y_offset int, c1, c2, c3 color.Color) error {
if cube_len%4 != 0 {
return fmt.Errorf("cube_len must be divisible by 4")
}
if cube_len <= 4 {
return fmt.Errorf("cube_len must be greater than 4")
}
half_len_zero_indexed := (cube_len / 2) - 1
quarter_len := cube_len / 4
// left/right part
yo := 0
for x := 0; x <= half_len_zero_indexed; x++ {
for y := 0; y <= half_len_zero_indexed; y++ {
// left
img.Set(x_offset+x, y_offset+y+quarter_len+yo, c1)
// right
img.Set(x_offset+cube_len-1-x, y_offset+y+quarter_len+yo, c2)
}
if x%2 == 0 {
yo = yo + 1
}
}
// upper part
yo = 0
yl := 1
for x := 0; x <= half_len_zero_indexed-1; x++ {
for y := 0; y <= yl; y++ {
// left
img.Set(x_offset+1+x, y_offset+quarter_len-1-yo+y, c3)
// right
img.Set(x_offset+cube_len-2-x, y_offset+quarter_len-1-yo+y, c3)
}
if x%2 != 0 {
yo = yo + 1
yl = yl + 2
}
}
return nil
}
func GetIsoNodeOrder(p *types.Pos) int {
return (64000 - p.X()) + p.Y() + (64000 - p.Z())
}
func addAndClampUint8(a uint8, b int) uint8 {
v := int(a) + b
if v > 255 {
return 255
} else if v < 0 {
return 0
} else {
return uint8(v)
}
}
func ColorAdjust(c *color.RGBA, value int) *color.RGBA {
return &color.RGBA{
R: addAndClampUint8(c.R, value),
G: addAndClampUint8(c.G, value),
B: addAndClampUint8(c.B, value),
A: c.A,
}
}
func BlendColor(bg, fg *color.RGBA, bf float64) *color.RGBA {
a := float64(fg.A) / 255 / bf
ai := 1 - a
return &color.RGBA{
R: uint8((float64(fg.R) * a) + (float64(bg.R) * ai)),
G: uint8((float64(fg.G) * a) + (float64(bg.G) * ai)),
B: uint8((float64(fg.B) * a) + (float64(bg.B) * ai)),
A: max(bg.A, fg.A),
}
}
func SortNodesWithColor(n1, n2 *NodeWithColor) int {
o1 := GetIsoNodeOrder(n1.Pos)
o2 := GetIsoNodeOrder(n2.Pos)
return o1 - o2
}