A Round Peg in a Ground Hole(POJ1584点在多边形内+是否为凸多边形)
2013-10-07 20:41
288 查看
题意:顺时针或逆时针给你一些点判断是否为凸多边形,如果不是输出HOLE IS ILL-FORMED
如果是在判断圆是否在多边形内
思路:圆在多边形内,判断原点到每个点的距离如果有一条边小于半径那就不在多边形内
如果是在判断圆是否在多边形内
思路:圆在多边形内,判断原点到每个点的距离如果有一条边小于半径那就不在多边形内
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; struct Point { double x,y; Point(double x = 0,double y = 0):x(x),y(y){} }; typedef Point Vector; Vector operator + (Vector a, Vector b) { return Vector(a.x+b.x,a.y+b.y) ;} Vector operator - (Vector a, Vector b) { return Vector(a.x-b.x,a.y-b.y) ;} Vector operator * (Vector a,double p) { return Vector(a.x*p,a.y*p) ;} Vector operator / (Vector a,double p) { return Vector(a.x/p,a.y/p) ;} double Dot(Vector a,Vector b) { return a.x*b.x + a.y*b.y ;} double Length(Vector a) { return sqrt(Dot(a,a)) ;} double Cross(Vector a, Vector b) { return a.x*b.y - a.y*b.x ;} const double eps = 1e-8; int dcmp(double x) { if(fabs(x) < eps) return 0; else return x < 0 ? -1 : 1; } bool operator == (Point a,Point b) { return dcmp(a.x-b.x) == 0&& dcmp(a.y-b.y) == 0; } bool operator < (Point a,Point b) { return a.x < b.x || (a.x == b.x && a.y < b.y); } bool Onsegment(Point p,Point a,Point b) { return dcmp(Cross(b-a,p-a)) == 0 && dcmp(Dot(b-p,a-p)) < 0 || (p == a) || (p == b); } bool OnLine(Point p,Point a,Point b) { return fabs(Cross(p-a,a-b)) / Length(b-a); } bool Segmentsection(Point a,Point b,Point c,Point d) { double d1 = Cross(b-a,c-a),d2 = Cross(b-a,d-a),d3 = Cross(d-c,a-c),d4 = Cross(d-c,b-c); if(dcmp(d1)*dcmp(d2) < 0 && dcmp(d3)*dcmp(d4) < 0) return true; else if(dcmp(d1) == 0 && Onsegment(c,a,b) ) return true; else if(dcmp(d2) == 0 && Onsegment(d,a,b) ) return true; else if(dcmp(d3) == 0 && Onsegment(a,c,d) ) return true; else if(dcmp(d4) == 0 && Onsegment(b,c,d) ) return true; else return false; } Point Segment(Point p,Vector v,Point q,Vector w) { Vector u = p-q; double t = Cross(w,u) / Cross(v,w); return p + v*t; } double Max(double a,double b) { return a > b ? a : b; } double PointtoLine(Point p,Point a,Point b) { return fabs(Cross(p-a,p-b)) / Length(b-a); } struct Line { Point s,e; Line(Point s = 0,Point e = 0) :s(s),e(e){} }; bool PointInpoly(Point p,Point poly[],int n) { int w = 0; for(int i = 0; i < n; i++) { if(Onsegment(p,poly[i],poly[(i+1)%n])) return -1; int k = dcmp(Cross(poly[(i+1)%n]-poly[i],p-poly[i])); int d1 = dcmp(poly[i].y - p.y); int d2 = dcmp(poly[(i+1)%n].y - p.y); if(k > 0 && d1 <= 0 && d2 > 0) w++; if(k < 0 && d2 <= 0 && d1 > 0) w--; } if(w != 0) return 1; else return 0; } Point poly[1005]; int main() { int n; while(scanf("%d",&n) != EOF) { if(n < 3) { break; } double r,x,y; scanf("%lf%lf%lf",&r,&x,&y); for(int i = 0; i < n; i++) { scanf("%lf%lf",&poly[i].x,&poly[i].y); } bool flag1 = true,flag2 = true,flag = true; for(int i = 0; i < n; i++) { if(Cross(poly[(i+1)%n]-poly[i],poly[(i+2)%n]-poly[i]) < 0) { flag1 = false; } if(Cross(poly[(i+1)%n]-poly[i],poly[(i+2)%n]-poly[i]) > 0) { flag2 = false; } } if(!flag1 && !flag2) { printf("HOLE IS ILL-FORMED\n"); } else { for(int i = 0; i < n; i++) { if(dcmp(PointtoLine(Point(x,y) ,poly[i],poly[(i+1)%n]) - r) < 0) { flag = false;break; } } if(!flag || !PointInpoly(Point(x,y) , poly,n) ) printf("PEG WILL NOT FIT\n"); else printf("PEG WILL FIT\n"); } } return 0; }
相关文章推荐
- poj 1584 A Round Peg in a Ground Hole(判断凸多边形+圆是否在凸多边形内)
- POJ 1584 A Round Peg in a Ground Hole(判断凸多边形,判断圆是否在凸多边形内)
- POJ 1584 A Round Peg in a Ground Hole(判断凸多边形,点到线段距离,点在多边形内)
- POJ1584 A Round Peg in a Ground Hole 判断多边形是否为凸多边形,判断凸多边形是否在圆内
- poj_1584 A Round Peg in a Ground Hole(凸包判断凸多边形+判断点在多边形内)
- POJ 1584: A Round Peg in a Ground Hole 射线法判断点是否在多边形内 点与直线距离 判断多边形是否为凸多边形
- POJ 1584 A Round Peg in a Ground Hole(凸多边形判断+点与多边形的位置关系)
- poj 1584 A Round Peg in a Ground Hole 判断多边形是否为凸多边形 + 圆心是否在凸多边形内 + 圆是否在凸多边形内部
- poj 1584 A Round Peg in a Ground Hole 点到直线的距离 点是否在多边形内 多边形是否为凸
- POJ 1584 A Round Peg in a Ground Hole (判断凸多边形模板)
- POJ 1584 A Round Peg in a Ground Hole 判断凸多边形,判断点在凸多边形内
- poj1584 A Round Peg in a Ground Hole 凸多边形判断 和多边形与圆的位置
- POJ-1584-A Round Peg in a Ground Hole-计算几何-凸多边形+多边形包含圆
- POJ1584 A Round Peg in a Ground Hole 判断多边形是否为凸多边形,判断凸多边形是否在圆内
- poj 1584 A Round Peg in a Ground Hole(叉积判断凸多边形)
- POJ 1584 A Round Peg in a Ground Hole(判断凸多边形,点到线段最短距离)
- poj1584--A Round Peg in a Ground Hole(判断凸包,并且判断圆是否在凸包内)
- POJ 1584 A Round Peg in a Ground Hole(点到直线距离,多边形是否为凸)
- A Round Peg in a Ground Hole(判断是否是凸包,点是否在凸包内,圆与多边形的关系)
- POJ 1584 A Round Peg in a Ground Hole(判断是否是凸包以及圆是否完全在凸包内)