-
Notifications
You must be signed in to change notification settings - Fork 1
/
plot.go
98 lines (85 loc) · 1.82 KB
/
plot.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
// This file contains helper functions to perform web-based plotting
package main
import (
"image/color"
"go-hep.org/x/hep/hplot"
"go-hep.org/x/hep/hplot/vgshiny"
"golang.org/x/exp/shiny/screen"
"golang.org/x/mobile/event/key"
"golang.org/x/mobile/event/paint"
"gonum.org/v1/plot"
"gonum.org/v1/plot/plotter"
"gonum.org/v1/plot/vg"
"gonum.org/v1/plot/vg/draw"
)
type Points struct {
N int
X []float64
Y []float64
}
func NewPoints(g *Grid) *Points {
points := &Points{}
for i := range g.C {
for j := range g.C[i] {
c := &g.C[i][j]
if c.state == Alive {
points.N += 1
points.X = append(points.X, float64(c.j)) // column
points.Y = append(points.Y, float64(c.i)) // row
}
}
}
return points
}
func (p *Points) Len() int {
return p.N
}
func (p *Points) XY(i int) (x, y float64) {
return p.X[i], p.Y[i]
}
func setAxisStyle(a *plot.Axis) {
a.Min = -0.5
a.Max = float64(N) + 0.5
a.Tick.Marker = &hplot.FreqTicks{N: N + 2, Freq: 1}
a.Tick.Label.Font.Size = 0
a.Tick.Length = 0
}
func Plot(g *Grid) *hplot.Plot {
points := NewPoints(g)
sca, _ := plotter.NewScatter(points)
sca.GlyphStyle.Color = color.RGBA{255, 0, 0, 255}
sca.GlyphStyle.Radius = vg.Points(3)
sca.GlyphStyle.Shape = draw.BoxGlyph{}
p := hplot.New()
setAxisStyle(&p.X)
setAxisStyle(&p.Y)
p.Add(sca, plotter.NewGrid())
return p
}
func GridGraph(scr screen.Screen) {
c, err := vgshiny.New(scr, 700, 700)
if err != nil {
panic(err)
}
c.Run(func(e interface{}) bool {
switch e := e.(type) {
case key.Event:
switch e.Code {
case key.CodeQ:
if e.Direction == key.DirPress {
return false
}
case key.CodeSpacebar:
if e.Direction == key.DirPress {
p := Plot(grid)
p.Draw(draw.New(c))
c.Send(paint.Event{})
grid.Evolve()
}
}
case paint.Event:
c.Paint()
}
return true
})
}