yarp
This commit is contained in:
@@ -1,41 +1,33 @@
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Shogi.Domain.YetToBeAssimilatedIntoDDD.Pathing
|
||||
namespace Shogi.Domain.YetToBeAssimilatedIntoDDD.Pathing;
|
||||
|
||||
[DebuggerDisplay("{Direction} - {Distance}")]
|
||||
public record Path(Vector2 Direction, Distance Distance = Distance.OneStep)
|
||||
{
|
||||
[DebuggerDisplay("{Direction} - {Distance}")]
|
||||
public class Path
|
||||
{
|
||||
public Vector2 Direction { get; }
|
||||
public Distance Distance { get; }
|
||||
public Path(Vector2 direction, Distance distance = Distance.OneStep)
|
||||
{
|
||||
Direction = direction;
|
||||
Distance = distance;
|
||||
}
|
||||
|
||||
public Path Invert() => new(Vector2.Negate(Direction), 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.Direction, end);
|
||||
var shortestDistance = Vector2.Distance(start + shortestPath.Direction, end);
|
||||
if (distance < shortestDistance)
|
||||
{
|
||||
shortestPath = path;
|
||||
}
|
||||
}
|
||||
return shortestPath;
|
||||
}
|
||||
}
|
||||
public Path Invert() => new(Vector2.Negate(Direction), 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.Direction, end);
|
||||
var shortestDistance = Vector2.Distance(start + shortestPath.Direction, end);
|
||||
if (distance < shortestDistance)
|
||||
{
|
||||
shortestPath = path;
|
||||
}
|
||||
}
|
||||
return shortestPath;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user