Algorithm for dividing a rectangle into n smaller rectangles and calculating each center

I am looking for a simple algorithm that, given a rectangle with a width of w and a height of h, breaks the rectangle into n more or less equal sizes and rectangles of the form and calculates the center of these rectangles.

EDIT: Forgot to mention that the shapes should be as square as possible.

Any tips on how to get started?

+2
source share
1 answer

A simple algorithm is to split vertically into n bands of equal size h and width w / n.

If you assume that the original rectangle has angles (0,0) and (w, h), then using this algorithm, the rectangle i th would have the center (w / n * (i + ½), h / 2), for 0 <= i <n.


Update: try to find all factorizations of the number n into pairs of factors (i, j), such that I * j = n, and find pairs of factors so that the ratio of factors is closest to the ratio of the sides of the rectangle. Then use two factors to create a regular grid of smaller rectangles.

For example, when n is 10, you can choose between (1, 10), (2, 5), (5, 2) and (10, 1). The following is an example of a grid using coefficients (5, 2):

  ------------------------------------
 |  |  |  |  |  |
 |  |  |  |  |  |
 ------------------------------------
 |  |  |  |  |  |
 |  |  |  |  |  |
 ------------------------------------

If your initial rectangle has a width of 60 and a height of 20, then using a pair of factors (5, 2) will give ten rectangles of size (60/5, 20/2) = (12, 10) that are close to the square.

+8
source

Source: https://habr.com/ru/post/885505/


All Articles