L
L
Lopus2020-10-01 10:36:43
Algorithms
Lopus, 2020-10-01 10:36:43

Algorithm for automatic distribution of objects by region?

A rectangular area of ​​a certain width and height is given. For example, 300x200 cells. In code, this is a two-dimensional array map[x][y].
Several objects with a smaller area are also given. 5x3, 2x2, 10x7, etc.
You need to place them in a random place on the rectangle. From the series table, chair, wardrobe in the room.
Tell me, what algorithms are needed for this?
Any one that is easy to understand will do. Even if it is slow in terms of optimization and clumsy.
I want to begin to understand the idea of ​​​​implementation itself.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dmshar, 2020-10-01
@dmshar

Everything is elementary.
Cycle within a cycle.
The outer loop generates an arbitrary point on the plane (well, for example, generating a random number in the range of your plane's dimensions in terms of width and height)
Try to place the center of the next object at this point.
It turned out - excellent, we continue the outer loop.
It did not work out (due to the fact that they either went beyond the plane, or overlapped with a previously placed figure) - there are two options for you to choose from. Either a shift of the current object (and again a check of the possibility), or we return to the point of random generation of the position.
And so on, until you sort through all the objects.
Failed to place all objects - go back a few objects (or all) and repeat the main process.
This is the most trivial algorithm. You will deal with it - then it will be possible to move on to all kinds of optimizations - starting with the fact that objects for placement would be taken from the largest to the smallest.

X
xmoonlight, 2020-10-01
@xmoonlight

The simplest algorithm:
1. In a circle, starting from the corners, from the maximum area to the minimum.
2. When a "pit" appears, check for its tiling with a figure with the same area: yes - tiling with it, no - go to the next figure in step 1 with a smaller area.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question