/
glot14.go
75 lines (61 loc) · 1.57 KB
/
glot14.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
// Seriál "Programovací jazyk Go"
// https://www.root.cz/serialy/programovaci-jazyk-go/
//
// Padesátá část
// Tvorba grafů v jazyce Go
// https://www.root.cz/clanky/tvorba-grafu-v-jazyce-go/
//
// Repositář:
// https://github.com/tisnik/go-root/
//
// Seznam demonstračních příkladů z padesáté části:
// https://github.com/tisnik/go-root/blob/master/article_50/README.md
//
// Dokumentace ve stylu "literate programming":
// https://tisnik.github.io/go-root/article_50/glot14.html
package main
import (
"github.com/Arafatk/glot"
"math"
)
type Plot struct {
*glot.Plot
}
func (plot *Plot) Save(filename string) {
plot.Cmd("set terminal pngcairo")
plot.Cmd("set output '" + filename + "'")
plot.Cmd("replot")
}
func NewPlot(dimensions int) *Plot {
plot, err := glot.NewPlot(dimensions, false, false)
if err != nil {
panic(err)
}
return &Plot{plot}
}
const points = 50
func main() {
plot := NewPlot(2)
defer plot.Close()
var pointsX [points]float64
for i := 0; i < points; i++ {
pointsX[i] = float64(i) * 2.0 * math.Pi / points
}
function1 := func(t float64) float64 {
// limita
if t == 0.0 {
return 1.0
}
return math.Sin(t) / t
}
plot.AddFunc2d("sinc t", "boxes", pointsX[:], function1)
plot.CheckedCmd("set style fill solid")
plot.CheckedCmd("set boxwidth 0.5 relative")
plot.SetTitle("Plot #14")
plot.SetXLabel("t")
plot.SetYLabel("y")
plot.SetXrange(0, 1+int(math.Round(2.0*math.Pi)))
plot.SetYrange(-1, 1)
plot.CheckedCmd(`set xtics ('0' 0, '{/Symbol p}' pi, '2{/Symbol p}' 2*pi)`)
plot.Save("glot14.png")
}