POJ 3130 How I Mathematician Wonder What You Are! (半平面相交)
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.
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.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 (xi, yi)–(xi + 1, yi + 1) (i = 1, …, n − 1) and the line segment (xn, yn)–(x1, y1) 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.
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 这两个题,都是输入一个简单多边形,判断是否存在核,套半平面交模版即可。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std; const double eps = 1e-;
const int maxn = ; int dq[maxn], top, bot, pn, order[maxn], ln;
struct Point {
double x, y;
} p[maxn]; struct Line {
Point a, b;
double angle;
} l[maxn]; int dblcmp(double k) {
if (fabs(k) < eps) return ;
return k > ? : -;
} double multi(Point p0, Point p1, Point p2) {
return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);
} bool cmp(int u, int v) {
int d = dblcmp(l[u].angle-l[v].angle);
if (!d) return dblcmp(multi(l[u].a, l[v].a, l[v].b)) > ; //大于0取向量左半部分为半平面,小于0,取右半部分
return d < ;
} void getIntersect(Line l1, Line l2, Point& p) {
double dot1,dot2;
dot1 = multi(l2.a, l1.b, l1.a);
dot2 = multi(l1.b, l2.b, l1.a);
p.x = (l2.a.x * dot2 + l2.b.x * dot1) / (dot2 + dot1);
p.y = (l2.a.y * dot2 + l2.b.y * dot1) / (dot2 + dot1);
} bool judge(Line l0, Line l1, Line l2) {
Point p;
getIntersect(l1, l2, p);
return dblcmp(multi(p, l0.a, l0.b)) < ; //大于小于符号与上面cmp()中注释处相反
} void addLine(double x1, double y1, double x2, double y2) {
l[ln].a.x = x1; l[ln].a.y = y1;
l[ln].b.x = x2; l[ln].b.y = y2;
l[ln].angle = atan2(y2-y1, x2-x1);
order[ln] = ln;
ln++;
} void halfPlaneIntersection() {
int i, j;
sort(order, order+ln, cmp);
for (i = , j = ; i < ln; i++)
if (dblcmp(l[order[i]].angle-l[order[j]].angle) > )
order[++j] = order[i];
ln = j + ;
dq[] = order[];
dq[] = order[];
bot = ;
top = ;
for (i = ; i < ln; i++) {
while (bot < top && judge(l[order[i]], l[dq[top-]], l[dq[top]])) top--;
while (bot < top && judge(l[order[i]], l[dq[bot+]], l[dq[bot]])) bot++;
dq[++top] = order[i];
}
while (bot < top && judge(l[dq[bot]], l[dq[top-]], l[dq[top]])) top--;
while (bot < top && judge(l[dq[top]], l[dq[bot+]], l[dq[bot]])) bot++;
} bool isThereACore() {
if (top-bot > ) return true;
return false;
} int main()
{
//freopen("de.txt","r",stdin);
int i;
while (scanf ("%d", &pn) && pn) {
for (i = ; i < pn; i++)
scanf ("%lf%lf", &p[i].x, &p[i].y);
for (ln = i = ; i < pn-; i++)
addLine(p[i].x, p[i].y, p[i+].x, p[i+].y);
addLine(p[i].x, p[i].y, p[].x, p[].y);
halfPlaneIntersection();
/*输出这个核
Point poly[55];
int k = 0;
for (int i=bot;i<=top;++i)
poly[k++] = p[i];
for (int i=bot;i<=top;++i)
printf("%.3f %.3f\n",poly[i].x,poly[i].y);
*/
if (isThereACore()) printf ("1\n");
else printf ("0\n");
}
return ;
}
POJ 3130 How I Mathematician Wonder What You Are! (半平面相交)的更多相关文章
- POJ 3130 How I Mathematician Wonder What You Are! (半平面交)
题目链接:POJ 3130 Problem Description After counting so many stars in the sky in his childhood, Isaac, n ...
- poj 3130 How I Mathematician Wonder What You Are! - 求多边形有没有核 - 模版
/* poj 3130 How I Mathematician Wonder What You Are! - 求多边形有没有核 */ #include <stdio.h> #include ...
- POJ 3130 How I Mathematician Wonder What You Are! /POJ 3335 Rotating Scoreboard 初涉半平面交
题意:逆时针给出N个点,求这个多边形是否有核. 思路:半平面交求多边形是否有核.模板题. 定义: 多边形核:多边形的核可以只是一个点,一条直线,但大多数情况下是一个区域(如果是一个区域则必为 ).核内 ...
- poj 3130 How I Mathematician Wonder What You Are!
http://poj.org/problem?id=3130 #include <cstdio> #include <cstring> #include <algorit ...
- POJ 3130 How I Mathematician Wonder What You Are!(半平面交求多边形的核)
题目链接 题意 : 给你一个多边形,问你该多边形中是否存在一个点使得该点与该多边形任意一点的连线都在多边形之内. 思路 : 与3335一样,不过要注意方向变化一下. #include <stdi ...
- poj 3130 How I Mathematician Wonder What You Are! 【半平面交】
求多边形的核,直接把所有边求半平面交判断有无即可 #include<iostream> #include<cstdio> #include<algorithm> # ...
- 三道半平面交测模板题 Poj1474 Poj 3335 Poj 3130
求半平面交的算法是zzy大神的排序增量法. ///Poj 1474 #include <cmath> #include <algorithm> #include <cst ...
- How I Mathematician Wonder What You Are! - POJ 3130(求多边形的核)
题目大意:判断多多边形是否存在内核. 代码如下: #include<iostream> #include<string.h> #include<stdio.h> # ...
- How I Mathematician Wonder What You Are!(poj 3130)
题意:求问多边形的核(能够看到所有点的点)是否存在. /* 对于这样的题目,我只能面向std编程了,然而还是不理解. 算法可参考:http://www.cnblogs.com/huangxf/p/40 ...
随机推荐
- PHP-密码和token
密码 直接 md5 和 sha1 不安全!!! crypt() 和 hash_equals(): http://php.net/manual/zh/function.crypt.php <?ph ...
- js面向对象程序设计之构造函数
再上一篇的开头说了创建对象的两种方式,一种是Object构造函数的方式,一种是对象字面量的方法.但这些方式创建多个对象的时候都会产生大量的重复代码.经过技术的进步也演化出来许多的创建对象的模式.本章会 ...
- Vue访问子组件实例或子元素
1 尽管存在 prop 和事件,有的时候你仍可能需要在 JavaScript 里直接访问一个子组件(例如,调用子组件的方法).为了达到这个目的,你可以通过 ref 特性为这个子组件赋予一个 ID 引用 ...
- Mac版-python环境配置(一):Python下载安装
Mac OS X系统自带python,可以在终端输入python查看版本[输入exit()即可退出],如下: 从上图中可以看到,mac自带python 2.7.10,版本相对较低.现在python已升 ...
- jQuery基础--选择器
2. 选择器 2.1. 什么是jQuery选择器 jQuery选择器是jQuery为我们提供的一组方法,让我们更加方便的获取到页面中的元素.注意:jQuery选择器返回的是jQuery对象. jQue ...
- Ubuntu下编译c文件时,遇到math.h头文件不能编译问题
以前都是在VC或者VS中编写c语言程序,今天尝试在Ubuntu下试着编写了一个简单的画正弦函数的程序,用到了头文件math.h,但是编译的时候报错了: 经查资料后才知道,数学函数位于libm.so库文 ...
- CentOS下编译Lua使得其支持动态链接
在Linux下编译Lua时,我一般都是使用的make generic,这样编译没有什么问题,运行lua的程序也都OK,但是,这样在加载外部的C动态 链接库,却总是报下面的错误 dynamic libr ...
- 使用bootstrap制作网站导航
除了制作选项卡和下拉菜单,bootstrap还能编写出美观的网站导航栏 一.仿知乎导航栏 <body> <nav class="navbar navbar-default ...
- 如何写出没有 bug 的代码?
来源:www.cnblogs.com/sherrywasp/p/9262877.html 1947年9月9日,美国海军准将 Grace Hopper 在哈佛学院计算机实验室里使用 Mark II 和 ...
- <转载>面试官: 讲讲MySql表设计需要注意什么?
作者:孤独烟 出处: http://rjzheng.cnblogs.com/ 综述 近期由于复习了一下MySQL的内容看到一篇比较好的文章,转载分享一下.大家看完,其实能避开很多坑.而且很多问题,都是 ...