# Thread: Take gravity of a polygon

1. Member
Join Date
Nov 2011
Posts
27
Rep Power
0

## 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 :)

2. ## 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

3. Member
Join Date
Nov 2011
Posts
27
Rep Power
0

## 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"

4. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
14

## 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.

5. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
14

## 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.

6. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
14

## Re: Take gravity of a polygon

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.
Last edited by pbrockway2; 01-27-2012 at 12:12 PM.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•