您的位置:首页 > 其它

HDU2108 Shape of HDU

2015-09-01 16:46 267 查看
题意简单就是,凸包的判定;

我们可以用向量叉积的正负来判定;

我们去除连续的三个点a,b,c,如果(a-b)叉乘(a-c),如果,大于零,则b点的角度小于180度,这样判断出所有的角度,就可以确定该多边形是不是个凸包。

代码如下

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
using namespace std;
#define ll long long
char str[1010];
struct point
{
double x,y;
}p[1010];
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);
}
int main()
{
int n;
while(scanf("%d",&n)&&n)
{
for(int i=0;i<n;i++)
{
scanf("%lf%lf",&p[i].x,&p[i].y);
}
p[n++]=p[0];
p[n++]=p[1];
bool ans=false;
for(int i=2;i<n;i++)
{
if(cross(p[i-2],p[i-1],p[i])<0)
{
ans=true;
break;
}
}
if(!ans) puts("convex");//是凸包
else puts("concave");//不是凸包
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: