Here, I’ve decided to update my point comparators. It’s part of a larger class but should be self-contained. If it isn’t, yell at me and I’ll fix it.
Aside from the classic NearestOf/FarthestOf tests which optionally return an index and/or a distance, there are now three types of distance functions to choose from (Manhattan/Euclidean/Chebyshev), and both Vector2 and Vector3 are supported. Each combination (2D/3D + NearestOf/FarthestOf) also provides a custom evaluator, which may be something else other than distance, and all public methods have a xmldoc commentary for easier navigation.
The entire algorithm running all overloads/variants is now contained in a single method to minimize potential errors by repetition.
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;
namespace MinMaxing {
static public class Comparator {
// .... this is supposed to replace the code from post #3 above ...
// NEAREST/FARTHEST COMPARATORS for VECTORS
public enum DistanceMethod {
Chebyshev = 0, // power: +inf
Manhattan = 1, // power: 1
Euclidean = 2 // power: 2
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static float max(float a, float b) => MathF.Max(a, b);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static float sgn(float v) => Math.Sign(v);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static float abs(float v) => Math.Abs(v);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static float sqrt(float v) => MathF.Sqrt(v);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static float applyInversePower(float value, int power = 0)
=> (power == 2)? sqrt(value) : value;
/// <summary>
/// Computes the Manhattan (taxicab) distance between two 3D points.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static public float ManhattanDistance(Vector3 a, Vector3 b)
=> abs(b.x - a.x) + abs(b.y - a.y) + abs(b.z - a.z);
/// <summary>
/// Computes the Manhattan (taxicab) distance between two 2D points.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static public float ManhattanDistance(Vector2 a, Vector2 b)
=> abs(b.x - a.x) + abs(b.y - a.y);
/// <summary>
/// Computes the squared Euclidean (geometric) distance between two 3D points.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static public float EuclideanDistance(Vector3 a, Vector3 b) {
float x = b.x - a.x, y = b.y - a.y, z = b.z - a.z;
return x * x + y * y + z * z;
}
/// <summary>
/// Computes the squared Euclidean (geometric) distance between two 2D points.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static public float EuclideanDistance(Vector2 a, Vector2 b) {
float x = b.x - a.x, y = b.y - a.y;
return x * x + y * y;
}
/// <summary>
/// Computes the Chebyshev (chessboard) distance between two 3D points.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static public float ChebyshevDistance(Vector3 a, Vector3 b)
=> max(max(abs(b.x - a.x), abs(b.y - a.y)), abs(b.z - a.z));
/// <summary>
/// Computes the Chebyshev (chessboard) distance between two 2D points.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static public float ChebyshevDistance(Vector2 a, Vector2 b)
=> max(abs(b.x - a.x), abs(b.y - a.y));
/// <summary>
/// Returns a reference to the selected 3D distance function.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static public Func<Vector3, Vector3, float> Distance3D(DistanceMethod method) {
switch((int)method) {
case 1: return (a,b) => ManhattanDistance(a, b);
case 2: return (a,b) => EuclideanDistance(a, b);
default: return (a,b) => ChebyshevDistance(a, b);
}
}
/// <summary>
/// Returns a reference to the selected 2D distance function.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static public Func<Vector2, Vector2, float> Distance2D(DistanceMethod method) {
switch((int)method) {
case 1: return (a,b) => ManhattanDistance(a, b);
case 2: return (a,b) => EuclideanDistance(a, b);
default: return (a,b) => ChebyshevDistance(a, b);
}
}
//----- Core method -----
private const float FARTHEST = 1f;
private const float NEAREST = -1f;
static private int nearfar_core_impl<V>(float sign, V point, out float best, IList<V> points, Func<V, V, float> df) /* where V : Vector2 or Vector3 */ {
if(points is null) throw new ArgumentNullException(); // point list mandatory
if(df is null) throw new ArgumentNullException(); // distance function mandatory
best = 0f;
int index = -1;
int len = points.Count;
if(len == 0) return index;
var odd = (len & 1) != 0;
best = df(point, points[0]);
index = 0;
if(len == 1) return index;
int hlen = len >> 1;
var dist = 0f;
for(int i = 0, j = len - 1; i <= hlen; i++, j--) {
if(i < hlen || odd) {
if(i > 0) {
dist = df(point, points[i]);
if(sgn(dist - best) * sign >= 0f) { best = dist; index = i; }
}
if(i < hlen) {
dist = df(point, points[j]);
if(sgn(dist - best) * sign >= 0f) { best = dist; index = j; }
}
}
}
return index;
}
//-------- Vector3 --------
/// <summary>
/// Returns a point that is nearest to the specified point in 3D.
/// The result is guaranteed to have the lowest Euclidean distance.
/// </summary>
static public Vector3 NearestOf(this Vector3 point, params Vector3[] points)
=> point.NearestOf(points, DistanceMethod.Euclidean);
/// <summary>
/// Returns a point that is nearest to the specified point in 3D.
/// The result is guaranteed to have the lowest Euclidean distance.
/// </summary>
/// <param name="distance">Distance of the winning point.</param>
static public Vector3 NearestOf(this Vector3 point, out float distance, params Vector3[] points)
=> point.NearestOf(out distance, points, DistanceMethod.Euclidean);
/// <summary>
/// Returns a point that is nearest to the specified point in 3D.
/// The result is guaranteed to have the lowest Euclidean distance.
/// </summary>
/// <param name="index">Index of the winning point.</param>
static public Vector3 NearestOf(this Vector3 point, out int index, params Vector3[] points)
=> point.NearestOf(out index, points, DistanceMethod.Euclidean);
/// <summary>
/// Returns a point that is nearest to the specified point in 3D.
/// The result is guaranteed to have the lowest Euclidean distance.
/// </summary>
/// <param name="index">Index of the winning point.</param>
/// <param name="distance">Distance of the winning point.</param>
static public Vector3 NearestOf(this Vector3 point, out int index, out float distance, params Vector3[] points)
=> point.NearestOf(out index, out distance, points, DistanceMethod.Euclidean);
/// <summary>
/// Returns a point that is nearest to the specified point in 3D.
/// The result is guaranteed to have the lowest distance value.
/// </summary>
/// <param name="method">Method to use for distance computation.</param>
static public Vector3 NearestOf(this Vector3 point, DistanceMethod method, params Vector3[] points)
=> point.NearestOf(points, method);
/// <summary>
/// Returns a point that is nearest to the specified point in 3D.
/// The result is guaranteed to have the lowest distance value.
/// </summary>
/// <param name="distance">Distance of the winning point.</param>
/// <param name="method">Method to use for distance computation.</param>
static public Vector3 NearestOf(this Vector3 point, out float distance, DistanceMethod method, params Vector3[] points)
=> point.NearestOf(out distance, points, method);
/// <summary>
/// Returns a point that is nearest to the specified point in 3D.
/// The result is guaranteed to have the lowest distance value.
/// </summary>
/// <param name="index">Index of the winning point.</param>
/// <param name="method">Method to use for distance computation.</param>
static public Vector3 NearestOf(this Vector3 point, out int index, DistanceMethod method, params Vector3[] points)
=> point.NearestOf(out index, points, method);
/// <summary>
/// Returns a point that is nearest to the specified point in 3D.
/// The result is guaranteed to have the lowest distance value.
/// </summary>
/// <param name="index">Index of the winning point.</param>
/// <param name="distance">Distance of the winning point.</param>
/// <param name="method">Method to use for distance computation.</param>
static public Vector3 NearestOf(this Vector3 point, out int index, out float distance, DistanceMethod method, params Vector3[] points)
=> point.NearestOf(out index, out distance, points, method);
/// <summary>
/// Returns a 3D point after comparing each point in a IList collection to the specified one.
/// The result is guaranteed to have the lowest value produced by the custom evaluator.
/// </summary>
/// <param name="index">Index of the winning point.</param>
/// <param name="distance">Distance of the winning point.</param>
/// <param name="customDistanceEval">Custom evaluation to use in place of distance computation. Use finalizer to avoid having to use MathF.Sqrt for example.</param>
/// <param name="finalDistanceEval">Optional. Value finalizer. For example MathF.Sqrt for Euclidean distance.</param>
static public Vector3 NearestOf(this Vector3 point, out int index, out float distance, IList<Vector3> points, Func<Vector3, Vector3, float> customDistanceEval, Func<float, float> finalDistanceEval = null) {
index = nearfar_core_impl(NEAREST, point, out var value, points, customDistanceEval);
distance = finalDistanceEval is null? value : finalDistanceEval(value);
return points[index];
}
/// <summary>
/// Returns a point that is nearest to the specified point in 3D.
/// The result is guaranteed to have the lowest distance value.
/// </summary>
/// <param name="method">Method to use for distance computation. Default: Euclidean.</param>
static public Vector3 NearestOf(this Vector3 point, IList<Vector3> points, DistanceMethod method = DistanceMethod.Euclidean)
=> points[nearfar_core_impl(NEAREST, point, out _, points, Distance3D(method))];
/// <summary>
/// Returns a point that is nearest to the specified point in 3D.
/// The result is guaranteed to have the lowest distance value.
/// </summary>
/// <param name="distance">Distance of the winning point.</param>
/// <param name="method">Method to use for distance computation. Default: Euclidean.</param>
static public Vector3 NearestOf(this Vector3 point, out float distance, IList<Vector3> points, DistanceMethod method = DistanceMethod.Euclidean) {
int index = nearfar_core_impl(NEAREST, point, out var value, points, Distance3D(method));
distance = applyInversePower(value, (int)method);
return points[index];
}
/// <summary>
/// Returns a point that is nearest to the specified point in 3D.
/// The result is guaranteed to have the lowest distance value.
/// </summary>
/// <param name="index">Index of the winning point.</param>
/// <param name="method">Method to use for distance computation. Default: Euclidean.</param>
static public Vector3 NearestOf(this Vector3 point, out int index, IList<Vector3> points, DistanceMethod method = DistanceMethod.Euclidean) {
index = nearfar_core_impl(NEAREST, point, out _, points, Distance3D(method));
return points[index];
}
/// <summary>
/// Returns a point that is nearest to the specified point in 3D.
/// The result is guaranteed to have the lowest distance value.
/// </summary>
/// <param name="index">Index of the winning point.</param>
/// <param name="distance">Distance of the winning point.</param>
/// <param name="method">Method to use for distance computation. Default: Euclidean.</param>
static public Vector3 NearestOf(this Vector3 point, out int index, out float distance, IList<Vector3> points, DistanceMethod method = DistanceMethod.Euclidean) {
index = nearfar_core_impl(NEAREST, point, out var value, points, Distance3D(method));
distance = applyInversePower(value, (int)method);
return points[index];
}
/// <summary>
/// Returns a point that is farthest from the specified point in 3D.
/// The result is guaranteed to have the highest Euclidean distance.
/// </summary>
static public Vector3 FarthestOf(this Vector3 point, params Vector3[] points)
=> point.FarthestOf(points, DistanceMethod.Euclidean);
/// <summary>
/// Returns a point that is farthest from the specified point in 3D.
/// The result is guaranteed to have the highest Euclidean distance.
/// </summary>
/// <param name="distance">Distance of the winning point.</param>
static public Vector3 FarthestOf(this Vector3 point, out float distance, params Vector3[] points)
=> point.FarthestOf(out distance, points, DistanceMethod.Euclidean);
/// <summary>
/// Returns a point that is farthest from the specified point in 3D.
/// The result is guaranteed to have the highest Euclidean distance.
/// </summary>
/// <param name="index">Index of the winning point.</param>
static public Vector3 FarthestOf(this Vector3 point, out int index, params Vector3[] points)
=> point.FarthestOf(out index, points, DistanceMethod.Euclidean);
/// <summary>
/// Returns a point that is farthest from the specified point in 3D.
/// The result is guaranteed to have the highest Euclidean distance.
/// </summary>
/// <param name="index">Index of the winning point.</param>
/// <param name="distance">Distance of the winning point.</param>
static public Vector3 FarthestOf(this Vector3 point, out int index, out float distance, params Vector3[] points)
=> point.FarthestOf(out index, out distance, points, DistanceMethod.Euclidean);
/// <summary>
/// Returns a point that is farthest from the specified point in 3D.
/// The result is guaranteed to have the highest distance value.
/// </summary>
/// <param name="method">Method to use for distance computation.</param>
static public Vector3 FarthestOf(this Vector3 point, DistanceMethod method, params Vector3[] points)
=> point.FarthestOf(points, method);
/// <summary>
/// Returns a point that is farthest from the specified point in 3D.
/// The result is guaranteed to have the highest distance value.
/// </summary>
/// <param name="distance">Distance of the winning point.</param>
/// <param name="method">Method to use for distance computation.</param>
static public Vector3 FarthestOf(this Vector3 point, out float distance, DistanceMethod method, params Vector3[] points)
=> point.FarthestOf(out distance, points, method);
/// <summary>
/// Returns a point that is farthest from the specified point in 3D.
/// The result is guaranteed to have the highest distance value.
/// </summary>
/// <param name="index">Index of the winning point.</param>
/// <param name="method">Method to use for distance computation.</param>
static public Vector3 FarthestOf(this Vector3 point, out int index, DistanceMethod method, params Vector3[] points)
=> point.FarthestOf(out index, points, method);
/// <summary>
/// Returns a point that is farthest from the specified point in 3D.
/// The result is guaranteed to have the highest distance value.
/// </summary>
/// <param name="index">Index of the winning point.</param>
/// <param name="distance">Distance of the winning point.</param>
/// <param name="method">Method to use for distance computation.</param>
static public Vector3 FarthestOf(this Vector3 point, out int index, out float distance, DistanceMethod method, params Vector3[] points)
=> point.FarthestOf(out index, out distance, points, method);
/// <summary>
/// Returns a 3D point after comparing each point in a IList collection to the specified one.
/// The result is guaranteed to have the highest value produced by the custom evaluator.
/// </summary>
/// <param name="index">Index of the winning point.</param>
/// <param name="distance">Distance of the winning point.</param>
/// <param name="customDistanceEval">Custom evaluation to use in place of distance computation. Use finalizer to avoid having to use MathF.Sqrt for example.</param>
/// <param name="finalDistanceEval">Optional. Value finalizer. For example MathF.Sqrt for Euclidean distance.</param>
static public Vector3 FarthestOf(this Vector3 point, out int index, out float distance, IList<Vector3> points, Func<Vector3, Vector3, float> customDistanceEval, Func<float, float> finalDistanceEval = null) {
index = nearfar_core_impl(FARTHEST, point, out var value, points, customDistanceEval);
distance = finalDistanceEval is null? value : finalDistanceEval(value);
return points[index];
}
/// <summary>
/// Returns a point that is farthest from the specified point in 3D.
/// The result is guaranteed to have the highest distance value.
/// </summary>
/// <param name="method">Method to use for distance computation. Default: Euclidean.</param>
static public Vector3 FarthestOf(this Vector3 point, IList<Vector3> points, DistanceMethod method = DistanceMethod.Euclidean)
=> points[nearfar_core_impl(FARTHEST, point, out _, points, Distance3D(method))];
/// <summary>
/// Returns a point that is farthest from the specified point in 3D.
/// The result is guaranteed to have the highest distance value.
/// </summary>
/// <param name="distance">Distance of the winning point.</param>
/// <param name="method">Method to use for distance computation. Default: Euclidean.</param>
static public Vector3 FarthestOf(this Vector3 point, out float distance, IList<Vector3> points, DistanceMethod method = DistanceMethod.Euclidean) {
int index = nearfar_core_impl(FARTHEST, point, out var value, points, Distance3D(method));
distance = applyInversePower(value, (int)method);
return points[index];
}
/// <summary>
/// Returns a point that is farthest from the specified point in 3D.
/// The result is guaranteed to have the highest distance value.
/// </summary>
/// <param name="index">Index of the winning point.</param>
/// <param name="method">Method to use for distance computation. Default: Euclidean.</param>
static public Vector3 FarthestOf(this Vector3 point, out int index, IList<Vector3> points, DistanceMethod method = DistanceMethod.Euclidean) {
index = nearfar_core_impl(FARTHEST, point, out _, points, Distance3D(method));
return points[index];
}
/// <summary>
/// Returns a point that is farthest from the specified point in 3D.
/// The result is guaranteed to have the highest distance value.
/// </summary>
/// <param name="index">Index of the winning point.</param>
/// <param name="distance">Distance of the winning point.</param>
/// <param name="method">Method to use for distance computation. Default: Euclidean.</param>
static public Vector3 FarthestOf(this Vector3 point, out int index, out float distance, IList<Vector3> points, DistanceMethod method = DistanceMethod.Euclidean) {
index = nearfar_core_impl(FARTHEST, point, out var value, points, Distance3D(method));
distance = applyInversePower(value, (int)method);
return points[index];
}
//-------- Vector2 --------
/// <summary>
/// Returns a point that is nearest to the specified point in 2D.
/// The result is guaranteed to have the lowest Euclidean distance.
/// </summary>
static public Vector2 NearestOf(this Vector2 point, params Vector2[] points)
=> point.NearestOf(points, DistanceMethod.Euclidean);
/// <summary>
/// Returns a point that is nearest to the specified point in 2D.
/// The result is guaranteed to have the lowest Euclidean distance.
/// </summary>
/// <param name="distance">Distance of the winning point.</param>
static public Vector2 NearestOf(this Vector2 point, out float distance, params Vector2[] points)
=> point.NearestOf(out distance, points, DistanceMethod.Euclidean);
/// <summary>
/// Returns a point that is nearest to the specified point in 2D.
/// The result is guaranteed to have the lowest Euclidean distance.
/// </summary>
/// <param name="index">Index of the winning point.</param>
static public Vector2 NearestOf(this Vector2 point, out int index, params Vector2[] points)
=> point.NearestOf(out index, points, DistanceMethod.Euclidean);
/// <summary>
/// Returns a point that is nearest to the specified point in 2D.
/// The result is guaranteed to have the lowest Euclidean distance.
/// </summary>
/// <param name="index">Index of the winning point.</param>
/// <param name="distance">Distance of the winning point.</param>
static public Vector2 NearestOf(this Vector2 point, out int index, out float distance, params Vector2[] points)
=> point.NearestOf(out index, out distance, points, DistanceMethod.Euclidean);
/// <summary>
/// Returns a point that is nearest to the specified point in 2D.
/// The result is guaranteed to have the lowest distance value.
/// </summary>
/// <param name="method">Method to use for distance computation.</param>
static public Vector2 NearestOf(this Vector2 point, DistanceMethod method, params Vector2[] points)
=> point.NearestOf(points, method);
/// <summary>
/// Returns a point that is nearest to the specified point in 2D.
/// The result is guaranteed to have the lowest distance value.
/// </summary>
/// <param name="distance">Distance of the winning point.</param>
/// <param name="method">Method to use for distance computation.</param>
static public Vector2 NearestOf(this Vector2 point, out float distance, DistanceMethod method, params Vector2[] points)
=> point.NearestOf(out distance, points, method);
/// <summary>
/// Returns a point that is nearest to the specified point in 2D.
/// The result is guaranteed to have the lowest distance value.
/// </summary>
/// <param name="index">Index of the winning point.</param>
/// <param name="method">Method to use for distance computation.</param>
static public Vector2 NearestOf(this Vector2 point, out int index, DistanceMethod method, params Vector2[] points)
=> point.NearestOf(out index, points, method);
/// <summary>
/// Returns a point that is nearest to the specified point in 2D.
/// The result is guaranteed to have the lowest distance value.
/// </summary>
/// <param name="index">Index of the winning point.</param>
/// <param name="distance">Distance of the winning point.</param>
/// <param name="method">Method to use for distance computation.</param>
static public Vector2 NearestOf(this Vector2 point, out int index, out float distance, DistanceMethod method, params Vector2[] points)
=> point.NearestOf(out index, out distance, points, method);
/// <summary>
/// Returns a 2D point after comparing each point in a IList collection to the specified one.
/// The result is guaranteed to have the lowest value produced by the custom evaluator.
/// </summary>
/// <param name="index">Index of the winning point.</param>
/// <param name="distance">Distance of the winning point.</param>
/// <param name="customDistanceEval">Custom evaluation to use in place of distance computation. Use finalizer to avoid having to use MathF.Sqrt for example.</param>
/// <param name="finalDistanceEval">Optional. Value finalizer. For example MathF.Sqrt for Euclidean distance.</param>
static public Vector2 NearestOf(this Vector2 point, out int index, out float distance, IList<Vector2> points, Func<Vector2, Vector2, float> customDistanceEval, Func<float, float> finalDistanceEval = null) {
index = nearfar_core_impl(NEAREST, point, out var value, points, customDistanceEval);
distance = finalDistanceEval is null? value : finalDistanceEval(value);
return points[index];
}
/// <summary>
/// Returns a point that is nearest to the specified point in 2D.
/// The result is guaranteed to have the lowest distance value.
/// </summary>
/// <param name="method">Method to use for distance computation. Default: Euclidean.</param>
static public Vector2 NearestOf(this Vector2 point, IList<Vector2> points, DistanceMethod method = DistanceMethod.Euclidean)
=> points[nearfar_core_impl(NEAREST, point, out _, points, Distance2D(method))];
/// <summary>
/// Returns a point that is nearest to the specified point in 2D.
/// The result is guaranteed to have the lowest distance value.
/// </summary>
/// <param name="distance">Distance of the winning point.</param>
/// <param name="method">Method to use for distance computation. Default: Euclidean.</param>
static public Vector2 NearestOf(this Vector2 point, out float distance, IList<Vector2> points, DistanceMethod method = DistanceMethod.Euclidean) {
int index = nearfar_core_impl(NEAREST, point, out var value, points, Distance2D(method));
distance = applyInversePower(value, (int)method);
return points[index];
}
/// <summary>
/// Returns a point that is nearest to the specified point in 2D.
/// The result is guaranteed to have the lowest distance value.
/// </summary>
/// <param name="index">Index of the winning point.</param>
/// <param name="method">Method to use for distance computation. Default: Euclidean.</param>
static public Vector2 NearestOf(this Vector2 point, out int index, IList<Vector2> points, DistanceMethod method = DistanceMethod.Euclidean) {
index = nearfar_core_impl(NEAREST, point, out _, points, Distance2D(method));
return points[index];
}
/// <summary>
/// Returns a point that is nearest to the specified point in 2D.
/// The result is guaranteed to have the lowest distance value.
/// </summary>
/// <param name="index">Index of the winning point.</param>
/// <param name="distance">Distance of the winning point.</param>
/// <param name="method">Method to use for distance computation. Default: Euclidean.</param>
static public Vector2 NearestOf(this Vector2 point, out int index, out float distance, IList<Vector2> points, DistanceMethod method = DistanceMethod.Euclidean) {
index = nearfar_core_impl(NEAREST, point, out var value, points, Distance2D(method));
distance = applyInversePower(value, (int)method);
return points[index];
}
/// <summary>
/// Returns a point that is farthest from the specified point in 2D.
/// The result is guaranteed to have the highest Euclidean distance.
/// </summary>
static public Vector2 FarthestOf(this Vector2 point, params Vector2[] points)
=> point.FarthestOf(points, DistanceMethod.Euclidean);
/// <summary>
/// Returns a point that is farthest from the specified point in 2D.
/// The result is guaranteed to have the highest Euclidean distance.
/// </summary>
/// <param name="distance">Distance of the winning point.</param>
static public Vector2 FarthestOf(this Vector2 point, out float distance, params Vector2[] points)
=> point.FarthestOf(out distance, points, DistanceMethod.Euclidean);
/// <summary>
/// Returns a point that is farthest from the specified point in 2D.
/// The result is guaranteed to have the highest Euclidean distance.
/// </summary>
/// <param name="index">Index of the winning point.</param>
static public Vector2 FarthestOf(this Vector2 point, out int index, params Vector2[] points)
=> point.FarthestOf(out index, points, DistanceMethod.Euclidean);
/// <summary>
/// Returns a point that is farthest from the specified point in 2D.
/// The result is guaranteed to have the highest Euclidean distance.
/// </summary>
/// <param name="index">Index of the winning point.</param>
/// <param name="distance">Distance of the winning point.</param>
static public Vector2 FarthestOf(this Vector2 point, out int index, out float distance, params Vector2[] points)
=> point.FarthestOf(out index, out distance, points, DistanceMethod.Euclidean);
/// <summary>
/// Returns a point that is farthest from the specified point in 2D.
/// The result is guaranteed to have the highest distance value.
/// </summary>
/// <param name="method">Method to use for distance computation.</param>
static public Vector2 FarthestOf(this Vector2 point, DistanceMethod method, params Vector2[] points)
=> point.FarthestOf(points, method);
/// <summary>
/// Returns a point that is farthest from the specified point in 2D.
/// The result is guaranteed to have the highest distance value.
/// </summary>
/// <param name="distance">Distance of the winning point.</param>
/// <param name="method">Method to use for distance computation.</param>
static public Vector2 FarthestOf(this Vector2 point, out float distance, DistanceMethod method, params Vector2[] points)
=> point.FarthestOf(out distance, points, method);
/// <summary>
/// Returns a point that is farthest from the specified point in 2D.
/// The result is guaranteed to have the highest distance value.
/// </summary>
/// <param name="index">Index of the winning point.</param>
/// <param name="method">Method to use for distance computation.</param>
static public Vector2 FarthestOf(this Vector2 point, out int index, DistanceMethod method, params Vector2[] points)
=> point.FarthestOf(out index, points, method);
/// <summary>
/// Returns a point that is farthest from the specified point in 2D.
/// The result is guaranteed to have the highest distance value.
/// </summary>
/// <param name="index">Index of the winning point.</param>
/// <param name="distance">Distance of the winning point.</param>
/// <param name="method">Method to use for distance computation.</param>
static public Vector2 FarthestOf(this Vector2 point, out int index, out float distance, DistanceMethod method, params Vector2[] points)
=> point.FarthestOf(out index, out distance, points, method);
/// <summary>
/// Returns a 2D point after comparing each point in a IList collection to the specified one.
/// The result is guaranteed to have the highest value produced by the custom evaluator.
/// </summary>
/// <param name="index">Index of the winning point.</param>
/// <param name="distance">Distance of the winning point.</param>
/// <param name="customDistanceEval">Custom evaluation to use in place of distance computation. Use finalizer to avoid having to use MathF.Sqrt for example.</param>
/// <param name="finalDistanceEval">Optional. Value finalizer. For example MathF.Sqrt for Euclidean distance.</param>
static public Vector2 FarthestOf(this Vector2 point, out int index, out float distance, IList<Vector2> points, Func<Vector2, Vector2, float> customDistanceEval, Func<float, float> finalDistanceEval = null) {
index = nearfar_core_impl(FARTHEST, point, out var value, points, customDistanceEval);
distance = finalDistanceEval is null? value : finalDistanceEval(value);
return points[index];
}
/// <summary>
/// Returns a point that is farthest from the specified point in 2D.
/// The result is guaranteed to have the highest distance value.
/// </summary>
/// <param name="method">Method to use for distance computation. Default: Euclidean.</param>
static public Vector2 FarthestOf(this Vector2 point, IList<Vector2> points, DistanceMethod method = DistanceMethod.Euclidean)
=> points[nearfar_core_impl(FARTHEST, point, out _, points, Distance2D(method))];
/// <summary>
/// Returns a point that is farthest from the specified point in 2D.
/// The result is guaranteed to have the highest distance value.
/// </summary>
/// <param name="distance">Distance of the winning point.</param>
/// <param name="method">Method to use for distance computation. Default: Euclidean.</param>
static public Vector2 FarthestOf(this Vector2 point, out float distance, IList<Vector2> points, DistanceMethod method = DistanceMethod.Euclidean) {
int index = nearfar_core_impl(FARTHEST, point, out var value, points, Distance2D(method));
distance = applyInversePower(value, (int)method);
return points[index];
}
/// <summary>
/// Returns a point that is farthest from the specified point in 2D.
/// The result is guaranteed to have the highest distance value.
/// </summary>
/// <param name="index">Index of the winning point.</param>
/// <param name="method">Method to use for distance computation. Default: Euclidean.</param>
static public Vector2 FarthestOf(this Vector2 point, out int index, IList<Vector2> points, DistanceMethod method = DistanceMethod.Euclidean) {
index = nearfar_core_impl(FARTHEST, point, out _, points, Distance2D(method));
return points[index];
}
/// <summary>
/// Returns a point that is farthest from the specified point in 2D.
/// The result is guaranteed to have the highest distance value.
/// </summary>
/// <param name="index">Index of the winning point.</param>
/// <param name="distance">Distance of the winning point.</param>
/// <param name="method">Method to use for distance computation. Default: Euclidean.</param>
static public Vector2 FarthestOf(this Vector2 point, out int index, out float distance, IList<Vector2> points, DistanceMethod method = DistanceMethod.Euclidean) {
index = nearfar_core_impl(FARTHEST, point, out var value, points, Distance2D(method));
distance = applyInversePower(value, (int)method);
return points[index];
}
}
}
Cheers