-
Notifications
You must be signed in to change notification settings - Fork 103
/
starfield.go
154 lines (130 loc) · 2.62 KB
/
starfield.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
package render
import (
"fmt"
"image"
"image/color"
"math"
"math/rand"
"github.com/tidbyt/gg"
)
type Starfield struct {
Widget
Child Widget
Color color.Color
Width int
Height int
stars []*Star
}
type Star struct {
X float64
Y float64
D float64
V float64
PrevX float64
PrevY float64
}
func (s *Star) Tick() {
s.PrevX = s.X
s.PrevY = s.Y
s.X = s.X / s.D
s.Y = s.Y / s.D
s.D *= 1 - s.V
}
// TODO: use PolyLine from path.go instead
func DrawLine(dc *gg.Context, x0, y0, x1, y1 int) {
dx := x1 - x0
if dx < 0 {
dx = -dx
}
sx := -1
if x0 < x1 {
sx = 1
}
dy := y1 - y0
if dy > 0 {
dy = -dy
}
sy := -1
if y0 < y1 {
sy = 1
}
if dx == 0 {
for ; y0 != y1; y0 += sy {
dc.SetPixel(x0, y0)
}
dc.SetPixel(x0, y0)
return
}
if dy == 0 {
for ; x0 != x1; x0 += sx {
dc.SetPixel(x0, y0)
}
dc.SetPixel(x0, y0)
return
}
err := dx + dy
dc.SetPixel(x0, y0)
for x0 != x1 && y0 != y1 {
e2 := 2 * err
if e2 >= dy {
err += dy
x0 += sx
}
if e2 <= dx {
err += dx
y0 += sy
}
dc.SetPixel(x0, y0)
}
}
func (s *Starfield) Init() error {
s.initStars(60)
return nil
}
func (s *Starfield) initStars(numStars int) {
s.stars = make([]*Star, numStars)
for i := 0; i < numStars; i++ {
s.stars[i] = &Star{
X: 2*rand.Float64() - 1,
Y: 2*rand.Float64() - 1,
D: 1.0, //rand.Float64(),
V: 0.01, //rand.Float64()
}
if i < 10 {
fmt.Println(s.stars[i].X, s.stars[i].Y)
}
}
}
func (s *Starfield) PaintBounds(bounds image.Rectangle, frameIdx int) image.Image {
return image.Rect(0, 0, 64, 32)
}
func (s *Starfield) Paint(dc *gg.Context, bounds image.Rectangle, frameIdx int) {
Black := color.RGBA{R: 0, G: 0, B: 0, A: 0xff}
dc.SetColor(Black)
dc.Clear()
for j := 0; j < len(s.stars); j++ {
s.stars[j].Tick()
if math.Abs(s.stars[j].X) > 1.0 || math.Abs(s.stars[j].Y) > 1.0 {
s.stars[j].X = rand.NormFloat64() * 0.3
s.stars[j].Y = rand.NormFloat64() * 0.3
s.stars[j].PrevX = 0
s.stars[j].PrevY = 0
s.stars[j].D = 0.9 //rand.Float64()
continue
}
pX := int(math.Round(float64(bounds.Dx()) * (s.stars[j].PrevX + 0.5)))
pY := int(math.Round(float64(bounds.Dy()) * (s.stars[j].PrevY + 0.5)))
X := int(math.Round(float64(bounds.Dx()) * (s.stars[j].X + 0.5)))
Y := int(math.Round(float64(bounds.Dy()) * (s.stars[j].Y + 0.5)))
if pX != 0 && pY != 0 && (pX != X || pY != Y) {
dc.SetColor(color.RGBA{0x22, 0x22, 0x22, 0xff})
DrawLine(dc, pX, pY, X, Y)
}
dc.SetColor(color.RGBA{0xff, 0xff, 0xff, 0xff})
dc.SetPixel(X, Y)
}
dc.SetColor(color.RGBA{0xff, 0xff, 0xff, 0xff})
}
func (s *Starfield) FrameCount() int {
return 300
}