鏈接:http://poj.org/problem?id=1279

Art Gallery
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 5337   Accepted: 2277

Description

The art galleries of the new and very futuristic building of the Center for Balkan Cooperation have the form of polygons (not necessarily convex). When a big exhibition is organized, watching over all of the pictures is a big security concern. Your task is that for a given gallery to write a program which finds the surface of the area of the floor, from which each point on the walls of the gallery is visible. On the figure 1. a map of a gallery is given in some co-ordinate system. The area wanted is shaded on the figure 2. 

Input

The number of tasks T that your program have to solve will be on the first row of the input file. Input data for each task start with an integer N, 5 <= N <= 1500. Each of the next N rows of the input will contain the co-ordinates of a vertex of the polygon ? two integers that fit in 16-bit integer type, separated by a single space. Following the row with the co-ordinates of the last vertex for the task comes the line with the number of vertices for the next test and so on.

Output

For each test you must write on one line the required surface - a number with exactly two digits after the decimal point (the number should be rounded to the second digit after the decimal point).

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逆,这种代码是以逆时针为主,我的面积顺时针为正,

需要改变方向

这是AC代码:
 #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 (半平面交)的更多相关文章

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

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

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

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

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

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

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

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

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

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

  6. poj 1279 Art Gallery (Half Plane Intersection)

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

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

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

  8. [POJ]1279: Art Gallery

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

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

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

随机推荐

  1. 69道Java Spring 面试&笔试题

    目录 Spring 概述 依赖注入 Spring beans Spring注解 Spring数据访问 Spring面向切面编程(AOP) Spring MVC Spring 概述 1. 什么是spri ...

  2. 160223、jquery中attr和prop的区别

    在高版本的jquery引入prop方法后,什么时候该用prop?什么时候用attr?它们两个之间有什么区别?这些问题就出现了. 关于它们两个的区别,网上的答案很多.这里谈谈我的心得,我的心得很简单: ...

  3. hadoop之Spark强有力竞争者Flink,Spark与Flink:对比与分析

    hadoop之Spark强有力竞争者Flink,Spark与Flink:对比与分析 Spark是一种快速.通用的计算集群系统,Spark提出的最主要抽象概念是弹性分布式数据集(RDD),它是一个元素集 ...

  4. mysql字段varchar区分大小写utf8_bin、utf8_general_ci编码区别

    mysql字段varchar区分大小写utf8_bin.utf8_general_ci编码区别 在mysql中存在着各种utf8编码格式:utf8_bin将字符串中的每一个字符用二进制数据存储,区分大 ...

  5. SQL Server系统表sysobjects介绍与使用

    关于SQL Server数据库的一切信息都保存在它的系统表格里.我怀疑你是否花过比较多的时间来检查系统表格,因为你总是忙于用户表格.但是,你可能需要偶尔做一点不同寻常的事,例如数据库所有的触发器.你可 ...

  6. html5 canvas 笔记二(添加样式和颜色)

    色彩 Colors fillStyle = color 设置图形的填充颜色. strokeStyle = color 设置图形轮廓的颜色. 透明度 Transparency globalAlpha = ...

  7. 微信支付 APP 支付方式的服务器端处理程序

    对于微信的APP的支付,客户服务说只能通过微信开放平台申请.后来在公众帐号确实发现了证据:  微信支付在申请的时候就比较严(麻烦),对服务类的一些支付,本来商品就是虚拟的,所以需要将商品描述的比较详细 ...

  8. Unix/Linux编程实践教程(一:进程、管道)

    execvp在程序中启动新程序: 用fork创建新进程: forkdemo2代码: 测试fork的时候参考<Linux权威指南>阅读笔记(3)  使用了patch: [root@local ...

  9. js 闭包理解

    闭包主要应用于两种情况: 1 函数作为返回值. 2 函数作为参数传递. 第一种举例: function fn(){ var max = 10; return function bar(){ if(x ...

  10. YTU 3019: 螺旋方阵

    3019: 螺旋方阵 时间限制: 1 Sec  内存限制: 128 MB 提交: 2  解决: 2 题目描述 以下是一个5*5阶螺旋方阵.设计一个程序,输出该形式的n*n阶方阵(顺时针方向旋进).   ...