c – 找到重叠矩形算法

假设我有一套巨大的整数坐标不重叠的矩形,它们是一次性固定的

我有另一个矩形A与坐标正在移动的整数坐标(但你可以假设它的大小是恒定的)

找到哪些矩形相交(或内)A最有效的方法是什么?
我不能简单地循环通过我的设置,因为它太大了.谢谢

编辑:矩形都平行于轴

就个人而言,我将用KD树或BIH树来解决这个问题.它们都是具有日志(n)搜索时间的自适应空间数据结构.我已经对我的Ray Tracer进行了两次实施,他们尖叫起来.

– 更新 –

将所有固定的矩形存储在KD-Tree中.当您测试交叉点时,如下所示遍历KD-Tree:

function FindRects(KDNode node, Rect searchRect, List<Rect> intersectionRects)

// searchRect is the rectangle you want to test intersections with
// node is the current node. This is a recursive function, so the first call
//    is the root node
// intersectionRects contains the list of rectangles intersected

int axis = node.Axis;

// Only child nodes actually have rects in them
if (node is child)
{
    // Test for intersections with each rectangle the node owns
    for each (Rect nRect in node.Rects)
    {
        if (nRect.Intersects(searchRect))
              intersectionRects.Add(nRect);
    }
}
else
{
    // If the searchRect's boundary extends into the left bi-section of the node
    // we need to search the left sub-tree for intersections
    if (searchRect[axis].Min  // Min would be the Rect.Left if axis == 0, 
                              // Rect.Top if axis == 1
                < node.Plane) // The absolute coordinate of the split plane
    {
        FindRects(node.LeftChild, searchRect, intersectionRects);
    }

    // If the searchRect's boundary extends into the right bi-section of the node
    // we need to search the right sub-tree for intersections
    if (searchRect[axis].Max  // Max would be the Rect.Right if axis == 0
                              // Rect.Bottom if axis == 1
                > node.Plane) // The absolute coordinate of the split plane
    {
        FindRects(node.RightChild, searchRect, intersectionRects);
    }
}

该函数从伪代码转换后应该工作,但算法是正确的.这是一个log(n)搜索算法,可能是最慢的实现(从递归转换为基于堆栈).

– 更新 – 增加了一个简单的KD树构建算法

包含区域/体积形状的KD树的最简单形式如下:

Rect bounds = ...; // Calculate the bounding area of all shapes you want to 
              // store in the tree
int plane = 0; // Start by splitting on the x axis

BuildTree(_root, plane, bounds, insertRects);

function BuildTree(KDNode node, int plane, Rect nodeBds, List<Rect> insertRects)

if (insertRects.size() < THRESHOLD /* Stop splitting when there are less than some
                                      number of rects. Experiment with this, but 3
                                      is usually a decent number */)
{
     AddRectsToNode(node, insertRects);
     node.IsLeaf = true;
     return;
}

float splitPos = nodeBds[plane].Min + (nodeBds[plane].Max - nodeBds[plane].Min) / 2;

// Once you have a split plane calculated, you want to split the insertRects list
// into a list of rectangles that have area left of the split plane, and a list of
// rects that have area to the right of the split plane.
// If a rect overlaps the split plane, add it to both lists
List<Rect> leftRects, rightRects;
FillLists(insertRects, splitPos, plane, leftRects, rightRects); 

Rect leftBds, rightBds; // Split the nodeBds rect into 2 rects along the split plane

KDNode leftChild, rightChild; // Initialize these
// Build out the left sub-tree
BuildTree(leftChild, (plane + 1) % NUM_DIMS, // 2 for a 2d tree
          leftBds, leftRects);
// Build out the right sub-tree
BuildTree(rightChild, (plane + 1) % NUM_DIMS,
          rightBds, rightRects);

node.LeftChild = leftChild;
node.RightChild = rightChild;

这里有一些明显的优化,但是建立时间通常不如搜索时间那么重要.就是说,一个很好的构建树是什么使搜索快.如果你想学习如何构建一个快速的kd-tree,请查找SAH-KD-Tree.

翻译自:https://stackoverflow.com/questions/7727758/find-overlapping-rectangles-algorithm

转载注明原文:c – 找到重叠矩形算法