Lifting the Stone

Interview

Time limit1sMemory limit128 MB

Summary
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 TT, the number of test cases.

Each test case begins with a line containing an integer NN (3≤N≤10000003 \le N \le 1000000), the number of vertices of the polygon. Each of the next NN lines contains two integers XiX_i and YiY_i (∣Xi∣,∣Yi∣≤20000|X_i|, |Y_i| \le 20000), the coordinates of the ii-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 XX and YY 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 0.0050.005 becomes 0.010.01 and −0.005-0.005 becomes −0.01-0.01). 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.

Examples3

  1. Example 1

    Input
    2
    4
    5 0
    0 5
    -5 0
    0 -5
    4
    1 1
    11 1
    11 11
    1 11
    
    Expected output
    0.00 0.00
    6.00 6.00
    
  2. Example 2

    Input
    1
    3
    0 0
    1 0
    0 1
    
    Expected output
    0.33 0.33
    
  3. Example 3

    Input
    1
    3
    0 0
    2 0
    0 2
    
    Expected output
    0.67 0.67