Page 1 of 1

Finding the visual center of a polygon

Posted: Sun Feb 18, 2024 6:00 pm
by whatevers
Need to find a lua algorithm to find the visual cetner of a polygon for my game
Is there a way to implement this in lua? Mabye a library for this?

Image

Re: Finding the visual center of a polygon

Posted: Sun Feb 18, 2024 7:30 pm
by dusoft
Some other languages such as R have libraries supporting this.

AFAIK there is nothing for LOVE / Lua, so in the meantime you can read on how such algorithm works and you can potentially program it yourself:
https://blog.mapbox.com/a-new-algorithm ... 77e6492fbc

Or just port it from JS:
https://github.com/mapbox/polylabel

Re: Finding the visual center of a polygon

Posted: Sun Feb 18, 2024 8:58 pm
by RNavega
Your image doesn't look like a polygon, but like something painted / raster data.
Is your input shape polygonal, or is it a bitmap, or something else? Is it supposed to be animated? Is the center always in the same position, or do you need to calculate the nearest center towards some arbitrary point?

Without knowing how it's supposed to work, it's difficult to suggest things.

Because it could be something as simple as casting a ray and getting the average between the "enter point" and "exit point":
temp.jpg
temp.jpg (29.44 KiB) Viewed 811 times

Re: Finding the visual center of a polygon

Posted: Mon Feb 19, 2024 11:48 am
by Bobble68
There might be algorithms to achieve this kind of thing, but I'm not sure how they could work, since where the centre is can be subjective if you aren't using centre of mass or something similar (e.g. a perfectly round ring would have an infinite numer of places it can be)

I guess the real question is what do you need it for? It's possible that there might be better ways to get what you want.