Пример #1
0
 public Window(int OffsetX, int OffsetY)
 {
     this.region  = new Background(0, 0, 0, 0);
     this.In      = new ListOperations();
     this.OffsetX = OffsetX;
     this.OffsetY = OffsetY;
 }
Пример #2
0
 public Window()
 {
     this.region  = new Background(0, 0, 0, 0);
     this.In      = new ListOperations();
     this.OffsetX = 0;
     this.OffsetY = 0;
 }
Пример #3
0
        public void XorRectRegion(Rectangle newRectangle)
        {
            //Step 0) Resize rectangle. Anything outside regionRectangle does not interest us as it will not be added to In
            int backMinX = this.region.getCoord()[0];
            int backMaxX = this.region.getCoord()[1];
            int backMinY = this.region.getCoord()[2];
            int backMaxY = this.region.getCoord()[3];

            Rectangle back       = new Rectangle(backMinX, backMaxX, backMinY, backMaxY);
            Rectangle Rintersect = Rectangle.intersection(back, newRectangle);

            if (!Rintersect.getInvalid())
            {
                // Step 1) Find Intersection with Rectangles
                ListOperations intersect = ListOperations.regionListIntersect(this.In, newRectangle);

                // Step 2) Add rectangles except for the intersection
                this.In = addNotInIntersect(intersect, newRectangle);

                // Step 3) do the offset thing
                this.OffsetRegion();
            }
            else
            {
                this.OrRectRegion(newRectangle);
            }

            this.region.RegionRect(In);
        }
Пример #4
0
        private void CutOrIn(ListOperations In, Rectangle newRectangle, bool simplify)
        {
            int minX = newRectangle.getCoord()[0];
            int maxX = newRectangle.getCoord()[1];
            int minY = newRectangle.getCoord()[2];
            int maxY = newRectangle.getCoord()[3];

            int       ctrX        = 0;
            Rectangle start       = new Rectangle(minX, minX, minY, minY);
            bool      isIntersect = ListOperations.boolListIntersect(In, start);

            for (int y = minY; y <= maxY; y++)
            {
                ctrX = 0;
                for (int x = minX; x <= maxX; x++)
                {
                    Rectangle r = new Rectangle(x, x, y, y);
                    if (x == minX)
                    {
                        isIntersect = ListOperations.boolListIntersect(In, r);
                    }
                    if (ListOperations.boolListIntersect(In, r))
                    {
                        //We were in open space
                        if (!isIntersect)
                        {
                            Rectangle toAdd = new Rectangle(x - ctrX, x - 1, y, y);
                            In.validAdd(toAdd, simplify);
                            ctrX = -1;
                        }
                        //Else: We used to intersect and we are intersecting now too
                        isIntersect = true;
                    }
                    else
                    {
                        //We are not intersecting and x == maxX. And we didn't intersect.
                        if ((x == maxX) && (!isIntersect))
                        {
                            Rectangle endAdd = new Rectangle(x - ctrX, x, y, y);
                            In.validAdd(endAdd, simplify);
                            ctrX = -1;
                        }
                        //We are not intersecting and x == maxX. But we used to intersect
                        if ((x == maxX) && (isIntersect))
                        {
                            Rectangle endAddSquare = new Rectangle(x, x, y, y);
                            In.validAdd(endAddSquare, simplify);
                            ctrX = -1;
                        }
                        if (isIntersect)
                        {
                            ctrX = 0;
                        }
                        isIntersect = false;
                    }
                    ctrX++;
                }
            }
        }
Пример #5
0
 public static bool boolListIntersect(ListOperations list, Rectangle intersect)
 {
     for (int c = list.Count() - 1; c >= 0; c--)
     {
         if (!(Rectangle.intersection(list[c], intersect).getInvalid()))
         {
             return(true);
         }
     }
     return(false);
 }
Пример #6
0
        //returns \cup_{rectangle in list}(rectanlge \cap intersect)
        public static ListOperations regionListIntersect(ListOperations rectList, Rectangle intersect)
        {
            ListOperations result = new ListOperations();

            foreach (Rectangle item in rectList)
            {
                result.Add(Rectangle.intersection(item, intersect));
                if (result[result.Count() - 1].getInvalid())
                {
                    result.RemoveAt(result.Count() - 1);
                }
            }
            return(result);
        }
