POJ1279 给一个多边形 求它的核的面积

所谓多边形的核 是多边形中的一个点集 满足其中的点与多边形边上的点的连线全部在多边形中

用多边形的每一条边所在的直线去切整个坐标平面 得到的一个凸包就是核

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std; const double eps=1e-9; int cmp(double x)
{
if(fabs(x)<eps)return 0;
if(x>0)return 1;
else return -1;
} const double pi=acos(-1.0); inline double sqr(double x)
{
return x*x;
} struct point
{
double x,y;
point (){}
point (double a,double b):x(a),y(b){}
void input()
{
scanf("%lf%lf",&x,&y);
}
friend point operator +(const point &a,const point &b)
{
return point(a.x+b.x,a.y+b.y);
}
friend point operator -(const point &a,const point &b)
{
return point(a.x-b.x,a.y-b.y);
}
friend bool operator ==(const point &a,const point &b)
{
return cmp(a.x-b.x)==0&&cmp(a.y-b.y)==0;
}
friend point operator *(const point &a,const double &b)
{
return point(a.x*b,a.y*b);
}
friend point operator*(const double &a,const point &b)
{
return point(a*b.x,a*b.y);
}
friend point operator /(const point &a,const double &b)
{
return point(a.x/b,a.y/b);
}
double norm()
{
return sqr(x)+sqr(y);
}
}; struct line
{
point a,b;
line(){};
line(point x,point y):a(x),b(y)
{ }
};
double det(const point &a,const point &b)
{
return a.x*b.y-a.y*b.x;
} double dot(const point &a,const point &b)
{
return a.x*b.x+a.y*b.y;
} double dist(const point &a,const point &b)
{
return (a-b).norm();
} point rotate_point(const point &p,double A)
{
double tx=p.x,ty=p.y;
return point(tx*cos(A)-ty*sin(A),tx*sin(A)+ty*cos(A));
} bool parallel(line a,line b)
{
return !cmp(det(a.a-a.b,b.a-b.b));
} bool line_joined(line a,line b,point &res)
{
if(parallel(a,b))return false;
double s1=det(a.a-b.a,b.b-b.a);
double s2=det(a.b-b.a,b.b-b.a);
res=(s1*a.b-s2*a.a)/(s1-s2);
return true;
} bool pointonSegment(point p,point s,point t)
{
return cmp(det(p-s,t-s))==0&&cmp(dot(p-s,p-t))<=0;
} void PointProjLine(const point p,const point s,const point t,point &cp)
{
double r=dot((t-s),(p-s))/dot(t-s,t-s);
cp=s+r*(t-s);
} struct polygon_convex
{
vector<point>P;
polygon_convex(int Size=0)
{
P.resize(Size);
}
}; struct halfPlane
{
double a,b,c;
halfPlane(point p,point q)
{
a=p.y-q.y;
b=q.x-p.x;
c=det(p,q);
}
halfPlane(double aa,double bb,double cc)
{
a=aa;b=bb;c=cc;
} }; double calc(halfPlane &L,point &a)
{
return a.x*L.a +a.y*L.b+L.c;
} point Intersect(point &a,point &b,halfPlane &L)
{
point res;
double t1=calc(L,a),t2=calc(L,b);
res.x=(t2*a.x-t1*b.x)/(t2-t1);
res.y=(t2*a.y-t1*b.y)/(t2-t1);
//cout<<res.x<<" "<<res.y<<endl;
return res;
} polygon_convex cut(polygon_convex &a,halfPlane &L)
{
int n=a.P.size();
polygon_convex res;
for(int i=0;i<n;i++)
{
if(calc(L,a.P[i])<-eps)res.P.push_back(a.P[i]);
else
{
int j;
j=i-1;
if(j<0)j=n-1;
if(calc(L,a.P[j])<-eps)
res.P.push_back(Intersect(a.P[j],a.P[i],L));
j=i+1;
if(j==n)j=0;
if(calc(L,a.P[j])<-eps)
res.P.push_back(Intersect(a.P[i],a.P[j],L));
}
}
return res;
}
double INF=1e+5;
polygon_convex core(vector<point> &a)
{
polygon_convex res;
res.P.push_back(point(-INF,-INF));
res.P.push_back(point(INF,-INF));
res.P.push_back(point(INF,INF));
res.P.push_back(point(-INF,INF));
int n=a.size();
for(int i=0;i<n;i++)
{
halfPlane L(a[i],a[(i+1)%n]);
res=cut(res,L);
}
return res;
}
bool comp_less(const point &a,const point &b)
{
return cmp(a.x-b.x)<0||cmp(a.x-b.x)==0&&cmp(a.y-b.y)<0; } polygon_convex convex_hull(vector<point> a)
{
polygon_convex res(2*a.size()+5);
sort(a.begin(),a.end(),comp_less);
a.erase(unique(a.begin(),a.end()),a.end());//删去重复点
int m=0;
for(int i=0;i<a.size();i++)
{
while(m>1&&cmp(det(res.P[m-1]-res.P[m-2],a[i]-res.P[m-2]))<=0)--m;
res.P[m++]=a[i];
}
int k=m;
for(int i=int(a.size())-2;i>=0;--i)
{
while(m>k&&cmp(det(res.P[m-1]-res.P[m-2],a[i]-res.P[m-2]))<=0)--m;
res.P[m++]=a[i];
}
res.P.resize(m);
if(a.size()>1)res.P.resize(m-1);
return res;
} bool is_convex(vector<point> &a)
{
for(int i=0;i<a.size();i++)
{
int i1=(i+1)%int(a.size());
int i2=(i+2)%int(a.size());
int i3=(i+3)%int(a.size());
if((cmp(det(a[i1]-a[i],a[i2]-a[i1]))*cmp(det(a[i2]-a[i1],a[i3]-a[i2])))<0)
return false;
}
return true;
}
int containO(const polygon_convex &a,const point &b)
{
int n=a.P.size();
point g=(a.P[0]+a.P[n/3]+a.P[2*n/3])/3.0;
int l=0,r=n;
while(l+1<r)
{
int mid=(l+r)/2;
if(cmp(det(a.P[l]-g,a.P[mid]-g))>0)
{
if(cmp(det(a.P[l]-g,b-g))>=0&&cmp(det(a.P[mid]-g,b-g))<0)r=mid;
else l=mid;
}else
{
if(cmp(det(a.P[l]-g,b-g))<0&&cmp(det(a.P[mid]-g,b-g))>=0)l=mid;
else r=mid;
}
}
r%=n;
int z=cmp(det(a.P[r]-b,a.P[l]-b))-1;
if(z==-2)return 1;
return z;
}
long long int distant(point a,point b)
{
return (int(b.x)-int(a.x))*(int(b.x)-int(a.x))+(int(b.y)-int(a.y))*(int(b.y)-int(a.y));
}
double convex_diameter(polygon_convex &a,int &First,int &Second)
{
vector<point> &p=a.P;
int n=p.size();
double maxd=0;
if(n==1)
{
First=Second=0;
return maxd;
}
#define next(i)((i+1)%n)
for(int i=0,j=1;i<n;++i)
{
while(cmp(det(p[next(i)]-p[i],p[j]-p[i])-det(p[next(i)]-p[i],p[next(j)]-p[i]))<0)
j=next(j);
double d=dist(p[i],p[j]);
if(d>maxd)
{
maxd=d;
First=i,Second=j;
}
d=dist(p[next(i)],p[next(j)]);
if(d>maxd)
{
maxd=d;
First=next(i),Second=next(j);
} }
return maxd;
} double area(vector<point>a)
{
double sum=0;
for(int i=0;i<a.size();i++)
sum+=det(a[(i+1)%(a.size())],a[i]);
return sum/2.;
}
vector<point> pp;
int main()
{freopen("t.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
pp.resize(n);
for(int i=0;i<n;i++)
pp[i].input();
polygon_convex pc=core(pp);
printf("%.2lf\n",-area(pc.P)+0.0005);
}
return 0;
}

  

