Beispiel #1
0
func (sys *LSystem) Execute(t *terrapin.Terrapin) {
	// Rewrite

	state := sys.StartState

	for i := 0; i < sys.Iterations; i++ {
		for from, to := range sys.Rules {
			state = strings.Replace(state, from, strings.ToLower(to), -1)
		}

		state = strings.ToUpper(state)
	}

	// Run turtle

	for _, a := range state {
		def := sys.Definitions[string(a)]

		value := float64(def.Value)
		valueRad := value * (math.Pi / 180)

		switch def.Function {
		case "fwd":
			t.Forward(value)
		case "left":
			t.Left(valueRad)
		case "right":
			t.Right(valueRad)
		}
	}
}
Beispiel #2
0
func poly(t *terrapin.Terrapin, size float64, sides int) {
	totalInterior := math.Pi * float64(sides-2)
	interior := totalInterior / float64(sides)
	exterior := math.Pi - interior

	// Get into position
	t.PenUp()
	t.Forward(size / 2)
	t.Right(math.Pi/2 + exterior/2)
	t.PenDown()

	// Draw it
	for i := 0; i < sides; i++ {
		t.Forward(size)
		t.Right(exterior)
	}

	// Get back to where we started
	t.PenUp()
	t.Left(math.Pi/2 + exterior/2)
	t.Backward(size / 2)
	t.PenDown()
}