-
Notifications
You must be signed in to change notification settings - Fork 15
/
lissajous.go
70 lines (63 loc) · 1.74 KB
/
lissajous.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package lissajous
import (
"fmt"
"image"
"image/color"
"image/gif"
"io"
"math"
"math/rand"
"os"
)
var pallette = []color.Color{
color.RGBA{0x00, 0x00, 0x00, 0xFF},
color.RGBA{0xFF, 0xFF, 0xFF, 0xFF},
color.RGBA{0xFF, 0x00, 0x00, 0xFF},
color.RGBA{0x00, 0xFF, 0x00, 0xFF},
color.RGBA{0x00, 0x00, 0xFF, 0xFF},
color.RGBA{0xFF, 0xFF, 0x00, 0xFF},
color.RGBA{0x00, 0xFF, 0xFF, 0xFF},
color.RGBA{0xFF, 0x00, 0xFF, 0xFF},
}
// pickColorIndex takes a value between 0 and 1 and returns
// an index into the provided color pallette.
func pickColorIndex(v float64, p []color.Color) uint8 {
switch {
case v <= 0:
return 0
case v >= 1:
return uint8(len(p) - 1)
default:
return uint8(v*float64(len(p)-2) + 1)
}
}
// Lissajous generates a funky pattern and writes it as an animated GIF
// to the provided writer.
func Lissajous(out io.Writer, cycles, freq float64) {
const (
res = 0.001 // angular resolution
size = 200 // image canvas covers [-size..+size]
nframes = 64 // number of animation frames
delay = 8 // delay between frames in 10ms units
)
freq = rand.Float64() * freq
anim := gif.GIF{LoopCount: nframes}
phase := 0.0 // phase difference
for i := 0; i < nframes; i++ {
rect := image.Rect(0, 0, 2*size+1, 2*size+1)
img := image.NewPaletted(rect, pallette)
for t := 0.0; t < cycles*2.0*math.Pi; t += res {
x := math.Sin(t)
y := math.Sin(t*freq + phase)
idx := pickColorIndex(float64(size+x*size)/float64(2*size), pallette)
img.SetColorIndex(size+int(x*size+0.5), size+int(y*size+0.5), idx)
}
phase += 0.1
anim.Delay = append(anim.Delay, delay)
anim.Image = append(anim.Image, img)
}
err := gif.EncodeAll(out, &anim)
if err != nil {
fmt.Fprintln(os.Stderr, "unable to encode gif:", err)
}
}