/*
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 - 求多边形核的面积的更多相关文章

  1. POJ 1279 Art Gallery(半平面交求多边形核的面积)

    题目链接 题意 : 求一个多边形的核的面积. 思路 : 半平面交求多边形的核,然后在求面积即可. #include <stdio.h> #include <string.h> ...

  2. poj 1279 -- Art Gallery (半平面交)

    鏈接:http://poj.org/problem?id=1279 Art Gallery Time Limit: 1000MS   Memory Limit: 10000K Total Submis ...

  3. poj 1279 Art Gallery (Half Plane Intersection)

    1279 -- Art Gallery 还是半平面交的问题,要求求出多边形中可以观察到多边形所有边的位置区域的面积.其实就是把每一条边看作有向直线然后套用半平面交.这题在输入的时候应该用多边形的有向面 ...

  4. POJ 1279 Art Gallery 半平面交求多边形核

    第一道半平面交,只会写N^2. 将每条边化作一个不等式,ax+by+c>0,所以要固定顺序,方便求解. 半平面交其实就是对一系列的不等式组进行求解可行解. 如果某点在直线右侧,说明那个点在区域内 ...

  5. POJ 1279 Art Gallery 半平面交/多边形求核

    http://poj.org/problem?id=1279 顺时针给你一个多边形...求能看到所有点的面积...用半平面对所有边取交即可,模版题 这里的半平面交是O(n^2)的算法...比较逗比.. ...

  6. POJ 1279 Art Gallery【半平面交】(求多边形的核)(模板题)

    <题目链接> 题目大意: 按顺时针顺序给出一个N边形,求N边形的核的面积. (多边形的核:它是平面简单多边形的核是该多边形内部的一个点集该点集中任意一点与多边形边界上一点的连线都处于这个多 ...

  7. [POJ]1279: Art Gallery

    题目大意:有一个N边形展馆,问展馆内有多少地方可以看到所有墙壁.(N<=1500) 思路:模板题,半平面交求出多边形的核后计算核的面积. #include<cstdio> #incl ...

  8. POJ 1279 Art Gallery 半平面交 多边形的核

    题意:求多边形的核的面积 套模板即可 #include <iostream> #include <cstdio> #include <cmath> #define ...

  9. POJ 1279 Art Gallery(半平面交)

    题目链接 回忆了一下,半平面交,整理了一下模版. #include <cstdio> #include <cstring> #include <string> #i ...

随机推荐

  1. GO里的“指针”

    指针 *T即为类型T的指针 &t即为获取变量t的地址 *p即为获取指针变量所指向的内容 var p *int 指针的*在左边  类型在右边.这里的 *int就是一个指针类型. 跟int str ...

  2. fsevents npm install是报错

    npm install 安装插件的时候,fsevents报错,这是node 8.x版本的问题,解决办法,把node 版本切换到6.x

  3. 前端打包工具之fis3的初级使用

    说到打包工具,大家都会想到webpack,我之前也接触过webpack,说实话个人觉得webpack上手容易,但是对于新手来说里面有太多坑,配置文件也不简单.于是乎,我转入了fis3阵营,发现fis3 ...

  4. 使用Dockerfile构建docker lnmp环境

    一.mysql 1.创建 Dockerfile mkdir mysql # 创建一个目录存放之后的Dockerfile,目录名无所谓 cd mysql # 进入目录 vi Dockerfile # 创 ...

  5. 洛谷P3811乘法逆元

    传送门 线性递推 #include <iostream> #include <cstdio> #include <cstring> #include <alg ...

  6. CSS常见简写规则整理

    外边距(margin) margin-top margin-right margin-bottom margin-left 简写顺序为顺时针方向(上.右.下.左),如:margin: 1px 2px ...

  7. java解析Xml格式的字符串

    最近在工作中,需要调别的接口,接口返回的是一个字符串,而且内容是xml格式的,结果在解析json的时候报错,最终修改了接口的返回方式,以Map返回, 才得以接收到这个xml的字符串,然后通过dom4j ...

  8. WINDOWS 2008 采用IP策略解决445,139等病毒攻击问题

    @echo off title 创建IP安全策略,屏蔽135.. . . . 等端口 :: 配置说明文档地址 :: http://blog.csdn.net/lpc_china/article/det ...

  9. WORDPRESS登录后台半天都无法访问或者是访问慢的解决方法

    wordpress登录后台如果打开速度慢,一般分为两部分,第一部分是php虚拟主机的原因,其中主机的原因,又分为很多种情况.第二部分就是WordPress程序本身的问题.这里无忧主机小编主要是讲第二部 ...

  10. .NETCore分布式微服务站点设计(1)-概念图

    自己画了一个简略结构图,准备按照这个搭建一套微服务型的站点 利用Identityserver4+Redis+Sqlserver+Swagger+阿里云OSS+RabbitMQ+Nginx来实现,按照自 ...