poj 1279 -- Art Gallery (半平面交)
鏈接:http://poj.org/problem?id=1279
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 5337 | Accepted: 2277 |
Description
![](http://poj.org/images/1279_1.jpg)
Input
Output
Sample Input
1
7
0 0
4 4
4 7
9 7
13 -1
8 -6
4 -4
Sample Output
80.00
Source
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm> #define eps 1e-8
#define MAXX 1510 typedef struct point
{
double x;
double y;
}point; point p[MAXX],s[MAXX]; using namespace std;
bool dy(double x,double y)
{
return x>y+eps;
}
bool xy(double x,double y)
{
return x<y-eps;
}
bool dyd(double x,double y)
{
return x>y-eps;
}
bool xyd(double x,double y)
{
return x<y+eps;
}
bool dd(double x,double y)
{
return fabs(x-y)<eps;
} double crossProduct(point a,point b,point c)
{
return (c.x-a.x)*(b.y-a.y)-(c.y-a.y)*(b.x-a.x);
} point IntersectPoint(point u1,point u2,point v1,point v2)
{
point ans=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));
ans.x += (u2.x-u1.x)*t;
ans.y += (u2.y-u1.y)*t;
return ans;
} double Area(point p[],int n)
{
double ans=0.0;
for(int i=; i<n-; i++)
{
ans += crossProduct(p[],p[i],p[i+]);
}
return fabs(ans)/2.0;
} double dist(point a,point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
} bool cmp(point a,point b)
{
double tmp=crossProduct(p[],a,b);
if(dd(tmp,0.0))
return dy(dist(p[],a),dist(p[],b));
return xy(tmp,0.0);
} point Getsort(int n)
{
int tmp=;
for(int i=; i<n; i++)
{
if(xy(p[i].x,p[tmp].x) || dd(p[i].x,p[tmp].x)&&xy(p[i].y,p[tmp].y))
{
tmp=i;
}
}// printf("%d^^",tmp);
swap(p[],p[tmp]);
sort(p+,p+n,cmp);
} void cut(point p[],point s[],int n,int &len)
{
point tp[MAXX];
p[n]=p[];
for(int i=; i<=n; i++)
{
tp[i]=p[i];
}
int cp=n,tc;
for(int i=; i<n; i++)
{
tc=;
for(int k=; k<cp; k++)
{
if(xyd(crossProduct(p[i],p[i+],tp[k]),0.0))
s[tc++]=tp[k];
if(xy(crossProduct(p[i],p[i+],tp[k])*
crossProduct(p[i],p[i+],tp[k+]),0.0))
s[tc++]=IntersectPoint(p[i],p[i+],tp[k],tp[k+]);
}
s[tc]=s[];
for(int k=; k<=tc; k++)
tp[k]=s[k];
cp=tc;
}
len=cp;
} int main()
{
int t,n,m,i,j;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=; i<n; i++)
{
scanf("%lf%lf",&p[i].x,&p[i].y);
}
//point tmp=IntersectPoint(p[0],p[1],p[2],p[3]);
//printf("%lf %lf\n",tmp.x,tmp.y);
Getsort(n);//for(i=0; i<n; i++)printf("%lf**%lf*\n",p[i].x,p[i].y);
int len;
cut(p,s,n,len);//for(i=0; i<len; i++)printf("%lf==%lf=\n",s[i].x,s[i].y);
double area=Area(s,len);
printf("%.2lf\n",area);
}
return ;
}
利用面积正负来判断顺or逆,这种代码是以逆时针为主,我的面积顺时针为正,
需要改变方向
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm> #define eps 1e-8
#define MAXX 1510 typedef struct point
{
double x;
double y;
}point; point p[MAXX],s[MAXX]; using namespace std;
bool dy(double x,double y)
{
return x>y+eps;
}
bool xy(double x,double y)
{
return x<y-eps;
}
bool dyd(double x,double y)
{
return x>y-eps;
}
bool xyd(double x,double y)
{
return x<y+eps;
}
bool dd(double x,double y)
{
return fabs(x-y)<eps;
} double crossProduct(point a,point b,point c)
{
return (c.x-a.x)*(b.y-a.y)-(c.y-a.y)*(b.x-a.x);
} point IntersectPoint(point u1,point u2,point v1,point v2)
{
point ans=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));
ans.x += (u2.x-u1.x)*t;
ans.y += (u2.y-u1.y)*t;
return ans;
} double Area(point p[],int n)
{
double ans=0.0;
p[n]=p[];
point tmp;
tmp.x=,tmp.y=;
for(int i=; i<n; i++)
{
ans += crossProduct(tmp,p[i],p[i+]);
}
return ans/2.0;
} void changeWise(point p[],int n)
{
for(int i=; i<n/; i++)
swap(p[i],p[n-i-]);
} double dist(point a,point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
/*
bool cmp(point a,point b)
{
double tmp=crossProduct(p[0],a,b);
if(dd(tmp,0.0))
return dy(dist(p[0],a),dist(p[0],b));
return xy(tmp,0.0);
} point Getsort(int n)
{
int tmp=0;
for(int i=1; i<n; i++)
{
if(xy(p[i].x,p[tmp].x) || dd(p[i].x,p[tmp].x)&&xy(p[i].y,p[tmp].y))
{
tmp=i;
}
}// printf("%d^^",tmp);
swap(p[0],p[tmp]);
sort(p+1,p+n,cmp);
}
*/
void cut(point p[],point s[],int n,int &len)
{
point tp[MAXX];
p[n]=p[];
for(int i=; i<=n; i++)
{
tp[i]=p[i];
}
int cp=n,tc;
for(int i=; i<n; i++)
{
tc=;
for(int k=; k<cp; k++)
{
if(xyd(crossProduct(p[i],p[i+],tp[k]),0.0))
s[tc++]=tp[k];
if(xy(crossProduct(p[i],p[i+],tp[k])*
crossProduct(p[i],p[i+],tp[k+]),0.0))
s[tc++]=IntersectPoint(p[i],p[i+],tp[k],tp[k+]);
}
s[tc]=s[];
for(int k=; k<=tc; k++)
tp[k]=s[k];
cp=tc;
}
len=cp;
} int main()
{
int t,n,m,i,j;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=; i<n; i++)
{
scanf("%lf%lf",&p[i].x,&p[i].y);
}
double tmp=Area(p,n);
if(dy(tmp,0.0))
changeWise(p,n);
//point tmp=IntersectPoint(p[0],p[1],p[2],p[3]);
//printf("%lf %lf\n",tmp.x,tmp.y);
//Getsort(n);for(i=0; i<n; i++)printf("%lf**%lf*\n",p[i].x,p[i].y);
int len;
cut(p,s,n,len);//for(i=0; i<len; i++)printf("%lf==%lf=\n",s[i].x,s[i].y);
double area=Area(s,len);
printf("%.2lf\n",fabs(area));
}
return ;
}
poj 1279 -- Art Gallery (半平面交)的更多相关文章
- POJ 1279 Art Gallery 半平面交/多边形求核
http://poj.org/problem?id=1279 顺时针给你一个多边形...求能看到所有点的面积...用半平面对所有边取交即可,模版题 这里的半平面交是O(n^2)的算法...比较逗比.. ...
- POJ 1279 Art Gallery 半平面交求多边形核
第一道半平面交,只会写N^2. 将每条边化作一个不等式,ax+by+c>0,所以要固定顺序,方便求解. 半平面交其实就是对一系列的不等式组进行求解可行解. 如果某点在直线右侧,说明那个点在区域内 ...
- POJ 1279 Art Gallery(半平面交)
题目链接 回忆了一下,半平面交,整理了一下模版. #include <cstdio> #include <cstring> #include <string> #i ...
- POJ 1279 Art Gallery 半平面交 多边形的核
题意:求多边形的核的面积 套模板即可 #include <iostream> #include <cstdio> #include <cmath> #define ...
- poj 1279 Art Gallery - 求多边形核的面积
/* poj 1279 Art Gallery - 求多边形核的面积 */ #include<stdio.h> #include<math.h> #include <al ...
- poj 1279 Art Gallery (Half Plane Intersection)
1279 -- Art Gallery 还是半平面交的问题,要求求出多边形中可以观察到多边形所有边的位置区域的面积.其实就是把每一条边看作有向直线然后套用半平面交.这题在输入的时候应该用多边形的有向面 ...
- POJ 1279 Art Gallery(半平面交求多边形核的面积)
题目链接 题意 : 求一个多边形的核的面积. 思路 : 半平面交求多边形的核,然后在求面积即可. #include <stdio.h> #include <string.h> ...
- [POJ]1279: Art Gallery
题目大意:有一个N边形展馆,问展馆内有多少地方可以看到所有墙壁.(N<=1500) 思路:模板题,半平面交求出多边形的核后计算核的面积. #include<cstdio> #incl ...
- POJ 1279 Art Gallery【半平面交】(求多边形的核)(模板题)
<题目链接> 题目大意: 按顺时针顺序给出一个N边形,求N边形的核的面积. (多边形的核:它是平面简单多边形的核是该多边形内部的一个点集该点集中任意一点与多边形边界上一点的连线都处于这个多 ...
随机推荐
- UINavigationController(转)
UINavigationController是IOS编程中比较常用的一种容器view controller,很多系统的控件(如UIImagePickerViewController)以及很多有名的AP ...
- FastJson--阿里巴巴公司开源的速度最快的Json和对象转换工具(转)
本文转自:http://blog.csdn.net/djun100/article/details/24237371 这是关于FastJson的一个使用Demo,在Java环境下验证的 class U ...
- 操作系统—Systemd
操作系统-Systemd 时间 2016-09-24 22:34:49 运维部落 原文 http://www.178linux.com/48990 主题 systemd Systemd 概述: C ...
- 【python cookbook】【数据结构与算法】3.保存最后N个元素
问题:希望在迭代或是其他形式的处理过程中对最后几项记录做一个有限的历史记录统计 解决方案:选择collections.deque. 如下的代码对一系列文本行做简单的文本匹配操作,当发现有匹配时就输出当 ...
- javaWeb request乱码处理
//解决get方式提交的乱码 String name = request.getParameter("name"); name=new String(u ...
- Linux I2C工具查看配置I2C设备【转】
转自:http://blog.chinaunix.net/uid-26895763-id-3478882.html 在處理音訊相關的問題時,我通常會找個方法來讀寫codec中register的值.幸好 ...
- $(document).ready vs $(window).load vs window.onload
原文地址: $(document).ready vs $(window).load vs window.onload $(document).ready We execute our code whe ...
- 一个很不错的bash脚本编写教程
转自 http://blog.chinaunix.net/uid-20328094-id-95121.html 一个很不错的bash脚本编写教程,至少没接触过BASH的也能看懂! 建立一个脚本 Lin ...
- ACM题目————A simple problem
Description Zty很痴迷数学问题..一天,yifenfei出了个数学题想难倒他,让他回答1 / n.但Zty却回答不了^_^. 请大家编程帮助他. Input 第一行整数T,表示测试组 ...
- 【转】MYSQL入门学习之五:MYSQL的字符集
转载地址:http://www.2cto.com/database/201212/175541.html MySQL的字符集支持(Character Set Support)有两个方面:字符集(Cha ...