ExamplePolygon_hexagons creates a heat map with hexagon shapes. The output of this example is at https://github.com/gonum/plot/blob/master/plotter/testdata/polygon_hexagons_golden.png.
()
| 82 | // The output of this example is at |
| 83 | // https://github.com/gonum/plot/blob/master/plotter/testdata/polygon_hexagons_golden.png. |
| 84 | func ExamplePolygon_hexagons() { |
| 85 | // hex returns a hexagon centered at (x,y) with radius r. |
| 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 // r is the hexagon radius. |
| 108 | // x0 and y0 are the beginning coordinates for the hexagon plot. |
| 109 | x0 = 0.0 |
| 110 | y0 = 0.0 |
| 111 | // nx and ny are the number of hexagons in the x and y directions. |
| 112 | nx = 5 |
| 113 | ny = 5 |
| 114 | ) |
| 115 | // dx and dy are the distance between hexgons. |
| 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 range nx { |
| 125 | y := ymin |
| 126 | for range ny { |
| 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 | } |
nothing calls this directly
no test coverage detected