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++; } } }
public static bool Equals(Rectangle a, Rectangle b) { if (!(a.getCoord().SequenceEqual(b.getCoord()))) { return false; } return (a.invalid == b.invalid) ? true : false; }
public static Rectangle intersection(Rectangle a, Rectangle b) { //Falls a oder b invalid gebe einen invalid rectangle zurück if((a.getInvalid() || b.getInvalid())) { Rectangle d = new Rectangle(0, -1, 0, 0); return d; } // get intersection x_Axis: int minX = Math.Max(a.getCoord()[0], b.getCoord()[0]); int maxX = Math.Min(a.getCoord()[1], b.getCoord()[1]); // get Intersection y-Axis: int minY = Math.Max(a.getCoord()[2], b.getCoord()[2]); int maxY = Math.Min(a.getCoord()[3], b.getCoord()[3]); //Falls intersection keinen sinn macht, z.B. minX>maxX, dann setzt konstruktor invalid auf true; Rectangle c = new Rectangle(minX, maxX, minY, maxY); return c; }
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; }