Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Polygon Centroid Example - Find the Center of a Polygon — Gideros Forum

Polygon Centroid Example - Find the Center of a Polygon

TeranthTeranth Member
edited June 2012 in Code snippets
Hey guys, this is my first post on the forums I usually just lurk around heh. Anyway this is an awesome group of people from what I can tell and I intend to make a lot more use of Gideros very soon with some Android apps. Since so many awesome people are sharing their code and libraries I wanted to do the same for you guys.

I hope someone finds this useful for something I haven't thought of yet.

One of my apps needed to place labels on the polygon's it is using so I wrote some functions to find the center--centroid --of any polygon they are given based on math formulas. I am guessing some of you guys could make some use of these types of functions, so here they are.

To make it work you will need to have your polygon points in a table with x, y values as pairs like the example below, or you can modify the functions to fit your data needs:
local poly = {
    {x=0, y=100},
    {x=100, y=50},
    {x=100, y=100}
-- Find the center points for this polygon.
local cent = findCentroid(poly)
-- Output the centroid points in case someone wants to check them. <img class="emoji" src="http://forum.giderosmobile.com/resources/emoji/smile.png" title=":)" alt=":)" height="20" />
print("Center X: "..cent.x, "Center Y: "..cent.y)
A much larger example is provided in the attached code as well. Enjoy :)
ThumbHurt Games / FB: ThumbHurt Games / FB: Eli/Teranth | Skype: teranth37
+1 -1 (+4 / -0 )Share on Facebook


Sign In or Register to comment.