Add Manhattan distance methods and `CoordinateNd` class for n-dimensionality support

This commit is contained in:
Sebastian Lindemeier 2025-12-16 08:55:13 +01:00
parent d878ab186e
commit 554ab0557e
1 changed files with 47 additions and 1 deletions

View File

@ -2,6 +2,12 @@
public record Coordinate(long X, long Y)
{
public long GetManhattanDistance(Coordinate other) =>
GetManhattanDistance(this, other);
public static long GetManhattanDistance(Coordinate a, Coordinate b) =>
Math.Abs(a.X - b.X) + Math.Abs(a.Y - b.Y);
public double GetEuclidianDistance(Coordinate other) =>
GetEuclidianDistance(this, other);
@ -20,6 +26,12 @@ public record Coordinate(long X, long Y)
public record Coordinate3d(long X, long Y, long Z)
{
public long GetManhattanDistance(Coordinate3d other) =>
GetManhattanDistance(this, other);
public static long GetManhattanDistance(Coordinate3d a, Coordinate3d b) =>
Math.Abs(a.X - b.X) + Math.Abs(a.Y - b.Y) + Math.Abs(a.Z - b.Z);
public double GetEuclidianDistance(Coordinate3d other) =>
GetEuclidianDistance(this, other);
@ -34,4 +46,38 @@ public record Coordinate3d(long X, long Y, long Z)
public static Coordinate3d operator *(Coordinate3d coord, int amount) =>
new(coord.X * amount, coord.Y * amount, coord.Z * amount);
}
}
public record CoordinateNd(long[] Axes)
{
public long GetManhattanDistance(CoordinateNd other) =>
GetManhattanDistance(this, other);
public static long GetManhattanDistance(CoordinateNd a, CoordinateNd b) =>
a.Axes.Length == b.Axes.Length
? a.Axes.Zip(b.Axes, (ax, bx) => Math.Abs(ax - bx)).Sum()
: throw new DimensionMismatchException();
public double GetEuclidianDistance(CoordinateNd other) =>
GetEuclidianDistance(this, other);
public static double GetEuclidianDistance(CoordinateNd a, CoordinateNd b) =>
a.Axes.Length == b.Axes.Length
? Math.Sqrt(a.Axes.Zip(b.Axes, (ax, bx) => (ax - bx) * (ax - bx)).Sum())
: throw new DimensionMismatchException();
public static CoordinateNd operator +(CoordinateNd left, CoordinateNd right) =>
left.Axes.Length == right.Axes.Length
? new CoordinateNd([..left.Axes.Zip(right.Axes, (l, r) => l + r)])
: throw new DimensionMismatchException();
public static CoordinateNd operator -(CoordinateNd left, CoordinateNd right) =>
left.Axes.Length == right.Axes.Length
? new CoordinateNd([..left.Axes.Zip(right.Axes, (l, r) => l - r)])
: throw new DimensionMismatchException();
public static CoordinateNd operator *(CoordinateNd coord, int amount) =>
new CoordinateNd([..coord.Axes.Select(a => a * amount)]);
}
public class DimensionMismatchException : Exception {}