天天看點

ACM-計算幾何之Lifting the Stone——hdu1115 Lifting the Stone

time limit: 2000/1000 ms (java/others)    memory limit: 65536/32768 k (java/others)

total submission(s): 5026    accepted submission(s): 2102

problem description

there are many secret openings in the floor which are covered by a big heavy stone. when the stone is lifted up, a special mechanism detects this and activates poisoned arrows that are shot near the opening. the only possibility is to lift the stone very slowly

and carefully. the acm team must connect a rope to the stone and then lift it using a pulley. moreover, the stone must be lifted all at once; no side can rise before another. so it is very important to find the centre of gravity and connect the rope exactly

to that point. the stone has a polygonal shape and its height is the same throughout the whole polygonal area. your task is to find the centre of gravity for the given polygon. 

input

the input consists of t test cases. the number of them (t) is given on the first line of the input file. each test case begins with a line containing a single integer n (3 <= n <= 1000000) indicating the number of points that form the polygon. this is followed

by n lines, each containing two integers xi and yi (|xi|, |yi| <= 20000). these numbers are the coordinates of the i-th point. when we connect the points in the given order, we get a polygon. you may assume that the edges never touch each other (except the

neighboring ones) and that they never cross. the area of the polygon is never zero, i.e. it cannot collapse into a single line. 

output

print exactly one line for each test case. the line should contain exactly two numbers separated by one space. these numbers are the coordinates of the centre of gravity. round the coordinates to the nearest number with exactly two digits after the decimal

point (0.005 rounds up to 0.01). note that the centre of gravity may be outside the polygon, if its shape is not convex. if there is such a case in the input data, print the centre anyway. 

sample input

sample output

source

題目:

這道題是求多邊形重心的題目,可以做個模闆。

該題目中的點順序是按逆時針順序給的。

求多邊形重心分兩種情況:

①品質集中在頂點上。n個頂點坐标為(xi,yi),品質為mi,則重心

  x = ∑( xi×mi ) / ∑mi

  y = ∑( yi×mi ) / ∑mi

  特殊地,若每個點的品質相同,則

  x = ∑xi / n

  y = ∑yi / n

②品質分布均勻。這個題就是這一類型,算法和上面的不同。

  特殊地,品質均勻的三角形重心:

  x = ( x0 + x1 + x2 ) / 3

  y = ( y0 + y1 + y2 ) / 3

是以,這道題解法:

1.以一個點為頂點,做多個三角形,然後求每個三角形的重心與品質。

2.然後用每個三角形的重心為頂點再構成一個多邊形。

3.這個多邊形就屬于上述的第一種情況了,品質在頂點上的多邊形面積。

4.套公式就ok拉~

ps:注意幾點:

由于三角形品質與面積成正比(why?因為品質分布均勻~。~),是以品質可用面積來代替。

再者用叉積求三角形面積要算正負情況(正負号要保留),

此舉是為了防止多邊形為凹多邊形的情況,三角形面積會在多邊形外。

ok~翠花,上模闆~,此模闆也會更新在我整理的

 中喲~~~