求多边形的面积
http://acm.hdu.edu.cn/showproblem.php?pid=1115
定理1 已知三角形△A1A2A3的顶点坐标Ai ( xi , yi ) ( i =1, 2, 3) 。它的重心坐标为:
xg = (x1+x2+x3) / 3 ; yg = (y1+y2+y3) / 3 ;
定理2 已知三角形△A1A2A3的顶点坐标Ai ( xi , yi ) ( i =1, 2, 3) 。该三角形的有向面积为:
S = ( (x2 - x1) * (y3 - y1) - (x3 - x1) * (y2 - y1) ) / 2 ;
△A1A2A3 边界构成逆时针回路时取+
#include <iostream> #include <cmath> #include <cstdio> #include <algorithm> using namespace std; struct point{ double x,y; }; double area(point p1,point p2,point p3) { return ((p2.x-p1.x)*(p3.y-p1.y)-(p2.y-p1.y)*(p3.x-p1.x))/2; } int main() { int n,t; cin>>t; while(t--) { scanf("%d",&n); point p1,p2,p3; double sum_x=0,sum_y=0,sum_area=0,s; scanf("%lf%lf",&p1.x,&p1.y); scanf("%lf%lf",&p2.x,&p2.y); for(int i=2;i<n;i++) { scanf("%lf%lf",&p3.x,&p3.y); s=area(p1,p2,p3); sum_area+=s; sum_x+=(p1.x+p2.x+p3.x)*s; sum_y+=(p1.y+p2.y+p3.y)*s; p2=p3; } printf("%.2lf %.2lf\n",sum_x/sum_area/3,sum_y/sum_area/3); } return 0; }