Пример #7
0
        public void AndRectRegion(Rectangle newRectangle)
        {
            ListOperations newIn = new ListOperations();
            Rectangle      a;

            for (int c = In.Count() - 1; c >= 0; c--)
            {
                a = Rectangle.intersection(this.In[c], newRectangle);
                this.CutOrIn(newIn, newRectangle, true);
            }
            this.OffsetRegion();
            this.region.RegionRect(newIn);
            this.In = newIn;
        }
Пример #8
0
        private ListOperations addNotInIntersect(ListOperations intersect, Rectangle newRectangle)
        {
            ListOperations result = new ListOperations();
            bool           added  = false;

            // Step 1) Add In. All rectangles inside In are disjoint.
            foreach (Rectangle item in this.In)
            {
                added = false;
                for (int c = intersect.Count() - 1; c >= 0; c--)
                {
                    //letztes Argument ist das XOR=true (die vereinigungsart);
                    if (!Rectangle.intersection(intersect[c], item).getInvalid() && !added)
                    {
                        result.AddRange(Rectangle.AddIntoRectangle(intersect[c], item, true));
                        added = true;
                    }
                    if ((c == 0) && (!added))
                    {
                        result.AddRange(Rectangle.AddIntoRectangle(intersect[c], item, true));
                    }
                }
            }

            // Step 2 add XOR to everywhere except for the intersections
            Window w = new Window();

            w.In = intersect;
            int length = w.In.Count();

            w.CutOrIn(intersect, newRectangle, false);
            w.In.RemoveRange(0, length);

            for (int i = w.In.Count() - 1; i >= 0; i--)
            {
                if (i > w.In.Count() - 1)
                {
                    i = w.In.Count() - 1;
                }
                w.In = w.In.simplifyByX(w.In, i);
                if (i > w.In.Count() - 1)
                {
                    i = w.In.Count() - 1;
                }
                w.In = w.In.simplifyByY(w.In, i);
            }
            result.AddRange(w.In);
            return(result);
        }
Пример #9
0
        //See how index can be simplified with other rectangles
        public ListOperations simplifyByY(ListOperations List, int index)
        {
            for (int i = List.Count() - 1; i >= 0; i--)
            {
                if (i != index) //Wir können nicht ein Rectangle mit sich selber zusammenfassen
                {
                    if ((List[i].getCoord()[2] == List[index].getCoord()[2]) && (List[i].getCoord()[3] == List[index].getCoord()[3]))
                    {
                        if ((Math.Abs(List[i].getCoord()[0] - List[index].getCoord()[1]) == 1) || (Math.Abs(List[i].getCoord()[1] - List[index].getCoord()[0]) == 1))
                        {
                            int minY = List[i].getCoord()[2];
                            int maxY = List[i].getCoord()[3];

                            int minXList  = List[i].getCoord()[0];
                            int minXIndex = List[index].getCoord()[0];
                            int minX      = Math.Min(minXList, minXIndex);

                            int maxXList  = List[i].getCoord()[1];
                            int maxXIndex = List[index].getCoord()[1];
                            int maxX      = Math.Max(maxXList, maxXIndex);

                            Rectangle simplified = new Rectangle(minX, maxX, minY, maxY);

                            if (i > index)
                            {
                                List.RemoveAt(i);
                                List.RemoveAt(index);
                            }
                            else
                            {
                                List.RemoveAt(index);
                                List.RemoveAt(i);
                            }

                            List.Insert(Math.Max(0, List.Count()), simplified);
                            index = Math.Max(0, List.Count() - 1);
                        }
                    }
                }
            }

            return(List);
        }
Пример #10
0
        public void RegionRect(ListOperations In)
        {
            if (In.Count != 0)
            {
                // Our regionRectangle must at least be as big as that
                this.minX = In[0].getCoord()[0];
                this.maxX = In[0].getCoord()[1];
                this.minY = In[0].getCoord()[2];
                this.maxY = In[0].getCoord()[3];

                foreach (Rectangle item in In)
                {
                    //minX
                    if (item.getCoord()[0] < this.minX)
                    {
                        this.minX = item.getCoord()[0];
                    }
                    // maxX
                    if (item.getCoord()[1] > this.maxX)
                    {
                        this.maxX = item.getCoord()[1];
                    }
                    // minY
                    if (item.getCoord()[2] < this.minY)
                    {
                        this.minY = item.getCoord()[2];
                    }
                    // maxY
                    if (item.getCoord()[3] > this.maxY)
                    {
                        this.maxY = item.getCoord()[3];
                    }
                }
            }
            else
            {
                this.minX = 0;
                this.maxX = 0;
                this.minY = 0;
                this.maxY = 0;
            }
        }
