C# - 创建点组

C# - creating groups of points

如何创建接受一个点和所有点的列表以及 returns 足够接近原始点或足够接近的点的点列表。

如果你还是不明白我的意思,给你一张图:

我试过:

    int range = 6;
    public List<IntPoint> getClosePoints(ref Dictionary<IntPoint,bool> allPoints,IntPoint startingPoint) {
        List<IntPoint> closePoints = new List<IntPoint>();
        closePoints.Add(startingPoint);
        bool gotBigger = true;

        while (gotBigger) {
            gotBigger = false;
            foreach (IntPoint proven in closePoints.ToList()) {
                foreach (IntPoint possible in allPoints.Keys.ToList()) {
                    if (isInRange(proven,possible,range) && !allPoints[possible]) {
                        gotBigger = true;
                        closePoints.Add(possible);
                        allPoints[possible] = true;
                    }
                }
            }
        }
        return closePoints;
    }

    public bool isInRange(IntPoint A, IntPoint B, int range){
        if(A.DistanceTo(B) < range)
            return true;
        return false;
    }

IntPoint类似于Point,它来自AForge,所有点都有bool值false) 但是考虑到它被称为循环一千次,这使得我的程序超级滞后。 :/(而且它现在似乎不起作用)

试试这个:

public IEnumerable<Point> GetPoints(Point origin, IEnumerable<Point> points, int distance)
{
    var result = new HashSet<Point>();
    var found = new Queue<Point>();
    found.Enqueue(origin)

    while(found.Count > 0)
    {
        var current = found.Dequeue();
        var candidates = points
            .Where(p => !result.Contains(p) &&
                   p.Distance(current) <= distance);

        foreach(var p in candidates)
        {
            result.Add(p);
            found.Enqueue(p)
        }
    }

    return result;
}

我认为这很直接,无论如何 HashSet 的特性是它可以判断它是否包含接近 O(1) 的项目。

这是一个聚类问题。简单几步,

  1. 获取所有接近你输入点的点;
  2. 将所有点添加到哈希集;
  3. 现在将哈希集中的所有点放入队列中并转到第 1 步。
  4. 当您的哈希集与上一次迭代相同时中断。你已经找到了所有正确的点。