poj 3525 半平面交求多边形内切圆最大半径【半平面交】+【二分】
<题目链接>
题目大意:
给出一个四面环海的凸多边形岛屿,求出这个岛屿中的点到海的最远距离。
解题分析:
仔细思考就会发现,其实题目其实就是让我们求该凸多边形内内切圆的最大半径是多少。但是,这个最大半径,没有什么比较好的求法,于是,我们可以想到二分答案求半径。对于二分的半径,我们可以将该凸多边形的边界向内平移 r 的距离,然后再用半平面交法,用这些平移后的直线去切割原凸多边形,如果最终切得的区域不为空,则二分枚举更大的半径,反之减小枚举的半径。知道恰好围成的区域为空(或恰好不为空)为止。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std; const double eps = 1e-;
const double inf = 1e9;
const int MAXN = ;
int m;//保存多边形的点数
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 = ; i <= m; ++i)p[i] = points[i];
p[m + ] = p[];
p[] = p[m];
cCnt = m;
} point intersect(point x, point y, double a, double b, double 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 = ;
for (int i = ; i <= cCnt; ++i)
{
if (a*p[i].x + b * p[i].y + c >= )q[++curCnt] = p[i]; // c因为精度问题,可能会偏小。所以有些点本应在右側而没在。
else
{
if (a*p[i - ].x + b * p[i - ].y + c > )
{
q[++curCnt] = intersect(p[i], p[i - ], a, b, c);
}
if (a*p[i + ].x + b * p[i + ].y + c > )
{
q[++curCnt] = intersect(p[i], p[i + ], a, b, c);
}
}
} for (int i = ; i <= curCnt; ++i)p[i] = q[i];
p[curCnt + ] = q[];
p[] = p[curCnt];
cCnt = curCnt;
} int dcmp(double x) //控制精度
{
if (fabs(x)<eps) return ;
else return x< ? - : ;
} void solve()
{
initial(); //初始化存放多边形顶点的p数组 for (int i = ; i <= m; ++i) { point ta, tb, tt; //得到平移后的直线
tt.x = points[i + ].y - points[i].y;
tt.y = points[i].x - points[i + ].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 + ].x + tt.x;
tb.y = points[i + ].y + tt.y; double a, b, c; //接下来用这些平移后的直线去切割原多边形
getline(ta, tb, a, b, c);
cut(a, b, c);
}
} void Reverse() { //规整化方向,逆时针变顺时针,顺时针变逆时针
for (int i = ; i < (m + ) / ; i++)
swap(points[i], points[m - i]);
} int main()
{
while (scanf("%d", &m) != EOF) {
if (m == ) break;
for (int i = ; i <= m; i++)
scanf("%lf%lf", &points[i].x, &points[i].y);
Reverse(); //由于点的顺序是逆时针输入,所以要将它改成顺时针
points[m + ] = points[]; double left = , right = inf, mid;
while ((right - left) >= eps) { //二分求半径,eps控制二分的精度
mid = (left + right) / 2.0;
r = mid; //r为内切圆半径
solve();
if (cCnt <= ) right = mid; //如果将该多边形顶点向内平移r的距离后,半平面交所得多边形为空,则说明r过大,应当适当缩小
else left = mid;
}
printf("%.6f\n", left);
}
return ;
}
2018-08-03
poj 3525 半平面交求多边形内切圆最大半径【半平面交】+【二分】的更多相关文章
- poj 1474 Video Surveillance - 求多边形有没有核
/* poj 1474 Video Surveillance - 求多边形有没有核 */ #include <stdio.h> #include<math.h> const d ...
- POJ 3130 How I Mathematician Wonder What You Are!(半平面交求多边形的核)
题目链接 题意 : 给你一个多边形,问你该多边形中是否存在一个点使得该点与该多边形任意一点的连线都在多边形之内. 思路 : 与3335一样,不过要注意方向变化一下. #include <stdi ...
- POJ 3525 Most Distant Point from the Sea (半平面交向内推进+二分半径)
题目链接 题意 : 给你一个多边形,问你里边能够盛的下的最大的圆的半径是多少. 思路 :先二分半径r,半平面交向内推进r.模板题 #include <stdio.h> #include & ...
- POJ 3525 Most Distant Point from the Sea (半平面交+二分)
Most Distant Point from the Sea Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 3476 ...
- POJ 3525/UVA 1396 Most Distant Point from the Sea(二分+半平面交)
Description The main land of Japan called Honshu is an island surrounded by the sea. In such an isla ...
- poj3675 求多边形与圆的面积交
题意:给出多边形的顶点坐标.圆的圆心坐标和半径,求面积交 sol:又是模板题啦= = 注意poj的C++好像认不出hypot函数,要稍微改写一下. hypot(double x,double y):即 ...
- poj 3348 Cows 凸包 求多边形面积 计算几何 难度:0 Source:CCC207
Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7038 Accepted: 3242 Description ...
- POJ 1279 Art Gallery(半平面交求多边形核的面积)
题目链接 题意 : 求一个多边形的核的面积. 思路 : 半平面交求多边形的核,然后在求面积即可. #include <stdio.h> #include <string.h> ...
- POJ 3335 Rotating Scoreboard(半平面交求多边形核)
题目链接 题意 : 给你一个多边形,问你在多边形内部是否存在这样的点,使得这个点能够看到任何在多边形边界上的点. 思路 : 半平面交求多边形内核. 半平面交资料 关于求多边形内核的算法 什么是多边形的 ...
随机推荐
- luogu P3674 小清新人渣的本愿
传送门 毒瘤lxl 本质是莫队,关键是怎么处理询问 这里需要开两个bitset(记为\(b1,b2\)),分别存\(x\)和\(n-x\)是否出现 对于询问1,即\(x-y=z\),由于\(y=x-z ...
- CSS —— 选择器
选择器种类 标签选择器 id选择器 类选择器 通配符 交集选择器 并集选择器 后代选择器 子代选择器 选择器设置样式优先级 默认样式 < 继承样式 < 通配符设置样式 < 标签选择器 ...
- Android 中查看内存的使用情况集常用adb命令
http://blog.csdn.net/bigconvience/article/details/35553983 http://blog.csdn.net/duantihi/article/det ...
- python3爬虫二
1.获取列表页文章url集合: scrapy shell http://blog.jobbole.com/all-posts/ response.css('div.post-meta a.archiv ...
- Qt5.7 无法输入中文问题
把libfcitxplatforminputcontextplugin.so复制到安装的Qt目录下的两个文件夹中 sudo apt install fcitx-frontend-qt5 sudo cp ...
- ubuntu 14.04 安装 OpenCV -2.4.13
1. 安装 (1) 更新软件源 sudo apt-get update sudo apt-get upgrade (2)删除以前安装的 FFMPEG 和 x264 库: sudo apt-get re ...
- windows安装anaconda 报错failed to create anacoda menu ?
windows安装anaconda 报错failed to create anacoda menu ? 装了无数次,每次都是 failed to create anacoda menu然后无选择忽略, ...
- TypeError: 'range' object does not support item assignment
TypeError: 'range' object does not support item assignment I was looking at some python 2.x code and ...
- P3567 [POI2014]KUR-Couriers
题目描述 Byteasar works for the BAJ company, which sells computer games. The BAJ company cooperates with ...
- Date ——日期型函数Date常用API
获取当前时间戳: let now = new Date().getTime() 获取某个时间点(比如12点)的时间戳: let date = new Date('2019-01-12 12:00:0 ...