Merge pull request #641 from Frassle/vector

Add Distance[Squared] methods to Vector(2/3)[d]
This commit is contained in:
Fraser Waters 2017-10-03 12:16:59 +01:00 committed by GitHub
commit cd0e300785
7 changed files with 218 additions and 3 deletions

View file

@ -514,6 +514,54 @@ namespace OpenTK
result.Y = vec.Y < min.Y ? min.Y : vec.Y > max.Y ? max.Y : vec.Y;
}
/// <summary>
/// Compute the euclidean distance between two vectors.
/// </summary>
/// <param name="vec1">The first vector</param>
/// <param name="vec2">The second vector</param>
/// <returns>The distance</returns>
public static float Distance(Vector2 vec1, Vector2 vec2)
{
float result;
Distance(ref vec1, ref vec2, out result);
return result;
}
/// <summary>
/// Compute the euclidean distance between two vectors.
/// </summary>
/// <param name="vec1">The first vector</param>
/// <param name="vec2">The second vector</param>
/// <param name="result">The distance</param>
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));
}
/// <summary>
/// Compute the squared euclidean distance between two vectors.
/// </summary>
/// <param name="vec1">The first vector</param>
/// <param name="vec2">The second vector</param>
/// <returns>The squared distance</returns>
public static float DistanceSquared(Vector2 vec1, Vector2 vec2)
{
float result;
DistanceSquared(ref vec1, ref vec2, out result);
return result;
}
/// <summary>
/// Compute the squared euclidean distance between two vectors.
/// </summary>
/// <param name="vec1">The first vector</param>
/// <param name="vec2">The second vector</param>
/// <param name="result">The squared distance</param>
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);
}
/// <summary>
/// Scale a vector to unit length
/// </summary>

View file

@ -507,6 +507,54 @@ namespace OpenTK
result.Y = vec.Y < min.Y ? min.Y : vec.Y > max.Y ? max.Y : vec.Y;
}
/// <summary>
/// Compute the euclidean distance between two vectors.
/// </summary>
/// <param name="vec1">The first vector</param>
/// <param name="vec2">The second vector</param>
/// <returns>The distance</returns>
public static double Distance(Vector2d vec1, Vector2d vec2)
{
double result;
Distance(ref vec1, ref vec2, out result);
return result;
}
/// <summary>
/// Compute the euclidean distance between two vectors.
/// </summary>
/// <param name="vec1">The first vector</param>
/// <param name="vec2">The second vector</param>
/// <param name="result">The distance</param>
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));
}
/// <summary>
/// Compute the squared euclidean distance between two vectors.
/// </summary>
/// <param name="vec1">The first vector</param>
/// <param name="vec2">The second vector</param>
/// <returns>The squared distance</returns>
public static double DistanceSquared(Vector2d vec1, Vector2d vec2)
{
double result;
DistanceSquared(ref vec1, ref vec2, out result);
return result;
}
/// <summary>
/// Compute the squared euclidean distance between two vectors.
/// </summary>
/// <param name="vec1">The first vector</param>
/// <param name="vec2">The second vector</param>
/// <param name="result">The squared distance</param>
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);
}
/// <summary>
/// Scale a vector to unit length
/// </summary>

View file

@ -237,7 +237,7 @@ namespace OpenTK
public static readonly Vector3 UnitY = new Vector3(0, 1, 0);
/// <summary>
/// /// Defines a unit-length Vector3 that points towards the Z-axis.
/// Defines a unit-length Vector3 that points towards the Z-axis.
/// </summary>
public static readonly Vector3 UnitZ = new Vector3(0, 0, 1);
@ -563,6 +563,54 @@ namespace OpenTK
result.Z = vec.Z < min.Z ? min.Z : vec.Z > max.Z ? max.Z : vec.Z;
}
/// <summary>
/// Compute the euclidean distance between two vectors.
/// </summary>
/// <param name="vec1">The first vector</param>
/// <param name="vec2">The second vector</param>
/// <returns>The distance</returns>
public static float Distance(Vector3 vec1, Vector3 vec2)
{
float result;
Distance(ref vec1, ref vec2, out result);
return result;
}
/// <summary>
/// Compute the euclidean distance between two vectors.
/// </summary>
/// <param name="vec1">The first vector</param>
/// <param name="vec2">The second vector</param>
/// <param name="result">The distance</param>
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));
}
/// <summary>
/// Compute the squared euclidean distance between two vectors.
/// </summary>
/// <param name="vec1">The first vector</param>
/// <param name="vec2">The second vector</param>
/// <returns>The squared distance</returns>
public static float DistanceSquared(Vector3 vec1, Vector3 vec2)
{
float result;
DistanceSquared(ref vec1, ref vec2, out result);
return result;
}
/// <summary>
/// Compute the squared euclidean distance between two vectors.
/// </summary>
/// <param name="vec1">The first vector</param>
/// <param name="vec2">The second vector</param>
/// <param name="result">The squared distance</param>
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);
}
/// <summary>
/// Scale a vector to unit length
/// </summary>

