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 ...
随机推荐
- TODO 思维模型 尺子 游标卡尺
100个人 两个思维切入点 1>做对的题怎么分配 m(x)表示答对x道题的最多人数f(x)表示答对x道题的人数 m(0)=5m(1)=20=>f(0)=0m(2)=m(1)+1=21=&g ...
- jmeter测试https请求
测试https请求时,需要添加证书 目录 1.下载证书 2.导入 3.执行https请求 1.下载证书 在浏览器中打开要测试的https协议的网站,以谷歌为例打开,下载证书到桌面 4.一直点击下一步 ...
- 微软手写识别模块sdk及delphi接口例子
http://download.csdn.net/download/coolstar1204/2008061 微软手写识别模块sdk及delphi接口例子
- Windows7下MongoDB的下载、安装与配置详解
一.Windows7下的运行库问题 虽然这个问题属于个例,但我觉得也有必要拿出来说说,保不齐你新装的系统就存在运行库没更新或者没装全的问题.出现这样的问题其实挺恼人的,具体表现就是系统经常会弹出警示框 ...
- [转]Scikit-learn使用总结
1 scikit-learn基础介绍 1.1 估计器(Estimator) 估计器,很多时候可以直接理解成分类器,主要包含两个函数: fit():训练算法,设置内部参数.接收训练集和类别两个参数. p ...
- maven基础--IDEA集成
创建项目 构建项目 查找依赖 依赖范围 provided:已提供依赖范围.编译和测试有效,运行无效.如servlet-api,在项目运行时,tomcat等容器已经提供
- 基于Select模型的Windows TCP服务端和客户端程序示例
最近跟着刘远东老师的<C++百万并发网络通信引擎架构与实现(服务端.客户端.跨平台)>,Bilibili视频地址为C++百万并发网络通信引擎架构与实现(服务端.客户端.跨平台),重新复习下 ...
- SpringMVC请求处理流程源码
我们首先引用<Spring in Action>上的一张图来了解Spring MVC 的核心组件和大致处理流程: 从上图中看到①.DispatcherServlet 是SpringMVC ...
- mybatis使用Dao和Mapper方式
1.配置jdcp.properties数据库连接文件 #mysql database setting jdbc.type=mysql jdbc.driver=com.mysql.jdbc.Driver ...
- python------生产者消费者模型 和 管道
一.为什么要使用生产者和消费者? 在线程世界里,生产者就是生产数据的线程,消费者就是消费数据的线程,在多线程开发当中,如果生产者处理速度很快,而消费者处理速度很慢,那么生产者就必须等待消费者处理完,才 ...