码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 1584 A Round Peg in a Ground Hole --判定点在形内形外形上

时间:2014-12-19 21:54:05      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

题意: 给一个圆和一个多边形,多边形点可能按顺时针给出,也可能按逆时针给出,先判断多边形是否为凸包,再判断圆是否在凸包内。

解法: 先判是否为凸包,沿着i=0~n,先得出初始方向dir,dir=1为逆时针,dir=-1为顺时针,然后如果后面有两个相邻的边叉积后得出旋转方向为nowdir,如果dir*nowdir < 0,说明方向逆转了,即出现了凹点,说明不是凸多边形。

然后判圆是否在多边形内: 先判圆心是否在多边形内,用环顾法,然后如果在之内,则依次判断圆心与每条凸包边的距离与半径的距离,如果所有的dis都大于等于R,说明圆在凸包内。

代码:

技术分享
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#define pi acos(-1.0)
#define eps 1e-8
using namespace std;

struct Point{
    double x,y;
    Point(double x=0, double y=0):x(x),y(y) {}
    void input() { scanf("%lf%lf",&x,&y); }
};
typedef Point Vector;
struct Circle{
    Point c;
    double r;
    Circle(){}
    Circle(Point c,double r):c(c),r(r) {}
    Point point(double a) { return Point(c.x + cos(a)*r, c.y + sin(a)*r); }
    void input() { scanf("%lf%lf%lf",&c.x,&c.y,&r); }
};
int dcmp(double x) {
    if(x < -eps) return -1;
    if(x > eps) return 1;
    return 0;
}
template <class T> T sqr(T x) { return x * x;}
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); }
bool operator < (const Point& a, const Point& b) { return a.x < b.x || (a.x == b.x && a.y < b.y); }
bool operator >= (const Point& a, const Point& b) { return a.x >= b.x && a.y >= b.y; }
bool operator <= (const Point& a, const Point& b) { return a.x <= b.x && a.y <= b.y; }
bool operator == (const Point& a, const Point& b) { return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0; }
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 Angle(Vector A, Vector B) { return acos(Dot(A, B) / Length(A) / Length(B)); }
double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; }

double DistanceToSeg(Point P, Point A, Point B)
{
    if(A == B) return Length(P-A);
    Vector v1 = B-A, v2 = P-A, v3 = P-B;
    if(dcmp(Dot(v1, v2)) < 0) return Length(v2);
    if(dcmp(Dot(v1, v3)) > 0) return Length(v3);
    return fabs(Cross(v1, v2)) / Length(v1);
}
//点是否在多边形内部
int CheckPointInPolygon(Point A,Point* p,int n){
    double TotalAngle = 0.0;
    for(int i=0;i<n;i++) {
        if(dcmp(Cross(p[i]-A,p[(i+1)%n]-A)) >= 0) TotalAngle += Angle(p[i]-A,p[(i+1)%n]-A);
        else TotalAngle -= Angle(p[i]-A,p[(i+1)%n]-A);
    }
    if(dcmp(TotalAngle) == 0)                 return 0;   //外部
    else if(dcmp(fabs(TotalAngle)-2*pi) == 0) return 1;   //完全内部
    else if(dcmp(fabs(TotalAngle)-pi) == 0)   return 2;   //边界上
    else                                      return 3;   //多边形顶点
}
//判断未知时针方向的多边形是否是凸包
bool CheckConvexHull(Point* p,int n){
    int dir = 0;   //旋转方向
    for(int i=0;i<n;i++) {
        int nowdir = dcmp(Cross(p[(i+1)%n]-p[i],p[(i+2)%n]-p[i]));
        if(!dir) dir = nowdir;
        if(dir*nowdir < 0) return false;     //非凸包
    }
    return true;
}

Point p[107];

int main()
{
    int n,i,j;
    Circle Peg;
    while(scanf("%d",&n)!=EOF && n >= 3)
    {
        scanf("%lf",&Peg.r); Peg.c.input();
        for(i=0;i<n;i++) p[i].input();
        if(!CheckConvexHull(p,n)) { puts("HOLE IS ILL-FORMED"); continue; }
        if(CheckPointInPolygon(Peg.c,p,n))
        {
            for(i=0;i<n;i++)
            {
                double dis = DistanceToSeg(Peg.c,p[i],p[(i+1)%n]);
                if(dcmp(dis-Peg.r) < 0) break;
            }
            if(i == n) { puts("PEG WILL FIT"); continue; }
        }
        puts("PEG WILL NOT FIT");
    }
    return 0;
}
View Code

 

参考文章: http://blog.csdn.net/lyy289065406/article/details/6648606

POJ 1584 A Round Peg in a Ground Hole --判定点在形内形外形上

标签:

原文地址:http://www.cnblogs.com/whatbeg/p/4174728.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!