Browse Source

Add Distance method to Point

pull/11829/head
robloo 3 years ago
parent
commit
87b8c63c1b
  1. 13
      src/Avalonia.Base/Point.cs
  2. 14
      src/Avalonia.Base/Vector.cs

13
src/Avalonia.Base/Point.cs

@ -164,6 +164,19 @@ namespace Avalonia
/// <returns>The resulting point.</returns>
public static Point operator *(Point point, Matrix matrix) => matrix.Transform(point);
/// <summary>
/// Computes the Euclidean distance between the two given points.
/// </summary>
/// <param name="value1">The first point.</param>
/// <param name="value2">The second point.</param>
/// <returns>The Euclidean distance.</returns>
public static double Distance(Point value1, Point value2)
{
double distanceSquared = ((value2.X - value1.X) * (value2.X - value1.X)) +
((value2.Y - value1.Y) * (value2.Y - value1.Y));
return Math.Sqrt(distanceSquared);
}
/// <summary>
/// Parses a <see cref="Point"/> string.
/// </summary>

14
src/Avalonia.Base/Vector.cs

@ -359,7 +359,7 @@ namespace Avalonia
internal Vector(Vector2 v) : this(v.X, v.Y)
{
}
/// <summary>
@ -379,21 +379,27 @@ namespace Avalonia
/// </summary>
public static Vector Max(Vector left, Vector right) =>
new(Math.Max(left.X, right.X), Math.Max(left.Y, right.Y));
/// <summary>
/// Returns a vector whose elements are the minimum of each of the pairs of elements in two specified vectors
/// </summary>
public static Vector Min(Vector left, Vector right) =>
new(Math.Min(left.X, right.X), Math.Min(left.Y, right.Y));
/// <summary>
/// Computes the Euclidean distance between the two given points.
/// </summary>
/// <param name="value1">The first point.</param>
/// <param name="value2">The second point.</param>
/// <returns>The Euclidean distance.</returns>
public static double Distance(Vector value1, Vector value2) => Math.Sqrt(DistanceSquared(value1, value2));
/// <summary>
/// Returns the Euclidean distance squared between two specified points
/// </summary>
/// <param name="value1">The first point.</param>
/// <param name="value2">The second point.</param>
/// <returns>The Euclidean distance squared.</returns>
public static double DistanceSquared(Vector value1, Vector value2)
{
var difference = value1 - value2;

Loading…
Cancel
Save