// Requires "go mod init main" and "go get github.com/holizz/terrapin" after // You may need to install git first: https://git-scm.com/ package main import ( "fmt" "github.com/holizz/terrapin" "image" "image/png" "math" "net/http" ) func kochCurve(t *terrapin.Terrapin, lung float64, liv int) { if liv == 0 { t.Forward(lung) } else { kochCurve(t, lung / 3, liv - 1) t.Left(math.Pi / 3.0) kochCurve(t, lung / 3, liv - 1) t.Right(2.0 * math.Pi / 3.0) kochCurve(t, lung/ 3, liv - 1) t.Left(math.Pi / 3.0) kochCurve(t, lung / 3, liv - 1) } } func kochSnowflake(t *terrapin.Terrapin, lung float64, liv int) { kochCurve(t, lung, liv) t.Right(2.0 * math.Pi / 3.0) kochCurve(t, lung, liv) t.Right(2.0 * math.Pi / 3.0) kochCurve(t, lung, liv) t.Right(2.0 * math.Pi / 3.0) } func pageMain(w http.ResponseWriter, r *http.Request) { w.Write([]byte(`
`))
}
func pageImage(w http.ResponseWriter, r *http.Request) {
i := image.NewRGBA(image.Rect(0, 0, 1000, 1000))
t := terrapin.NewTerrapin(i, terrapin.Position{200.0, 800.0})
kochSnowflake(t, 500.0, 5)
png.Encode(w, i)
}
func main() {
http.HandleFunc("/", pageMain)
http.HandleFunc("/koch.png", pageImage)
fmt.Println("Listening on http://localhost:3000/")
http.ListenAndServe("localhost:3000", nil)
}