static void Koch(int X1, int Y1, int X2, int Y2, int X3, int Y3, int Step) { if (Step >= 0) { var x4 = (int)((X2 + 2 * X1) / 3); var y4 = (int)((Y2 + 2 * Y1) / 3); var x5 = (int)((2 * X2 + X1) / 3); var y5 = (int)((Y1 + 2 * Y2) / 3); var xs = (int)((X2 + X1) / 2); var ys = (int)((Y2 + Y1) / 2); var xn = (int)((4 * xs - X3) / 3); var yn = (int)((4 * ys - Y3) / 3); GraphicsWindow.DrawLine(x4, y4, xn, yn); GraphicsWindow.DrawLine(x5, y5, xn, yn); GraphicsWindow.DrawLine(x4, y4, x5, y5); Koch(x4, y4, xn, yn, x5, y5, Step - 1); Koch(xn, yn, x5, y5, x4, y4, Step - 1); Koch(X1, Y1, x4, y4, (int)((2 * X1 + X3) / 3), (int)((2 * Y1 + Y3) / 3) + 1, Step - 1); Koch(x5, y5, X2, Y2, (int)((2 * X2 + X3) / 3), (int)((2 * Y2 + Y3) / 3) + 1, Step - 1); } }
private static void GraphicsWindow_MouseDown() { int x1 = GraphicsWindow.Width / 2, y1 = 10; int x2 = 400, y2 = GraphicsWindow.Height - 350; int x3 = GraphicsWindow.Width - 400, y3 = GraphicsWindow.Height - 350; GraphicsWindow.PenWidth = 2; GraphicsWindow.DrawLine(x1, y1, x2, y2); GraphicsWindow.DrawLine(x2, y2, x3, y3); GraphicsWindow.DrawLine(x3, y3, x1, y1); Koch(x1, y1, x2, y2, x3, y3, brok); Koch(x2, y2, x3, y3, x1, y1, brok); Koch(x3, y3, x1, y1, x2, y2, brok); brok++; }
static void DrawSquare(int Deep, double alp, params int[] P) { for (int i = 0; i < 8; i += 2) { GraphicsWindow.DrawLine(P[i % 8], P[(i + 1) % 8], P[(i + 2) % 8], P[(i + 3) % 8]); } if (Deep > 0) { int x1 = (int)(P[0] * (1 - alp) + P[2] * alp); int x2 = (int)(P[2] * (1 - alp) + P[4] * alp); int x3 = (int)(P[4] * (1 - alp) + P[6] * alp); int x4 = (int)(P[6] * (1 - alp) + P[0] * alp); int y1 = (int)(P[1] * (1 - alp) + P[3] * alp); int y2 = (int)(P[3] * (1 - alp) + P[5] * alp); int y3 = (int)(P[5] * (1 - alp) + P[7] * alp); int y4 = (int)(P[7] * (1 - alp) + P[1] * alp); DrawSquare(Deep - 1, alp, x1, y1, x2, y2, x3, y3, x4, y4); } }
// Analyseert de lijst en voert bijbehorende commando's uit. private static void AnalyseAndPerformActions(List <string> commandList) { for (var i = 0; i < commandList.Count; i++) { switch (commandList[i]) { case "fd": case "forward": Turtle.Move(GetNextNumber(i, commandList)); UpdatePositionVariables(); i += lengthOfNumericalInput; break; case "bk": case "backward": Turtle.Move(-GetNextNumber(i, commandList)); UpdatePositionVariables(); i += lengthOfNumericalInput; break; case "rt": case "turn": Turtle.Turn(GetNextNumber(i, commandList)); variables["angle"] = Turtle.Angle; i += lengthOfNumericalInput; break; case "lt": Turtle.Turn(-GetNextNumber(i, commandList)); variables["angle"] = Turtle.Angle; i += lengthOfNumericalInput; break; case "wait": System.Threading.Thread.Sleep((int)System.Math.Round(GetNextNumber(i, commandList) * 1000 / 60)); i += lengthOfNumericalInput; break; case "speed": Turtle.Speed = GetNextNumber(i, commandList); variables["speed"] = Turtle.Speed; i += lengthOfNumericalInput; break; case "msg": case "message": case "print": if (i + 1 < commandList.Count) { TextWindow.WriteLine(RemoveBrackets(commandList[i + 1])); i++; } else { ThrowException(5); } break; case "line": SplitMultiNumberList(i, commandList, 4); GraphicsWindow.DrawLine(multiNumberList[0], multiNumberList[1], multiNumberList[2], multiNumberList[3]); i++; break; case "setpos": SplitMultiNumberList(i, commandList, 2); Turtle.X = multiNumberList[0]; Turtle.Y = multiNumberList[1]; UpdatePositionVariables(); i++; break; case "setpc": SplitMultiNumberList(i, commandList, fromPenColor: true); GraphicsWindow.PenColor = GraphicsWindow.GetColorFromRGB( multiNumberList[0], multiNumberList[1], multiNumberList[2]); i++; break; case "setbc": SplitMultiNumberList(i, commandList); GraphicsWindow.BackgroundColor = GraphicsWindow.GetColorFromRGB( multiNumberList[0], multiNumberList[1], multiNumberList[2]); i++; break; case "hide": case "hideturtle": case "ht": Turtle.Hide(); turtleIsHidden = true; break; case "show": case "showturtle": case "st": if (turtleIsHidden) { Turtle.Show(); } turtleIsHidden = false; break; case "pu": case "penup": Turtle.PenUp(); break; case "pd": case "pendown": Turtle.PenDown(); break; case "file": if (i + 1 < commandList.Count) { DrawFromFile(RemoveBrackets(commandList[i + 1])); i++; } else { ThrowException(5); } break; case "setorientation": Turtle.Angle = GetNextNumber(i, commandList); variables["angle"] = Turtle.Angle; i += lengthOfNumericalInput; break; case "setpx": case "setpixel": SplitMultiNumberList(i, commandList, 2); GraphicsWindow.SetPixel(multiNumberList[0], multiNumberList[1], GraphicsWindow.PenColor); i++; break; case "repeat": i += RepeatCommands(i, commandList); break; case "for": ForLoop(i, commandList); i += 2; break; case "while": WhileLoop(i, commandList); i += 2; break; case "circle": int circleRadius = (int)GetNextNumber(i, commandList); GraphicsWindow.DrawEllipse(Turtle.X - circleRadius, Turtle.Y - circleRadius, circleRadius * 2, circleRadius * 2); i += lengthOfNumericalInput; break; case "cs": ResetTurtle(); break; case "ct": TextWindow.Clear(); break; case "help": PrintHelp(); break; case "if": i += CompareInputs(i, commandList); break; case "make": AddVariable(i, commandList); i += lengthOfNumericalInput + 1; break; case "to": i += ToCommandBase(i, commandList) + 2; break; default: if (toShortcuts.ContainsKey(commandList[i])) { AnalyseAndPerformActions(toShortcuts[commandList[i]]); } else if (commandList[i].StartsWith(":")) { TextWindow.WriteLine(GetNextNumber(i, commandList, 0)); i += lengthOfNumericalInput - 1; } else { ThrowException(0, commandList[i]); } break; } } }