using System.Drawing; using LeoTurtle; namespace SimpleDrawings; public static class TurtlePath { /// /// Executes the logic for adding turtle path steps. /// Called automatically when the application starts. /// /// The instance to use public static void Walk(Turtle turtle) { EulerianPath(turtle); Shapes(turtle); } /// /// Draws a 'Haus des Nikolaus' as an Eulerian path /// /// The instance to use private static void EulerianPath(Turtle turtle) { const double Distance = 6.0D; const double ShortDistance = 4.24D; const double LongDistance = 8.49D; turtle.Teleport(5, 20); for (int i = 0; i < 4; i++) { DrawLine(turtle, Distance, 90); } turtle.Turn(45); DrawLine(turtle, LongDistance, -90); for (int i = 0; i < 2; i++) { DrawLine(turtle, ShortDistance, -90); } DrawLine(turtle, LongDistance, 0); } /// /// Draws several shapes. /// /// The instance to use private static void Shapes(Turtle turtle) { DrawOctagon(turtle, 5, 5); DrawRhombus(turtle, 20, 5); DrawTriangle(turtle, 40, 5); } /// /// Draws an octagon. /// /// The instance to use /// Initial turtle x position when starting to draw /// Initial turtle y position when starting to draw private static void DrawOctagon(Turtle turtle, int startX, int startY) { const double Distance = 4.0D; turtle.Teleport(startX, startY); for (int i = 0; i < 8; i++) { DrawLine(turtle, Distance, 45); } } /// /// Draws a rhombus. /// /// The instance to use /// Initial turtle x position when starting to draw /// Initial turtle y position when starting to draw private static void DrawRhombus(Turtle turtle, int startX, int startY) { const double Distance = 8.0D; const double Angle1 = 82; const double Angle2 = (360 - (Angle1 * 2)) / 2; turtle.Teleport(startX + Distance / 2, startY - Distance / 2); turtle.Turn(-Angle1 / 2); for (int i = 0; i < 4; i++) { DrawLine(turtle, Distance, i % 2 == 0 ? Angle1 : Angle2); } } /// /// Draws two triangles, one offset off the other. /// /// The instance to use /// Initial turtle x position when starting to draw /// Initial turtle y position when starting to draw private static void DrawTriangle(Turtle turtle, int startX, int startY) { const double Offset = 1.5D; const double ShortSide = 6.0D; const double LongSide = 8.0D; DrawTriangle(turtle, startX, startY, LongSide, ShortSide); DrawTriangle(turtle, startX + Offset, startY - Offset*2, LongSide + Offset*(LongSide/2), ShortSide + Offset*(ShortSide/2)); } /// /// Draws a single triangle at the given position and with the given size. /// Angels are fixed. /// /// The instance to use /// Initial turtle x position when starting to draw /// Initial turtle y position when starting to draw /// Length of the side c /// Length of sides a & b private static void DrawTriangle(Turtle turtle, double startX, double startY, double longSide, double shortSide) { turtle.Teleport(startX, startY); double angle = ConvertToDegrees(Math.Asin(shortSide / longSide)); // Corrected to arcsin DrawLine(turtle, longSide, 180 + angle); // Long side DrawLine(turtle, shortSide, 0); turtle.Teleport(startX, startY); turtle.Turn(-angle); DrawLine(turtle, shortSide, 0); } /// /// Converts an angel from degrees to radians. /// /// Degrees to convert /// Radians value private static double ConvertToRadians(double angleDegrees) { return angleDegrees * Math.PI / 180; } private static double ConvertToDegrees(double angleRadians) { return angleRadians / Math.PI * 180; } private static void DrawLine(Turtle turtle, double distance, double rotation) { turtle.MoveForward(distance); turtle.Turn(rotation); } }