private static Rectangle[] GetLShapeInformation(Rectangle first, Rectangle second, RectangleSide side, RectangleSide extendDirection) { Rectangle smaller; Rectangle bigger; if (side.IsVertical() && first.width < second.width || side.IsHorizontal() && first.height < second.height) { smaller = first; bigger = second; } else { smaller = second; bigger = first; side = side.OppositeDirection(); } if (side.IsVertical()) { int movedXPos = extendDirection == RectangleSide.Right ? smaller.width : 0; int movedYPos = side == RectangleSide.Bottom ? bigger.height : 0; Rectangle newSmaller = new Rectangle(smaller.width, smaller.height + bigger.height, smaller.x, smaller.y - movedYPos); Rectangle newBigger = new Rectangle(bigger.width - smaller.width, bigger.height, bigger.x + movedXPos, bigger.y); return(new Rectangle[] { newSmaller, newBigger }); } else { int movedXPos = side == RectangleSide.Left ? bigger.width : 0; int movedYPos = extendDirection == RectangleSide.Top ? smaller.height : 0; Rectangle newSmaller = new Rectangle(smaller.width + bigger.width, smaller.height, smaller.x - movedXPos, smaller.y); Rectangle newBigger = new Rectangle(bigger.width, bigger.height - smaller.height, bigger.x, bigger.y + movedYPos); return(new Rectangle[] { newSmaller, newBigger }); } }
private static bool IsLSplitWorthIt(Rectangle first, Rectangle second, RectangleSide side) { int horizontalSegment; int verticalSegment; bool isAlreadyOptimallySplit; //Check if splitting even improves the rectangles if (side.IsVertical()) { if (first.width < second.width) { horizontalSegment = first.width; verticalSegment = second.height; } else { horizontalSegment = second.width; verticalSegment = first.height; } isAlreadyOptimallySplit = Rectangle.ShouldSplitAtHorizontalLineSegment(verticalSegment, horizontalSegment); } else if (side.IsHorizontal()) { if (first.height < second.height) { horizontalSegment = second.width; verticalSegment = first.height; } else { horizontalSegment = first.width; verticalSegment = second.height; } isAlreadyOptimallySplit = !Rectangle.ShouldSplitAtHorizontalLineSegment(verticalSegment, horizontalSegment); } else { throw new InternalRuntimeException("Logic error."); } bool doesSplitMatter = verticalSegment != horizontalSegment; return(doesSplitMatter && !isAlreadyOptimallySplit); }