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 part of the program is to judge if shining object in the
sky is really a star. As a mathematician, the only way he knows is to apply a mathematical definition of stars.

The mathematical definition of a star shape is as follows: A planar shape F is star-shaped if and only if there is a point C ∈ F such that, for any point P ∈ F, the line segment CP is contained in F. Such a point
C is called a center of F. To get accustomed to the definition let’s see some examples below.

The first two are what you would normally call stars. According to the above definition, however, all shapes in the first row are star-shaped. The two in the second row are not. For each star shape, a center is indicated with a dot. Note that a star shape
in general has infinitely many centers. Fore Example, for the third quadrangular shape, all points in it are centers.

Your job is to write a program that tells whether a given polygonal shape is star-shaped or not.

Input

The input is a sequence of datasets followed by a line containing a single zero. Each dataset specifies a polygon, and is formatted as follows.

n  
x1 y1
x2 y2

xn yn

The first line is the number of vertices, n, which satisfies 4 ≤ n ≤ 50. Subsequent n lines are the x- and y-coordinates of the n vertices. They are integers and satisfy 0 ≤ xi ≤ 10000
and 0 ≤ yi ≤ 10000 (i = 1, …, n). Line segments (xiyi)–(xi + 1yi + 1) (i = 1, …, n ? 1) and the
line segment (xnyn)–(x1y1) form the border of the polygon in the counterclockwise order. That is, these line segments see the inside of the polygon in the left of their directions.

You may assume that the polygon is simple, that is, its border never crosses or touches itself. You may assume assume that no three edges of the polygon meet at a single point even when they are infinitely extended.

Output

For each dataset, output “1” if the polygon is star-shaped and “0” otherwise. Each number must be in a separate line and the line should not contain any other characters.

Sample Input

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

Sample Output

1
0

Source

Japan 2006

题意:

逆时针给出坐标!问此多边形是否有核!

不错的讲解:http://blog.csdn.net/accry/article/details/6070621

代码如下:

#include <cstdio>
#include <cmath>
#include <iostream>
using namespace std;
#define eps 1e-8
const int MAXN=10017;
int n;
double r;
int cCnt,curCnt;//此时cCnt为最终切割得到的多边形的顶点数、暂存顶点个数
struct point
{
    double x,y;
};
point points[MAXN],p[MAXN],q[MAXN];//读入的多边形的顶点(顺时针)、p为存放最终切割得到的多边形顶点的数组、暂存核的顶点

void getline(point x,point y,double &a,double &b,double   &c){//两点x、y确定一条直线a、b、c为其系数
    a = y.y - x.y;
    b = x.x - y.x;
    c = y.x * x.y - x.x * y.y;
}
void initial()
{
    for(int i = 1; i <= n; i++)p[i] = points[i];
    p[n+1] = p[1];
    p[0] = p[n];
    cCnt = n;//cCnt为最终切割得到的多边形的顶点数,将其初始化为多边形的顶点的个数
}
point intersect(point x,point y,double a,double b,double c){//求x、y形成的直线与已知直线a、b、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;
	int i;
    for(i = 1; i <= cCnt; ++i)
	{
        if(a*p[i].x + b*p[i].y + c >= 0)q[++curCnt] = p[i];// c由于精度问题,可能会偏小,所以有些点本应在右侧而没在,
        //故应该接着判断
        else
		{
            if(a*p[i-1].x + b*p[i-1].y + c > 0){//如果p[i-1]在直线的右侧的话,
                //则将p[i],p[i-1]形成的直线与已知直线的交点作为核的一个顶点(这样的话,由于精度的问题,核的面积可能会有所减少)
                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(i = 1; i <= curCnt; ++i)p[i] = q[i];//将q中暂存的核的顶点转移到p中
    p[curCnt+1] = q[1];p[0] = p[curCnt];
    cCnt = curCnt;
}
void solve()
{
	//注意:默认点是顺时针,如果题目不是顺时针,规整化方向
    initial();
    for(int i = 1; i <= n; ++i)
	{
        double a,b,c;
        getline(points[i],points[i+1],a,b,c);
        cut(a,b,c);
    }
	/*
    如果要向内推进r,用该部分代替上个函数
    for(int i = 1; i <= n; ++i){
	Point ta, tb, tt;
	tt.x = points[i+1].y - points[i].y;
	tt.y = points[i].x - points[i+1].x;
	double k = r / sqrt(tt.x * tt.x + tt.y * tt.y);
	tt.x = tt.x * k;
	tt.y = tt.y * k;
	ta.x = points[i].x + tt.x;
	ta.y = points[i].y + tt.y;
	tb.x = points[i+1].x + tt.x;
	tb.y = points[i+1].y + tt.y;
	double a,b,c;
	getline(ta,tb,a,b,c);
	cut(a,b,c);
}*/
/*   //多边形核的面积
double area = 0;
for(int i = 1; i <= curCnt; ++i)
area += p[i].x * p[i + 1].y - p[i + 1].x * p[i].y;
area = fabs(area / 2.0);
	*/
}
/*void GuiZhengHua(){
//规整化方向,逆时针变顺时针,顺时针变逆时针
for(int i = 1; i < (n+1)/2; i ++)
swap(points[i], points[n-i]);
}*/
int main()
{
    while(scanf("%d",&n) && n)
    {
        int i;
        for(i = n; i >= 1; i--)//逆时针,顺时针反一下1->n就好了
		{
			scanf("%lf%lf",&points[i].x,&points[i].y);
		}
        points[n+1] = points[1];
        solve();
        if(cCnt < 1)
			printf("0\n");//无核
        else
			printf("1\n");//有核
    }
	return 0;
}
时间: 2024-11-08 05:54:09

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

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

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

POJ 3335 Rotating Scoreboard(半平面交 多边形是否有核 模板)

题目链接:http://poj.org/problem? id=3335 Description This year, ACM/ICPC World finals will be held in a hall in form of a simple polygon. The coaches and spectators are seated along the edges of the polygon. We want to place a rotating scoreboard somewhe

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 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

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 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(

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