Пример #1
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="turtle">Turtle instance</param>
        /// <param name="pen">Drawing pen (optional)</param>
        public State(Turtle turtle, Pen pen = null)
        {
            Turtle = turtle;

            if (pen != null)
                Pen = pen;
            else
                Pen = new Pen(Color.Black);
        }
Пример #2
0
        /// <summary>
        /// Generate L-System polylines
        /// </summary>
        /// <returns>List of generated polylines</returns>
        public List<IDrawable> Generate()
        {
            List<IDrawable> polylines = new List<IDrawable>();
            Turtle turtle = new Turtle(_startPoint, _startAngle, _step, _delta, _angleDelta, _stepDelta);
            turtle.Randomizer = _random;
            Polyline polyline = new Polyline();
            polyline.Pen = (Pen) _pen.Clone();
            polyline.Add(turtle.Position);

            for (int i = 0; i < _expandedGrammar.Length; i++)
            {
                switch (_expandedGrammar[i])
                {
                    case 'A':       // draw forward
                    case 'B':
                    case 'C':
                    case 'D':
                    case 'E':
                    case 'F':
                    case 'G':
                    case 'H':
                    case 'I':
                    case 'J':
                        turtle.Forward();
                        polyline.Add(turtle.Position);
                        break;
                    case 'K':       // draw backward
                    case 'L':
                    case 'M':
                    case 'N':
                        turtle.Backward();
                        polyline.Add(turtle.Position);
                        break;
                    case 'a':       // move forward without drawing
                    case 'b':
                    case 'c':
                    case 'd':
                    case 'e':
                    case 'f':
                    case 'g':
                    case 'h':
                    case 'i':
                    case 'j':
                        if(polyline.Points.Count > 1) // save polyline only if there is an actual line
                            polylines.Add(polyline);
                        polyline = new Polyline();  // start new polyline
                        polyline.Pen = (Pen) _pen.Clone();
                        turtle.Forward();
                        polyline.Add(turtle.Position);
                        break;
                    case '+':       // turn left
                        turtle.Left();
                        break;
                    case '-':       // turn right
                        turtle.Right();
                        break;
                    case '|':       // turn around
                        turtle.TurnAround();
                        break;
                    case '[':       // save state to stack
                        _stateStack.Push(new State((Turtle)turtle.Clone()));
                        break;
                    case ']':       // load state from stack
                        State state = _stateStack.Pop();
                        turtle = state.Turtle;
                        if (polyline.Points.Count > 1)
                            polylines.Add(polyline);
                        polyline = new Polyline();
                        polyline.Pen = (Pen) _pen.Clone();
                        polyline.Add(turtle.Position);
                        break;
                    default:
                        break;
                }
            }

            // add last polyline to polylines list (only when it's not empty)
            if(polyline.Points.Count > 1)
                polylines.Add(polyline);

            return polylines;
        }
Пример #3
0
 /// <summary>
 /// Clone turtle object
 /// </summary>
 /// <returns>Clone of turtle object</returns>
 public object Clone()
 {
     Turtle clone = new Turtle(_position, _angle, _step, _delta, _angleDelta, _stepDelta);
     clone.Randomizer = Randomizer;
     return clone;
 }
Пример #4
0
        /// <summary>
        /// Generate L-System polylines
        /// </summary>
        /// <returns>List of generated polylines</returns>
        public List <IDrawable> Generate()
        {
            List <IDrawable> polylines = new List <IDrawable>();
            Turtle           turtle    = new Turtle(_startPoint, _startAngle, _step, _delta, _angleDelta, _stepDelta);

            turtle.Randomizer = _random;
            Polyline polyline = new Polyline();

            polyline.Pen = (Pen)_pen.Clone();
            polyline.Add(turtle.Position);

            for (int i = 0; i < _expandedGrammar.Length; i++)
            {
                switch (_expandedGrammar[i])
                {
                case 'A':           // draw forward
                case 'B':
                case 'C':
                case 'D':
                case 'E':
                case 'F':
                case 'G':
                case 'H':
                case 'I':
                case 'J':
                    turtle.Forward();
                    polyline.Add(turtle.Position);
                    break;

                case 'K':           // draw backward
                case 'L':
                case 'M':
                case 'N':
                    turtle.Backward();
                    polyline.Add(turtle.Position);
                    break;

                case 'a':           // move forward without drawing
                case 'b':
                case 'c':
                case 'd':
                case 'e':
                case 'f':
                case 'g':
                case 'h':
                case 'i':
                case 'j':
                    if (polyline.Points.Count > 1)    // save polyline only if there is an actual line
                    {
                        polylines.Add(polyline);
                    }
                    polyline     = new Polyline();  // start new polyline
                    polyline.Pen = (Pen)_pen.Clone();
                    turtle.Forward();
                    polyline.Add(turtle.Position);
                    break;

                case '+':           // turn left
                    turtle.Left();
                    break;

                case '-':           // turn right
                    turtle.Right();
                    break;

                case '|':           // turn around
                    turtle.TurnAround();
                    break;

                case '[':           // save state to stack
                    _stateStack.Push(new State((Turtle)turtle.Clone()));
                    break;

                case ']':           // load state from stack
                    State state = _stateStack.Pop();
                    turtle = state.Turtle;
                    if (polyline.Points.Count > 1)
                    {
                        polylines.Add(polyline);
                    }
                    polyline     = new Polyline();
                    polyline.Pen = (Pen)_pen.Clone();
                    polyline.Add(turtle.Position);
                    break;

                default:
                    break;
                }
            }

            // add last polyline to polylines list (only when it's not empty)
            if (polyline.Points.Count > 1)
            {
                polylines.Add(polyline);
            }

            return(polylines);
        }