简体   繁体   English

确定矩形是否完全被另一组矩形覆盖所需的算法

[英]Algorithm required to determine if a rectangle is completely covered by another set of rectangles

I am searching for an algorithm that will determine if a new rectangle is completely covered by a set of existing rectangles. 我正在寻找一种算法,该算法将确定一个新矩形是否被一组现有矩形完全覆盖。 Another way of putting the question, is does the new rectangle exist completely with the area covered by the existing rectangles? 提出问题的另一种方法是,新矩形是否完全存在于现有矩形覆盖的区域?

There seem to be lots of algorithms to determine rectangle overlap and so on, but I can't really find anything that solves this exact problem. 似乎有很多算法来确定矩形重叠等等,但我找不到能解决这个问题的任何东西。

The rectangles will be represented using x, y coordinates. 矩形将使用x,y坐标表示。 This problem relates to geographical mapping. 该问题涉及地理绘图。

Edit - from comment posted by the OP: 编辑 - 来自OP发布的评论:

The rectangles are aligned on the X/Y axis 矩形在X / Y轴上对齐

If rectangles are aligned that's easy: 如果矩形对齐很容易:

Let's say you have rectangle A0 and want to know if it is fully covered by (B1, B2, B3...) = B 假设您有矩形A0并想知道它是否被(B1,B2,B3 ......)完全覆盖= B

A := (A0)
while P := pop B
  for R in A
    if P fully covers R:
      remove R from A
    else if P and R does overlap:
      remove R from A
      break R in subrentangles S := (S1, S2, S3,...) following the intersections \
                                                     with P edges
      push S into A
if A is empty:
   say B covers A0
else:
   say B does not fully cover A0

If the rectangles all have the same angle; 如果矩形都具有相同的角度; then the following might me more efficient and easier to program: 然后以下可能会更有效,更容易编程:

Determine for every y coordinate which rectangles cover that y coordinate (you only have to do this for y coordinates at which the covering changes;ie that correspond to the upper or lower limit of a rectangle). 确定矩形覆盖y坐标的每个y坐标(您只需对覆盖变化的y坐标执行此操作;即对应于矩形的上限或下限)。 Once you know that, solve the problem for each such y coordinate (ie check whether all x values are covered by the rectangles that are "active" for that Y coordinate). 一旦你知道了,解决每个这样的y坐标的问题(即检查所有x值是否被该Y坐标“活动”的矩形覆盖)。

Edit: I think this is O(n^2 log(n)^2) complexity, as two sorts are all the hard work you have to do 编辑:我认为这是O(n ^ 2 log(n)^ 2)的复杂性,因为两种类型都是你需要做的艰苦工作

I have done something similar in the past. 我过去做过类似的事。 the idea was to compare the new rectangle with each of the existing (one by one) 我的想法是将新矩形与现有的每个矩形进行比较(逐个)

if there is an intersection discard it (the intersected part), and add uncovered segments to a rectangle array 如果有一个交叉点丢弃它(相交的部分),并将未覆盖的段添加到矩形数组中

next, search for intersection between the new segments, and other existing (still unchecked) rectangles. 接下来,搜索新段与其他现有(仍未检查)矩形之间的交集。

do the algorithm recursively discarding the intersections, and leaving only the uncovered parts. 算法递归地丢弃交叉点,只留下未覆盖的部分。

in the end, if there is no rectangles in the array, you have a complete overlap 最后,如果数组中没有矩形,则表示完全重叠

if there are still some rectangles in the array, the overlapping is not full as there are still some parts left. 如果阵列中仍有一些矩形,则重叠不完整,因为仍有一些部分留下。

hope this helps 希望这可以帮助

I can try to find my code if this is what you are looking for. 如果这是您正在寻找的,我可以尝试找到我的代码。 I think its in C# 我认为它在C#

another idea is to convert all existing rectangles into a polygon, and then check if new rectangle is inside the polygon, but I would not recommend this if you aren't using a language (or framework) which knows how to work with polygons. 另一个想法是将所有现有的矩形转换为多边形,然后检查新的矩形是否在多边形内部,但如果您没有使用知道如何使用多边形的语言(或框架),我不建议这样做。

R-tree may be useful. R树可能很有用。 if there might be rotated rectangles, you can enclose them in bounding rectangles. 如果可能有旋转的矩形,则可以将它们包含在边界矩形中。

Try this 试试这个

Source Rectangle : X0, Y0, breadth, height 源矩形:X0,Y0,宽度,高度

// Basically comparing the edges //基本上比较边缘

