题目链接:POJ 3130

Problem 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 \in F\) such that, for any point \(P \in 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 \le n \le 50\). Subsequent \(n\) lines are the \(x\)- and \(y\)-coordinates of the \(n\) vertices. They are integers and satisfy \(0 \le x_i \le 10000\) and \(0 \le yi \le 10000 (i = 1, …, n)\). Line segments \((x_i, y_i)–(x_{i + 1}, y_{i + 1}) (i = 1, …, n − 1)\) and the line segment \((x_n, y_n)–(x_1, y_1)\) 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

Solution

题意

给定 \(n\) 个点的多边形,求多边形是否有核。

题解

半平面交

半平面交求多边形的核的面积,如果面积为 0,就没有核。

Code

#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef double db;
const db eps = 1e-10;
const db pi = acos(-1.0);
const ll inf = 0x3f3f3f3f3f3f3f3f;
const ll maxn = 1e3 + 10; inline int dcmp(db x) {
if(fabs(x) < eps) return 0;
return x > 0? 1: -1;
} class Point {
public:
double x, y;
Point(double x = 0, double y = 0) : x(x), y(y) {}
inline void input() {
scanf("%lf%lf", &x, &y);
}
bool operator<(const Point &a) const {
return (!dcmp(x - a.x))? dcmp(y - a.y) < 0: x < a.x;
}
bool operator==(const Point &a) const {
return dcmp(x - a.x) == 0 && dcmp(y - a.y) == 0;
}
db dis2(const Point a) {
return pow(x - a.x, 2) + pow(y - a.y, 2);
}
db dis(const Point a) {
return sqrt(dis2(a));
} db dis2() {
return x * x + y * y;
}
db dis() {
return sqrt(dis2());
}
Point operator+(const Point a) {
return Point(x + a.x, y + a.y);
}
Point operator-(const Point a) {
return Point(x - a.x, y - a.y);
}
Point operator*(double p) {
return Point(x * p, y * p);
}
Point operator/(double p) {
return Point(x / p, y / p);
}
db dot(const Point a) {
return x * a.x + y * a.y;
}
db cross(const Point a) {
return x * a.y - y * a.x;
}
db ang(Point a) {
return acos((a.dis() * dis()) / dot(a));
}
};
typedef Point Vector; Point p[maxn], ip[maxn]; class Line {
public:
Point s, e;
db angle;
Line() {}
Line(Point s, Point e) : s(s), e(e) {}
inline void input() {
s.input();e.input();
}
bool operator<(const Line &a) const {
Line l = a;
if(dcmp(angle - l.angle) == 0) {
return l.toLeftTest(s) == 1;
}
return angle < l.angle;
}
void get_angle() {
angle = atan2(e.y - s.y, e.x - s.x);
}
int toLeftTest(Point p) {
if((e - s).cross(p - s) > 0) return 1;
else if((e - s).cross(p - s) < 0) return -1;
return 0;
}
int linecrossline(Line l) {
if(dcmp((e - s).cross(l.e - l.s)) == 0) {
if(dcmp((l.s - e).cross(l.e - s)) == 0) {
return 0;
}
return 1;
}
return 2;
}
Point crosspoint(Line l) {
db a1 = (l.e - l.s).cross(s - l.s);
db a2 = (l.e - l.s).cross(e - l.s);
db x = (s.x * a2 - e.x * a1) / (a2 - a1);
db y = (s.y * a2 - e.y * a1) / (a2 - a1);
if(dcmp(x) == 0) x = 0;
if(dcmp(y) == 0) y = 0;
return Point(x, y);
}
}; Line l[maxn], q[maxn]; db half_plane(int cnt) {
sort(l + 1, l + 1 + cnt);
int tmp = 1;
for(int i = 2; i <= cnt; ++i) {
if(dcmp(l[i].angle - l[tmp].angle) == 1) l[++tmp] = l[i];
}
cnt = tmp;
int head = 1, tail = 2;
q[1] = l[1], q[2] = l[2];
for(int i = 3; i <= cnt; ++i) {
while(head < tail && l[i].toLeftTest(q[tail].crosspoint(q[tail - 1])) == -1) {
--tail;
}
while(head < tail && l[i].toLeftTest(q[head].crosspoint(q[head + 1])) == -1) {
++head;
}
q[++tail] = l[i];
} while(head < tail && q[head].toLeftTest(q[tail].crosspoint(q[tail - 1])) == -1) {
--tail;
}
while(head < tail && q[tail].toLeftTest(q[head].crosspoint(q[head + 1])) == -1) {
++head;
} if(tail - head + 1 <= 2) {
return 0.0;
} tmp = 0;
for(int i = head; i < tail; ++i) {
ip[++tmp] = q[i].crosspoint(q[i + 1]);
}
ip[++tmp] = q[head].crosspoint(q[tail]);
db ans = 0;
for(int i = 3; i <= tmp; ++i) {
ans += (ip[i - 1] - ip[1]).cross(ip[i] - ip[1]);
}
return ans * 0.5;
} int main() {
int n;
while(~scanf("%d", &n) && n) {
int cnt = 0;
for(int i = 1; i <= n; ++i) {
p[i].input();
if(i > 1) {
l[++cnt].e = p[i];
l[cnt].s = p[i - 1];
l[cnt].get_angle();
}
}
l[++cnt].e = p[1];
l[cnt].s = p[n];
l[cnt].get_angle();
if(dcmp(half_plane(cnt)) == 0) {
printf("0\n");
} else {
printf("1\n");
}
}
return 0;
}

