-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlissajous.go
147 lines (125 loc) · 3.38 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// This siple program will generate gifs of Lissajous figures
// http://www.fotoacustica.fis.ufba.br/daniele/FIS3/roteiro%208%20oscilosc%C3%B3pio%20Digital%20FigurasLissajous.pdf
package main
import (
"fmt"
"image"
"image/color"
"image/gif"
"io"
"log"
"math"
"math/rand"
"net/http"
"net/url"
"os"
"strconv"
"time"
)
var palette = []color.Color{
color.Black,
color.White,
color.RGBA64{0xFFFF, 0x0000, 0x0000, 0xFF},
color.RGBA64{0x0000, 0xFFFF, 0x0000, 0xFF},
color.RGBA64{0x0000, 0x0000, 0xFFFF, 0xFF},
}
type LissajousConfig struct {
NFrames int
AngularResolution float64
ImageSize int
FrameTimeMs int
OscilatoroCycles float64
Frequency float64
}
func LissajousConfigCreate() *LissajousConfig {
config := &LissajousConfig{}
config.NFrames = 120
config.OscilatoroCycles = 10
config.FrameTimeMs = 10
config.AngularResolution = 0.001
config.ImageSize = 120
return config
}
func Lissajous(fouot io.Writer, config *LissajousConfig) {
nframes := config.NFrames
angular_resolution := config.AngularResolution
image_size := config.ImageSize
frame_time_ms := config.FrameTimeMs
oscilator_cycles := config.OscilatoroCycles
freq := config.Frequency
anim := gif.GIF{LoopCount: nframes}
phase_diff := 0.0
for i := 0; i < nframes; i++ {
rect := image.Rect(0, 0, 2*image_size+1, 2*image_size+1)
img := image.NewPaletted(rect, palette)
for t := 0.0; t < oscilator_cycles*2.0*math.Pi; t += angular_resolution {
x := math.Sin(t)
y := math.Sin(t*freq + phase_diff)
colorIndex := uint8(rand.Int()%len(palette)-1) + 1
x_ := image_size + int(x*(float64(image_size)+0.5))
y_ := image_size + int(y*(float64(image_size)+0.5))
img.SetColorIndex(x_, y_, colorIndex)
}
phase_diff += 0.1
anim.Delay = append(anim.Delay, frame_time_ms)
anim.Image = append(anim.Image, img)
}
gif.EncodeAll(fouot, &anim)
}
func getInt(key string, form url.Values) (int, error) {
if v, ok := form[key]; ok == true {
v, err := strconv.Atoi(v[0])
if err != nil {
log.Print(err)
}
return v, nil
}
return 0, fmt.Errorf("not found")
}
func getFloat(key string, form url.Values) (float64, error) {
if v, ok := form[key]; ok == true {
v, err := strconv.ParseFloat(v[0], 64)
if err != nil {
log.Print(err)
}
return v, nil
}
return 0.0, fmt.Errorf("not found")
}
func handler(w http.ResponseWriter, r *http.Request) {
config := LissajousConfigCreate()
if err := r.ParseForm(); err == nil {
form := r.Form
if v, err := getInt("NFrames", form); err == nil && v > 0 {
config.NFrames = v
}
if v, err := getFloat("Frequency", form); err == nil {
config.Frequency = v
}
if v, err := getInt("ImageSize", form); err == nil && v > 0 {
config.ImageSize = v
}
if v, err := getFloat("OscilatoroCycles", form); err == nil && v > 0 {
config.OscilatoroCycles = v
}
if v, err := getFloat("AngularResolution", form); err == nil && v > 0 {
config.AngularResolution = v
}
if v, err := getInt("FrameTimeMs", form); err == nil && v > 0 {
config.FrameTimeMs = v
}
}
fmt.Fprintf(os.Stdout, "Config: %v\n", config)
Lissajous(w, config)
}
func main() {
rand.Seed(time.Now().UTC().UnixNano())
// Lissajous(os.Stdout)
listen := "localhost:3000"
http.HandleFunc("/", handler)
fmt.Fprintf(os.Stdout, "Listening on '%s'\n", listen)
err := http.ListenAndServe(listen, nil)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
}
}