#include #include #define DIM 2 /* Dimension of points */ typedef int tPointi[DIM]; /* type integer point */ #define PMAX 1000 /* Max # of pts in polygon */ typedef tPointi tPolygoni[PMAX];/* type integer polygon */ int Area2( tPointi a, tPointi b, tPointi c ); int AreaPoly2( int n, tPolygoni P ); main() { int n,a=0; tPolygoni P; n = ReadPoints( P ); a =abs( AreaPoly2(n,P)/2); printf("%d",a); return a; } /* Returns twice the signed area of the triangle determined by a,b,c, positive if a,b,c are oriented ccw, and negative if cw. */ int Area2( tPointi a, tPointi b, tPointi c ) { /* The text has this: return a[0] * b[1] - a[1] * b[0] + a[1] * c[0] - a[0] * c[1] + b[0] * c[1] - c[0] * b[1]; The following computation is algebraically equivalent but uses four fewer multiplications. It is obtained by shifting the coordinate system so that point a is the origin. */ return (b[0] - a[0]) * (c[1] - a[1]) - (c[0] - a[0]) * (b[1] - a[1]); } /* Returns twice the area of polygon P. */ int AreaPoly2( int n, tPolygoni P ) { int i; int sum = 0; /* Partial area sum */ for (i = 1; i < n-1; i++) sum += Area2( P[0], P[i], P[i+1] ); return sum; } int ReadPoints( tPolygoni P) /* Reads in the coordinates of the vertices of a polygon from stdin, puts them into P, and returns n, the number of vertices. The input is assumed to be pairs of whitespace-separated coordinates, one pair per line. The number of points is not part of the input. */ { int n = 0; char *s1 =(char *) malloc(sizeof(char)*10),*s2 =(char *) malloc(sizeof(char)*10); /* printf("Polygon:\n"); printf(" i x y\n"); */ while ( (n < PMAX) && (scanf("%s %s",s1,s2) /* &P[n][0],&P[n][1]) */ != EOF) ) { if (n==0) sscanf(s1,"[[%d",&P[n][0]); else sscanf(s1,"[%d",&P[n][0]); sscanf(s2,"%d",&P[n][1]); /* printf("%3d%4d%4d\n", n, P[n][0], P[n][1]); */ ++n; } if (n < PMAX) /* printf("n = %3d vertices read\n",n); */; else /* printf("Error in ReadPoints:\ too many points; max is %d\n", PMAX); */; /* putchar('\n'); */ return n; }