lissajous.go 923 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package main
  2. import (
  3. "image"
  4. "image/color"
  5. "image/gif"
  6. "io"
  7. "math"
  8. "math/rand"
  9. "os"
  10. "time"
  11. )
  12. var palette = []color.Color{color.White, color.Black}
  13. const (
  14. whiteIndex = 0
  15. blackIndex = 1
  16. )
  17. func main() {
  18. rand.Seed(time.Now().UTC().UnixNano())
  19. lissajous(os.Stdout)
  20. }
  21. func lissajous(out io.Writer) {
  22. const (
  23. cycles = 5
  24. res = 0.001
  25. size = 100
  26. nframes = 64
  27. delay = 8
  28. )
  29. freq := rand.Float64() * 3.0
  30. anim := gif.GIF{LoopCount: nframes}
  31. phase := 0.0
  32. for i := 0; i < nframes; i++ {
  33. rect := image.Rect(0, 0, 2*size+1, 2*size+1)
  34. img := image.NewPaletted(rect, palette)
  35. for t := 0.0; t < cycles*2*math.Pi; t += res {
  36. x := math.Sin(t)
  37. y := math.Sin(t*freq + phase)
  38. img.SetColorIndex(size+int(x*size+0.5),
  39. size+int(y*size+0.5),
  40. blackIndex)
  41. }
  42. phase += 0.1
  43. anim.Delay = append(anim.Delay, delay)
  44. anim.Image = append(anim.Image, img)
  45. }
  46. gif.EncodeAll(out, &anim)
  47. }