Arrange labels for objects in 2D space without overlapping

I need to attach text labels to objects that are randomly distributed across the screen and keep moving.

By default, the ideal position for the label is on the right side of the object to which it refers. I need a way to dynamically rearrange (or possibly combine) labels so that they never overlap each other or other objects.

All the same, they should be as close as possible to the objects and should not, provided that the objects themselves move smoothly, show any disgusting jerky movement.

I have no idea how to do this, is there an algorithm for something like that?

+3
source share
3 answers

I solved it using pretty brute force

for each object as a
    for each colliding object as c
        if should_swallow(a, c)
            swallow(a, c)

and do it several times in the loop because the objects grow when they swallow other objects, so the collision check must be done again (new overlaps may appear in the middle of the loop). Never press more than three cycles, but with the number of objects that I work with.

The should_swallow () function determines whether it is better to swallow or absorb objects, depending on size, position, and what happened in previous frames to prevent flickering.

0
source

. spring , ( ), , .

+3

: , , . .

: .

: , , . - , . . , , , - , .

0

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


All Articles