From a613d1ba9f2897cf98de424d27d1a1355558b337 Mon Sep 17 00:00:00 2001 From: "Brandon C. Irizarry" Date: Thu, 5 Mar 2026 21:05:21 -0500 Subject: feat: create a gif --- main.go | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 45362f8..7e652eb 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,11 @@ package main import ( - "fmt" + "image" "image/color" + "image/gif" "io" + "math" "math/rand/v2" "os" ) @@ -13,6 +15,11 @@ var palette = []color.Color{ color.Black, } +const ( + whiteIndex = iota + blackIndex +) + func main() { lissajous(os.Stdout) } @@ -29,8 +36,23 @@ func lissajous(out io.Writer) { // Note that this generates the exact same random number per // run of the program. freq := rand.Float64() * 3.0 + anim := gif.GIF{LoopCount: nframes} + phase := 0.0 + + for range nframes { + rect := image.Rect(0, 0, 2*size+1, 2*size+1) + img := image.NewPaletted(rect, palette) - for range 10 { - fmt.Printf("random number: %.2f\n", freq) + 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) } -- cgit v1.2.3