54 lines
1.3 KiB
C#
54 lines
1.3 KiB
C#
using System.Diagnostics;
|
|
using static Shogi.Domain.YetToBeAssimilatedIntoDDD.Pathing.Path;
|
|
|
|
namespace Shogi.Domain.YetToBeAssimilatedIntoDDD.Pathing;
|
|
|
|
[DebuggerDisplay("{Step} - {Distance}")]
|
|
public record Path
|
|
{
|
|
public Vector2 Step { get; }
|
|
public Vector2 NormalizedStep => Vector2.Normalize(Step);
|
|
public Distance Distance { get; }
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="step">The smallest distance that can occur during a move.</param>
|
|
/// <param name="distance"></param>
|
|
public Path(Vector2 step, Distance distance = Distance.OneStep)
|
|
{
|
|
Step = step;
|
|
this.Distance = distance;
|
|
}
|
|
public Path Invert() => new(Vector2.Negate(Step), Distance);
|
|
|
|
//public enum PathingResult
|
|
//{
|
|
// Obstructed,
|
|
// CompletedWithoutObstruction
|
|
//}
|
|
}
|
|
|
|
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.Step, end);
|
|
var shortestDistance = Vector2.Distance(start + shortestPath.Step, end);
|
|
if (distance < shortestDistance)
|
|
{
|
|
shortestPath = path;
|
|
}
|
|
}
|
|
return shortestPath;
|
|
}
|
|
}
|