naivePlot implements a naive rendering approach for contours. It is here as a debugging mode since it simply draws line segments generated by conrec without further computation.
(c draw.Canvas, plt *plot.Plot)
| 191 | // It is here as a debugging mode since it simply draws line segments |
| 192 | // generated by conrec without further computation. |
| 193 | func (h *Contour) naivePlot(c draw.Canvas, plt *plot.Plot) { |
| 194 | var pal []color.Color |
| 195 | if h.Palette != nil { |
| 196 | pal = h.Palette.Colors() |
| 197 | } |
| 198 | |
| 199 | trX, trY := plt.Transforms(&c) |
| 200 | |
| 201 | // Sort levels prior to palette scaling since we can't depend on |
| 202 | // sorting as a side effect from calling contourPaths. |
| 203 | sort.Float64s(h.Levels) |
| 204 | // ps is a palette scaling factor to scale the palette uniformly |
| 205 | // across the given levels. This enables a discordance between the |
| 206 | // number of colours and the number of levels. |
| 207 | ps := float64(len(pal)-1) / (h.Levels[len(h.Levels)-1] - h.Levels[0]) |
| 208 | if len(h.Levels) == 1 { |
| 209 | ps = 0 |
| 210 | } |
| 211 | |
| 212 | levelMap := make(map[float64]int) |
| 213 | for i, z := range h.Levels { |
| 214 | levelMap[z] = i |
| 215 | } |
| 216 | |
| 217 | // Draw each line segment as conrec generates it. |
| 218 | var pa vg.Path |
| 219 | conrec(h.GridXYZ, h.Levels, func(_, _ int, l line, z float64) { |
| 220 | if math.IsNaN(z) { |
| 221 | return |
| 222 | } |
| 223 | |
| 224 | pa = pa[:0] |
| 225 | |
| 226 | x1, y1 := trX(l.p1.X), trY(l.p1.Y) |
| 227 | x2, y2 := trX(l.p2.X), trY(l.p2.Y) |
| 228 | |
| 229 | pt1 := vg.Point{X: x1, Y: y1} |
| 230 | pt2 := vg.Point{X: x2, Y: y2} |
| 231 | if !c.Contains(pt1) || !c.Contains(pt2) { |
| 232 | return |
| 233 | } |
| 234 | |
| 235 | pa.Move(pt1) |
| 236 | pa.Line(pt2) |
| 237 | pa.Close() |
| 238 | |
| 239 | style := h.LineStyles[levelMap[z]%len(h.LineStyles)] |
| 240 | var col color.Color |
| 241 | switch { |
| 242 | case z < h.Min: |
| 243 | col = h.Underflow |
| 244 | case z > h.Max: |
| 245 | col = h.Overflow |
| 246 | case len(pal) == 0: |
| 247 | col = style.Color |
| 248 | default: |
| 249 | col = pal[int((z-h.Levels[0])*ps+0.5)] // Apply palette scaling. |
| 250 | } |