首页 > 其他 > 详细

CUGB计算几何专题:B - A Round Peg in a Ground Hole判断线段相交

时间:2014-03-10 01:14:20      阅读:597      评论:0      收藏:0      [点我收藏+]

B - A Round Peg in a Ground Hole
Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u

Description

The DIY Furniture company specializes in assemble-it-yourself furniture kits. Typically, the pieces of wood are attached to one another using a wooden peg that fits into pre-cut holes in each piece to be attached. The pegs have a circular cross-section and so are intended to fit inside a round hole. 
A recent factory run of computer desks were flawed when an automatic grinding machine was mis-programmed. The result is an irregularly shaped hole in one piece that, instead of the expected circular shape, is actually an irregular polygon. You need to figure out whether the desks need to be scrapped or if they can be salvaged by filling a part of the hole with a mixture of wood shavings and glue. 
There are two concerns. First, if the hole contains any protrusions (i.e., if there exist any two interior points in the hole that, if connected by a line segment, that segment would cross one or more edges of the hole), then the filled-in-hole would not be structurally sound enough to support the peg under normal stress as the furniture is used. Second, assuming the hole is appropriately shaped, it must be big enough to allow insertion of the peg. Since the hole in this piece of wood must match up with a corresponding hole in other pieces, the precise location where the peg must fit is known. 
Write a program to accept descriptions of pegs and polygonal holes and determine if the hole is ill-formed and, if not, whether the peg will fit at the desired location. Each hole is described as a polygon with vertices (x1, y1), (x2, y2), . . . , (xn, yn). The edges of the polygon are (xi, yi) to (x i+1, y i+1) for i = 1 . . . n ? 1 and (xn, yn) to (x1, y1).

Input

Input consists of a series of piece descriptions. Each piece description consists of the following data: 
Line 1 < nVertices > < pegRadius > < pegX > < pegY > 
number of vertices in polygon, n (integer) 
radius of peg (real) 
X and Y position of peg (real) 
n Lines < vertexX > < vertexY > 
On a line for each vertex, listed in order, the X and Y position of vertex The end of input is indicated by a number of polygon vertices less than 3.

Output

For each piece description, print a single line containing the string: 
HOLE IS ILL-FORMED if the hole contains protrusions 
PEG WILL FIT if the hole contains no protrusions and the peg fits in the hole at the indicated position 
PEG WILL NOT FIT if the hole contains no protrusions but the peg will not fit in the hole at the indicated position

Sample Input

5 1.5 1.5 2.0
1.0 1.0
2.0 2.0
1.75 2.0
1.0 3.0
0.0 2.0
5 1.5 1.5 2.0
1.0 1.0
2.0 2.0
1.75 2.5
1.0 3.0
0.0 2.0
1

Sample Output

HOLE IS ILL-FORMED
PEG WILL NOT FIT

这题刚开始想的是从后面往回判断相交的,不过……不行,应该是从前往后判断才对,其中的原因可以画图来看……

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <list>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#define PI acos(-1.0)
#define eps 1e-8
#define mem(a,b) memset(a,b,sizeof(a))
#define sca(a) scanf("%d",&a)
#define pri(a) printf("%d\n",a)
#define f(i,a,n) for(i=a;i<n;i++)
#define F(i,a,n) for(i=a;i<=n;i++)
#define MM 100005
#define MN 505
#define INF 10000007
using namespace std;
typedef long long ll;
inline double sqr(const double &x){ return x * x;}
inline double sgn(const double &x){ return x < -eps ? -1 : x > eps;}
struct Point{
    double x, y;
    Point(const double &x = 0, const double &y = 0):x(x), y(y){}
    Point operator - (const Point &a)const{ return Point(x - a.x, y - a.y);}
    Point operator + (const Point &a)const{ return Point(x + a.x, y + a.y);}
    Point operator * (const double &a)const{ return Point(x * a, y * a);}
    Point operator / (const double &a)const{ return Point(x / a, y / a);}
    bool operator < (const Point &a)const{ return sgn(x - a.x) < 0 || (sgn(x - a.x) == 0 && sgn(y - a.y) < 0);}
    friend double det(const Point &a, const Point &b){ return a.x * b.y - a.y * b.x;}
    friend double dot(const Point &a, const Point &b){ return a.x * b.x + a.y * b.y;}
    friend double dist(const Point &a, const Point &b){ return sqrt(sqr(a.x - b.x) + sqr(a.y - b.y));}
    double len(){ return sqrt(dot(*this, *this));}

