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 ...
随机推荐
- Python3 出现'ascii' codec can't encode characters问题
当使用urllib.request.urlopen打开包含中文的链接时报错: from urllib import request url = 'https://baike.baidu.com/ite ...
- 日期时间设置 "2018-05-04T16:36:23.6341371+08:00" 格式
using System;using System.Collections.Generic;using System.Globalization;using System.Text; namespac ...
- 洛谷P2422 良好的感觉
题目意思就是:最大化一个区间的和与这个区间的最小值的乘积. 换一个角度看问题,如果我们穷举一个最小值 $ a_i $ ,然后往左右扩展,显然是对的,复杂度 $ O(n^2) $.所以我们要优化一下这个 ...
- type Iterator does not take parameters
在ubuntu编译java程序时报错:type Iterator does not take parameters 源码如下: package object; import java.util.*; ...
- SQLSERVER 2008 编辑所有或者任意行
选中表 右键选择“编辑前200行”,然后选择左上角的 sql图标,然后在右侧的SQL语句去掉 top 200 然后执行查询 就可以编辑所有的行了,可以选择自己需要写SQL,然后查询编辑. 第二种方法: ...
- 第六届CCF软件能力认证
1.数位之和 问题描述 给定一个十进制整数n,输出n的各位数字之和. 输入格式 输入一个整数n. 输出格式 输出一个整数,表示答案. 样例输入 20151220 样例输出 13 样例说明 201512 ...
- 丑数(UVa136)
题目具体描述见:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=835&a ...
- Rookey.Frame之菜单设置
在上一篇博文 Rookey.Frame企业级快速开发框架开源了 中我们介绍了Rookey.Frame极速开发框架的最新更新及开源介绍,后面慢慢介绍该框架的使用方法,本人文笔不好,写得不够好的地方请大家 ...
- xmanager
[root@upright91 run]# ./runBenchmark.sh updbtpcc.properties sqlTableCreates Exception in thread &quo ...
- idea导入或打开项目配置问题
learn项目遇到问题: 1.IntelliJ Idea编译报错:请使用 -source 7 或更高版本以启用 diamond 运算符 file - project structure或者直接快捷键: ...