Reputation: 388
I'm seeking for an algorithm to layout rectangle windows, the requirements are like below:
A global constraint (max_x_offset, max_y_offset) is given so that the relocated new position of each window (new_x, new_y) satisfied the constraint:
abs(new_x - x) <= max_x_offset and abs(new_y - y) <= max_y_offset
The global constraint is a hard constraint, which means if there is no such layout can satisfy both 4 and 5, we must satisfy the global constraint and let some window overlap.
I searched google and wikipedia and some research papers, but still failed to find a suitable algorithm for this task. Any suggestions? Thanks!
Update: Yes I understand this is a 2-D knapsack problem and it is NP-hard. What I want is a fast algorithm to get a good-enough result.
Upvotes: 0
Views: 1311
Reputation: 181785
You could create a physics-like model, in which windows repel each other, with a force that depends on the distance between them. In each time step, enforce your absolute position constraint. If you don't find an overlap-free solution within a certain number of time steps, abort the algorithm and give the candidate solution found at this point.
Of course, this won't always find a solution if one exists. But I think, in general, that is very hard or even impossible to do efficiently.
Upvotes: 2