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;
}
直接上代码
#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;
}
相关文章推荐
- POJ 3348 Cows(凸包面积)
- POJ 3348 Cows 求凸包面积
- POJ-3348 Cows 计算几何 求凸包 求多边形面积
- POJ 3348 Cows 求凸包面积
- poj 3348 Cows 凸包 求多边形面积 计算几何 难度:0 Source:CCC207
- Poj 3348 Cows (凸包面积)
- poj 3348 Cows(求凸包面积)
- poj 3348 Cows 求凸包面积
- POJ 3348 Cows 凸包 + 多边形面积公式
- POJ 3348 || Cows(求凸包面积
- POJ 3348 Cows(凸包面积)
- POJ 3348 Cows (凸包面积)
- POJ 3348 Cows(凸包求面积)
- POJ 3348 Cows(凸包+多边形面积)
- POJ 3348 Cows 凸包 + 多边形面积公式
- poj 3348 Cows 求凸包以及凸包的面积
- POJ 3348 Cows(凸包面积)
- POJ 3348 Cows(凸包+多边形面积)
- Cows - POJ 3348(凸包求面积)
- POJ 3348 Cows(凸包面积)