如何从2 List <point> </point>获得最近的2个点

时间:2015-02-24 11:32:50

标签: c# algorithm list point

我有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个列表。

3 个答案:

答案 0 :(得分:3)

我假设您希望在两个列表中找到最接近的两个点,其中一个点位于一个列表中,另一个点位于另一个列表中。

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

然后你不需要记住所有的距离;你只需要记住你到目前为止找到的两个最近点之间的距离,以及到目前为止最接近的点。

您还可以创建一个简单的小类,只返回最近的两个点,而不是通过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;
        }
    }
}

答案 1 :(得分:1)

如果从方法返回整个键值对,则可以使用简单的循环从一个点扩展到整个列表:

KeyValuePoint<double,int> best = new KeyValuePair<double,int>(double.MaxValue, -1);
int best2 = -1;
for (int i = 0 ; i != list2.Count ; i++) {
    Point pt = list2[i];
    KeyValuePoint<double,int> current = NearestPoint(pt, list1);
    if (current.Key < best.Key) {
        best = current;
        best2 = i;
    }
}

最后,best.Key的距离最短,best.Value的点数为list1best2的点数为list2 1}}。

答案 2 :(得分:0)

让我们将两组点称为S1和S2。设p1为S1的迭代器,p2为S2的迭代器。设valueAt是一个告诉迭代器位置值的函数(如p1或p2)。设dist是计算两点之间距离的函数。并且,让dmin成为保持两点之间距离的变量。

假设这两个集合也可以包含负值,我提出以下逻辑:

  
      
  • 排序S1和S2
  •   
  • 如果valueAt(p1)&lt; valueAt(p2)
      { d = dist (p1, p2 ); if ( dmin > dist ( p1, p2 ) ) { dmin = dist (p1, p2); record p1 and p2 as closest; }

    increment p1;
    
         

    }

  •   
  • 如果valueAt(p1)&gt; valueAt(p2)
      { d = dist (p1, p2 ); if ( dmin > dist ( p1, p2 ) ) { dmin = dist (p1, p2); record p1 and p2 as closest; }

    increment p2;
    
         

    }

  •   
  • if valueAt(p1)== valueAt(p2)
      { record p1 and p2 as closest; }
  •