GO制作利萨如图形
程序员文章站
2022-06-18 23:35:37
GO制作利萨如图形 ......
go制作利萨如图形
package main import ( "image" "image/color" "image/gif" "io" "log" "math" "math/rand" "net/http" "os" "time" ) var palette = []color.color{color.white, color.black} const ( whiteindex = 0 blackindex = 1 ) func main() { rand.seed(time.now().utc().unixnano()) if len(os.args) > 1 && os.args[1] == "web" { handler := func(w http.responsewriter, r *http.request) { lissajous(w) } http.handlefunc("/", handler) log.fatal(http.listenandserve("localhost:8000", nil)) return } lissajous(os.stdout) } func lissajous(out io.writer) { const ( cycles = 5 res = 0.001 size = 100 nframes = 64 delay = 8 ) freq := rand.float64() * 3.0 anim := gif.gif{loopcount: nframes} phase := 0.0 for i := 0; i < nframes; i++ { rect := image.rect(0, 0, 2*size+1, 2*size+1) img := image.newpaletted(rect, palette) for t := 0.0; t < cycles*2*math.pi; t += res { x := math.sin(t) y := math.sin(t*freq + phase) img.setcolorindex(size+int(x*size+0.5), size+int(y*size+0.5), blackindex) } phase += 0.1 anim.delay = append(anim.delay, delay) anim.image = append(anim.image, img) } gif.encodeall(out, &anim) }