Skip to content

Instantly share code, notes, and snippets.

@codeskyblue
Created September 20, 2013 10:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codeskyblue/6635625 to your computer and use it in GitHub Desktop.
Save codeskyblue/6635625 to your computer and use it in GitHub Desktop.
a gift to 15.Aug moon
// 8.15 project main.go
// generate a pic of moon
package main
import (
"fmt"
"image"
"image/color"
"image/draw"
"image/png"
"os"
)
func drawCircle(img draw.Image, x, y, radius int, fill color.Color) {
// Algorithm taken from
// http://en.wikipedia.org/wiki/Midpoint_circle_algorithm
x0, y0 := x, y
f := 1 - radius
ddF_x, ddF_y := 1, -2*radius
x, y = 0, radius
img.Set(x0, y0+radius, fill)
img.Set(x0, y0-radius, fill)
img.Set(x0+radius, y0, fill)
img.Set(x0-radius, y0, fill)
for x < y {
if f >= 0 {
y--
ddF_y += 2
f += ddF_y
}
x++
ddF_x += 2
f += ddF_x
img.Set(x0+x, y0+y, fill)
img.Set(x0-x, y0+y, fill)
img.Set(x0+x, y0-y, fill)
img.Set(x0-x, y0-y, fill)
img.Set(x0+y, y0+x, fill)
img.Set(x0-y, y0+x, fill)
img.Set(x0+y, y0-x, fill)
img.Set(x0-y, y0-x, fill)
}
}
func main() {
fd, err := os.Create("15.png")
if err != nil {
fmt.Println("create file failed")
return
}
defer fd.Close()
img := image.NewRGBA(image.Rect(0, 0, 400, 300))
draw.Draw(img, img.Bounds(), &image.Uniform{color.Black}, image.ZP, draw.Src)
drawCircle(img, 100, 100, 50, color.White)
png.Encode(fd, img)
fmt.Println("Hello World!")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment