-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
78 lines (58 loc) · 1.43 KB
/
main.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
package main
import (
"github.com/llgcode/draw2d/draw2dimg"
"github.com/llgcode/draw2d/draw2dkit"
"github.com/markfarnan/go-canvas/canvas"
"image/color"
"math/rand"
"time"
)
var boids []Boid
var done chan struct{}
var cvs *canvas.Canvas2d
var width float64
var height float64
var s = rand.NewSource(time.Now().UnixNano())
var r = rand.New(s)
func main() {
cvs, _ = canvas.NewCanvas2d(true)
height = float64(cvs.Height())
width = float64(cvs.Width())
for i := 0; i < 70; i++ {
boids = append(boids, NewBoid(float32(r.Intn(cvs.Width())), float32(r.Intn(cvs.Height())), &boids))
}
cvs.Start(60, Render)
<-done
}
func tick() {
for i, _ := range boids {
boids[i].Tick()
}
}
func drawBoid(gc *draw2dimg.GraphicContext, b Boid) {
gc.SetFillColor(b.col)
gc.SetStrokeColor(b.col)
gc.BeginPath()
draw2dkit.Circle(gc, float64(b.position.X()), float64(b.position.Y()), 5)
gc.FillStroke()
gc.Close()
x, y := float64(b.position[0]), float64(b.position[1])
sec := b.position.Sub(b.velocity.Normalize().Mul(10))
tX, tY := float64(sec[0]), float64(sec[1])
gc.BeginPath()
gc.MoveTo(x, y)
gc.LineTo(tX, tY)
gc.Stroke()
gc.Close()
}
func Render(gc *draw2dimg.GraphicContext) bool {
tick()
gc.SetFillColor(color.RGBA{0xff, 0xff, 0xff, 0xff})
gc.Clear()
gc.SetFillColor(color.RGBA{0xff, 0x00, 0x00, 0xff})
gc.SetStrokeColor(color.RGBA{0xff, 0x00, 0x00, 0xff})
for _, boid := range boids {
drawBoid(gc, boid)
}
return true
}