gpt4 book ai didi

c# - 如何从 2 List 中获取最近的 2 Points

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:33:24 25 4
gpt4 key购买 nike

我有 2 个点列表:

        var listA = new List<Point>();
listA.Add(new Point(10, 1));
listA.Add(new Point(5, 5));
listA.Add(new Point(15, 35));

var listB = new List<Point>();
listB.Add(new Point(1, 1));
listB.Add(new Point(5, 4));
listB.Add(new Point(35, 15));

现在我想做这样的事情:

        getClosesPoints(listA,listB, Out pointA, Out pointB);

我现在可以再次检查列表 1 点

        private int NearestPoint(Point srcPt, List<Point> lookIn)
{
if (lookIn.Count == 1)
return 0;

KeyValuePair<double, int> smallestDistance = new KeyValuePair<double, int>();

for (int i = 0; i < lookIn.Count; i++)
{
double distance = Math.Sqrt(Math.Pow(srcPt.X - lookIn[i].X, 2) + Math.Pow(srcPt.Y - lookIn[i].Y, 2));

if (i == 0)
smallestDistance = new KeyValuePair<double, int>(distance, i);
else if (distance < smallestDistance.Key)
smallestDistance = new KeyValuePair<double, int>(distance, i);
}

Console.WriteLine("smallest Distance:" + smallestDistance.Key);
return smallestDistance.Value;
}

但我不确定如何扩展此代码以检查 2 个列表。

最佳答案

我假设您想要在两个列表中找到最近的两个点,其中一个点在一个列表中,另一个点在另一个列表中。

您可以使用内部循环和外部循环来解决这个问题。外层循环遍历第一个列表中的所有点;对于这些点中的每一个,您都使用内部循环将其与第二个列表中的所有点进行比较。

这样你就不需要记住所有的距离;您只需要记住到目前为止找到的两个最近点之间的距离,以及到目前为止最近的点本身。

您还可以创建一个简单的小类来返回最近的两个点,而不是通过 out 返回结果参数。为方便起见,您可以添加 Distance()对此类起作用,因此您无需存储实际距离。 (但是,如果您经常使用此值,那么您可能希望将距离缓存在本地字段中,而不是每次都计算它。)

最后,参数给FindClosestPoints()方法可以是IEnumerable<Point>而不是 List<Point>这允许您将它与更多集合类型一起使用。

将所有这些放在一起会得到类似这样的东西(一个可编译的控制台应用程序):

using System;
using System.Collections.Generic;
using System.Drawing;

namespace Demo
{
// Class just used to return the result from FindClosestPoints()

public sealed class ClosestPoints
{
public ClosestPoints(Point p1, Point p2)
{
_p1 = p1;
_p2 = p2;
}

public Point P1 { get { return _p1; } }
public Point P2 { get { return _p2; } }

public double Distance()
{
double dx = P1.X - P2.X;
double dy = P1.Y - P2.Y;

return Math.Sqrt(dx*dx + dy*dy);
}

private readonly Point _p1;
private readonly Point _p2;
}

public static class Program
{
public static void Main()
{
var listA = new List<Point>();
listA.Add(new Point(10, 1));
listA.Add(new Point(5, 5));
listA.Add(new Point(15, 35));

var listB = new List<Point>();
listB.Add(new Point(1, 1));
listB.Add(new Point(5, 4));
listB.Add(new Point(35, 15));

var answer = FindClosestPoints(listA, listB);

Console.WriteLine("Closest points are {0} and {1}", answer.P1, answer.P2);
}

public static ClosestPoints FindClosestPoints(IEnumerable<Point> seq1, IEnumerable<Point> seq2)
{
double closest = double.MaxValue;
ClosestPoints result = null;

foreach (var p1 in seq1)
{
foreach (var p2 in seq2)
{
double dx = p1.X - p2.X;
double dy = p1.Y - p2.Y;

double distance = dx*dx + dy*dy;

if (distance >= closest)
continue;

result = new ClosestPoints(p1, p2);
closest = distance;
}
}

return result;
}
}
}

关于c# - 如何从 2 List<Point> 中获取最近的 2 Points,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28694699/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com