    Point rotateA(const double &angle)const{ return rotateS(cos(angle), sin(angle));}
    Point rotateS(const double &cosa, const double &sina)const{ return Point(x * cosa - y * sina, x * sina + y * cosa);}

    void in(){  scanf("%lf %lf", &x, &y); }
    void out()const{ printf("%.2f %.2f\n",x, y);}
};
struct Line{
    Point s, t;
    Line(const Point &s = Point(), const Point &t = Point()):s(s), t(t){}
    Point dire()const{ return t - s;}
    double len()const{ return dire().len();}
    bool isPointInLine(const Point &p)const{ return sgn(det(p - s, t - s)) == 0 && sgn(dot(p - s, p - t)) <= 0;}
    bool isPointInLineEx(const Point &p)const{ return sgn(det(p - s, t - s)) == 0 && sgn(dot(p - s, p - t)) < 0;}//不含端点
    Point pointProjLine(const Point &p){ return s + dire() * ((dot(p - s, dire()) / dire().len()) /(dire().len()));}
    double pointDistLine(const Point &p){ return fabs(det(p - s, dire()) / dire().len());}

    friend bool sameSide(const Line &line , const Point &a, const Point &b){
        return sgn(det(b - line.s, line.dire())) * sgn(det(a - line.s, line.dire())) > 0;
    }
    friend bool isLineInsectLine(const Line &l1, const Line &l2){
        if(sgn(det(l2.s - l1.s, l1.dire())) == 0 && sgn(det(l2.t - l1.s, l1.dire())) == 0
           && sgn(det(l1.s - l2.s, l2.dire())) == 0 && sgn(det(l1.t - l2.s, l2.dire())) == 0){
            return l1.isPointInLine(l2.s) || l1.isPointInLine(l2.t) || l2.isPointInLine(l1.s) ||l2.isPointInLine(l1.t);
        }
        return !sameSide(l1, l2.s, l2.t) && !sameSide(l2, l1.s, l1.t);
    }

    friend Point lineInsectLine(const Line &l1, const Line &l2){
        double s1 = det(l1.s - l2.s, l2.dire()), s2 = det(l1.t - l2.s, l2.dire());
        return (l1.t * s1 - l1.s * s2) / (s1 - s2);
    }

    void in(){ s.in(); t.in();}
    void out()const{ s.out(); t.out(); }
};
Point a[MM][2];
int vis[MM],s[MM];
int main()
{
    int n;
    while(sca(n)&&n)
    {
        Line b,b1;
        int k=0,i,j;
        mem(vis,0);
        for(i=1;i<=n;i++)
            a[i][0].in(),a[i][1].in();
        for(i=1;i<=n;i++)
        {
            if(vis[i]) continue;
            int flag=0;
            b.s=a[i][0],b.t=a[i][1];
            for(j=i+1;j<=n;j++)
            {
                if(!vis[j])
                {
                    b1.s=a[j][0],b1.t=a[j][1];
                    if(isLineInsectLine(b,b1))
                    {
                        vis[i]=1; flag=1;
                        break;
                    }
                }
            }
            if(!flag) s[k++]=i;
        }
        printf("Top sticks: ");
        for(i=0;i<k;i++)
            if(i!=k-1) printf("%d, ",s[i]);
            else printf("%d.\n",s[i]);
    }
    return 0;
}


CUGB计算几何专题:B - A Round Peg in a Ground Hole判断线段相交,布布扣,bubuko.com

CUGB计算几何专题:B - A Round Peg in a Ground Hole判断线段相交

原文:http://blog.csdn.net/u011466175/article/details/20868743

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!