ex-rep-04/PinwheelTiling/PinwheelTilingPath.cs
2024-10-08 12:10:58 +02:00

109 lines
3.6 KiB
C#

using Avalonia.Controls.Selection;
using LeoTurtle;
namespace PinwheelTiling;
public static class PinwheelTilingPath
{
private const double Alpha = 26.57D;
private const double Beta = 90D - Alpha;
private const double Gamma = 90D;
private static Point _startPoint = new Point(0, 0.2);
private static Point _endPoint = new Point(0, 0);
/// <summary>
/// Executes the logic for adding turtle path steps.
/// Called automatically when application starts.
/// </summary>
/// <param name="turtle">The <see cref="SmartTurtle"/> instance to use</param>
public static void Walk(SmartTurtle turtle)
{
var initialTriangle = new Triangle(new Point(2.5D, 10.0D), new Point(82.5D, 50.0D), new Point(82.5D, 10.0D));
const double sideA = 40.0D;
const double sideB = 80.0D;
double sideC = CalculateHyptonuse(sideA, sideB);
MakeTriangle(turtle, sideA, sideB, sideC);
}
// TODO - implement your algorithm
private static void MakeTriangle(SmartTurtle turtle, double sideA, double sideB, double sideC)
{
double height = CalculateHeight(sideA, sideB, sideC);
double sideE = height / 2;
double sideF = CalculateHyptonuse(sideE, height);
_endPoint = new Point(_startPoint.X + sideB, _startPoint.Y + sideA);
//Draw it now
DrawTriangle(turtle, sideA, sideB, sideC, height, sideE, sideF);
double CalculateHeight(double a, double b, double c)
{
double s = (a + b + c) / 2;
return (2 / c) * Math.Sqrt(s * (s - a) * (s - b) * (s - c));
}
}
private static void DrawTriangle(SmartTurtle turtle, double a, double b, double c, double h, double e, double f)
{
//Draw base triangle
turtle.Teleport(_startPoint.X, _startPoint.Y);
turtle.Turn(90);
DrawLine(turtle, b, -Gamma);
DrawLine(turtle, a, -(Gamma + Alpha));
DrawLine(turtle, c);
//Draw the height
turtle.Teleport(_endPoint.X, _startPoint.Y);
turtle.Turn(-Alpha);
DrawLine(turtle, e, -90);
double[] halfHeightPosition = turtle.GetPosition();
DrawLine(turtle, h);
turtle.Teleport(halfHeightPosition[0], halfHeightPosition[1]);
turtle.Turn(-Alpha);
DrawLine(turtle, e, -(90 + Alpha));
DrawLine(turtle, f);
//reset angle
ResetAngle(turtle);
turtle.Turn(-Alpha);
DrawLine(turtle, e);
void DrawLine(SmartTurtle turtle, double distance, double angle = 0)
{
turtle.MoveForward(distance);
turtle.Turn(angle);
}
void ResetAngle(SmartTurtle turtle)
{
turtle.Teleport(turtle.GetPosition()[0], turtle.GetPosition()[1]);
}
}
private static double CalculateHyptonuse(double a, double b)
{
return Math.Sqrt(Math.Pow(a, 2) + Math.Pow(b, 2));
}
/// <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 / 180D;
}
/// <summary>
/// Calculates the distance between two points.
/// </summary>
/// <param name="a">First point</param>
/// <param name="b">Second point</param>
/// <returns>Distance between the two points</returns>
private static double DistanceBetweenPoints(Point a, Point b)
{
return Math.Sqrt(Math.Pow(b.X - a.X, 2D) + Math.Pow(b.Y - a.Y, 2D));
}
}