POJ 3130 How I Mathematician Wonder What You Are! (半平面交+多边形内核判断)

【题目链接】:click here~~

【题目大意】:按顺序给出一些点。可以构成一个多边形,问多边形是否有核。

【思路】:跟前一道题目是一样的。

代码:

/*
* 半平面交+多边形内核判断
* Problem: POJ No.3130
* Running time: 16MS
* Complier: G++
* Author: herongwei
* Create Time: 15:34 2015/10/1 星期四
*/
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;

const double pi=acos(-1.0);
const double e=exp(1.0);
const double eps=1e-8;
const int maxn=10005;
struct Point          // 点或向量结构
{
    double x,y;
    Point(double _x=0.0,double _y=0.0):x(_x),y(_y) {}
    Point operator - (const Point &p)
    {
        return Point(x-p.x,y-p.y);
    }
    double sqrx()    //向量的模
    {
        return sqrt(x*x+y*y);
    }
} point[maxn];//记录最开始的多边形

Point temp[maxn];//临时保存新切割的多边形
Point p[maxn];//保存新切割出的多边形
int pre_point,last_point; //原先的点数,新切割出的多边形的点数
double a,b,c;

int dcmp(double x)
{
    return (x>eps)-(x<-eps);
}
/*
已知两点(x1,y1),(x2,y2)
则直线坐标为:(y2-y1)*x+(x1-x2)*y+(y1*x2-x1*y2)==0
*/
void getline(Point x,Point y)//获取直线ax+by+c==0
{
    a=y.y-x.y;
    b=x.x-y.x;
    c=y.x*x.y-x.x*y.y;
}

Point intersect(Point x,Point y)//获取直线ax+by+c==0  和点x和y所连直线的交点
{
    double u=fabs(a*x.x+b*x.y+c);
    double v=fabs(a*y.x+b*y.y+c);
    Point ans;
    ans.x=(x.x*v+y.x*u)/(u+v);
    ans.y=(x.y*v+y.y*u)/(u+v);
    return ans;
}

void cut()//用直线ax+by+c==0切割多边形
{
    int cut_num=0;
    for(int i=1; i<=last_point; ++i)
    {
        if(a*p[i].x+b*p[i].y+c<=0){
            temp[++cut_num]=p[i];
        }
        else
        {
            if(a*p[i-1].x+b*p[i-1].y+c<0)
            {
                temp[++cut_num]=intersect(p[i-1],p[i]);
            }
            if(a*p[i+1].x+b*p[i+1].y+c<0)
            {
                temp[++cut_num]=intersect(p[i+1],p[i]);
            }
        }
    }
    for(int i=1; i<=cut_num; ++i)
    {
        p[i]=temp[i];
    }
    p[cut_num+1]=temp[1];
    p[0]=temp[cut_num];
    last_point=cut_num;
}

void solve()
{
    for(int i=1; i<=pre_point; ++i){
        p[i]=point[i];
    }
    point[pre_point+1]=point[1];
    p[pre_point+1]=p[1];
    p[0]=p[pre_point];
    last_point=pre_point;
    for(int i=1; i<=pre_point; ++i)
    {
        getline(point[i],point[i+1]);//根据point[i]和point[i+1]确定直线ax+by+c==0
        cut();//用直线ax+by+c==0切割多边形
    }
}
int main()
{
     while(scanf("%d",&pre_point),pre_point){
         for(int i=1; i<=pre_point; ++i){
             scanf("%lf%lf",&point[i].x,&point[i].y);
         }
         solve();
         if(last_point==0) puts("0");
         else puts("1");
     }
     return 0;
}
/*
 6
66 13
96 61
76 98
13 94
4 0
45 68
8
27 21
55 14
93 12
56 95
15 48
38 46
51 65
64 31
0

1
0
*/

ps:若输入按顺时针,则判断大于号,否则判断小于号

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值