[英]How to find point closest to 0,0 point with LINQ and C#
我有一个点列表(列表)
我想使用linq表达式来获得最接近0,0的点。 例如 - 对于此列表,我预计值为5,42。
如何用LINQ找到最接近0,0点的点?
下面找到具有最低L^2
范数的点(在两个维度中最常见的“距离”定义),而不执行整个列表的昂贵排序:
var closestToOrigin = points
.Select(p => new { Point = p, Distance2 = p.X * p.X + p.Y * p.Y })
.Aggregate((p1, p2) => p1.Distance2 < p2.Distance2 ? p1 : p2)
.Point;
试试这个:
List<Point> points = new List<Point>();
// populate list
var p = points.OrderBy(p => p.X * p.X + p.Y * p.Y).First();
或更快的解决方案:
var p = points.Aggregate(
(minPoint, next) =>
(minPoint.X * minPoint.X + minPoint.Y * minPoint.Y)
< (next.X * next.X + next.Y * next.Y) ? minPoint : next);
罗林的解决方案肯定更短,但这是另一种选择
// project every element to get a map between it and the square of the distance
var map = pointsList
.Select(p => new { Point = p, Distance = p.x * p.x + p.y * p.y });
var closestPoint = map // get the list of points with the min distance
.Where(m => m.Distance == map.Min(t => t.Distance))
.First() // get the first item in that list (guaranteed to exist)
.Point; // take the point
如果您需要找到距离最短的所有元素0,0
,只需删除First
并执行Select(p => p.Point)
以获取点(而不是映射)。
作为替代方法,您可以考虑向标准库添加IEnumerable.MinBy()和IEnumerable.MaxBy()的实现。
如果您有可用的,代码将变为:
var result = points.MinBy( p => p.X*p.X + p.Y*p.Y );
Jon Skeet提供了MinBy和MaxBy的良好实现。
他在这里讨论它: 如何使用LINQ选择具有最小或最大属性值的对象
那里的链接已经过时了; 最新版本在这里:
http://code.google.com/p/morelinq/source/browse/MoreLinq/MinBy.cs
http://code.google.com/p/morelinq/source/browse/MoreLinq/MaxBy.cs
这是一个完整的样本。 很明显,这是一个破解坚果的大锤,但我认为这些方法足以包含在您的标准库中:
using System;
using System.Collections.Generic;
using System.Drawing;
namespace Demo
{
public static class EnumerableExt
{
public static TSource MinBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> selector, IComparer<TKey> comparer)
{
using (IEnumerator<TSource> sourceIterator = source.GetEnumerator())
{
if (!sourceIterator.MoveNext())
{
throw new InvalidOperationException("Sequence was empty");
}
TSource min = sourceIterator.Current;
TKey minKey = selector(min);
while (sourceIterator.MoveNext())
{
TSource candidate = sourceIterator.Current;
TKey candidateProjected = selector(candidate);
if (comparer.Compare(candidateProjected, minKey) < 0)
{
min = candidate;
minKey = candidateProjected;
}
}
return min;
}
}
public static TSource MinBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> selector)
{
return source.MinBy(selector, Comparer<TKey>.Default);
}
}
public static class Program
{
static void Main(string[] args)
{
List<Point> points = new List<Point>
{
new Point(7, 43),
new Point(7, 42),
new Point(6, 42),
new Point(5, 42),
new Point(6, 43),
new Point(5, 43)
};
var result = points.MinBy( p => p.X*p.X + p.Y*p.Y );
Console.WriteLine(result);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.