main.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Lissajous generates GIF animations of random Lissajous figures.
  2. package main
  3. import (
  4. "image"
  5. "image/color"
  6. "image/gif"
  7. "io"
  8. "math"
  9. "math/rand"
  10. "os"
  11. "time"
  12. )
  13. var palette = []color.Color{color.White, color.Black}
  14. const (
  15. whiteIndex = 0 // first color in palette
  16. blackIndex = 1 // next color in palette
  17. )
  18. func main() {
  19. // The sequence of images is deterministic unless we seed
  20. // the pseudo-random number generator using the current time.
  21. // Thanks to Randall McPherson for pointing out the omission.
  22. rand.Seed(time.Now().UTC().UnixNano())
  23. lissajous(os.Stdout)
  24. }
  25. func lissajous(out io.Writer) {
  26. const (
  27. cycles = 5 // number of complete x oscillator revolutions
  28. res = 0.001 // angular resolution
  29. size = 100 // image canvas covers [-size..+size]
  30. nframes = 64 // number of animation frames
  31. delay = 8 // delay between frames in 10ms units
  32. )
  33. freq := rand.Float64() * 3.0 // relative frequency of y oscillator
  34. anim := gif.GIF{LoopCount: nframes}
  35. phase := 0.0 // phase difference
  36. for i := 0; i < nframes; i++ {
  37. rect := image.Rect(0, 0, 2*size+1, 2*size+1)
  38. img := image.NewPaletted(rect, palette)
  39. for t := 0.0; t < cycles*2*math.Pi; t += res {
  40. x := math.Sin(t)
  41. y := math.Sin(t*freq + phase)
  42. img.SetColorIndex(size+int(x*size+0.5), size+int(y*size+0.5),
  43. blackIndex)
  44. }
  45. phase += 0.1
  46. anim.Delay = append(anim.Delay, delay)
  47. anim.Image = append(anim.Image, img)
  48. }
  49. gif.EncodeAll(out, &anim) // NOTE: ignoring encoding errors
  50. }