-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EuclideanDistance ManhattanDistance ChebyshevDistance MinkowskiDistance
- Loading branch information
1 parent
2672d04
commit fda0ca1
Showing
3 changed files
with
198 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
using System.Numerics; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace SMath.GeometryD2 | ||
{ | ||
/// <summary> | ||
/// Point in three dimensions. | ||
/// </summary> | ||
public static class Point3 | ||
{ | ||
/// <summary> | ||
/// Euclidean distance of the point and origin. | ||
/// </summary> | ||
/// <remarks> | ||
/// <a href="https://en.wikipedia.org/wiki/Euclidean_distance">Wikipedia</a> | ||
/// </remarks> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static N Distance<N>((N X, N Y, N Z) point) | ||
where N : IRootFunctions<N> | ||
=> PT.Hypotenuse(point); | ||
|
||
/// <summary> | ||
/// Euclidean distance of two points. | ||
/// </summary> | ||
/// <remarks> | ||
/// <a href="https://en.wikipedia.org/wiki/Euclidean_distance">Wikipedia</a> | ||
/// </remarks> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static N Distance<N>((N X, N Y, N Z) point1, (N X, N Y, N Z) point2) | ||
where N : IRootFunctions<N> | ||
=> PT.Hypotenuse(point2.X - point1.X, point2.Y - point1.Y, point2.Z - point1.Z); | ||
|
||
/// <summary> | ||
/// Manhattan or taxicab distance of point and origin. | ||
/// </summary> | ||
/// <remarks> | ||
/// <a href="https://en.wikipedia.org/wiki/Minkowski_distance">Wikipedia</a> | ||
/// </remarks> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static N ManhattanDistance<N>((N X, N Y, N Z) point) | ||
where N : INumberBase<N> | ||
=> N.Abs(point.X + point.Y + point.Z); | ||
|
||
/// <summary> | ||
/// Manhattan or taxicab distance of two points. | ||
/// </summary> | ||
/// <remarks> | ||
/// <a href="https://en.wikipedia.org/wiki/Minkowski_distance">Wikipedia</a> | ||
/// </remarks> | ||
public static N ManhattanDistance<N>((N X, N Y, N Z) point1, (N X, N Y, N Z) point2) | ||
where N : INumberBase<N> | ||
=> N.Abs(point1.X - point2.X) + N.Abs(point1.Y - point2.Y) + N.Abs(point1.Z - point2.Z); | ||
|
||
/// <summary> | ||
/// Chebyshev distance of point and origin | ||
/// </summary> | ||
/// <remarks> | ||
/// <a href="https://en.wikipedia.org/wiki/Chebyshev_distance">Wikipedia</a> | ||
/// </remarks> | ||
public static N ChebyshevDistance<N>((N X, N Y, N Z) point) | ||
where N : INumber<N> | ||
=> N.Max(N.Max(N.Abs(point.X), N.Abs(point.Y)), N.Abs(point.Z)); | ||
|
||
/// <summary> | ||
/// Chebyshev distance of two points. | ||
/// </summary> | ||
/// <remarks> | ||
/// <a href="https://en.wikipedia.org/wiki/Chebyshev_distance">Wikipedia</a> | ||
/// </remarks> | ||
public static N ChebyshevDistance<N>((N X, N Y, N Z) point1, (N X, N Y, N Z) point2) | ||
where N : INumber<N> | ||
=> N.Max(N.Max(N.Abs(point1.X - point2.X), N.Abs(point1.Y - point2.Y)), N.Abs(point1.Z - point2.Z)); | ||
|
||
/// <summary> | ||
/// Minkowski distance of point and origin. | ||
/// </summary> | ||
/// <remarks> | ||
/// <a href="https://en.wikipedia.org/wiki/Minkowski_distance">Wikipedia</a> | ||
/// </remarks> | ||
public static N MinkowskiDistance<N>((N X, N Y, N Z) point, N r) | ||
where N : IPowerFunctions<N> | ||
=> N.Pow(N.Pow(N.Abs(point.X), r) + N.Pow(N.Abs(point.Y), r) + N.Pow(N.Abs(point.Z), r), N.One / r); | ||
|
||
/// <summary> | ||
/// Minkowski distance of two points. | ||
/// </summary> | ||
/// <remarks> | ||
/// <a href="https://en.wikipedia.org/wiki/Minkowski_distance">Wikipedia</a> | ||
/// </remarks> | ||
public static N MinkowskiDistance<N>((N X, N Y, N Z) point1, (N X, N Y, N Z) point2, N r) | ||
where N : IPowerFunctions<N> | ||
=> N.Pow( | ||
N.Pow(N.Abs(point1.X - point2.X), r) | ||
+ N.Pow(N.Abs(point1.Y - point2.Y), r) | ||
+ N.Pow(N.Abs(point1.Z - point2.Z), r), | ||
N.One / r); | ||
|
||
/// <summary> | ||
/// Canberra distance of point and origin. | ||
/// </summary> | ||
/// <remarks> | ||
/// <a href="https://en.wikipedia.org/wiki/Canberra_distance">Wikipedia</a> | ||
/// </remarks> | ||
//public static N CanberraDistance<N>((N X, N Y, N Z) point) | ||
// where N : INumberBase<N> | ||
// => N.Abs(point.X) / N.Abs(point.X); //todo | ||
|
||
/// <summary> | ||
/// Canberra distance of two points. | ||
/// </summary> | ||
/// <remarks> | ||
/// <a href="https://en.wikipedia.org/wiki/Canberra_distance">Wikipedia</a> | ||
/// </remarks> | ||
public static N CanberraDistance<N>((N X, N Y, N Z) point1, (N X, N Y, N Z) point2) | ||
where N : INumberBase<N> | ||
=> N.Abs(point1.X - point2.X) / (N.Abs(point1.X) + N.Abs(point2.X)) | ||
+ N.Abs(point1.Y - point2.Y) / (N.Abs(point1.Y) + N.Abs(point2.Y)) | ||
+ N.Abs(point1.Z - point2.Z) / (N.Abs(point1.Z) + N.Abs(point2.Z)); | ||
|
||
/// <summary> | ||
/// Bray–Curtis dissimilarity or distance of point and origin. | ||
/// </summary> | ||
/// <remarks> | ||
/// <a href="https://en.wikipedia.org/wiki/Bray%E2%80%93Curtis_dissimilarity">Wikipedia</a> | ||
/// </remarks> | ||
//public static double BrayCurtisDissimilarity<N>((N X, N Y, N Z) point) | ||
// where N : INumber<N> | ||
// => default; | ||
|
||
/// <summary> | ||
/// Bray–Curtis dissimilarity or distance of two points. | ||
/// </summary> | ||
/// <remarks> | ||
/// <a href="https://en.wikipedia.org/wiki/Bray%E2%80%93Curtis_dissimilarity">Wikipedia</a> | ||
/// </remarks> | ||
//public static N BrayCurtisDissimilarity<N>((N X, N Y, N Z) point1, (N X, N Y, N Z) point2) | ||
// where N : INumberBase<N> | ||
// => default; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using SMath.GeometryD2; | ||
using Xunit; | ||
|
||
namespace SMath.Geometry2D | ||
{ | ||
public class Point3Tests | ||
{ | ||
[Theory] | ||
[InlineData(0, 0, 0, 1, 0, 0, 1)] | ||
[InlineData(0, 0, 0, 1, 1, 1, 3)] | ||
[InlineData(1, 1, 1, -1, -1, -1, 6)] | ||
public void ManhattanDistance(double x1, double y1, double z1, double x2, double y2, double z2, double distance) | ||
{ | ||
Assert.Equal(distance, Point3.ManhattanDistance((x1, y1, z1), (x2, y2, z2))); | ||
Assert.Equal(distance, Point3.ManhattanDistance((x2 - x1, y2 - y1, z2 - z1))); | ||
} | ||
|
||
[Theory] | ||
[InlineData(0, 0, 0, 1, 0, 0, 1)] | ||
[InlineData(0, 0, 0, 1, 1, 1, 1)] | ||
[InlineData(1, 1, 1, -1, -1, -1, 2)] | ||
public void ChebyshevDistance(double x1, double y1, double z1, double x2, double y2, double z2, double distance) | ||
{ | ||
Assert.Equal(distance, Point3.ChebyshevDistance((x1, y1, z1), (x2, y2, z2))); | ||
Assert.Equal(distance, Point3.ChebyshevDistance((x2 - x1, y2 - y1, z2 - z1))); | ||
} | ||
|
||
[Theory] | ||
[InlineData(0, 0, 0, 1, 0, 0, 1, 1)] | ||
[InlineData(0, 0, 0, 1, 1, 1, 1, 3)] | ||
[InlineData(1, 1, 1, -1, -1, -1, 1, 6)] | ||
public void MinkowskiDistance(double x1, double y1, double z1, double x2, double y2, double z2, double r, double distance) | ||
{ | ||
Assert.Equal(distance, Point3.MinkowskiDistance((x1, y1, z1), (x2, y2, z2), r)); | ||
Assert.Equal(distance, Point3.MinkowskiDistance((x2 - x1, y2 - y1, z2 - z1), r)); | ||
} | ||
|
||
//[Theory] | ||
//[InlineData(0, 0, 1, 0, 1)] //todo | ||
//public void CanberraDistance(double x1, double y1, double x2, double y2, double distance) | ||
//{ | ||
// Assert.Equal(distance, Point2.CanberraDistance((x1, y1), (x2, y2))); | ||
//} | ||
|
||
//[Theory] | ||
//[InlineData(0, 0, 1, 0, 1)] //todo | ||
//public void BrayCurtisDissimilarity(double x1, double y1, double x2, double y2, double distance) | ||
//{ | ||
// Assert.Equal(distance, Point2.BrayCurtisDissimilarity((x1, y1), (x2, y2))); | ||
//} | ||
} | ||
} |