diff --git a/serwer.go b/serwer.go index f2a0cd6..bbb693b 100644 --- a/serwer.go +++ b/serwer.go @@ -1,11 +1,48 @@ +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() { -db := database{"buty": 50, "skarpety": 5} -log.Fatal(http.ListenAndServe("localhost:8000", db)) +http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { +lissajous(w) +}) +log.Fatal(http.ListenAndServe("localhost:8000", nil)) } -type dollars float32 -func (d dollars) String() string { return fmt.Sprintf("%.2f PLN", d) } -type database map[string]dollars -func (db database) ServeHTTP(w http.ResponseWriter, req *http.Request) { -for item, price := range db { -fmt.Fprintf(w, "%s: %s\n", item, price) +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 }