1: public class Turtle
2: {
3: // Collection of geometries to draw the figure
4: private GeometryGroup Geometries { get; set; }
5: // current angle of the turtle
6: public double Angle { get; private set; }
7: // current position of the turtle
8: public Point Position { get; private set; }
9: // position of the pen
10: public bool IsPenDown { get; private set; }
11:
12: /// <summary>
13: /// Inizializza una nuova istanza della classe <see cref="Turtle"/>.
14: /// </summary>
15: public Turtle(Path path)
16: {
17: this.Geometries = new GeometryGroup();
18: path.Data = this.Geometries;
19: this.Angle = 0.0;
20: this.Position = new Point(0, 0);
21: this.IsPenDown = true;
22: }
23:
24: // lift up the pen
25: public void PenUp()
26: {
27: this.IsPenDown = false;
28: }
29:
30: // lower the pen
31: public void PenDown()
32: {
33: this.IsPenDown = true;
34: }
35:
36: // move forward
37: public void Forward(double size)
38: {
39: this.Move(size);
40: }
41:
42: // move backward
43: public void Backward(double size)
44: {
45: this.Move(-size);
46: }
47:
48: // rotate to the left
49: public void Left(double angle)
50: {
51: this.Angle += ToRadians(angle);
52: }
53:
54: // rotate to the right
55: public void Right(double angle)
56: {
57: this.Angle -= ToRadians(angle);
58: }
59:
60: // move the pen of the specified size
61: private void Move(double size)
62: {
63: Point past = this.Position;
64:
65: this.Position =
66: new Point(
67: past.X + Math.Sin(this.Angle) * size,
68: past.Y + Math.Cos(this.Angle) * size);
69:
70: if (IsPenDown)
71: {
72: this.Geometries.Children.Add(
73: new LineGeometry
74: {
75: StartPoint = past,
76: EndPoint = this.Position,
77: });
78: }
79: }
80:
81: // convert degrees to radians
82: public double ToRadians(double angle)
83: {
84: return angle * Math.PI / 180.0;
85: }
86: }