View file

@ -235,7 +235,7 @@ namespace OpenTK
public static readonly Vector3d UnitY = new Vector3d(0, 1, 0);
/// <summary>
/// /// Defines a unit-length Vector3d that points towards the Z-axis.
/// Defines a unit-length Vector3d that points towards the Z-axis.
/// </summary>
public static readonly Vector3d UnitZ = new Vector3d(0, 0, 1);
@ -557,6 +557,54 @@ namespace OpenTK
result.Z = vec.Z < min.Z ? min.Z : vec.Z > max.Z ? max.Z : vec.Z;
}
/// <summary>
/// Compute the euclidean distance between two vectors.
/// </summary>
/// <param name="vec1">The first vector</param>
/// <param name="vec2">The second vector</param>
/// <returns>The distance</returns>
public static double Distance(Vector3d vec1, Vector3d vec2)
{
double result;
Distance(ref vec1, ref vec2, out result);
return result;
}
/// <summary>
/// Compute the euclidean distance between two vectors.
/// </summary>
/// <param name="vec1">The first vector</param>
/// <param name="vec2">The second vector</param>
/// <param name="result">The distance</param>
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));
}
/// <summary>
/// Compute the squared euclidean distance between two vectors.
/// </summary>
/// <param name="vec1">The first vector</param>
/// <param name="vec2">The second vector</param>
/// <returns>The squared distance</returns>
public static double DistanceSquared(Vector3d vec1, Vector3d vec2)
{
double result;
DistanceSquared(ref vec1, ref vec2, out result);
return result;
}
/// <summary>
/// Compute the squared euclidean distance between two vectors.
/// </summary>
/// <param name="vec1">The first vector</param>
/// <param name="vec2">The second vector</param>
/// <param name="result">The squared distance</param>
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);
}
/// <summary>
/// Scale a vector to unit length
/// </summary>

View file

@ -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<IndexOutOfRangeException>(f) |> ignore
static member ApproximatelyEqual(a : float32, b : float32) =
if not <| approxEqDelta a b then raise <| new Xunit.Sdk.EqualException(a, b)

View file

@ -75,6 +75,16 @@ module Vector2 =
Assert.Equal(lsq, v.LengthSquared)
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Distance =
[<Property>]
let ``Distance(a, b) = (b - a).Length`` (a : Vector2, b : Vector2) =
Assert.ApproximatelyEqual(Vector2.Distance(a, b), (b - a).Length)
[<Property>]
let ``DistanceSquared(a, b) = (b - a).LengthSquared`` (a : Vector2, b : Vector2) =
Assert.ApproximatelyEqual(Vector2.DistanceSquared(a, b), (b - a).LengthSquared)
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module ``Unit vectors and perpendicularity`` =
//

View file

@ -124,6 +124,16 @@ module Vector3 =
Assert.Equal(lsq, v.LengthSquared)
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Distance =
[<Property>]
let ``Distance(a, b) = (b - a).Length`` (a : Vector3, b : Vector3) =
Assert.ApproximatelyEqual(Vector3.Distance(a, b), (b - a).Length)
[<Property>]
let ``DistanceSquared(a, b) = (b - a).LengthSquared`` (a : Vector3, b : Vector3) =
Assert.ApproximatelyEqual(Vector3.DistanceSquared(a, b), (b - a).LengthSquared)
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Normalization =
//