func clear(s draw.Image) { b := s.Bounds() for i := b.Min.X; i < b.Max.X; i++ { for j := b.Min.Y; j < b.Max.Y; j++ { s.Set(i, j, black) } } }
// http://en.wikipedia.org/wiki/Midpoint_circle_algorithm func drawCircle(img draw.Image, cx int, cy int, r int, c image.RGBAColor, fc image.RGBAColor) { x := r y := 0 error := -r for x >= y { var t int t = cy + y if fc.A != uint8(0) { drawHorizontalLine(img, cx-x, cx+x, t, fc) } if c.A != uint8(0) { set(img, cx+x, t, c) set(img, cx-x, t, c) } t = cy - y if fc.A != uint8(0) { drawHorizontalLine(img, cx-x, cx+x, t, fc) } if c.A != uint8(0) { set(img, cx+x, t, c) set(img, cx-x, t, c) } t = cy + x if fc.A != uint8(0) { drawHorizontalLine(img, cx-y, cx+y, t, fc) } if c.A != uint8(0) { set(img, cx+y, t, c) set(img, cx-y, t, c) } t = cy - x if fc.A != uint8(0) { drawHorizontalLine(img, cx-y, cx+y, t, fc) } if c.A != uint8(0) { img.Set(cx+y, t, c) img.Set(cx-y, t, c) } error += y y += 1 error += y if error >= 0 { x -= 1 error -= x error -= x } } }
func plot(s draw.Image, v []Point) { clear(s) min, max := scale(v) b := s.Bounds() xscale := float64(b.Max.X-b.Min.X) / (max.x - min.x) * Shrink yscale := float64(b.Max.Y-b.Min.Y) / (max.y - min.y) * Shrink xoffset := -(min.x * xscale) + 4 yoffset := -(min.y * yscale) + 4 for _, p := range v { x := int(xscale*p.x + xoffset) y := int(yscale*p.y + yoffset) if x < 0 || y < 0 { fmt.Println(min, max) fmt.Println(x, y) fmt.Println(p.x, xscale, xoffset) fmt.Println(p.y, yscale, yoffset) } s.Set(x, y, Color) } }
func set(img draw.Image, x, y int, c image.RGBAColor) { if x >= 0 && y >= 0 && x < img.Width() && y < img.Height() { img.Set(x, y, c) } }