题目:http://poj.org/problem?id=3348
求凸包面积
算法:先对点的横坐标排序,从左到右先计算下凸边,再从右到左计算上凸边.复杂度比Graham Scan法稍稍要高(两次遍历点集),但实现较容易
#include <stdio.h> #include <algorithm> using namespace std; struct point{ double x,y; }; point a[10005]; point res[10005]; bool cmp(point x,point y); int clockwise(point pre,point now,point nxt); double area(point o,point x,point y); int main(){ int n; while (scanf("%d",&n)!=EOF){ for (int i=1;i<=n;i++){ scanf("%lf%lf",&a[i].x,&a[i].y); } sort(a+1,a+n+1,cmp); int num = 0; for (int i=1;i<=n;i++){ while (num>=2&&!clockwise(res[num-1],res[num],a[i])) num--; res[++num] = a[i]; } int k = num; for (int i=n-1;i>=1;i--){ while (k+1<=num&&!clockwise(res[num-1],res[num],a[i])) num--; res[++num] = a[i]; } num--; double sum = 0; for (int i=3;i<=num;i++){ sum += area(res[1],res[i],res[i-1]); } printf("%d\n",(int)(sum/100)); } return 0; } bool cmp(point x,point y){ if (x.x<y.x) return true; else return false; } int clockwise(point pre,point now,point nxt){ double x1 = now.x-pre.x; double y1 = now.y-pre.y; double x2 = nxt.x-now.x; double y2 = nxt.y-now.y; if (x1*y2-x2*y1>0) return 1; else return 0; } double area(point o,point x,point y){ double x1 = x.x-o.x; double y1 = x.y-o.y; double x2 = y.x-o.x; double y2 = y.y-o.y; if (x1*y2-x2*y1>0) return x1*y2-x2*y1; else return ((-1.0)*(x1*y2-x2*y1)); }
[笔记] 计算几何-凸包 POJ-3348 Cows,布布扣,bubuko.com
原文:http://www.cnblogs.com/phynotape/p/3911581.html