Пример #1
0
func handleLSystemPng(w http.ResponseWriter, r *http.Request, rr render.Render) {
	err := r.ParseForm()
	if err != nil {
		turtleError(w)
		return
	}

	// Parse form into an LSystem
	sys := LSystem{}
	err = sys.ParseForm(r.Form)
	if err != nil {
		turtleError(w)
		return
	}

	// Execute lsystem

	size := 780

	i := image.NewRGBA(image.Rect(0, 0, size, size))
	t := terrapin.NewTerrapin(i, terrapin.Position{
		float64(size) / 2,
		float64(size) / 2,
	})

	sys.Execute(t)

	png.Encode(w, i)
}
Пример #2
0
func main() {
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte(`
			<!doctype html>
			<title>Terrapin example</title>
			<h1>Terrapin example</h1>

			<img src="/example1.png">
			<img src="/example2.png">
		`))
	})

	http.HandleFunc("/example1.png", func(w http.ResponseWriter, r *http.Request) {
		i := image.NewRGBA(image.Rect(0, 0, 300, 300))

		t := terrapin.NewTerrapin(i, terrapin.Position{150.0, 150.0})

		s := 20.0

		// Triangle
		t.Forward(s)
		t.Right(math.Pi * 1 / 2)
		t.Forward(s)
		t.Right(math.Pi * 3 / 4)
		t.Forward(math.Hypot(s, s))

		png.Encode(w, i)
	})

	http.HandleFunc("/example2.png", func(w http.ResponseWriter, r *http.Request) {
		i := image.NewRGBA(image.Rect(0, 0, 300, 300))

		t := terrapin.NewTerrapin(i, terrapin.Position{150.0, 150.0})

		// Polygons!
		poly(t, 20.0, 3)
		poly(t, 40.0, 5)
		poly(t, 60.0, 7)

		png.Encode(w, i)
	})

	fmt.Println("Listening on http://localhost:3000")
	log.Fatalln(http.ListenAndServe(":3000", nil))
}
Пример #3
0
func turtleError(w io.Writer) {
	size := 780
	x := 50.0

	i := image.NewRGBA(image.Rect(0, 0, size, size))
	t := terrapin.NewTerrapin(i, terrapin.Position{
		float64(size)/2 - x*5,
		float64(size)/2 - x*5,
	})

	for _, c := range "ERROR" {
		switch c {
		case 'E':
			t.Right(math.Pi / 2)
			t.Forward(x)
			t.Right(math.Pi)
			t.Forward(x)
			t.Left(math.Pi / 2)
			t.Forward(x)
			t.Left(math.Pi / 2)
			t.Forward(x)
			t.Right(math.Pi)
			t.Forward(x)
			t.Left(math.Pi / 2)
			t.Forward(x)
			t.Left(math.Pi / 2)
			t.Forward(x * 2)
			t.Left(math.Pi / 2)
		case 'R':
			t.Forward(x * 2)
			for i := 0; i < 3; i++ {
				t.Right(math.Pi / 2)
				t.Forward(x)
			}
			t.Left(math.Pi * 3 / 4)
			t.Forward(math.Hypot(x, x))
			t.Left(math.Pi / 4)
			t.Forward(x)
			t.Left(math.Pi / 2)
		case 'O':
			for i := 0; i < 2; i++ {
				t.Forward(x * 2)
				t.Right(math.Pi / 2)
				t.Forward(x)
				t.Right(math.Pi / 2)
			}
			t.Right(math.Pi / 2)
			t.Forward(x * 2)
			t.Left(math.Pi / 2)
		}
	}

	png.Encode(w, i)
}