17/11/2009

Where is the center of a polygon?

So you want to know the center point of a polygon, but you can't find an attribute that stores it...Nor can you find a MEL command that will give you this information.

Which all means we're going to have to write our own script to calculate the center point of a polygon.

First up, let's decide what we mean when we say the 'center' of a polygon. You (might) remember from school plenty of equations for regular polygons, and there's loads to be said about triangles...

But I could recall nothing about finding the center of an irregular polygon, so I was somewhat reassured when I read this:

"Irregular polygons are not considered as having a center, since there is unlikely to be any one point
equally distant form each vertex. However, an irregular polygon can have a centroid, or center of gravity."

I checked the equation of a polygon centroid, and then decided it was overkill. What's more, the centroid is not what determines the position of that little blue dot....

1) Find the average position of the vertices:

Here's the simplest and crudest way to find the 'center' of a polygon: add the vertex positions together and then divide by the number of points. For many polygons, this will give you a point somewhere near the center.....

// this script assumes that the first item // on the selection list is a polyface. // you need a locator called locator1 // for the last line to work