149 lines
5.1 KiB
C#
149 lines
5.1 KiB
C#
using System.Drawing;
|
|
using LeoTurtle;
|
|
|
|
namespace SimpleDrawings;
|
|
|
|
public static class TurtlePath
|
|
{
|
|
/// <summary>
|
|
/// Executes the logic for adding turtle path steps.
|
|
/// Called automatically when the application starts.
|
|
/// </summary>
|
|
/// <param name="turtle">The <see cref="Turtle"/> instance to use</param>
|
|
public static void Walk(Turtle turtle)
|
|
{
|
|
EulerianPath(turtle);
|
|
Shapes(turtle);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Draws a 'Haus des Nikolaus' as an Eulerian path
|
|
/// </summary>
|
|
/// <param name="turtle">The <see cref="Turtle"/> instance to use</param>
|
|
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);
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// Draws several shapes.
|
|
/// </summary>
|
|
/// <param name="turtle">The <see cref="Turtle"/> instance to use</param>
|
|
private static void Shapes(Turtle turtle)
|
|
{
|
|
DrawOctagon(turtle, 5, 5);
|
|
DrawRhombus(turtle, 20, 5);
|
|
DrawTriangle(turtle, 40, 5);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Draws an octagon.
|
|
/// </summary>
|
|
/// <param name="turtle">The <see cref="Turtle"/> instance to use</param>
|
|
/// <param name="startX">Initial turtle x position when starting to draw</param>
|
|
/// <param name="startY">Initial turtle y position when starting to draw</param>
|
|
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);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Draws a rhombus.
|
|
/// </summary>
|
|
/// <param name="turtle">The <see cref="Turtle"/> instance to use</param>
|
|
/// <param name="startX">Initial turtle x position when starting to draw</param>
|
|
/// <param name="startY">Initial turtle y position when starting to draw</param>
|
|
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);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Draws two triangles, one offset off the other.
|
|
/// </summary>
|
|
/// <param name="turtle">The <see cref="Turtle"/> instance to use</param>
|
|
/// <param name="startX">Initial turtle x position when starting to draw</param>
|
|
/// <param name="startY">Initial turtle y position when starting to draw</param>
|
|
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));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Draws a single triangle at the given position and with the given size.
|
|
/// Angels are fixed.
|
|
/// </summary>
|
|
/// <param name="turtle">The <see cref="Turtle"/> instance to use</param>
|
|
/// <param name="startX">Initial turtle x position when starting to draw</param>
|
|
/// <param name="startY">Initial turtle y position when starting to draw</param>
|
|
/// <param name="longSide">Length of the side c</param>
|
|
/// <param name="shortSide">Length of sides a & b</param>
|
|
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);
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts an angel from degrees to radians.
|
|
/// </summary>
|
|
/// <param name="angleDegrees">Degrees to convert</param>
|
|
/// <returns>Radians value</returns>
|
|
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);
|
|
}
|
|
}
|