-
Notifications
You must be signed in to change notification settings - Fork 0
/
turtle.go
57 lines (42 loc) · 960 Bytes
/
turtle.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package lindenmayer
import "math"
type Turtle struct {
Pos position
Orientation float64
Pen bool
Dispatcher chan *Turtle
}
type position struct {
X, Y float64
}
func NewTurtle(x float64, y float64) *Turtle {
return &Turtle{position{x, y}, 0.0, false, make(chan *Turtle)}
}
func (t *Turtle) Forward(dist float64) {
t.Pos.X += dist * math.Sin(t.Orientation)
t.Pos.Y += dist * math.Cos(t.Orientation)
t.Dispatcher <- t
}
func (t *Turtle) Left(radians float64) {
t.Orientation += radians
}
func (t *Turtle) Right(radians float64) {
t.Left(-radians)
}
func (t *Turtle) PenUp() {
t.Pen = false
}
func (t *Turtle) PenDown() {
t.Pen = true
}
func (t *Turtle) Clone() *Turtle {
return &Turtle{position{t.Pos.X, t.Pos.Y}, t.Orientation, t.Pen, t.Dispatcher}
}
func (t *Turtle) Restore(from *Turtle) {
t.PenUp()
t.Pos.X = from.Pos.X
t.Pos.Y = from.Pos.Y
t.Orientation = from.Orientation
t.Dispatcher <- t
t.PenDown()
}