Files
Shogi/Shogi.Domain/YetToBeAssimilatedIntoDDD/Pathing/Path.cs
2024-10-11 11:10:38 -05:00

41 lines
1.1 KiB
C#

using System.Diagnostics;
namespace Shogi.Domain.YetToBeAssimilatedIntoDDD.Pathing;
[DebuggerDisplay("{Direction} - {Distance}")]
public record Path
{
public Vector2 NormalizedDirection { get; }
public Distance Distance { get; }
public Path(Vector2 direction, Distance distance = Distance.OneStep)
{
NormalizedDirection = Vector2.Normalize(direction);
this.Distance = distance;
}
public Path Invert() => new(Vector2.Negate(NormalizedDirection), Distance);
}
public static class PathExtensions
{
public static Path GetNearestPath(this IEnumerable<Path> paths, Vector2 start, Vector2 end)
{
if (!paths.DefaultIfEmpty().Any())
{
throw new ArgumentException("No paths to get nearest path from.");
}
var shortestPath = paths.First();
foreach (var path in paths.Skip(1))
{
var distance = Vector2.Distance(start + path.NormalizedDirection, end); //Normalizing the direction probably broke this.
var shortestDistance = Vector2.Distance(start + shortestPath.NormalizedDirection, end); // And this.
if (distance < shortestDistance)
{
shortestPath = path;
}
}
return shortestPath;
}
}