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