Lifting the Stone
InterviewTime limit1sMemory limit128 MB
Compute the centroid (area centre) of a simple polygon given up to a million vertices, with careful half-up rounding to two decimals.
- Level
Medium4 of 10
- Topics
- Geometry, Math, Implementation
- Solved
- No attempts yet
Problem
A heavy stone covers a secret opening in the floor. Lifting it triggers a mechanism that fires poisoned arrows, so the stone must be raised very slowly and kept perfectly level: no side may rise before another. To do this, a rope is tied exactly at the stone's centre of gravity and the stone is lifted with a pulley.
The stone has the shape of a polygon and the same height everywhere, so its centre of gravity is the centroid (area centre) of that polygon. Given the polygon, compute its centre of gravity.
Input
The first line contains an integer , the number of test cases.
Each test case begins with a line containing an integer (), the number of vertices of the polygon. Each of the next lines contains two integers and (), the coordinates of the -th vertex. Connecting the vertices in the given order forms the polygon.
Edges never cross, and only neighbouring edges touch, at their shared vertex. The polygon's area is never zero (it never collapses to a line). Vertices may be given in either clockwise or counter-clockwise order.
Output
For each test case, print one line with two numbers separated by a single space: the coordinates and of the centre of gravity.
Round each coordinate to exactly two digits after the decimal point. Rounding is half-up: a value exactly halfway is rounded away from zero (for example becomes and becomes ). If a coordinate rounds to zero, print it as 0.00, never -0.00.
The centre of gravity may lie outside the polygon when the polygon is not convex; print it in that case as well.