3 0 0 1 0 0 1 4 1 0 0 1 -1 0 0 -1 0
0.5 2.0
代码:
#include<stdio.h> int x[100],y[100]; int main() { int n,i,sum; while(scanf("%d",&n)&&n){ sum=0; for(i=0;i<n;i++) scanf("%d%d",&x[i],&y[i]); for(i=0;i<n-1;i++) sum+=(x[i]*y[i+1]-x[i+1]*y[i]); sum+=x[n-1]*y[0]-x[0]*y[n-1]; printf("%.1f\n",sum*0.5); } return 0; }
注:这题相当不错,让我学到了很多。特别注意:最后一项xn*y1-x1*yn。
以下来自网友解释:
1.把(xi,yi)按逆时针排好。(i=1,2,3,....n, x(n+1)=x1);
2.计算它的有向面积:
(1)先看:(0,0),(xj,yj),(xk,yk)三点的面积S=
¦ 0 0 1 ¦
¦ xj yj 1 ¦*(1/2) =(xj*yk-yj*xk)/2
¦ xk yk 1 ¦
(2)多边形面积可以划分为三角形面积的和:
S[(0,0),(x1,y1),(x2,y2)]+S[(0,0),(x2,y2),(x3,y3)]+...
+ S[(0,0),(x(n-1),y(n-1),(xn,yn)]+S[(0,0),(xn,yn),(x1,y1)]
=((x1*y2-x2*y1)+(x2*y3-x3*y2)+...+(x(n-1)*yn-xn*y(n-1))+(xn*y1-x1*yn))/2
=(x1*(y2-yn)+x2*(y3-y1)+x3*(y4-y2)+...+x(n-1)*(yn-y(n-2))+xn*(y1-y(n-1)))/2
(或:(y1*(xn-x2)+y2*(x1-x3)+y3*(x2-x4)+...+y(n-1)*(x(n-2)-xn)+yn*(x(n-1)-
x1))/2 )
关于证明,可以参考一些资料。这里不作详述!