UVa 10002

From Algorithmist

Jump to: navigation, search

Contents

[edit] 10002 - Center of Masses

[edit] Summary

This problem is about finding the position of the centroid of a convex polygon.

[edit] Explanation

Calculating the position of the centroid of a convex polygon is a basic algorithm in computational geometry. Let {(x0,y0),(x1,y1),...,(xN - 1,yN - 1)} be the set of N vertices of a convex polygon P, where

  • the Y coordinates of all vertices are nonnegative;
  • (x0,y0) is the leftmost vertex;
  • The vertices are ordered clockwise.

We takes (xN,yN) = (x0,y0). Then the area of P is \frac{1}{2} \sum_{i=0}^{N-1} ((x_{i+1} - x_i) (y_i + y_{i+1})), or (in the simplified form) \frac{1}{2} \sum_{i=0}^{N-1} (x_i y_{i+1} - x_{i+1} y_i). Let A be the area of P, then the coordinates of P's centroid are  x = \frac{1}{6A} \sum_{i=0}^{N-1} ((x_i + x_{i+1}) (x_i y_{i+1} - x_{i+1} y_i)) and  y = \frac{1}{6A} \sum_{i=0}^{N-1} ((y_i + y_{i+1}) (x_i y_{i+1} - x_{i+1} y_i)) .

[edit] Gotchas

  • If some of the vertices have negative Y coordinates, "shift" the polygon upwards first.
  • The order of vertices from the input is randomized. So we need to find out the leftmost vertices and sort them clockwisely before applying the formulas.
  • In case that there are two leftmost vertices, i.e. the two vertices have the same X coordinate, use the one with smaller Y coordinate.

[edit] Notes

  • N/A

[edit] Input

4 0 1 1 1 0 0 1 0
3 1 2 1 0 0 0
7
-4 -4
-6 -3
-4 -10
-7 -12
-9 -8
-3 -6
-8 -3
1

[edit] Output

0.500 0.500
0.667 0.667
-6.102 -7.089

[edit] References

  1. http://astronomy.swin.edu.au/~pbourke/geometry/polyarea/
Personal tools