Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arrange labels for objects in 2D space without overlap

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

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

They should still be as close to the objects as possible and should not, provided that the objects themselves move smoothly, show any suddent jerky movement.

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

like image 886
Tomas Andrle Avatar asked Sep 18 '25 19:09

Tomas Andrle


1 Answers

I would suggest using physics. Attach the label to the object with a spring and apply a repelling force to every label so they avoid getting close to other objects (and their labels), excluding the one they describe.

like image 181
macbirdie Avatar answered Sep 20 '25 13:09

macbirdie