()
| 53 | } |
| 54 | |
| 55 | func ExampleField_colors() { |
| 56 | f := plotter.NewField(field{ |
| 57 | r: 5, c: 9, |
| 58 | fn: func(x, y float64) plotter.XY { |
| 59 | return plotter.XY{ |
| 60 | X: -0.75*x + y, |
| 61 | Y: -0.75*y - x, |
| 62 | } |
| 63 | }, |
| 64 | }) |
| 65 | |
| 66 | pal := moreland.ExtendedBlackBody() |
| 67 | pal.SetMin(0) |
| 68 | pal.SetMax(1.1) // Use 1.1 to make highest magnitude vectors visible on white. |
| 69 | |
| 70 | // Provide a DrawGlyph function to render a custom |
| 71 | // vector instead of the default monochrome arrow. |
| 72 | f.DrawGlyph = func(c vg.Canvas, sty draw.LineStyle, v plotter.XY) { |
| 73 | c.Push() |
| 74 | defer c.Pop() |
| 75 | mag := math.Hypot(v.X, v.Y) |
| 76 | var pa vg.Path |
| 77 | if mag == 0 { |
| 78 | // Draw a black dot for zero vectors. |
| 79 | c.SetColor(color.Black) |
| 80 | pa.Move(vg.Point{X: sty.Width}) |
| 81 | pa.Arc(vg.Point{}, sty.Width, 0, 2*math.Pi) |
| 82 | pa.Close() |
| 83 | c.Fill(pa) |
| 84 | return |
| 85 | } |
| 86 | // Choose a color from the palette for the magnitude. |
| 87 | col, err := pal.At(mag) |
| 88 | if err != nil { |
| 89 | panic(err) |
| 90 | } |
| 91 | c.SetColor(col) |
| 92 | pa.Move(vg.Point{}) |
| 93 | pa.Line(vg.Point{X: 1, Y: 0}) |
| 94 | pa.Close() |
| 95 | c.Stroke(pa) |
| 96 | } |
| 97 | |
| 98 | p := plot.New() |
| 99 | p.Title.Text = "Vortex" |
| 100 | p.X.Tick.Marker = integerTicks{} |
| 101 | p.Y.Tick.Marker = integerTicks{} |
| 102 | |
| 103 | p.Add(f) |
| 104 | |
| 105 | img := vgimg.New(250, 175) |
| 106 | dc := draw.New(img) |
| 107 | |
| 108 | p.Draw(dc) |
| 109 | w, err := os.Create("testdata/color_field.png") |
| 110 | if err != nil { |
| 111 | log.Panic(err) |
| 112 | } |
nothing calls this directly
no test coverage detected