-
Notifications
You must be signed in to change notification settings - Fork 7
/
helper.go
171 lines (154 loc) · 3.6 KB
/
helper.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package wordcloud_go
import (
"math"
"os"
"fmt"
"image/png"
"github.com/fogleman/gg"
)
func TwoByBitmap(imgpath string) *WorldMap {
worldMap := &WorldMap{
CollisionMap: make([]int, 0),
}
file, err := os.Open(imgpath)
if err != nil {
fmt.Println(err)
}
img, err := png.Decode(file)
file.Close()
bounds := img.Bounds()
w := bounds.Size().X
h := bounds.Size().Y
worldMap.Width = w / XUNIT
worldMap.Height = h / YUNIT
worldMap.RealImageWidth = w
worldMap.RealImageHeight = h
for y := 0; y < worldMap.Height; y++ {
for x := 0; x < worldMap.Width; x++ {
color := img.At(x*XUNIT, y*YUNIT)
_, _, _, alpha := color.RGBA()
if alpha == 0 {
worldMap.CollisionMap = append(worldMap.CollisionMap, 1)
} else {
worldMap.CollisionMap = append(worldMap.CollisionMap, 0)
}
}
}
return worldMap
}
func TwoByBlock(width, height int) ([]*Position, int, int) {
positions := make([]*Position, 0)
maxX := width / XUNIT
maxY := height / YUNIT
//maxX += 2
//maxY += 2
for y := 0; y < maxY; y++ {
for x := 0; x < maxX; x++ {
position := NewPosition(x, y, IS_NOT_FIT, 0, 0)
positions = append(positions, position)
}
}
return positions, maxX, maxY
}
func DrawText(dc *gg.Context, text string, xpos, ypos, rotation float64) {
if rotation != 0 {
dc.RotateAbout(rotation, xpos, ypos)
}
dc.DrawStringAnchored(text, xpos, ypos, 0.5, 0.5)
if rotation != 0 {
dc.RotateAbout(-rotation, xpos, ypos)
}
}
func GetTextBound(measureDc *gg.Context, text string) (w, h, xdiff, ydiff float64) {
measureDc.SetRGBA(0, 0, 0, 0)
measureDc.Clear()
measureDc.SetRGB(0, 0, 0)
measureDc.DrawStringAnchored(text, 375, 375, 0.5, 0.5)
img := measureDc.Image()
width := measureDc.Width()
height := measureDc.Height()
maxX := 0
maxY := 0
minX := 9999999
minY := 9999999
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
color := img.At(x, y)
_, _, _, alpha := color.RGBA()
if alpha != 0 {
if minX > x {
minX = x
}
if minY > y {
minY = y
}
if maxX < x {
maxX = x
}
if maxY < y {
maxY = y
}
}
}
}
w1, h1 := measureDc.MeasureString(text)
wdiff := float64(maxX - minX)
hdiff := float64(maxY - minY)
xdiff = float64(w1 - wdiff)
ydiff = float64(h1- hdiff)
return wdiff, hdiff, xdiff, ydiff
}
/*
*先设置清空颜色,再进行清空
*/
func Clear(dc *gg.Context) {
dc.SetRGBA(0, 0, 0, 0)
dc.Clear()
}
func Rotate(grid *Grid, angle float64, centerX, centerY int) {
maxX := grid.Width
maxY := grid.Height
width := maxX * XUNIT
height := maxY * YUNIT
halfX := width / 2
halfY := height / 2
tempX := 0
tempY := 0
gridData := grid.positions
sinPi := SinT(angle)
cosPi := CosT(angle)
for y := 0; y < maxY; y++ {
for x := 0; x < maxX; x++ {
index := y*maxX + x
position := gridData[index]
position.Xpos = x
position.Ypos = y
position.Xpos = position.Xpos*XUNIT - halfX
position.Ypos = position.Ypos*YUNIT - halfY
tempX = position.Xpos
tempY = position.Ypos
position.Xpos = (int)(float64(tempX)*cosPi - float64(tempY)*sinPi)
position.Ypos = (int)(float64(tempX)*sinPi + float64(tempY)*cosPi)
position.Xpos /= XUNIT
position.Ypos /= YUNIT
position.Xpos += centerX
position.Ypos += centerY
position.XLeiji = position.Xpos
position.YLeiji = position.Ypos
}
}
}
func CeilT(value float64) float64 {
return math.Ceil(value)
}
func CosT(angle float64) float64 {
angle = angle / DEGREE_180 * math.Pi
return math.Cos(angle)
}
func SinT(angle float64) float64 {
angle = angle / DEGREE_180 * math.Pi
return math.Sin(angle)
}
func Angle2Pi(angle float64) float64 {
return angle / DEGREE_180 * math.Pi
}