1. 程式人生 > >POJ 3348 Cows

POJ 3348 Cows

opera poj mat int() pre tdi name {} 面積

簡單的求凸多邊形面積

求不規則多邊形也是類似 只要選擇的點是沿著多邊形邊選就行了 通過容斥會得到正確答案

#include<cmath>
#include<cstdio>
#include<algorithm>
#define db double
using namespace std;
const int N=1e4+50;
const db eps=1e-9;
struct Point
{
    db x,y;Point(){}
    Point(db _x,db _y) {x=_x,y=_y;}
}p[N],q[N];
Point 
operator - (Point A,Point B) {return Point(A.x-B.x,A.y-B.y);} db operator * (Point A,Point B) {return A.x*B.x+A.y*B.y;} db operator ^ (Point A,Point B) {return A.x*B.y-A.y*B.x;} db dist(Point a,Point b) {return sqrt((a-b)*(a-b));} bool cmp(Point a,Point b) { db c=(a-p[1])^(b-p[1]);
if(fabs(c)<=eps) return dist(a,p[1])<dist(b,p[1]); return c<eps; } int main() { int n;while(scanf("%d",&n)!=EOF) { if(n==1||n==2) {puts("0");continue;} for(int i=1;i<=n;i++) { scanf("%lf%lf",&p[i].x,&p[i].y);
if(i>1&&p[i].y<p[1].y) swap(p[i],p[1]); } sort(p+2,p+n+1,cmp); int ed=1; q[ed]=p[1]; for(int i=2;i<=n;i++) { while(ed>1&&((p[i]-q[ed-1])^(q[ed]-q[ed-1]))<eps) ed--; q[++ed]=p[i]; } db res=0; for(int i=2;i<ed;i++) res+=((q[i+1]-q[1])^(q[i]-q[1]))/2; // 有向面積的一半就是該三角形的面積 printf("%d\n",(int)(res/50)); } return 0; }

POJ 3348 Cows