多边形有现成的面积公式,直接套用即可。area函数接受两个参数:顶点坐标,顶点个数。
#include <stdio.h>
#include <math.h>struct point
{int x;int y;
};float area(point p[], int n)
{int i;float sum = 0.0;for (i = 0; i < n; i++){sum += p[i].x * p[(i + 1) % n].y - p[(i + 1) % n].x * p[i].y;}return fabs(sum) / 2;
}int main()
{point p[5] = { {3,4},{5,11},{12,8},{9,5},{5,6} };printf("area=%f\n", area(p, 5));return 0;
}