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

前端 未结 7 2262
轻奢々
轻奢々 2021-02-08 13:45

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 ne

相关标签:
7条回答
  • 2021-02-08 14:02

    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, ..., }.

    First you calculate the union area of rectangles in B, we get area_1 as the value.

    Then you calculate the union area of rectangles in B+ {a}, we get area_2 as the value.
    So if area_1 == area_2, then you are sure that rectangle a is covered by rectangles B. Otherwise, the answer is false.

    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.

    0 讨论(0)
  • 2021-02-08 14:06

    R-tree may be useful. if there might be rotated rectangles, you can enclose them in bounding rectangles.

    0 讨论(0)
  • 2021-02-08 14:12

    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

    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
    
    0 讨论(0)
  • 2021-02-08 14:19

    Try this

    Source Rectangle : X0, Y0, breadth, height

    // Basically comparing the edges

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

    0 讨论(0)
  • 2021-02-08 14:22

    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#

    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.

    0 讨论(0)
  • 2021-02-08 14:24

    here is my code, as you requested:

    the first method "subtracts" (returns uncovered parts) of 2 rectangles.

    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;
        }
    
    0 讨论(0)
提交回复
热议问题