diff --git a/src/OpenTK/Math/Vector2.cs b/src/OpenTK/Math/Vector2.cs index d01f2952..a3d4ea44 100644 --- a/src/OpenTK/Math/Vector2.cs +++ b/src/OpenTK/Math/Vector2.cs @@ -514,6 +514,54 @@ namespace OpenTK result.Y = vec.Y < min.Y ? min.Y : vec.Y > max.Y ? max.Y : vec.Y; } + /// + /// Compute the euclidean distance between two vectors. + /// + /// The first vector + /// The second vector + /// The distance + public static float Distance(Vector2 vec1, Vector2 vec2) + { + float result; + Distance(ref vec1, ref vec2, out result); + return result; + } + + /// + /// Compute the euclidean distance between two vectors. + /// + /// The first vector + /// The second vector + /// The distance + public static void Distance(ref Vector2 vec1, ref Vector2 vec2, out float result) + { + result = (float)Math.Sqrt((vec2.X - vec1.X) * (vec2.X - vec1.X) + (vec2.Y - vec1.Y) * (vec2.Y - vec1.Y)); + } + + /// + /// Compute the squared euclidean distance between two vectors. + /// + /// The first vector + /// The second vector + /// The squared distance + public static float DistanceSquared(Vector2 vec1, Vector2 vec2) + { + float result; + DistanceSquared(ref vec1, ref vec2, out result); + return result; + } + + /// + /// Compute the squared euclidean distance between two vectors. + /// + /// The first vector + /// The second vector + /// The squared distance + public static void DistanceSquared(ref Vector2 vec1, ref Vector2 vec2, out float result) + { + result = (vec2.X - vec1.X) * (vec2.X - vec1.X) + (vec2.Y - vec1.Y) * (vec2.Y - vec1.Y); + } + /// /// Scale a vector to unit length /// diff --git a/src/OpenTK/Math/Vector2d.cs b/src/OpenTK/Math/Vector2d.cs index cd887ba7..1bf9b0d6 100644 --- a/src/OpenTK/Math/Vector2d.cs +++ b/src/OpenTK/Math/Vector2d.cs @@ -507,6 +507,54 @@ namespace OpenTK result.Y = vec.Y < min.Y ? min.Y : vec.Y > max.Y ? max.Y : vec.Y; } + /// + /// Compute the euclidean distance between two vectors. + /// + /// The first vector + /// The second vector + /// The distance + public static double Distance(Vector2d vec1, Vector2d vec2) + { + double result; + Distance(ref vec1, ref vec2, out result); + return result; + } + + /// + /// Compute the euclidean distance between two vectors. + /// + /// The first vector + /// The second vector + /// The distance + public static void Distance(ref Vector2d vec1, ref Vector2d vec2, out double result) + { + result = Math.Sqrt((vec2.X - vec1.X) * (vec2.X - vec1.X) + (vec2.Y - vec1.Y) * (vec2.Y - vec1.Y)); + } + + /// + /// Compute the squared euclidean distance between two vectors. + /// + /// The first vector + /// The second vector + /// The squared distance + public static double DistanceSquared(Vector2d vec1, Vector2d vec2) + { + double result; + DistanceSquared(ref vec1, ref vec2, out result); + return result; + } + + /// + /// Compute the squared euclidean distance between two vectors. + /// + /// The first vector + /// The second vector + /// The squared distance + public static void DistanceSquared(ref Vector2d vec1, ref Vector2d vec2, out double result) + { + result = (vec2.X - vec1.X) * (vec2.X - vec1.X) + (vec2.Y - vec1.Y) * (vec2.Y - vec1.Y); + } + /// /// Scale a vector to unit length /// diff --git a/tests/OpenTK.Tests/Assertions.fs b/tests/OpenTK.Tests/Assertions.fs index 0d0df594..b3d8fe3f 100644 --- a/tests/OpenTK.Tests/Assertions.fs +++ b/tests/OpenTK.Tests/Assertions.fs @@ -16,7 +16,7 @@ module private AssertHelpers = let approxEq a b = MathHelper.ApproximatelyEquivalent(a, b, EquivalenceTolerance) - let approxEqDelta a b = MathHelper.ApproximatelyEqual(a,b,BitAccuracy) + let approxEqDelta a b = MathHelper.ApproximatelyEqual(a, b, BitAccuracy) let approxEqSingleEpsilon a b = MathHelper.ApproximatelyEqualEpsilon(a, b, 0.00001f) let approxEqDoubleEpsilon a b = MathHelper.ApproximatelyEqualEpsilon(a, b, 0.00001) @@ -74,3 +74,6 @@ type internal Assert = if approxEqDoubleEpsilonWithError(a, b, c) then raise <| new Xunit.Sdk.EqualException(a,b) static member ThrowsIndexExn(f:unit -> unit) = Assert.Throws(f) |> ignore + + static member ApproximatelyEqual(a : float32, b : float32) = + if not <| approxEqDelta a b then raise <| new Xunit.Sdk.EqualException(a, b) diff --git a/tests/OpenTK.Tests/Vector2Tests.fs b/tests/OpenTK.Tests/Vector2Tests.fs index e44d8de6..6e352259 100644 --- a/tests/OpenTK.Tests/Vector2Tests.fs +++ b/tests/OpenTK.Tests/Vector2Tests.fs @@ -75,6 +75,16 @@ module Vector2 = Assert.Equal(lsq, v.LengthSquared) + [ |])>] + module Distance = + [] + let ``Distance(a, b) = (b - a).Length`` (a : Vector2, b : Vector2) = + Assert.ApproximatelyEqual(Vector2.Distance(a, b), (b - a).Length) + + [] + let ``DistanceSquared(a, b) = (b - a).LengthSquared`` (a : Vector2, b : Vector2) = + Assert.ApproximatelyEqual(Vector2.DistanceSquared(a, b), (b - a).LengthSquared) + [ |])>] module ``Unit vectors and perpendicularity`` = //