Calculate the size of windows tiling

531 views Asked by At

I have a number of windows and I'd like to tile them to cover the entire workingarea of the screen. If there are less windows, the individual windows are bigger. The windows are almost squares -- an example is 800x585. They always scale with a fixed ratio.

enter image description here

In this example I only have 4 windows, so my calculation should figure out that filling the screen is done with a 2x2 grid.

enter image description here

In this example I have 8 windows, but instead of 4 cols x 2 rows (which would leave a huge gap underneath the 2nd row because of the fixed ratio) the windows are divided in 3x3 with one empty spot.

The basic idea is to leave as little uncovered screen space as possible. I'm trying to do this in AutoIt, but if someone can explain this in C# or Python I am equally happy :)

1

There are 1 answers

0
ben rudgers On

A brute force algorithm in pseudo-code:


Begin:

Let n be the number of windows.

Find s such that:

  • squareroot s is a positive integer
  • s >= n

Let wasted-area = the actual wasted area in the square grid of s slots.

Let x = square root s

Let y = square root s

For each (i, j) where:

  • i and j are positive integers

  • i * j = n --------------> i and j are factors of n

Let a = the actual wasted area of the rectangular grid (i, j)

When a < wasted-area then

  • set x to i
  • set y to j
  • set wasted-area to a

Next (i, j)

Tile screen with (x, y)

End


Note: that if some assumptions can be made about he ratio of the window and the ratio of the screen, then some pairs of factors can be excluded. If no assumptions can be made, then brute force is as good as I can do. Someone with a stronger math background might do better.

Keeping in mind that on a real computer n may seldom be large in an absolute sense, brute force probably is acceptable for many situations.