From 211fceddf7d4acfd0e49e0513a291e4426ad5e3a Mon Sep 17 00:00:00 2001 From: Fraser Waters Date: Sat, 26 Aug 2017 13:09:38 +0100 Subject: [PATCH 1/3] Remove erroneous comment slashes --- src/OpenTK/Math/Vector3.cs | 2 +- src/OpenTK/Math/Vector3d.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/OpenTK/Math/Vector3.cs b/src/OpenTK/Math/Vector3.cs index b37a7184..6e2d1c11 100644 --- a/src/OpenTK/Math/Vector3.cs +++ b/src/OpenTK/Math/Vector3.cs @@ -237,7 +237,7 @@ namespace OpenTK public static readonly Vector3 UnitY = new Vector3(0, 1, 0); /// - /// /// Defines a unit-length Vector3 that points towards the Z-axis. + /// Defines a unit-length Vector3 that points towards the Z-axis. /// public static readonly Vector3 UnitZ = new Vector3(0, 0, 1); diff --git a/src/OpenTK/Math/Vector3d.cs b/src/OpenTK/Math/Vector3d.cs index a2d4109e..9100f9cd 100644 --- a/src/OpenTK/Math/Vector3d.cs +++ b/src/OpenTK/Math/Vector3d.cs @@ -235,7 +235,7 @@ namespace OpenTK public static readonly Vector3d UnitY = new Vector3d(0, 1, 0); /// - /// /// Defines a unit-length Vector3d that points towards the Z-axis. + /// Defines a unit-length Vector3d that points towards the Z-axis. /// public static readonly Vector3d UnitZ = new Vector3d(0, 0, 1); From 9d596f674b55b6821ec335e769cda2dae02700cb Mon Sep 17 00:00:00 2001 From: Fraser Waters Date: Sat, 26 Aug 2017 12:58:22 +0100 Subject: [PATCH 2/3] Add Distance[Squared] methods to Vector2[d] --- src/OpenTK/Math/Vector2.cs | 48 ++++++++++++++++++++++++++++++ src/OpenTK/Math/Vector2d.cs | 48 ++++++++++++++++++++++++++++++ tests/OpenTK.Tests/Assertions.fs | 5 +++- tests/OpenTK.Tests/Vector2Tests.fs | 10 +++++++ 4 files changed, 110 insertions(+), 1 deletion(-) 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`` = // From 4f9e057358b1d3a6e0f15a4274fb68ba207e338a Mon Sep 17 00:00:00 2001 From: Fraser Waters Date: Sat, 26 Aug 2017 13:12:12 +0100 Subject: [PATCH 3/3] Add Distance[Squared] methods to Vector3[d] --- src/OpenTK/Math/Vector3.cs | 48 ++++++++++++++++++++++++++++++ src/OpenTK/Math/Vector3d.cs | 48 ++++++++++++++++++++++++++++++ tests/OpenTK.Tests/Vector3Tests.fs | 10 +++++++ 3 files changed, 106 insertions(+) diff --git a/src/OpenTK/Math/Vector3.cs b/src/OpenTK/Math/Vector3.cs index 6e2d1c11..24a0a342 100644 --- a/src/OpenTK/Math/Vector3.cs +++ b/src/OpenTK/Math/Vector3.cs @@ -563,6 +563,54 @@ namespace OpenTK result.Z = vec.Z < min.Z ? min.Z : vec.Z > max.Z ? max.Z : vec.Z; } + /// + /// Compute the euclidean distance between two vectors. + /// + /// The first vector + /// The second vector + /// The distance + public static float Distance(Vector3 vec1, Vector3 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 Vector3 vec1, ref Vector3 vec2, out float result) + { + result = (float)Math.Sqrt((vec2.X - vec1.X) * (vec2.X - vec1.X) + (vec2.Y - vec1.Y) * (vec2.Y - vec1.Y) + (vec2.Z - vec1.Z) * (vec2.Z - vec1.Z)); + } + + /// + /// Compute the squared euclidean distance between two vectors. + /// + /// The first vector + /// The second vector + /// The squared distance + public static float DistanceSquared(Vector3 vec1, Vector3 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 Vector3 vec1, ref Vector3 vec2, out float result) + { + result = (vec2.X - vec1.X) * (vec2.X - vec1.X) + (vec2.Y - vec1.Y) * (vec2.Y - vec1.Y) + (vec2.Z - vec1.Z) * (vec2.Z - vec1.Z); + } + /// /// Scale a vector to unit length /// diff --git a/src/OpenTK/Math/Vector3d.cs b/src/OpenTK/Math/Vector3d.cs index 9100f9cd..0216e460 100644 --- a/src/OpenTK/Math/Vector3d.cs +++ b/src/OpenTK/Math/Vector3d.cs @@ -557,6 +557,54 @@ namespace OpenTK result.Z = vec.Z < min.Z ? min.Z : vec.Z > max.Z ? max.Z : vec.Z; } + /// + /// Compute the euclidean distance between two vectors. + /// + /// The first vector + /// The second vector + /// The distance + public static double Distance(Vector3d vec1, Vector3d 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 Vector3d vec1, ref Vector3d vec2, out double result) + { + result = Math.Sqrt((vec2.X - vec1.X) * (vec2.X - vec1.X) + (vec2.Y - vec1.Y) * (vec2.Y - vec1.Y) + (vec2.Z - vec1.Z) * (vec2.Z - vec1.Z)); + } + + /// + /// Compute the squared euclidean distance between two vectors. + /// + /// The first vector + /// The second vector + /// The squared distance + public static double DistanceSquared(Vector3d vec1, Vector3d 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 Vector3d vec1, ref Vector3d vec2, out double result) + { + result = (vec2.X - vec1.X) * (vec2.X - vec1.X) + (vec2.Y - vec1.Y) * (vec2.Y - vec1.Y) + (vec2.Z - vec1.Z) * (vec2.Z - vec1.Z); + } + /// /// Scale a vector to unit length /// diff --git a/tests/OpenTK.Tests/Vector3Tests.fs b/tests/OpenTK.Tests/Vector3Tests.fs index c12c0f18..18cb89a9 100644 --- a/tests/OpenTK.Tests/Vector3Tests.fs +++ b/tests/OpenTK.Tests/Vector3Tests.fs @@ -124,6 +124,16 @@ module Vector3 = Assert.Equal(lsq, v.LengthSquared) + [ |])>] + module Distance = + [] + let ``Distance(a, b) = (b - a).Length`` (a : Vector3, b : Vector3) = + Assert.ApproximatelyEqual(Vector3.Distance(a, b), (b - a).Length) + + [] + let ``DistanceSquared(a, b) = (b - a).LengthSquared`` (a : Vector3, b : Vector3) = + Assert.ApproximatelyEqual(Vector3.DistanceSquared(a, b), (b - a).LengthSquared) + [ |])>] module Normalization = //