public void TestMinDistance()
{
List<Point> list = new List<Point>()
{
new Point(1, 1),
new Point(2, 1),
new Point(2, 0),
};
Point pivot = new Point(3, 1);
Func<Point, Point, double> DistanceTo = (point1, point2) =>
{
var a = (double)(point2.X - point1.X);
var b = (double)(point2.Y - point1.Y);
return Math.Sqrt(a * a + b * b);
};
Point nearest = list
.Aggregate((x, y) => DistanceTo(x, pivot) < DistanceTo(y, pivot) ? x : y);
Debug.WriteLine(nearest);
// sort by distance
List<Point> sortedList = list
.OrderBy(x => DistanceTo(x, pivot))
.ToList();
sortedList.ForEach(p => Debug.WriteLine(p));
}
카테고리 없음
댓글