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

前端 未结 7 2256
轻奢々
轻奢々 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: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 SubtractRectangles(Rectangle baseRect, Rectangle splitterRect)
        {
            List newRectaglesList = new List();
    
            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 SubtractRectangles(Rectangle baseRect, List splitterRectList)
        {
            List fragmentsList = new List();
            fragmentsList.Add(baseRect);
    
            foreach (Rectangle splitter in splitterRectList)
            {
                List toAddList = new List();
    
                foreach (Rectangle fragment in fragmentsList)
                {
                    List newFragmentsList = SubtractRectangles(fragment, splitter);
                    toAddList.AddRange(newFragmentsList);
                }
    
                if (toAddList.Count != 0)
                {
                    fragmentsList.Clear();
                    fragmentsList.AddRange(toAddList);
                }
            }
    
            return fragmentsList;
        }
    

提交回复
热议问题