poj 1279 Art Gallery - 求多边形核的面积
/*
poj 1279 Art Gallery - 求多边形核的面积
*/
#include<stdio.h>
#include<math.h>
#include <algorithm>
using namespace std; const double eps=1e-8;
struct point
{
double x,y;
}dian[20000+10];
point jiao[203];
struct line
{
point s,e;
double angle;
}xian[20000+10];
int n,yong;
bool mo_ee(double x,double y)
{
double ret=x-y;
if(ret<0) ret=-ret;
if(ret<eps) return 1;
return 0;
}
bool mo_gg(double x,double y) { return x > y + eps;} // x > y
bool mo_ll(double x,double y) { return x < y - eps;} // x < y
bool mo_ge(double x,double y) { return x > y - eps;} // x >= y
bool mo_le(double x,double y) { return x < y + eps;} // x <= y
point mo_intersection(point u1,point u2,point v1,point v2)
{
point ret=u1;
double t=((u1.x-v1.x)*(v1.y-v2.y)-(u1.y-v1.y)*(v1.x-v2.x))
/((u1.x-u2.x)*(v1.y-v2.y)-(u1.y-u2.y)*(v1.x-v2.x));
ret.x+=(u2.x-u1.x)*t;
ret.y+=(u2.y-u1.y)*t;
return ret;
}
double mo_xmult(point p2,point p0,point p1)//p1在p2左返回负,在右边返回正
{
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
} void mo_HPI_addl(point a,point b)
{
xian[yong].s=a;
xian[yong].e=b;
xian[yong].angle=atan2(b.y-a.y,b.x-a.x);
yong++;
}
//半平面交
bool mo_HPI_cmp(const line& a,const line& b)
{
if(mo_ee(a.angle,b.angle))
{
return mo_gg( mo_xmult(b.e,a.s,b.s),0);
}else
{
return mo_ll(a.angle,b.angle);
}
}
int mo_HPI_dq[20000+10];
bool mo_HPI_isout(line cur,line top,line top_1)
{
point jiao=mo_intersection(top.s,top.e,top_1.s,top_1.e);
return mo_ll( mo_xmult(cur.e,jiao,cur.s),0);
}
int mo_HalfPlaneIntersect(line *xian,int n,point *jiao)
{
int i,j,ret=0;
sort(xian,xian+n,mo_HPI_cmp);
for (i = 0, j = 0; i < n; i++)
{
if (mo_gg(xian[i].angle,xian[j].angle))
{
xian[++j] = xian[i];
}
}
n=j+1;
mo_HPI_dq[0]=0;
mo_HPI_dq[1]=1;
int top=1,bot=0;
for (i = 2; i < n; i++)
{
while (top > bot && mo_HPI_isout(xian[i], xian[mo_HPI_dq[top]], xian[mo_HPI_dq[top-1]])) top--;
while (top > bot && mo_HPI_isout(xian[i], xian[mo_HPI_dq[bot]], xian[mo_HPI_dq[bot+1]])) bot++;
mo_HPI_dq[++top] = i; //当前半平面入栈
}
while (top > bot && mo_HPI_isout(xian[mo_HPI_dq[bot]], xian[mo_HPI_dq[top]], xian[mo_HPI_dq[top-1]])) top--;
while (top > bot && mo_HPI_isout(xian[mo_HPI_dq[top]], xian[mo_HPI_dq[bot]], xian[mo_HPI_dq[bot+1]])) bot++;
mo_HPI_dq[++top] = mo_HPI_dq[bot];
for (ret = 0, i = bot; i < top; i++, ret++)
{
jiao[ret]=mo_intersection(xian[mo_HPI_dq[i+1]].s,xian[mo_HPI_dq[i+1]].e,xian[mo_HPI_dq[i]].s,xian[mo_HPI_dq[i]].e);
}
return ret;
}
//求多边形面积
double mo_area_polygon(point *dian,int n)
{
int i;
point yuan;
yuan.x=yuan.y=0;
double ret=0;
for(i=0;i<n;++i)
{
ret+=mo_xmult(dian[(i+1)%n],yuan,dian[i]);
}
return ret;
} int main()
{
int i,iofcase=1,t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
yong=0;
for(i=0;i<n;++i)
{
scanf("%lf%lf",&dian[i].x,&dian[i].y);
}
double area=mo_area_polygon(dian,n);
if(area<0)//若是顺时针
{
for(i=0;i<n;++i)
{
mo_HPI_addl(dian[(i+1)%n],dian[i]);
}
}else
{
for(i=0;i<n;++i)
{
mo_HPI_addl(dian[i],dian[(i+1)%n]);
}
}
int ret=mo_HalfPlaneIntersect(xian,n,jiao);
area=mo_area_polygon(jiao,ret);
if(area<0) area=-area;
area=area/2;
printf("%.2lf\n",area);
}
return 0;
}
poj 1279 Art Gallery - 求多边形核的面积的更多相关文章
- POJ 1279 Art Gallery(半平面交求多边形核的面积)
题目链接 题意 : 求一个多边形的核的面积. 思路 : 半平面交求多边形的核,然后在求面积即可. #include <stdio.h> #include <string.h> ...
- poj 1279 -- Art Gallery (半平面交)
鏈接:http://poj.org/problem?id=1279 Art Gallery Time Limit: 1000MS Memory Limit: 10000K Total Submis ...
- poj 1279 Art Gallery (Half Plane Intersection)
1279 -- Art Gallery 还是半平面交的问题,要求求出多边形中可以观察到多边形所有边的位置区域的面积.其实就是把每一条边看作有向直线然后套用半平面交.这题在输入的时候应该用多边形的有向面 ...
- POJ 1279 Art Gallery 半平面交求多边形核
第一道半平面交,只会写N^2. 将每条边化作一个不等式,ax+by+c>0,所以要固定顺序,方便求解. 半平面交其实就是对一系列的不等式组进行求解可行解. 如果某点在直线右侧,说明那个点在区域内 ...
- POJ 1279 Art Gallery 半平面交/多边形求核
http://poj.org/problem?id=1279 顺时针给你一个多边形...求能看到所有点的面积...用半平面对所有边取交即可,模版题 这里的半平面交是O(n^2)的算法...比较逗比.. ...
- POJ 1279 Art Gallery【半平面交】(求多边形的核)(模板题)
<题目链接> 题目大意: 按顺时针顺序给出一个N边形,求N边形的核的面积. (多边形的核:它是平面简单多边形的核是该多边形内部的一个点集该点集中任意一点与多边形边界上一点的连线都处于这个多 ...
- [POJ]1279: Art Gallery
题目大意:有一个N边形展馆,问展馆内有多少地方可以看到所有墙壁.(N<=1500) 思路:模板题,半平面交求出多边形的核后计算核的面积. #include<cstdio> #incl ...
- POJ 1279 Art Gallery 半平面交 多边形的核
题意:求多边形的核的面积 套模板即可 #include <iostream> #include <cstdio> #include <cmath> #define ...
- POJ 1279 Art Gallery(半平面交)
题目链接 回忆了一下,半平面交,整理了一下模版. #include <cstdio> #include <cstring> #include <string> #i ...
随机推荐
- Win10搜索不能用
使用win10进行搜索时,一直显示win10特色的滚动条,但就是检索不出东西,我的主要是检索不到windows的内容: (个人感觉使用win10检索网页内容不太专业,就关闭了Web搜索) 最后有发现网 ...
- jQuery之字体大小的设置
先获取字体大小,进行处理. 再将修改的值保存. slice() 方法可从已有的数组中返回选定的元素.arrayObject.slice(start,end).start 必需.规定从何处开始选 ...
- js中的事件委托或是事件代理
JavaScript(jQuery)中的事件委托 https://www.cnblogs.com/zhoushengxiu/p/5703095.html js中的事件委托或是事件代理详解 https: ...
- 序列化 json和pickle
序列化 1. 什么叫序列化 将原本的字典.列表等内容转换成一个字符串的过程就叫做序列化. 2. json dumps loads 一般对字典和列表序列化 dump load 一般对 ...
- POJ 1236 Network of Schools(tarjan求强连通分量+思维)
题目链接:http://poj.org/problem?id=1236 题目大意: 给你一个网络(有向图),有两个任务: ①求出至少同时需要几份副本可以使得整个网络都获得副本 ②至少添加多少信息表(有 ...
- python 多线程删除MySQL表
一.需求分析 在<python 统计MySQL表信息>这篇博客中,链接如下: https://www.cnblogs.com/xiao987334176/p/9901692.html 已经 ...
- Linux学习笔记:mv移动或文件重命名
mv命令是move的缩写,可以用来移动文件或者将文件改名(move (rename) files),是Linux系统下常用的命令,经常用来备份文件或者目录. 语法:mv 源文件 目标文件 视mv命令中 ...
- Hive(四)Hive的3种连接方式与DbVisualizer连接Hive
一.CLI连接 进入到 bin 目录下,直接输入命令: [root@node21 ~]# hive SLF4J: Class path contains multiple SLF4J bindings ...
- pyqt5猜数小程序
程序界面用qt设计师制作,并用pyuic5命令转换成form.py文件 #-*- coding:utf-8 -*- from PyQt5.QtWidgets import QApplication,Q ...
- Ubuntu 17.04 搭建 NodeJS
可以在云主机上执行以下的命令: apt-get update apt-get install -y python-software-properties software-properties-com ...