当前位置: 移动技术网 > IT编程>脚本编程>Go语言 > GO制作利萨如图形

GO制作利萨如图形

2020年05月08日  | 移动技术网IT编程  | 我要评论

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)
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网