题目传送门

题意:判断给定的多边形是否为凸的,peg(pig?)是否在多边形内,且以其为圆心的圆不超出多边形(擦着边也不行)。

分析:判断凸多边形就用凸包,看看点集的个数是否为n。在多边形内用叉积方向来判断,最后再用点到直线的距离和半径比大小(不是线段)

/************************************************
* Author :Running_Time
* Created Time :2015/11/2 星期一 19:49:13
* File Name :POJ_1584.cpp
************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 1e5 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const double EPS = 1e-10;
const double PI = acos (-1.0);
int dcmp(double x) { //三态函数,减少精度问题
if (fabs (x) < EPS) return 0;
else return x < 0 ? -1 : 1;
}
struct Point { //点的定义
double x, y;
Point () {}
Point (double x, double y) : x (x), y (y) {}
Point operator + (const Point &r) const { //向量加法
return Point (x + r.x, y + r.y);
}
Point operator - (const Point &r) const { //向量减法
return Point (x - r.x, y - r.y);
}
Point operator * (double p) const { //向量乘以标量
return Point (x * p, y * p);
}
Point operator / (double p) const { //向量除以标量
return Point (x / p, y / p);
}
bool operator < (const Point &r) const { //点的坐标排序
return x < r.x || (x == r.x && y < r.y);
}
bool operator == (const Point &r) const { //判断同一个点
return dcmp (x - r.x) == 0 && dcmp (y - r.y) == 0;
}
};
typedef Point Vector; //向量的定义
Point read_point(void) { //点的读入
double x, y;
scanf ("%lf%lf", &x, &y);
return Point (x, y);
}
double dot(Vector A, Vector B) { //向量点积
return A.x * B.x + A.y * B.y;
}
double cross(Vector A, Vector B) { //向量叉积
return A.x * B.y - A.y * B.x;
}
double polar_angle(Vector A) { //向量极角
return atan2 (A.y, A.x);
}
double length(Vector A) { //向量长度,点积
return sqrt (dot (A, A));
}
double angle(Vector A, Vector B) { //向量转角,逆时针,点积
return acos (dot (A, B) / length (A) / length (B));
}
Vector rotate(Vector A, double rad) { //向量旋转,逆时针
return Vector (A.x * cos (rad) - A.y * sin (rad), A.x * sin (rad) + A.y * cos (rad));
}
Vector nomal(Vector A) { //向量的单位法向量
double len = length (A);
return Vector (-A.y / len, A.x / len);
}
Point line_line_inter(Point p, Vector V, Point q, Vector W) { //两直线交点,参数方程
Vector U = p - q;
double t = cross (W, U) / cross (V, W);
return p + V * t;
}
double point_to_line(Point p, Point a, Point b) { //点到直线的距离,两点式
Vector V1 = b - a, V2 = p - a;
return fabs (cross (V1, V2)) / length (V1);
}
double point_to_seg(Point p, Point a, Point b) { //点到线段的距离,两点式
if (a == b) return length (p - a);
Vector V1 = b - a, V2 = p - a, V3 = p - b;
if (dcmp (dot (V1, V2)) < 0) return length (V2);
else if (dcmp (dot (V1, V3)) > 0) return length (V3);
else return fabs (cross (V1, V2)) / length (V1);
}
Point point_line_proj(Point p, Point a, Point b) { //点在直线上的投影,两点式
Vector V = b - a;
return a + V * (dot (V, p - a) / dot (V, V));
}
bool can_seg_seg_inter(Point a1, Point a2, Point b1, Point b2) { //判断线段相交,两点式
double c1 = cross (a2 - a1, b1 - a1), c2 = cross (a2 - a1, b2 - a1),
c3 = cross (b2 - b1, a1 - b1), c4 = cross (b2 - b1, a2 - b1);
return dcmp (c1) * dcmp (c2) < 0 && dcmp (c3) * dcmp (c4) < 0;
}
bool can_line_seg_inter(Point a1, Point a2, Point b1, Point b2) { //判断直线与线段相交,两点式
double c1 = cross (a2 - a1, b1 - a1), c2 = cross (a2 - a1, b2 - a1);
return dcmp (c1 * c2) <= 0;
}
bool on_seg(Point p, Point a1, Point a2) { //判断点在线段上,两点式
return dcmp (cross (a1 - p, a2 - p)) == 0 && dcmp (dot (a1 - p, a2 - p)) < 0;
}
double area_triangle(Point a, Point b, Point c) { //三角形面积,叉积
return fabs (cross (b - a, c - a)) / 2.0;
}
double area_poly(Point *p, int n) { //多边形面积,叉积
double ret = 0;
for (int i=1; i<n-1; ++i) {
ret += fabs (cross (p[i] - p[0], p[i+1] - p[0]));
}
return ret / 2;
}
/*
点集凸包,输入点的集合,返回凸包点的集合。
如果不希望在凸包的边上有输入点,把两个 <= 改成 <
*/
vector<Point> convex_hull(vector<Point> ps) {
sort (ps.begin (), ps.end ()); //x - y排序
ps.erase (unique (ps.begin (), ps.end ()), ps.end ()); //删除重复点
int n = ps.size (), k = 0;
vector<Point> qs (n * 2);
for (int i=0; i<n; ++i) {
while (k > 1 && cross (qs[k-1] - qs[k-2], ps[i] - qs[k-1]) < 0) k--;
qs[k++] = ps[i];
}
for (int i=n-2, t=k; i>=0; --i) {
while (k > t && cross (qs[k-1] - qs[k-2], ps[i] - qs[k-1]) < 0) k--;
qs[k++] = ps[i];
}
qs.resize (k-1);
return qs;
} struct Circle {
Point c;
double r;
Circle () {}
Circle (Point c, double r) : c (c), r (r) {}
Point point(double a) {
return Point (c.x + cos (a) * r, c.y + sin (a) * r);
}
};
struct Line {
Point p;
Vector v;
double r;
Line () {}
Line (const Point &p, const Vector &v) : p (p), v (v) {
r = polar_angle (v);
}
Point point(double a) {
return p + v * a;
}
};
/*
直线相交求交点,返回交点个数,交点保存在P中
*/
int line_cir_inter(Line L, Circle C, double &t1, double &t2, vector<Point> &P) {
double a = L.v.x, b = L.p.x - C.c.x, c = L.v.y, d = L.p.y - C.c.y;
double e = a * a + c * c, f = 2 * (a * b + c * d), g = b * b + d * d - C.r * C.r;
double delta = f * f - 4 * e * g;
if (dcmp (delta) < 0) return 0;
if (dcmp (delta) == 0) {
t1 = t2 = -f / (2 * e); P.push_back (L.point (t1));
return -1;
}
t1 = (-f - sqrt (delta)) / (2 * e); P.push_back (L.point (t1));
t2 = (-f + sqrt (delta)) / (2 * e); P.push_back (L.point (t2));
if (dcmp (t1) < 0 || dcmp (t2) < 0) return 0;
return 2;
} /*
两圆相交求交点,返回交点个数。交点保存在P中
*/
int cir_cir_inter(Circle C1, Circle C2, vector<Point> &P) {
double d = length (C1.c - C2.c);
if (dcmp (d) == 0) {
if (dcmp (C1.r - C2.r) == 0) return -1; //两圆重叠
else return 0;
}
if (dcmp (C1.r + C2.r - d) < 0) return 0;
if (dcmp (fabs (C1.r - C2.r) - d) < 0) return 0;
double a = polar_angle (C2.c - C1.c);
double da = acos ((C1.r * C1.r + d * d - C2.r * C2.r) / (2 * C1.r * d)); //C1C2到C1P1的角?
Point p1 = C1.point (a - da), p2 = C2.point (a + da);
P.push_back (p1);
if (p1 == p2) return 1;
else P.push_back (p2);
return 2;
}
/*
过点到圆的切线,返回切线条数,切线保存在V中
*/
int point_cir_tan(Point p, Circle C, Vector *V) {
Vector u = C.c - p;
double dis = length (u);
if (dis < C.r) return 0;
else if (dcmp (dis - C.r) == 0) {
V[0] = rotate (u, PI / 2); return 1;
}
else {
double ang = asin (C.r / dis);
V[0] = rotate (u, -ang);
V[1] = rotate (u, +ang);
return 0;
}
}
/*
两圆的公切线,返回公切线条数,切线短点保存在a和b中
*/
int cir_cir_tan(Circle A, Circle B, Point *a, Point *b) {
int cnt = 0;
if (A.r < B.r) {
swap (A, B); swap (a, b);
}
double d = dot (A.c - B.c, A.c - B.c);
double rsub = A.r - B.r, rsum = A.r + B.r;
if (dcmp (d - rsub) < 0) return 0; //内含
double base = polar_angle (B.c - A.c);
if (dcmp (d) == 0 && dcmp (A.r - B.r) == 0) return -1; //两圆重叠
if (dcmp (d - rsub) == 0) { //内切,一条切线
a[cnt] = A.point (base); b[cnt] = B.point (base); cnt++;
return 1;
}
//有外公切线
double ang = acos (rsub / d);
a[cnt] = A.point (base + ang); b[cnt] = B.point (base + ang); cnt++;
a[cnt] = A.point (base - ang); b[cnt] = B.point (base - ang); cnt++;
if (d == rsum) {
a[cnt] = A.point (base); b[cnt] = B.point (base + PI); cnt++;
}
else if (dcmp (d - rsum) > 0) { //两条内公切线
double ang2 = acos (rsum / d);
a[cnt] = A.point (base + ang2); b[cnt] = B.point (base + ang2 + PI); cnt++;
a[cnt] = A.point (base - ang2); b[cnt] = B.point (base - ang2 + PI); cnt++;
}
return cnt;
} int main(void) {
int n;
vector<Point> ps;
double r;
Point peg;
while (scanf ("%d", &n) == 1) {
if (n < 3) break;
scanf ("%lf", &r);
peg = read_point ();
ps.clear ();
for (int i=0; i<n; ++i) {
ps.push_back (read_point ());
}
vector<Point> qs = convex_hull (ps);
if (qs.size () < n) {
puts ("HOLE IS ILL-FORMED"); continue;
}
qs.push_back (qs[0]);
bool flag = true;
for (int i=0; i<n; ++i) {
if (dcmp (point_to_line (peg, qs[i], qs[i+1]) - r) < 0) {
flag = false; break;
}
if (cross (qs[i+1] - qs[i], peg - qs[i]) < 0) {
flag = false; break;
}
}
if (flag) puts ("PEG WILL FIT");
else puts ("PEG WILL NOT FIT");
} //cout << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; return 0;
}

  

