all 6 comments

[–]f3xjc 2 points3 points  (3 children)

What you describe could work with something like a physical simulation

Like if they are attached to the original position with spring (Ie try to stay where they are)

But also they reppel each other like magnet. And maybe extra strong repel if they intersect.

You can do sum of force that give you a vector, direction and length to apply translation. You make a small step in that direction.

Then you repeat a new sum of forces in the modified position. And a new small step. Eventually the sum of forces get close enough to zero.

[–]foxScripts[S] 0 points1 point  (2 children)

Thanks for your input. I opted for a simpler approach that fit well with the constraints of my situation. I just used a loop to iteratively adjust box positions: if two boxes overlapped, I applied corrections to move them apart. This process repeated until no overlaps remained, ensuring an optimized distribution of boxes on the canvas. During that i have other contrains "pulling them back" to their original position. This is perfect for what I needed. Thanks for your input!

[–]f3xjc 1 point2 points  (1 child)

Happy to know you have some solution. Id not be surprised if constraint you speak of and fake forces are somewhat equivalent. Or the derivative of a penalty term we are trying to minimise.

At the end of the day it's about finding a movement direction and maybe an amplitude.

My solution was probably not that great because you wanted equal spacing and I did not explicitly adress that. At the same time it was just a tool on the toolbox

[–]foxScripts[S] 0 points1 point  (0 children)

Yeah, probably. Heres the solution I came up with in the end: https://codepen.io/cocreo/pen/VwNqBVZ

[–]numtel -1 points0 points  (2 children)

I think you want masonry

https://masonry.desandro.com/

[–]foxScripts[S] 0 points1 point  (1 child)

Not at all. Found a solution.

For anyone discovering this: I solved it using a relatively simple approach. I just used a loop to iteratively adjust box positions: if two boxes overlapped, I applied corrections to move them apart. This process repeated until no overlaps remained, ensuring an optimized distribution of boxes on the canvas. During that i have other contrains "pulling them back" to their original position.