Pracowania_programowania/serwer.go

49 lines
1.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
"log"
"net/http"
"image"
"image/color"
"image/gif"
"io"
"math"
"math/rand"
)
var palette = []color.Color{color.White, color.Black}
const (
whiteIndex = 0 // pierwszy kolor w zmiennej palette
blackIndex = 1 // następny kolor w zmiennej palette
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
lissajous(w)
})
log.Fatal(http.ListenAndServe("localhost:8000", nil))
}
func lissajous(out io.Writer) {
const (
cycles = 5 // liczba pełnych obiegów oscylatora x
res = 0.001 // rozdzielczość kątowa
size = 100 // rozmiar płótna obrazu [size..+size]
nframes = 64 // liczba klatek animacji
delay = 8 // opóźnienie między klatkami w jednostkach 10 ms
)
freq := rand.Float64() * 3.0 // częstotliwość względna oscylatora y
anim := gif.GIF{LoopCount: nframes}
phase := 0.0 // przesunięcie fazowe
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.Sin(t)
y := math.Sin(t*freq + phase)
img.SetColorIndex(size+int(x*size+0.5), size+int(y*size+0.5),
blackIndex)
}
phase += 0.1
anim.Delay = append(anim.Delay, delay)
anim.Image = append(anim.Image, img)
}
gif.EncodeAll(out, &anim) //ignorowanie błędów kodowania
}