-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplot.go
67 lines (58 loc) · 1.5 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
package gplot
import (
"log"
"reflect"
"gonum.org/v1/gonum/floats"
)
const (
GopPackage = true
Gop_game = "Figure"
Gop_sprite = "Axis"
)
type Ploter interface {
initPlot()
finishPlot()
}
// Gopt_Figure_Main is required by Go+ compiler as the entry of a .plot project.
func Gopt_Figure_Main(plot Ploter) {
plot.initPlot()
defer plot.finishPlot()
plot.(interface{ MainEntry() }).MainEntry()
}
// Gopt_Figure_Run is required by Go+ compiler as the entry of a .plot project.
func Gopt_Figure_Run(plot Ploter, x, y int) {
v := reflect.ValueOf(plot).Elem()
t := reflect.TypeOf(plot).Elem()
p := instance(v)
pos := 0
for i, n := 0, v.NumField(); i < n; i++ {
typ := t.Field(i).Type
m, ok := reflect.PtrTo(typ).MethodByName("Main")
if ok {
parent, axis := instanceAxis(t.Field(i))
pos += 1
m.Func.Call([]reflect.Value{parent})
p.Subplot(x, y, pos)
p.Align[p.pos.row][p.pos.col] = axis
}
}
}
func Linspace(l, r float64, n int) Vector {
s := make([]float64, n)
dst := floats.Span(s, l, r)
return newVec(dst)
}
func instance(plotter reflect.Value) *Figure {
fld := plotter.FieldByName("Figure")
if !fld.IsValid() {
log.Panicf("type %v doesn't has field gplot.Figure", plotter.Type())
}
return fld.Addr().Interface().(*Figure)
}
func instanceAxis(field reflect.StructField) (reflect.Value, *Axis) {
typ := field.Type
parent := reflect.New(typ)
axis := parent.Elem().FieldByName("Axis")
axis.Set(reflect.ValueOf(NewAxis()).Elem())
return parent, axis.Addr().Interface().(*Axis)
}