# Take gravity of a polygon

• 01-27-2012, 10:17 AM
hqt
Take gravity of a polygon
I'm trying to use MouseMove Event to rotate a polygon. But, (with my algorithm), I need to know what gravity of an image. I don't know in Java has a function for my work or I must do by myself ?

If Java doesn't have this function. What your idea about this ? (take gravity of a polygon). My solution uses some Mathematical background. (sum of vectors).

thanks :)
• 01-27-2012, 10:22 AM
DarrylBurke
Re: Take gravity of a polygon
To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem. Indicate what the present program does, and what you would like it to do.

Just remember the first 'S' stands for short.

db
• 01-27-2012, 10:59 AM
hqt
Re: Take gravity of a polygon
Oh, sorry for my poor English :(
Maybe the true title for my problem is: "which Java function return centre of Gravity of a Polygon"
• 01-27-2012, 11:44 AM
pbrockway2
Re: Take gravity of a polygon
There is no method in the Polygon class (that I know of). But the centroid can be calculated from the coordinates of each of the vertices. See, eg, Centroid - Wikipedia, the free encyclopedia.
• 01-27-2012, 11:59 AM
pbrockway2
Re: Take gravity of a polygon
Notice that the centroid of a polygon is *not* the mean of it vertices considered as vectors. You can see this by considering a square whose centroid is its center. Now form a polygon by adding lots of points all very near the top edge of the square. The mean value of the vertices will now be very near the top edge although the polygon is still more or less a square and its centroid shouldn't have changed.
• 01-27-2012, 12:05 PM
pbrockway2
Re: Take gravity of a polygon
Quote:

the object now isn't a polygon, it's an image
I love those sliding requirements!

Now you *can* take the mean position of all the pixels in the image. I'm no expert but I don't see that any speed up is possible in general - only if there is some symmetry to the image.

 No that's wrong. You don't need symmetry you just need to approximate the image area with simple geometries like straight lines. Eg approximate it with a polygon and proceed as before.