Add Distance[Squared] methods to Vector2[d]

This commit is contained in:
Fraser Waters 2017-08-26 12:58:22 +01:00
parent 211fceddf7
commit 9d596f674b
4 changed files with 110 additions and 1 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

@ -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`` =
//