Пример #11
0
        public static ListOperations AddIntoRectangle(Rectangle old, Rectangle add, bool XOR)
        {
            ListOperations aList = new ListOperations();

            int oldMinX = old.getCoord()[0];
            int oldMaxX = old.getCoord()[1];
            int oldMinY = old.getCoord()[2];
            int oldMaxY = old.getCoord()[3];

            int addMinX = add.getCoord()[0];
            int addMaxX = add.getCoord()[1];
            int addMinY = add.getCoord()[2];
            int addMaxY = add.getCoord()[3];

            if (subset(old,add))
            {
                if (XOR)
                {
                    Rectangle upper = new Rectangle(oldMinX, oldMaxX, oldMaxY + 1, addMaxY);
                    Rectangle lower = new Rectangle(oldMinX, oldMaxX, addMinY, oldMinY - 1);
                    Rectangle left = new Rectangle(addMinX, oldMinX - 1, addMinY, addMaxY);
                    Rectangle right = new Rectangle(oldMaxX + 1, addMaxX, addMinY, addMaxY);
                    aList.validAdd(upper, true);
                    aList.validAdd(lower, true);
                    aList.validAdd(left, true);
                    aList.validAdd(right, true);
                    return aList;
                }
                aList.Add(add);
                return aList;
            }
            if(subset(add, old))
            {
                if (XOR)
                {
                    Rectangle upper = new Rectangle(oldMinX, oldMaxX, oldMaxY + 1, addMaxY);
                    Rectangle lower = new Rectangle(oldMinX, oldMaxX, addMinY, oldMinY - 1);
                    Rectangle left = new Rectangle(addMinX, oldMinX - 1, addMinY, addMaxY);
                    Rectangle right = new Rectangle(oldMaxX + 1, addMaxX, addMinY, addMaxY);
                    aList.validAdd(upper, true);
                    aList.validAdd(lower, true);
                    aList.validAdd(left, true);
                    aList.validAdd(right, true);
                    return aList;
                }
                return aList;
            }
            if((Rectangle.intersection(old,add).getInvalid()))
            {
                aList.Add(add);
                return aList;
            }

            //new Rectangle between old bounds
            if ((oldMinX <= addMinX) &&  (addMaxX <= oldMaxX))
            {
                Rectangle lower = new Rectangle(addMinX, addMaxX, addMinY, oldMinY-1);
                Rectangle upper = new Rectangle(addMinX, addMaxX, oldMaxY+1, addMaxY);
                aList.validAdd(lower, true);
                aList.validAdd(upper, true);
                return aList;
            }

            //new Rectangle exceeds old bounds
            if ((addMinX <= oldMinX) && (oldMaxX <= addMaxX))
            {
                Rectangle upper = new Rectangle(oldMinX, oldMaxX, oldMaxY+1, addMaxY);
                Rectangle lower = new Rectangle(oldMinX, oldMaxX, addMinY, oldMinY-1);
                Rectangle left = new Rectangle(addMinX, oldMinX-1, addMinY, addMaxY);
                Rectangle right = new Rectangle(oldMaxX+1, addMaxX, addMinY, addMaxY);
                aList.validAdd(upper, true);
                aList.validAdd(lower, true);
                aList.validAdd(left, true);
                aList.validAdd(right, true);
                return aList;
            }

            //new Rectangle left of old bounds
            if ((oldMinX <= addMaxX) && (addMaxX <= oldMaxX))
            {
                Rectangle upper = new Rectangle(oldMinX, addMaxX, oldMaxY+1, addMaxY);
                Rectangle lower = new Rectangle(oldMinX, addMaxX, addMinY, oldMinY-1);
                Rectangle left = new Rectangle(addMinX, oldMinX-1, addMinY, addMaxY);
                aList.validAdd(upper, true);
                aList.validAdd(lower, true);
                aList.validAdd(left, true);
                return aList;
            }

            //new Rectangle right of old bounds
            if ((addMinX <= oldMaxX) && (oldMaxX <= addMaxX))
            {
                Rectangle upper = new Rectangle(addMinX, oldMaxX, oldMaxY+1, addMaxY);
                Rectangle lower = new Rectangle(addMinX, oldMaxX, addMinY, oldMinY-1);
                Rectangle right = new Rectangle(oldMaxX+1, addMaxX, addMinY, addMaxY);
                aList.validAdd(upper, true);
                aList.validAdd(lower, true);
                aList.validAdd(right, true);
                return aList;
            }
            return aList;
        }