您的位置:首页 > 其它

POJ 3348 Cows 寻找凸包 凸包面积

2013-09-08 22:29 417 查看
非常简单的凸包      找凸包  凸包面积的计算

直接上代码   

#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define eps 1e-8
using namespace std;

struct Point
{
double x,y;
Point (double x = 0,double y = 0):x(x),y(y){}
};

typedef struct Point Vector;

Vector operator -(Vector A,Vector B)
{
return Vector(A.x-B.x,A.y-B.y);
}

double Cross(Vector A, Vector B)
{
return A.x*B.y -A.y*B.x;
}

double PolygonArea(Point *p, int n)
{
double area = 0;
for(int i = 1 ; i < n-1; i++)
area += Cross(p[i] - p[0], p[i+1] - p[0]);
return area / 2.0;
}

int cmp(Point a,Point b)
{
if(a.x<b.x)return 1;
else if(a.x == b.x && a.y < b.y)return 1;
return 0;
}

int ConvexHull(Point *s,int n,Point *ch)
{
sort(s,s+n,cmp);
int m = 0;
for(int i = 0; i < n; i++)
{
while(m >1 && Cross(ch[m-1]-ch[m-2],s[i]-ch[m-2])<= 0)m--;
ch[m++] = s[i];
}
int k = m;
for(int i = n-2; i >=0; i--)
{
while(m > k && Cross(ch[m-1]-ch[m-2],s[i]-ch[m-2]) <= 0)m--;
ch[m++] = s[i];
}
if(n>1)m--;
return m;
}

int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif // LOCAL
int N;
while(scanf("%d",&N)!=EOF)
{
struct Point p
,ch
;
for(int i = 0; i < N;i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
int m = ConvexHull(p,N,ch);
double area = PolygonArea(ch,m);
printf("%d\n",(int)(area/50.0));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  计算几何 凸包