Polygon Centroid Example - Find the Center of a Polygon — Gideros Forum

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# Polygon Centroid Example - Find the Center of a Polygon

Member
edited June 2012
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. 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
Tagged:
+1 -1 (+4 / -0 )Share on Facebook

• Member
Thanks for sharing

Welcome to the forum :-h

cheers

evs
• Maintainer
Welcome, Teranth - hope you'll love Gideros Studio, as we all do here
• Member
Thanks guys, so far I am really loving the language and environment Gideros provides. I should have a few releases out later this month to really test the waters.

P.S. (The forums are really awesome as well I love the fact I am hearing posts from the devs all the time and answers to questions, it's a great feeling for the SDK)

Likes: gorkem

ThumbHurt Games / FB: ThumbHurt Games / FB: Eli/Teranth | Skype: teranth37
+1 -1 (+1 / -0 )Share on Facebook
• Guru
Useful function - thanks
WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
#MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
• Guru
@Teranth
welcome here !
and thanks for sharing these useful functions!

ciao ciao
Gianluca.
TNT ENGiNE for Gideors Studio - Particle Engine, Virtual Pad, Animator Studio, Collision Engine - DOWNLOAD NOW !!! IT'S FREE!!! -
www.tntengine.com
• Member
Bookmarked! I will need this someday..
• Member
Thanks guys, I'm really liking this community

Likes: gorkem

ThumbHurt Games / FB: ThumbHurt Games / FB: Eli/Teranth | Skype: teranth37
+1 -1 (+1 / -0 )Share on Facebook