简单几何(点的位置) POJ 1584 A Round Peg in a Ground Hole的更多相关文章

  1. POJ 1584 A Round Peg in a Ground Hole 判断凸多边形 点到线段距离 点在多边形内

    首先判断是不是凸多边形 然后判断圆是否在凸多边形内 不知道给出的点是顺时针还是逆时针,所以用判断是否在多边形内的模板,不用是否在凸多边形内的模板 POJ 1584 A Round Peg in a G ...

  2. POJ 1584 A Round Peg in a Ground Hole【计算几何=_=你值得一虐】

    链接: http://poj.org/problem?id=1584 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...

  3. POJ - 1584 A Round Peg in a Ground Hole(判断凸多边形,点到线段距离,点在多边形内)

    http://poj.org/problem?id=1584 题意 按照顺时针或逆时针方向输入一个n边形的顶点坐标集,先判断这个n边形是否为凸包. 再给定一个圆形(圆心坐标和半径),判断这个圆是否完全 ...

  4. POJ 1584 A Round Peg in a Ground Hole --判定点在形内形外形上

    题意: 给一个圆和一个多边形,多边形点可能按顺时针给出,也可能按逆时针给出,先判断多边形是否为凸包,再判断圆是否在凸包内. 解法: 先判是否为凸包,沿着i=0~n,先得出初始方向dir,dir=1为逆 ...

  5. POJ 1584 A Round Peg in a Ground Hole(判断凸多边形,点到线段距离,点在多边形内)

    A Round Peg in a Ground Hole Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4438   Acc ...

  6. POJ 1584 A Round Peg in a Ground Hole 判断凸多边形,判断点在凸多边形内

    A Round Peg in a Ground Hole Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5456   Acc ...

  7. POJ 1584 A Round Peg in a Ground Hole

    先判断是不是N多边形,求一下凸包,如果所有点都用上了,那么就是凸多边形 判断圆是否在多边形内, 先排除圆心在多边形外的情况 剩下的情况可以利用圆心到每条边的最短距离与半径的大小来判断 #include ...

  8. POJ 1584 A Round Peg in a Ground Hole[判断凸包 点在多边形内]

    A Round Peg in a Ground Hole Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6682   Acc ...

  9. POJ 1518 A Round Peg in a Ground Hole【计算几何=_=你值得一虐】

    链接: http://poj.org/problem?id=1584 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...