if(((X0 >= xi) && (X0+breadth <= Xi)) && ((Y0 >= Yi)&&(Y0+height <= Yi)) { //consider the rectangle } else { // discard } if(((X0> = xi)&&(X0 + breadth <= Xi))&&((Y0> = Yi)&&(Y0 + height <= Yi)){//考虑矩形} else {// discard}

here is my code, as you requested: 这是我的代码,正如您所要求的:

the first method "subtracts" (returns uncovered parts) of 2 rectangles. 第一种方法是“减去”(返回未覆盖的部分)的2个矩形。

the second method subtracts a list of rectangles from the base rectangle. 第二种方法从基本矩形中减去矩形列表。

in your case list contains existing rectangles, and the new one is base 在您的案例列表中包含现有的矩形,新的矩形是基础

to check if there is a full intersection the list returned from the second method should have no elements. 检查是否存在完整的交集,从第二个方法返回的列表应该没有元素。

public static List<Rectangle> SubtractRectangles(Rectangle baseRect, Rectangle splitterRect)
    {
        List<Rectangle> newRectaglesList = new List<Rectangle>();

        Rectangle intersection = Rectangle.Intersect(baseRect, splitterRect);
        if (!intersection.IsEmpty)
        {
            Rectangle topRect = new Rectangle(baseRect.Left, baseRect.Top, baseRect.Width, (intersection.Top - baseRect.Top));
            Rectangle bottomRect = new Rectangle(baseRect.Left, intersection.Bottom, baseRect.Width, (baseRect.Bottom - intersection.Bottom));

            if ((topRect != intersection) && (topRect.Height != 0))
            {
                newRectaglesList.Add(topRect);
            }

            if ((bottomRect != intersection) && (bottomRect.Height != 0))
            {
                newRectaglesList.Add(bottomRect);
            }
        }
        else
        {
            newRectaglesList.Add(baseRect);
        }

        return newRectaglesList;
    }

    public static List<Rectangle> SubtractRectangles(Rectangle baseRect, List<Rectangle> splitterRectList)
    {
        List<Rectangle> fragmentsList = new List<Rectangle>();
        fragmentsList.Add(baseRect);

        foreach (Rectangle splitter in splitterRectList)
        {
            List<Rectangle> toAddList = new List<Rectangle>();

            foreach (Rectangle fragment in fragmentsList)
            {
                List<Rectangle> newFragmentsList = SubtractRectangles(fragment, splitter);
                toAddList.AddRange(newFragmentsList);
            }

            if (toAddList.Count != 0)
            {
                fragmentsList.Clear();
                fragmentsList.AddRange(toAddList);
            }
        }

        return fragmentsList;
    }

You can use the algorithm which is used to calculate the union area of rectangles. 您可以使用用于计算矩形的并集区域的算法。 As you want to check whether rectangle a is covered by rectangles B={b_1, b_2, ..., }. 如您要检查矩形a是否被矩形B = {b_1,b_2,...,}覆盖。

First you calculate the union area of rectangles in B, we get area_1 as the value. 首先计算B中矩形的联合区域,我们得到area_1作为值。

Then you calculate the union area of rectangles in B+ {a}, we get area_2 as the value. 然后你计算B + {a}中矩形的联合区域,我们得到area_2作为值。
So if area_1 == area_2, then you are sure that rectangle a is covered by rectangles B. Otherwise, the answer is false. 因此,如果area_1 == area_2,那么您确定矩形a被矩形B覆盖。否则,答案是错误的。

So the main problem is how to calculate union area of rectangles. 所以主要问题是如何计算矩形的联合面积。 This problem can be solved by existing excellent algorithm. 这个问题可以通过现有的优秀算法来解决。 This algorithm can be brief introduced as first to discretize value of points of rectangles, and then using Segmentation Tree to accelerate calculation of areas of each block. 该算法可以简要介绍为首先对矩形点的值进行离散化,然后使用分割树来加速每个块的面积计算。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 确定矩形是否被一组多边形完全覆盖所需的算法 - Algorithm required to determine if a rectangle is completely covered by a set of polygons 查找完全覆盖矩形集所需的最少固定大小矩形的算法 - Algorithm for finding the fewest fixed size rectangles required to completely cover set of rectangles 在另一个矩形内均匀分配矩形所需的算法 - Algorithm needed for distributing rectangles evenly within another rectangle 计算一组矩形和单个矩形之间差异的算法 - Algorithm to compute the difference between a set of rectangles and a single rectangle 算法问题:如何确定矩形是否在另一个矩形内? - Algorithm Questions: how to determine rectangle is inside the another rectangle? 确定矩形(x,y)坐标的算法,以便周围矩形的面积最小? - Algorithm to determine (x,y) coordinates for rectangles so the area of the surrounding rectangle is minimal? 在较大的矩形中拟合不同大小的矩形的算法 - Algorithm for fitting different sized rectangles in a larger rectangle 算法从坐标确定矩形 - algorithm to determine a rectangle from coordinates 我如何确定一个矩形是否可以被另一个矩形覆盖? - How do I find out if a rectangle can be covered by another rectangle? 从中心开始用较小的矩形填充大矩形的算法 - Algorithm to fill large rectangle with smaller rectangles starting from center
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM