POJ 3130

这题,加了精度错了,不加精度反而对了。。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int MAXN=110;
const double eps=1e-8;

struct point {
    double x,y;
};
point pts[MAXN],p[MAXN],q[MAXN];
int n,cCnt,curCnt;

int DB(double d){
    if(d>eps) return 1;
    if(d<-eps) return -1;
    return 0;
}

void initial(){
    for(int i=1;i<=n;i++)
    p[i]=pts[i];
    p[n+1]=p[1];
    p[0]=p[n];
    cCnt=n;
}

void getline(point x,point y,double &a,double &b,double &c){
    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,double a,double b,double c){
    double u = fabs(a * x.x + b * x.y + c);
    double v = fabs(a * y.x + b * y.y + c);
    point pt;
    pt.x=(x.x * v + y.x * u) / (u + v);
    pt.y=(x.y * v + y.y * u) / (u + v);
    return  pt;
}

void cut(double a,double b,double c){
    curCnt=0;
    for(int i=1;i<=cCnt;i++){
        if(a*p[i].x+b*p[i].y+c <=0) q[++curCnt] = p[i];
        else {
            if(a*p[i-1].x + b*p[i-1].y + c <=0){
                q[++curCnt] = intersect(p[i],p[i-1],a,b,c);
            }
             if(a*p[i+1].x + b*p[i+1].y + c <=0){
                q[++curCnt] = intersect(p[i],p[i+1],a,b,c);
            }
        }
    }
    for(int i = 1; i <= curCnt; ++i)p[i] = q[i];
    p[curCnt+1] = q[1];p[0] = p[curCnt];
    cCnt = curCnt;
}

void slove(){
    initial();
    for(int i=1;i<=n;i++){
        double a,b,c;
        getline(pts[i],pts[i+1],a,b,c);
        cut(a,b,c);
    }
}

int main(){
    while(true){
        scanf("%d",&n);
        if(n==0) break;
        for(int i=1;i<=n;i++)
        scanf("%lf%lf",&pts[i].x,&pts[i].y);
        pts[n+1]=pts[1];
        slove();
        if(cCnt>=1) printf("1\n");
        else printf("0\n");
    }
    return 0;
}

  

POJ 3130

时间: 2024-10-11 21:53:07

POJ 3130的相关文章

poj 3130 How I Mathematician Wonder What You Are!

How I Mathematician Wonder What You Are! Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3568   Accepted: 1906 Description After counting so many stars in the sky in his childhood, Isaac, now an astronomer and a mathematician uses a big

半平面交 模板 poj 3335 poj 3130 poj 1474 判断半平面交是否为空集

半平面交模板 const double pi= acos(-1.0); #define arc(x) (x / 180 * pi) const double EPS = 1e-8; const int Max_N = 105; struct Point{ double x,y; Point(){} Point(double x, double y):x(x),y(y){} Point operator - (Point p){ return Point(x- p.x , y - p.y ) ;

poj 3335 /poj 3130/ poj 1474 半平面交 判断核是否存在 / poj1279 半平面交 求核的面积

1 /*************** 2 poj 3335 点序顺时针 3 ***************/ 4 #include <iostream> 5 #include <cmath> 6 #include <algorithm> 7 using namespace std; 8 const double eps = 1e-8; 9 const double maxn = 0x7f7f7f7f; 10 int dcmp(double x){ 11 if(fabs(

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

How I Mathematician Wonder What You Are! POJ - 3130 题意:判断多边形是否有内核 思路:半平面交题,逆时针存入 1 // 2 // Created by HJYL on 2020/2/6. 3 // 4 #include<iostream> 5 #include<stdio.h> 6 #include<algorithm> 7 #include<math.h> 8 using namespace std; 9

POJ 3130 How I Mathematician Wonder What You Are!(半平面相交 多边形是否有核 模板)

题目链接:http://poj.org/problem?id=3130 Description After counting so many stars in the sky in his childhood, Isaac, now an astronomer and a mathematician uses a big astronomical telescope and lets his image processing program count stars. The hardest pa

POJ 3130 How I Mathematician Wonder What You Are!(半平面交求多边形的核)

题目链接 题意 : 给你一个多边形,问你该多边形中是否存在一个点使得该点与该多边形任意一点的连线都在多边形之内. 思路 : 与3335一样,不过要注意方向变化一下. 1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <math.h> 5 6 using namespace std ; 7 8 struct node 9 { 10 double x; 11 d

POJ 3130 How I Mathematician Wonder What You Are! 半平面交

和POJ3335一样,只不过这题是逆时针 #include <iostream> #include <cstdio> #include <cmath> #define eps 1e-18 using namespace std; const int MAXN = 105; double a, b, c; int n, cnt; struct Point { double x, y; }point[MAXN], p[MAXN], tp[MAXN]; void Get_eq

POJ 3130 How I Mathematician Wonder What You Are! 半平面交求多边形内核是否存在

题目大意:定义一种多边形,叫做星形多边形.这种多边形就是有内核的多边形.给出一些多边形,问是否是星形多边形. 思路:利用半平面交求解.PS:我的第一个多边形内核的代码不对..一定要看这个,这个是我看了学长的代码之后才发现之前的代码的问题的,这个也不用微调,是准确值,总值千万不要去看前面的那篇!!!! 由于内核中的所有点到图形上所有点的连线之间不能有边阻挡,所以为了满足任意一条边,需要满足内核的点必须在这条边所在直线的左边,所以将所有组成多边形的边所在的直线进行半平面交即可.由于一个多边形的内核也

POJ题目分类推荐 (很好很有层次感)

著名题单,最初来源不详.直接来源:http://blog.csdn.net/a1dark/article/details/11714009 OJ上的一些水题(可用来练手和增加自信) (POJ 3299,POJ 2159,POJ 2739,POJ 1083,POJ 2262,POJ 1503,POJ 3006,POJ 2255,POJ 3094) 初期: 一.基本算法: 枚举. (POJ 1753,POJ 2965) 贪心(POJ 1328,POJ 2109,POJ 2586) 递归和分治法. 递