随机推荐

  1. Unix如何轻松快速复制

    笔者在实践中总结了一套Unix操作系统硬盘的快速复制方法,成功地运用于建行几大Unix操作系统网络的建设中,收到了良好的效果.现将该方法介绍如下,供读者参考. 系统要求,两台主机软尽量相同.要求被复制 ...

  2. Unity3d 去掉exe版本的边框

    原地址:http://blog.sina.com.cn/s/blog_697b1b8c0101gd4h.html using System; using System.Runtime.InteropS ...

  3. vimcommandfilepatchcmdfold VIM技巧之分隔窗口 一级精华

    VIM技巧之分隔窗口 分类: 技术2010-07-08 09:57 754人阅读 评论(1) 收藏 举报   同时显示两个不同的文件, 或者同时查看同一个文件的两个不同位置, 或者是同步显示两个文件的 ...

  4. Redis学习手册(Sorted-Sets数据类型)

    一.概述: Sorted-Sets和Sets类型极为相似,它们都是字符串的集合,都不允许重复的成员出现在一个Set中.它们之间的主要差别是Sorted-Sets中的每一个成员都会有一个分数(score ...

  5. k Sum | & ||

    k Sum Given n distinct positive integers, integer k (k <= n) and a number target. Find k numbers ...

  6. css调用外部样式和css样式说明剧中显示

    <title>边走边乔</title><link href="css/style.css" rel="stylesheet" ty ...

  7. maven web项目build失败

    通过maven build发布web项目到tomcat时报如下异常: [INFO] ---------------------------------------------------------- ...

  8. poj2778

    题意:给出字符串长度n(<=2000000000),给出不可以包含的序列,最多10个,每个长度最大是10.问长度为n的合法序列有多少个?序列中只可能包含ACTG四个字符. 分析:AC自动机(DF ...

  9. Java for LeetCode 041 First Missing Positive

    Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] ...

  10. codeforces 486B.OR in Matrix 解题报告

    题目链接:http://codeforces.com/problemset/problem/486/B 题目意思:给出一个m行n列的矩阵B(每个元素只由0/1组成),问是否可以利用矩阵B,通过一定的运 ...