0

I have a set of rectangle tiles, each of them with different shapes on them. One tile could for example contain the texture of a circle, another a rectangle, or maybe even a polygon.

These shapes do not fill up the whole tile, instead they are somewhere on the texture. One tile could for example just contain a small rectangle in the top-right corner. The other parts of the tiles are empty or transparent, i.e. these other pixels have an alpha value of 0.

Now I need to calcuate the "center of gravity" (CoG) within each tile. I know that is not be the best term to describe it, but I don't know of any better. With CoG in this context I mean the spot on the tile that is the center point of the shapes, i.e. those parts of the tiles that are not transparent.

For example, if the tile has one small rectangle in the top-right corner, then the CoG as I mean it would be in the center of the rectangle. In this case thus that CoG would not be in the center of the tile, but also somewhere in the top-right corner.

Important is that fact that the color of the shapes do not count. I am solely interested in the transparent vs. non-transparent pixels/areas on the tile.

Is there any "best practice" to calculate what I am looking for?

Matthias
  • 9,817
  • 14
  • 66
  • 125
  • The word that you are looking for is probably _centroid_ and there are questions and answers about that scattered all around the internet, it's a quite popular problem in math http://stackoverflow.com/questions/2792443/finding-the-centroid-of-a-polygon – user2485710 Aug 05 '14 at 20:01
  • The [minimum bounding box](http://en.wikipedia.org/wiki/Minimum_bounding_box) of the non-transparent pixels may be a useful way to quickly find the approximate center of your shape. Take the average of the top and bottom of the box, and the left and right of the box. This will fail for "weird" shapes, but works pretty well for things like rectangles and ovals. – Kevin Aug 05 '14 at 20:17
  • @Kevin: Thanks! One question though: what do you mean by "take the average of ..."? – Matthias Aug 05 '14 at 20:52
  • For a bounding box with top=15, bottom=16, left=23, right=42, the center-y coordinate is (15+16)/2 = 15.5. The center-x coordinate is (23+42)/2 = 32.5. – Kevin Aug 06 '14 at 11:52
  • Yes I understand. Your approach worked very well for me, thanks! – Matthias Aug 06 '14 at 21:19

0 Answers0