//Event for resetting the program. private void ResetButton_Click(object sender, EventArgs e) { LSystemString = axiom; LSystemDrawingCanvas.ResetTransform(); LSystemDrawingCanvas.TranslateTransform(200, 400); LSystemDrawingCanvas.Clear(LSystemPictureBox.BackColor); LSystemPictureBox.Refresh(); InstructionsTextbox.Text = ""; length = 75.0f; }
//The turtle which executes the instructions encoded in the LSystemString created by GenerateLSystem. private void Turtle(string LSystemString) { //Stack which holds all the saved Information. List <GraphicsState> transformationStateStack = new List <GraphicsState>(); //Loop for checking the whole string, letter by letter and execute the instructions associated with it. for (int i = 0; i < LSystemString.Length; i++) { string CurrentSymbol = LSystemString.Substring(i, 1); if (CurrentSymbol == "F") { LSystemDrawingCanvas.DrawLine(WhitePen, 0, 0, 0, -length); LSystemDrawingCanvas.TranslateTransform(0, -length); } else if (CurrentSymbol == "+") { LSystemDrawingCanvas.RotateTransform(30); } else if (CurrentSymbol == "-") { LSystemDrawingCanvas.RotateTransform(-30); } else if (CurrentSymbol == "[") { Push(LSystemDrawingCanvas.Save(), transformationStateStack); } else if (CurrentSymbol == "]") { LSystemDrawingCanvas.Restore(Pop(transformationStateStack)); } } //After all instructions have been executed refresh the PictureBox to start the paint event. LSystemPictureBox.Refresh(); }