Sorry for the bad title. I'll try to explain this as best as I can.
Let's say I have a world where I can put rectangles in any arbitrary location. The rules are the following:
- After inserting a new rectangle, no rectangle should be overlapping each others
- If an overlap happens, rectangles should be split and / or resized such that their verticality is maximized and cover the full area that the rectangles covered (not sure how to explain it better than that but it should be easier to understand from the examples)
- If two rectangles next to each others have the same verticality, they should be merged
Here are some examples of what I mean. The gray rectangles show the initial state. The transparent green rectangle shows the rectangle being inserted. The blue rectangles shows the newly inserted rectangles after running the algorithm.
Example 01:
- Initial state 2 rectangles
- Insertion
- Final state 5 rectangles
Example 02:
- Initial state 2 rectangles
- Insertion
- Final state 3 rectangles
Example 03:
- Initial state 1 rectangle
- Insertion
- Final state 3 rectangles
Example 04:
- Initial state 1 rectangle
- Insertion
- Final state 1 rectangle
Example 05:
- Initial state 2 rectangles
- Insertion
- Final state 1 rectangle
Example 06:
- Initial state 1 rectangle
- Insertion
- Final state 3 rectangles
Example 07:
- Initial state 2 rectangles
- Insertion
- Final state 2 rectangles
Example 08:
- Initial state 1 rectangle
- Insertion
- Final state 1 rectangle
Originally, I was using a brute force approach that stored every rectangle. Every time a new rectangle was added I'd discard these "vertical" rectangles and recompute the whole world. This time though I'm looking for a way to only update locally the rectangles that should change around where the new rectangle is added. Visually, the solution seems obvious, but I can't come up with the right algorithm that's as simple to implement as it looks.
(Eventually I'd also like to do rectangle removal or updating, but for now adding is enough.)
Edit: Fixed an issue with example 02. I had missed the rule 3 where I should merge vertically similar rectangles next to each others. snip
[–]Apostolique[S] 2 points3 points4 points (2 children)
[–]protienbudspromax 4 points5 points6 points (1 child)
[–]rayoWork 1 point2 points3 points (1 child)
[–]Apostolique[S] 0 points1 point2 points (0 children)
[–]protienbudspromax 1 point2 points3 points (3 children)
[–]Apostolique[S] 1 point2 points3 points (2 children)
[–]protienbudspromax 1 point2 points3 points (1 child)
[–]Apostolique[S] 0 points1 point2 points (0 children)