POJ1279 Art Gallery 多边形的核的更多相关文章

  1. 再来一道测半平面交模板题 Poj1279 Art Gallery

    地址:http://poj.org/problem?id=1279 题目: Art Gallery Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

  2. [poj1279]Art Gallery

    题意:求多边形的核的面积. 敲一下半平面交模板........  然后我wa了一早上就因为写了%lf  不知道poj什么破机制还不能用lf的,真的想跳楼 #include<iostream> ...

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

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

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

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

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

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

  6. poj 1279 Art Gallery - 求多边形核的面积

    /* poj 1279 Art Gallery - 求多边形核的面积 */ #include<stdio.h> #include<math.h> #include <al ...

  7. 【POJ】【2068】Art Gallery

    计算几何/半平面交 裸的半平面交,关于半平面交的入门请看神犇博客:http://blog.csdn.net/accry/article/details/6070621 然而代码我是抄的proverbs ...

  8. poj 1279 Art Gallery (Half Plane Intersection)

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

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

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

随机推荐

  1. tomcat时间与系统时间不一致问题

    我在部署应用到centos系统上的tomcat服务器中运行,发现操作系统的时间和tomcat中的访问日志的时间与系统时间不一致,但是查看当前操作系统的时区也是CST时区(中国标准时区). 查看系统的时 ...

  2. css布局的各种FC简单介绍:BFC,IFC,GFC,FFC

    什么是FC? Formatting Context,格式化上下文,指页面中一个渲染区域,拥有一套渲染规则,它决定了其子元素如何定位,以及与其他元素的相互关系和作用. BFC 什么是BFC Block ...

  3. 《深入浅出深度学习:原理剖析与python实践》第八章前馈神经网络(笔记)

    8.1 生物神经元(BN)结构 1.人脑中有100亿-1000亿个神经元,每个神经元大约会和其他1万个神经元相连 2.细胞体:神经元的主体,细胞体=细胞核+细胞质+细胞膜,存在膜电位 3.树突:从细胞 ...

  4. scrapy实现全站抓取数据

    1. scrapy.CrawlSpider scrapy框架提供了多种类型的spider,大致分为两类,一类为基本spider(scrapy.Spider),另一类为通用spider(scrapy.s ...

  5. 78-DeMarker,价格波动指数.(2015.7.1)

    DeMarker 价格波动指数 观井映天 2015.7.1

  6. 3W法则-学习Docker

    一.前言       5W1H法则是在一次面试中学习到的,后来在工作也开始使用这种东西,虽然最后没去那家公司,但是也是学习到了,关注开这些东西以后,也发现了一些简化版的3W法则,最近公司也要搞Doce ...

  7. 找到多个与名为“Home”的控制器匹配的类型。

    原因分析 其实上面已经讲的很清楚了,找到了两个同名Home控制器,需要配置命名空间来区分. 解决方法 方法一:修改RouteConfig.cs 方法二:修改RouteConfig.cs 和 Admin ...

  8. [luoguP1074] 靶形数独(搜索)

    传送门 75分,太菜,不会优化了,吐了. 几点优化. 1.先搜索容易确定的位置 2.从中心往周围搜 3.枚举数字的时候倒序枚举 4.如果没有枚举到的数字都是最优情况的话也不能比当前ans大就剪枝 5. ...

  9. JAVA生成扫描条形码

    条形码是一种可视化.机器可读的数据,这些数据通常描述了携带该条码的物品的相关信息.条形码已经广泛被应用在商品流通,图书管理,邮政管理和银行系统等领域.在这篇文章中,将介绍如何生成和扫描一些常见的一维和 ...

  10. java开发中涉及到的调优

    JVM内存的调优 默认的Java虚拟机的大小比较小,在对大数据进行处理时java就会报错:java.lang.OutOfMemoryError. 1. Heap设定与垃圾回收Java Heap分为3个 ...