Skip to content

Instantly share code, notes, and snippets.

@osdrv
Created August 20, 2019 20:18
Show Gist options
  • Save osdrv/3cb4b06ef3d5b93447d579a41ac7b8ac to your computer and use it in GitHub Desktop.
Save osdrv/3cb4b06ef3d5b93447d579a41ac7b8ac to your computer and use it in GitHub Desktop.
Archimedean spiral in golang using web server and png renderer
package main
import (
"image"
"image/color"
"image/png"
"io"
"math"
"net/http"
)
var palette = []color.Color{color.White, color.Black}
const (
White = 0
Black = 1
)
func draw(out io.Writer) {
w, h := 640, 640
rect := image.Rect(0, 0, w, h)
img := image.NewPaletted(rect, palette)
a := 1.5
b := -2.4
for t := 0; t < 360*20; t++ {
trad := float64(t) * math.Pi / 180.0
x := (a + b*trad) * math.Cos(trad)
y := (a + b*trad) * math.Sin(trad)
img.SetColorIndex(w/2+int(x), h/2+int(y), Black)
}
png.Encode(out, img)
}
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
draw(w)
})
http.ListenAndServe(":8080", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment