#include<stdio.h> #include<math.h> const int maxn = 100000; struct Point{ double x,y; Point() {} Point(double _x, double _y) { x = _x; y = _y; } Point operator -(const Point &B) const { return Point(x-B.x, y-B.y); } }p[maxn]; double eps = 1e-10; int dcmp(double x) { if(fabs(x) < eps) return 0; else return x < 0 ? -1 : 1; } double Cross(Point A, Point B) { return A.x*B.y - A.y*B.x; } /** 判断多边形是否是凸多边形【含共线】*/ bool isConvex(Point *p, int n) { p[n] = p[0]; // 边界处理 p[n+1] = p[1]; // 注意也可以用 %n 处理, 下标从 0 开始 int now = dcmp(Cross(p[1]-p[0], p[2]-p[1])); for(int i = 1; i < n; i++) { int next = dcmp(Cross(p[i+1]-p[i], p[i+2]-p[i+1])); if(now*next < 0) //此处可以共线 { return false; } now = next; //注意记录临界条件 } return true; } int main() { int n; while(scanf("%d", &n) != EOF) { if(n == 0) break; for(int i = 0; i < n; i++) scanf("%lf%lf", &p[i].x, &p[i].y); bool flag = isConvex(p,n); if(flag) printf("convex\n"); else printf("concave\n"); } return 0; }
时间: 2024-10-06 00:44:06