C# - 创建点组

时间:2015-09-18 17:54:10

标签: c# coordinates cluster-analysis point

如何创建接受一个点和所有点列表的东西,并返回足够接近原始点或足够接近点的点列表。

如果你仍然无法理解我的照片:

Point grouping

我试过了:

    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;
    }

IntPointPoint类似,来自AForge,所有点都有bool值false 但这使我的程序超级滞后,因为它被称为循环的千倍。 :/(而且它现在似乎也没有用)

2 个答案:

答案 0 :(得分:3)

试试这个:

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)

这是一个群集问题。只需简单的步骤,

  1. 获取靠近输入点的所有点;
  2. 将所有积分添加到Hashset;
  3. 现在将您的Hashset中的所有点放入队列并转到步骤1.
  4. 当您的Hashset与上一次迭代相同时中断。你找到了所有正确的观点。