1
2
3
4
5 package plotter_test
6
7 import (
8 "image/color"
9 "log"
10 "math"
11
12 "gonum.org/v1/plot"
13 "gonum.org/v1/plot/palette/moreland"
14 "gonum.org/v1/plot/plotter"
15 "gonum.org/v1/plot/vg"
16 )
17
18
19
20
21
22
23
24
25 func ExamplePolygon_holes() {
26
27 outer1 := plotter.XYs{{X: 0, Y: 0}, {X: 4, Y: 0}, {X: 4, Y: 4}, {X: 0, Y: 4}}
28
29
30
31 inner1 := plotter.XYs{{X: 0.5, Y: 0.5}, {X: 1.5, Y: 0.5}, {X: 1.5, Y: 1.5}, {X: 0.5, Y: 1.5}}
32
33
34
35 inner2 := plotter.XYs{{X: 3.5, Y: 2.5}, {X: 2.5, Y: 2.5}, {X: 2.5, Y: 3.5}, {X: 3.5, Y: 3.5}}
36
37 poly, err := plotter.NewPolygon(outer1, inner1, inner2)
38 if err != nil {
39 log.Panic(err)
40 }
41 poly.Color = color.NRGBA{B: 255, A: 255}
42
43 p := plot.New()
44 p.Title.Text = "Polygon with holes"
45 p.X.Label.Text = "X"
46 p.Y.Label.Text = "Y"
47 p.Add(poly)
48 p.Legend.Add("key", poly)
49 p.Legend.TextStyle.Font.Size = vg.Points(8)
50 p.Legend.TextStyle.Color = color.White
51 p.Legend.ThumbnailWidth = vg.Points(10)
52
53
54
55
56
57
58 err = p.Save(100, 100, "testdata/polygon_holes.png")
59 if err != nil {
60 log.Panic(err)
61 }
62
63
64
65
66
67 err = p.Save(100, 100, "testdata/polygon_holes.svg")
68 if err != nil {
69 log.Panic(err)
70 }
71 err = p.Save(100, 100, "testdata/polygon_holes.pdf")
72 if err != nil {
73 log.Panic(err)
74 }
75 err = p.Save(100, 100, "testdata/polygon_holes.eps")
76 if err != nil {
77 log.Panic(err)
78 }
79 }
80
81
82
83
84 func ExamplePolygon_hexagons() {
85
86 hex := func(x, y, r float64) plotter.XYs {
87 g := make(plotter.XYs, 6)
88 for i := range g {
89 g[i].X = x + r*math.Cos(math.Pi/3*float64(i))
90 g[i].Y = y + r*math.Sin(math.Pi/3*float64(i))
91 }
92 return g
93 }
94
95 var err error
96 p := plot.New()
97 p.Title.Text = "Hexagons"
98 p.X.Label.Text = "X"
99 p.Y.Label.Text = "Y"
100
101 colorMap := moreland.SmoothBlueRed()
102 colorMap.SetMax(2)
103 colorMap.SetMin(-2)
104 colorMap.SetConvergePoint(0)
105
106 const (
107 r = math.Pi / 4
108
109 x0 = 0.0
110 y0 = 0.0
111
112 nx = 5
113 ny = 5
114 )
115
116 dx := 3 * r
117 dy := r * math.Sqrt(3)
118
119 xstart := []float64{x0, x0 - 1.5*r}
120 ystart := []float64{y0, y0 - r}
121 for i, xmin := range xstart {
122 ymin := ystart[i]
123 x := xmin
124 for ix := 0; ix < nx; ix++ {
125 y := ymin
126 for iy := 0; iy < ny; iy++ {
127 var poly *plotter.Polygon
128 poly, err = plotter.NewPolygon(hex(x, y, r))
129 if err != nil {
130 log.Panic(err)
131 }
132 poly.Color, err = colorMap.At(math.Sin(x) + math.Sin(y))
133 if err != nil {
134 log.Panic(err)
135 }
136 poly.LineStyle.Width = 0
137 p.Add(poly)
138 y += dy
139 }
140 x += dx
141 }
142 }
143 if err = p.Save(100, 100, "testdata/polygon_hexagons.png"); err != nil {
144 log.Panic(err)
145 }
146 }
147
View as plain text