MCPcopy Index your code
hub / github.com/gonum/plot / ExampleField_gophers

Function ExampleField_gophers

plotter/field_example_test.go:120–175  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

118}
119
120func ExampleField_gophers() {
121 file, err := os.Open("testdata/gopher_running.png")
122 if err != nil {
123 log.Panic(err)
124 }
125 defer file.Close()
126 gopher, err := png.Decode(file)
127 if err != nil {
128 log.Panic(err)
129 }
130
131 f := plotter.NewField(field{
132 r: 5, c: 9,
133 fn: func(x, y float64) plotter.XY {
134 return plotter.XY{
135 X: -0.75*x + y,
136 Y: -0.75*y - x,
137 }
138 },
139 })
140
141 // Provide a DrawGlyph function to render a custom
142 // vector glyph instead of the default arrow.
143 f.DrawGlyph = func(c vg.Canvas, _ draw.LineStyle, v plotter.XY) {
144 // The canvas is unscaled if the vector has a zero
145 // magnitude, so return in that case.
146 if math.Hypot(v.X, v.Y) == 0 {
147 return
148 }
149 // Vector glyphs are scaled to half unit length by the
150 // plotter, so scale the gopher to twice unit size so
151 // it fits the cell, and center on the cell.
152 c.Translate(vg.Point{X: -1, Y: -1})
153 c.DrawImage(vg.Rectangle{Max: vg.Point{X: 2, Y: 2}}, gopher)
154 }
155
156 p := plot.New()
157 p.Title.Text = "Gopher vortex"
158 p.X.Tick.Marker = integerTicks{}
159 p.Y.Tick.Marker = integerTicks{}
160
161 p.Add(f)
162
163 img := vgimg.New(250, 175)
164 dc := draw.New(img)
165
166 p.Draw(dc)
167 w, err := os.Create("testdata/gopher_field.png")
168 if err != nil {
169 log.Panic(err)
170 }
171 png := vgimg.PngCanvas{Canvas: img}
172 if _, err = png.WriteTo(w); err != nil {
173 log.Panic(err)
174 }
175}

Callers

nothing calls this directly

Calls 10

WriteToMethod · 0.95
NewFieldFunction · 0.92
NewFunction · 0.92
NewFunction · 0.92
NewFunction · 0.92
CloseMethod · 0.80
TranslateMethod · 0.65
DrawImageMethod · 0.65
DrawMethod · 0.65
AddMethod · 0.45

Tested by

no test coverage detected