圆分离距离 - 最近邻问题

我在二维平面上有一组给定位置和半径的圆。我想确定每个圆圈是否与任何其他圆相交,以及将两者分开所需的距离。在我目前的实现中,我只是通过所有可能的圆组合然后进行计算。不幸的是,这个算法是O(n ^ 2),这很慢。 圆圈通常会成组聚集,并且它们具有相似(但不同)的半径。圆圈的近似最大值约为200.算法不必精确,但应该接近。 这是我目前在JavaScript中的一个(慢)实现:
// Makes a new circle
var circle = function(x,y,radius) {
    return {
        x:x,
        y:y,
        radius:radius
    };
};

// These points are not representative of the true data set. I just made them up.
var points = [
    circle(3,3,2),
    circle(7,5,4),
    circle(16,6,4),
    circle(17,12,3),
    circle(26,20,1)
];


var k = 0,
    len = points.length;
for (var i = 0; i < len; i++) {
    for (var j = k; j < len; j++) {
        if (i !== j) {
            var c1 = points[i],
                c2 = points[j],
                radiiSum = c1.radius+c2.radius,
                deltaX = Math.abs(c1.x-c2.x);

            if (deltaX < radiiSum) {
                var deltaY = Math.abs(c1.y-c2.y);

                if (deltaY < radiiSum) {
                    var distance = Math.sqrt(deltaX*deltaX+deltaY*deltaY);

                    if (distance < radiiSum) {
                        var separation = radiiSum - distance;
                        console.log(c1,c2,separation);
                    }
                }
            }
        }
    }

    k++;
}
另外,如果您用简单的英语解释一个好的算法(KD树?),我将不胜感激: - /     
已邀请:
对于初学者,如果你刚刚跳过SQRT调用,你的算法将大大加快。这是用于比较距离的最着名的简单优化。您还可以预先计算“方形半径”距离,这样就不会冗余地重新计算它。 此外,在您的某些算法中看起来还有很多其他小错误。以下是我对如何修复它的看法。 此外,如果你想摆脱O(N-Squared)算法,你可以看看使用kd树。构建KD树有一个前期成本,但是可以更快地搜索最近的邻居。
function Distance_Squared(c1, c2) {

    var deltaX = (c1.x - c2.x);
    var deltaY = (c1.y - c2.y);
    return (deltaX * deltaX + deltaY * deltaY);
}



// returns false if it's possible that the circles intersect.  Returns true if the bounding box test proves there is no chance for intersection
function TrivialRejectIntersection(c1, c2) {
    return ((c1.left >= c2.right) || (c2.right <= c1.left) || (c1.top >= c2.bottom) || (c2.bottom <= c1.top));
}

    var circle = function(x,y,radius) {
        return {
            x:x,
            y:y,
            radius:radius,

            // some helper properties
            radius_squared : (radius*radius), // precompute the "squared distance"
            left : (x-radius),
            right: (x+radius),
            top : (y - radius),
            bottom : (y+radius)
        };
    };

    // These points are not representative of the true data set. I just made them up.
    var points = [
        circle(3,3,2),
        circle(7,5,4),
        circle(16,6,4),
        circle(17,12,3),
        circle(26,20,1)
    ];


    var k = 0;
    var len = points.length;
    var c1, c2;
    var distance_squared;
    var deltaX, deltaY;
    var min_distance;
    var seperation;

    for (var i = 0; i < len; i++) {
        for (var j = (i+1); j < len; j++) {
            c1 = points[i];
            c2 = points[j];

            // try for trivial rejections first. Jury is still out if this will help
            if (TrivialRejectIntesection(c1, c2)) {
                 continue;
            }



            //distance_squared is the actual distance between c1 and c2 'squared'
            distance_squared = Distance_Squared(c1, c2);

            // min_distance_squared is how much "squared distance" is required for these two circles to not intersect
            min_distance_squared = (c1.radius_squared + c2.radius_squared + (c1.radius*c2.radius*2)); // D**2 == deltaX*deltaX + deltaY*deltaY + 2*deltaX*deltaY

            // and so it follows
            if (distance_squared < min_distance_squared) {

                // intersection detected

                // now subtract actual distance from "min distance"
                seperation = c1.radius + c2.radius - Math.sqrt(distance_squared);
                Console.log(c1, c2, seperation);
            }
        }
    }
    
这篇文章已经蛰伏了很长时间,但我已经遇到并且很好地解决了这个问题,因此将发布以便其他人不必做同样的头部刮擦。 您可以将最近的圆邻居问题视为kd树或八叉树中的3d点最近邻搜索。将两个圆A和B之间的距离定义为
D(A,B) =  sqrt( (xA - xB)^2 + (yA - yB)^2 ) - rA - rB
如果圆圈重叠,则这是负数量。对于这个讨论,我将假设一个八叉树,但k = 3的kd树是相似的。 在每个圆圈的八叉树中存储三元组(x,y,r)。 要查找目标圆T的最近邻居,请使用标准算法:
def search(node, T, nst)
  if node is a leaf
    update nst with node's (x,y,r) nearest to T
  else
    for each cuboid C subdividing node (there are 8 of them)
       if C contains any point nearer to T than nst
          search(C, T, nst)
  end
end
这里
nst
是对到目前为止找到的T的最近圆的参考。最初它是空的。 稍微棘手的部分是确定
if C contains any point nearer to T than nst
。为此,考虑C中的唯一点(x,y,r)就足够了,该点是欧几里德在x和y中最接近T并且具有包含在长方体中的r范围的最大值。换句话说,长方体表示一组圆,其中心在x和y的矩形区域上并且具有半径范围。您要检查的点是表示中心最接近T且半径最大的圆的点。 注意,T的半径根本不参与算法。你只是想知道T的中心在任何其他圆圈内的距离。 (我希望这一开始就像现在看起来一样明显......)     

要回复问题请先登录注册