Created
May 13, 2021 13:36
-
-
Save pbzona/3d9a93fedeeed35c7a5b1a64a714e315 to your computer and use it in GitHub Desktop.
Weird wave band animation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"image" | |
"image/color" | |
"image/gif" | |
"io" | |
"math" | |
"math/rand" | |
"os" | |
) | |
var fgColor = color.RGBA{0x00, 0xff, 0x00, 0xff} | |
var bgColor = color.Black | |
var palette = []color.Color{bgColor, fgColor} | |
const ( | |
bgIndex = 0 // first color in palette | |
fgIndex = 1 // next color in palette | |
) | |
func main() { | |
lissajous(os.Stdout) | |
} | |
func lissajous(out io.Writer) { | |
const ( | |
cycles = 3 // number of complete x osc revolutions | |
res = 0.001 // angular resolution | |
size = 100 // image canvas covers [-size .. +size] | |
nframes = 64 // number of animation frames | |
delay = 8 // delay between frames in 10ms units | |
) | |
freq := rand.Float64() * 6.0 // relative frequency of y osc | |
anim := gif.GIF{LoopCount: nframes} | |
phase := 1.0 // phase difference | |
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.Tan(t) | |
y := math.Tan(t * freq + phase) | |
img.SetColorIndex(size + int(x*size+0.5), size + int(y*size+0.5), fgIndex) | |
} | |
phase += 0.1 | |
anim.Delay = append(anim.Delay, delay) | |
anim.Image = append(anim.Image, img) | |
} | |
gif.EncodeAll(out, &anim) // Ignoring encoding errors | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment