POJ3348 Cows 计算几何 凸包

欢迎访问~原文出处——博客园-zhouzhendong

去博客园看该题解


题目传送门 - POJ3348


题意概括

  求凸包面积(答案÷50)


题解

  凸包裸题。


代码

#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cmath>
using namespace std;
const int N=10005;
const double Eps=1e-8;
int n,st[N],top;
int Dcmp(double x){
	if (fabs(x)<Eps)
		return 0;
	return x<0?-1:1;
}
struct Point{
	double x,y;
}p[N],O;
double sqr(double x){
	return x*x;
}
double dis(Point a,Point b){
	return sqrt(sqr(a.x-b.x)+sqr(a.y-b.y));
}
bool cmp_O(Point a,Point b){
	if (Dcmp(a.y-b.y)==0)
		return a.x<b.x;
	return a.y<b.y;
}
double cross(Point a,Point b,Point c){
	return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
}
bool cmp_Angle(Point a,Point b){
	double c=cross(O,a,b);
	if (Dcmp(c)==0)
		return dis(O,a)<dis(O,b);
	return Dcmp(c)>0;
}
int main(){
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
		scanf("%lf%lf",&p[i].x,&p[i].y);
	for (int i=2;i<=n;i++)
		if (!cmp_O(p[1],p[i]))
			swap(p[1],p[i]);
	O=p[1];
	sort(p+2,p+n+1,cmp_Angle);
	memset(st,0,sizeof st);
	top=0;
	st[++top]=1,st[++top]=2;
	for (int i=3;i<=n;i++){
		while (top>=2&&Dcmp(cross(p[st[top-1]],p[st[top]],p[i]))<=0)
			top--;
		st[++top]=i;
	}
	double ans=0;
	for (int i=2;i<top;i++)
		ans+=fabs(cross(p[st[1]],p[st[i]],p[st[i+1]]));
	ans/=2;
	printf("%d",(int)(ans/50));
	return 0;
}

  

原文地址:https://www.cnblogs.com/zhouzhendong/p/POJ3348.html