POJ 3130 How I Mathematician Wonder What You Are! (半平面交)的更多相关文章

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

  2. poj 3130 How I Mathematician Wonder What You Are! - 求多边形有没有核 - 模版

    /* poj 3130 How I Mathematician Wonder What You Are! - 求多边形有没有核 */ #include <stdio.h> #include ...

  3. POJ 3130 How I Mathematician Wonder What You Are! /POJ 3335 Rotating Scoreboard 初涉半平面交

    题意:逆时针给出N个点,求这个多边形是否有核. 思路:半平面交求多边形是否有核.模板题. 定义: 多边形核:多边形的核可以只是一个点,一条直线,但大多数情况下是一个区域(如果是一个区域则必为 ).核内 ...

  4. poj 3130 How I Mathematician Wonder What You Are!

    http://poj.org/problem?id=3130 #include <cstdio> #include <cstring> #include <algorit ...

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

    题目链接 题意 : 给你一个多边形,问你该多边形中是否存在一个点使得该点与该多边形任意一点的连线都在多边形之内. 思路 : 与3335一样,不过要注意方向变化一下. #include <stdi ...

  6. poj 3130 How I Mathematician Wonder What You Are! 【半平面交】

    求多边形的核,直接把所有边求半平面交判断有无即可 #include<iostream> #include<cstdio> #include<algorithm> # ...

  7. POJ 3525 Most Distant Point from the Sea (半平面交向内推进+二分半径)

    题目链接 题意 : 给你一个多边形,问你里边能够盛的下的最大的圆的半径是多少. 思路 :先二分半径r,半平面交向内推进r.模板题 #include <stdio.h> #include & ...

  8. 【POJ 3335】 Rotating Scoreboard (多边形的核- - 半平面交应用)

    Rotating Scoreboard Description This year, ACM/ICPC World finals will be held in a hall in form of a ...

  9. POJ 3525 Most Distant Point from the Sea (半平面交+二分)

    Most Distant Point from the Sea Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3476   ...

随机推荐

  1. Linux随笔 - DNS搭建

    域名系统(英文:Domain Name System,缩写:DNS)是因特网的一项服务.它作为将域名和IP地址相互映射的一个分布式数据库,能够使人更方便的访问互联网.DNS 使用TCP和UDP端口53 ...

  2. HP Server BIOS实验报告

    原文链接http://www.hpiss.com/3924.html 注意:红色字体为HP手册中查找到的资源,及个人感觉应该注意的一些信息,个人翻译的也为红字体,网络中自行查找到的资源,以及询问各位师 ...

  3. Rust <0>:源代码组织,Cargo 入门

    Rust 源代码组织,使用配套的 Cargo 工具,其功能强大,程序员可摆脱 C/C++ 中需要自行维护 make.cmake 之类配置的工作量. 初始化一个项目: cargo new --bin h ...

  4. Django框架(二十六)—— Django rest_framework-分页器与版本控制

    目录 分页器与版本控制 一.三种分页器 二.分页器 1.普通分页(PageNumberPagination) 2.偏移分页(LimitOffsetPagination) 3.加密分页(CursorPa ...

  5. java 重新学习 (五)

    Set 集合 一.HashSet按照Hash算法存储集合元素(hashCode方法获取hashCode值,根据hashCode值获取元素位置,通过equals判断对象是否相等并且hashCode值是否 ...

  6. PHP数组循环遍历的几种方式

    PHP数组循环遍历 1.for循环 <?php //语法 for (init counter; test counter; increment counter) { code to be exe ...

  7. Java技术专区-虚拟机系列-虚拟机参数(常用)

    基础参数系类(内存分配) -server:一定要作为第一个参数,在多个CPU时性能佳 -Xmn:young generation的heap大小,一般设置为Xmx的3.4分之一-Xms:初始Heap大小 ...

  8. springMVC 框架的xml配置文件的说明

    springMVC框架xml文件配置的说明,直接上代码: 我们介绍四个xml文件配置以及xml内容的理解:application.xml.spring-mvc.xml.pom.xml 和 web.xm ...

  9. css3 新特性(动画)

    1. 制作动画 先定义动画,再使用(调用)动画 使用 keyframes(关键帧)定义动画(类似定义类选择器) @keyframes 动画名称{ 0%{ width:100px; } 100%{ wi ...

  10. Mac下homebrew的安装与卸载

    mac系统常用的软件安装工具就是homebrew 个人认为通过brew安装比较简单,下面介绍下如何安装 安装和卸载homebrew 安装 /usr/bin/ruby -e "$(curl - ...