HDU 2108 Shape of HDU(凸or凹多边形判定)

本文介绍了通过两种方法来判断一个多边形是凸是凹:一是比较多边形点数与凸包点数;二是检查所有相邻边的叉积符号是否一致。提供了完整的C++代码实现。

HDU 2108 Shape of HDU(凸or凹多边形判定)

http://acm.hdu.edu.cn/showproblem.php?pid=2108

题意:

       按逆时针顺序给你多边形的n个点的坐标,现在要你判断这个多边形是凸的还是凹的?

分析:

       根据顶点坐标判断一个多边形有两种方法:

1.     求出该多边形的凸包,看看多边形的点数目是否等于凸包的点数目.如果相等,那么就是凸多边形,否则就是凹多边形.注意这里凸包要求最大点集(即相邻3点可能共线).

2.     看看多边形的任意相邻两条边的向量叉积都是同时大于0或小于0. 即多边形每条边的转向都是一致的.

第一种:

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;

//精度控制
const double eps=1e-10;
int dcmp(double x)
{
    if(fabs(x)<eps) return 0;
    return x<0?-1:1;
}

//点
struct Point
{
    double x,y;
    Point(){}
    Point(double x,double y):x(x),y(y){}
    bool operator==(const Point& rhs)const
    {
        return dcmp(x-rhs.x)==0 && dcmp(y-rhs.y)==0;
    }
    bool operator<(const Point& rhs)const
    {
        return dcmp(x-rhs.x)<0 || (dcmp(x-rhs.x)==0 && dcmp(y-rhs.y)<0);
    }
};

//向量
typedef Point Vector;

//点-点==向量
Vector operator-(Point A,Point 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;
}

//求凸包
int ConvexHull(Point *p,int n,Point *ch)//点数最多的凸包  
//一条边上可以多点共线
{
    sort(p,p+n);
    n=unique(p,p+n)-p;
    int m=0;
    for(int i=0;i<n;i++)
    {
        while(m>1 && Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<0) m--;
        ch[m++]=p[i];
    }
    int k=m;
    for(int i=n-2;i>=0;i--)
    {
        while(m>k && Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<0) m--;
        ch[m++]=p[i];
    }
    if(n>1) m--;
    return m;
}

const int maxn=1000+5;
Point p[maxn],ch[maxn];
int main()
{
    int n;
    while(scanf("%d",&n)==1 && n)
    {
        for(int i=0;i<n;i++)
            scanf("%lf%lf",&p[i].x,&p[i].y);
        int m=ConvexHull(p,n,ch);
        printf("%s\n",m==n?"convex":"concave");
    }
    return 0;
}


方法三:

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;

//精度控制
const double eps=1e-10;
int dcmp(double x)
{
    if(fabs(x)<eps) return 0;
    return x<0?-1:1;
}

//点
struct Point
{
    double x,y;
    Point(){}
    Point(double x,double y):x(x),y(y){}
    bool operator==(const Point& rhs)const
    {
        return dcmp(x-rhs.x)==0 && dcmp(y-rhs.y)==0;
    }
    bool operator<(const Point& rhs)const
    {
        return dcmp(x-rhs.x)<0 || (dcmp(x-rhs.x)==0 && dcmp(y-rhs.y)<0);
    }
};

//向量
typedef Point Vector;

//点-点==向量
Vector operator-(Point A,Point 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;
}

bool check(Point *p,int n)
{
	for(int i=0;i<n;i++)
	{
		double c1=Cross(p[(i+1)%n]-p[i], p[(i+2)%n]-p[(i+1)%n]);
        double c2=Cross(p[(i+2)%n]-p[(i+1)%n], p[(i+3)%n]-p[(i+2)%n]);
        if(dcmp(c1)*dcmp(c2)<0) return false;
	}
	return true;
}
const int maxn=1000+5;
Point p[maxn],ch[maxn];
int main()
{
    int n;
    while(scanf("%d",&n)==1 && n)
    {
        for(int i=0;i<n;i++)
            scanf("%lf%lf",&p[i].x,&p[i].y);
        int flag=check(p,n);
        printf("%s\n",flag?"convex":"concave");
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值