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

前端 未结 7 1067
时光说笑
时光说笑 2021-02-08 13:49

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:15

    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;
        }
    

提交回复
热议问题