POJ 1279 Art Gallery【半平面交】(求多边形的核)(模板题)
<题目链接>
题目大意:
按顺时针顺序给出一个N边形,求N边形的核的面积。
(多边形的核:它是平面简单多边形的核是该多边形内部的一个点集该点集中任意一点与多边形边界上一点的连线都处于这个多边形内部。)
#include <cstdio>
#include <cmath>
#include <iostream>
using namespace std;
#define eps 1e-8
const int MAXN=;
int n;
double r;
int cCnt,curCnt;
struct point
{
double x,y;
point(double a=,double b=):x(a),y(b){}
};
point points[MAXN],p[MAXN],q[MAXN]; point operator - (point A,point B) { return point(A.x-B.x,A.y-B.y); } double Cross(point A,point B){ return A.x*B.y-A.y*B.x; } void getline(point x,point y,double &a,double &b,double &c) //得到直线的方程系数
{
a = y.y - x.y;
b = x.x - y.x;
c = y.x * x.y - x.x * y.y;
} void initial() //初始化放置多边形顶点的p数组
{
for(int i = ; i <= n; i++)p[i] = points[i]; p[n+] = p[];
p[] = p[n]; cCnt = n;
} point intersect(point x,point y,double a,double b,double c) //求点x、y形成的直线与已知直线a、b、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 = ;
int i;
for(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(i = ; i <= curCnt; ++i)p[i] = q[i];
p[curCnt+] = q[];
p[] = p[curCnt];
cCnt = curCnt;
} void solve()
{
initial();
for(int i = ; i <= n; ++i)
{
double a,b,c;
getline(points[i],points[i+],a,b,c);
cut(a,b,c); //用原多边形的边界去切割p数组表示的内核多边形
}
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i = ; i <= n; i++)
{
scanf("%lf%lf",&points[i].x,&points[i].y);
}
points[n+] = points[];
solve();
if(cCnt<=) //如果1或者两个点,直接输出面积为0.00
printf("%.2lf\n",);
else
{
double area=; //利用叉乘算出多边形的面积
for(int i=;i<cCnt;i++)
{
area+=Cross(p[i]-p[],p[i+]-p[]);
}
printf("%.2lf\n",fabs(area)/2.0);
}
}
return ;
}
2018-08-03
POJ 1279 Art Gallery【半平面交】(求多边形的核)(模板题)的更多相关文章
- POJ 1279 Art Gallery 半平面交求多边形核
第一道半平面交,只会写N^2. 将每条边化作一个不等式,ax+by+c>0,所以要固定顺序,方便求解. 半平面交其实就是对一系列的不等式组进行求解可行解. 如果某点在直线右侧,说明那个点在区域内 ...
- POJ 1279 Art Gallery 半平面交 多边形的核
题意:求多边形的核的面积 套模板即可 #include <iostream> #include <cstdio> #include <cmath> #define ...
- POJ 1279 Art Gallery 半平面交/多边形求核
http://poj.org/problem?id=1279 顺时针给你一个多边形...求能看到所有点的面积...用半平面对所有边取交即可,模版题 这里的半平面交是O(n^2)的算法...比较逗比.. ...
- POJ 1279 Art Gallery(半平面交)
题目链接 回忆了一下,半平面交,整理了一下模版. #include <cstdio> #include <cstring> #include <string> #i ...
- POJ 3130 How I Mathematician Wonder What You Are!(半平面交求多边形的核)
题目链接 题意 : 给你一个多边形,问你该多边形中是否存在一个点使得该点与该多边形任意一点的连线都在多边形之内. 思路 : 与3335一样,不过要注意方向变化一下. #include <stdi ...
- POJ 1279 Art Gallery(半平面交求多边形核的面积)
题目链接 题意 : 求一个多边形的核的面积. 思路 : 半平面交求多边形的核,然后在求面积即可. #include <stdio.h> #include <string.h> ...
- poj 1279 Art Gallery - 求多边形核的面积
/* poj 1279 Art Gallery - 求多边形核的面积 */ #include<stdio.h> #include<math.h> #include <al ...
- poj 1279 -- Art Gallery (半平面交)
鏈接:http://poj.org/problem?id=1279 Art Gallery Time Limit: 1000MS Memory Limit: 10000K Total Submis ...
- poj 3525 半平面交求多边形内切圆最大半径【半平面交】+【二分】
<题目链接> 题目大意:给出一个四面环海的凸多边形岛屿,求出这个岛屿中的点到海的最远距离. 解题分析: 仔细思考就会发现,其实题目其实就是让我们求该凸多边形内内切圆的最大半径是多少.但是, ...
- poj 1279 Art Gallery (Half Plane Intersection)
1279 -- Art Gallery 还是半平面交的问题,要求求出多边形中可以观察到多边形所有边的位置区域的面积.其实就是把每一条边看作有向直线然后套用半平面交.这题在输入的时候应该用多边形的有向面 ...
随机推荐
- transition,过渡效果
语法: transtion:property time change-speed delay. 人话就是:属性(property )在多少秒内(time )通过什么样的速度(change-speed) ...
- html 速查表
HTML 速查列表 HTML 速查列表. 你可以打印它,以备日常使用. HTML 基本文档 <!DOCTYPE html> <html> <head> <ti ...
- 【CXF】: No binding operation info while invoking unknown method with params unknown.
CXF发布webservice之后访问报错: org.apache.cxf.interceptor.Fault: No binding operation info while invoking un ...
- Junit的Assert用法
package junit.framework; /** * A set of assert methods. Messages are only displayed when an assert f ...
- ubuntu14.04 放开串口权限
可以用如下命令查看串口信息: ls -l /dev/ttyUSB*来查看相关的信息. 但是普通用户没有usb操作权限(函数open()打不开串口:refused),如果我们想在ROS程序里面打开串口, ...
- Docker容器命令
★根本前提:本地主机有镜像才能创建容器 ⒈docker run [Options] 镜像名称或镜像ID [Command] [Arg...] 用途:利用镜像创建容器实例 Options说明(常用):注 ...
- 身份证号校验原理及JavaScript实现
在网站中,总有各种各样的表单,用户使用表单来向服务器发送数据,进行交互. 然而,代代相传的经验是,永远不要信任用户的输入,一定要对数据进行验证.如果使用不经验证的表单,轻则会有大量无效提交 ...
- linux关机时候执行命令脚本或程序
Write a service file and place it in /etc/systemd/system/beforeshuttingdown.service code: [Unit] Des ...
- Bootstrap3.0学习第二轮(栅格系统原理)
详情请查看 http://aehyok.com/Blog/Detail/8.html 个人网站地址:aehyok.com QQ 技术群号:206058845,验证码为:aehyok 本文文章链接:ht ...
- Websphere MQ Cluster
大纲: 1.什么是集群 2.建立一个基本的集群 3.DISPLAY命令 4.负载均衡 5.高级配置和管理 6.答疑 7.关于文章.红宝书等 一. 什么是集群 集群就是Websphere M ...