zoj 1010 (线段相交判断+多边形求面积)
链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=10
Area
Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge
Jerry, a middle school student, addicts himself to mathematical research. Maybe the problems he has thought are really too easy to an expert. But as an amateur, especially as a 15-year-old boy, he had done very well. He is so rolling in thinking the mathematical problem that he is easily to try to solve every problem he met in a mathematical way. One day, he found a piece of paper on the desk. His younger sister, Mary, a four-year-old girl, had drawn some lines. But those lines formed a special kind of concave polygon by accident as Fig. 1 shows.
Fig. 1 The lines his sister had drawn
"Great!" he thought, "The polygon seems so regular. I had just learned how to calculate the area of triangle, rectangle and circle. I'm sure I can find out how to calculate the area of this figure." And so he did. First of all, he marked the vertexes in the polygon with their coordinates as Fig. 2 shows. And then he found the result--0.75 effortless.
Fig.2 The polygon with the coordinates of vertexes
Of course, he was not satisfied with the solution of such an easy problem. "Mmm, if there's a random polygon on the paper, then how can I calculate the area?" he asked himself. Till then, he hadn't found out the general rules on calculating the area of a random polygon. He clearly knew that the answer to this question is out of his competence. So he asked you, an erudite expert, to offer him help. The kind behavior would be highly appreciated by him.
Input
The input data consists of several figures. The first line of the input for each figure contains a single integer n, the number of vertexes in the figure. (0 <= n <= 1000).
In the following n lines, each contain a pair of real numbers, which describes the coordinates of the vertexes, (xi, yi). The figure in each test case starts from the first vertex to the second one, then from the second to the third, ���� and so on. At last, it closes from the nth vertex to the first one.
The input ends with an empty figure (n = 0). And this figure not be processed.
Output
As shown below, the output of each figure should contain the figure number and a colon followed by the area of the figure or the string "Impossible".
If the figure is a polygon, compute its area (accurate to two fractional digits). According to the input vertexes, if they cannot form a polygon (that is, one line intersects with another which shouldn't be adjoined with it, for example, in a figure with four lines, the first line intersects with the third one), just display "Impossible", indicating the figure can't be a polygon. If the amount of the vertexes is not enough to form a closed polygon, the output message should be "Impossible" either.
Print a blank line between each test cases.
Sample Input
5
0 0
0 1
0.5 0.5
1 1
1 0
4
0 0
0 1
1 0
1 1
0
Output for the Sample Input
Figure 1: 0.75
Figure 2: Impossible
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
一开始看错题意,WA了好多次,要注意与当前线段相邻接的线段不判断
主要就是第一个线段,要跳过与下一条线段的相交性,以及最后一条线段的相交性,其他线段只需要向下跳过一个线段判断相交即可
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <math.h> #define MAXX 1005
#define eps 1e-8
using namespace std; typedef struct
{
double x;
double y;
}point; typedef struct
{
point st;
point ed;
}line; point p[MAXX];
line li[MAXX]; 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);
} 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 xy(double x,double y){ return x < y - eps; }
bool dy(double x,double y){ return x > y + eps; }
bool xyd(double x,double y){ return x < y + eps; }
bool dyd(double x,double y){ return x > y - eps; }
bool dd(double x,double y){ return fabs(x-y)<eps; } bool onSegment(point a,point b,point c)
{
double maxx=max(a.x,b.x);
double maxy=max(a.y,b.y);
double minx=min(a.x,b.x);
double miny=min(a.y,b.y); if(dd(crossProduct(a,b,c),0.0)&&xyd(c.x,maxx)&&dyd(c.x,minx)
&&xyd(c.y,maxy)&&dyd(c.y,miny))
return true;
return false;
} bool segIntersect(point p1,point p2,point p3,point p4)
{
double d1=crossProduct(p3,p4,p1);
double d2=crossProduct(p3,p4,p2);
double d3=crossProduct(p1,p2,p3);
double d4=crossProduct(p1,p2,p4); if(xy(d1*d2,0.0)&&xy(d3*d4,0.0))
return true;
if(dd(d1,0.0)&&onSegment(p3,p4,p1))
return true;
if(dd(d2,0.0)&&onSegment(p3,p4,p2))
return true;
if(dd(d3,0.0)&&onSegment(p1,p2,p3))
return true;
if(dd(d4,0.0)&&onSegment(p1,p2,p4))
return true;
return false;
} double Area(int n)
{
double ans=0.0; for(int i=; i<n; i++)
{
ans+=crossProduct(p[],p[i-],p[i]);
}
return fabs(ans)/2.0;
} int main()
{
int n,m,i,j;
double x,y;
int cas=;
while(scanf("%d",&n)!=EOF&&n)
{
for(i=; i<n; i++)
{
scanf("%lf%lf",&p[i].x,&p[i].y);
} for(i=; i<n-; i++)
{
li[i].st.x=p[i].x;
li[i].st.y=p[i].y;
li[i].ed.x=p[i+].x;
li[i].ed.y=p[i+].y;
}
li[n-].st.x=p[n-].x;
li[n-].st.y=p[n-].y;
li[n-].ed.x=p[].x;
li[n-].ed.y=p[].y;
bool flag=false;
for(i=; i<n; i++)
{
for(j=i+; j<n; j++)
{
if(i == && j == n-)continue;
/*if((li[i].st.x == li[j].st.x && li[i].st.y == li[j].st.y)
|| li[i].st.x == li[j].ed.x && li[i].st.y == li[j].ed.y
|| li[i].ed.x == li[j].st.x && li[i].ed.y == li[j].st.y
|| li[i].ed.x == li[j].ed.x && li[i].ed.y == li[j].ed.y)
continue;*/
if(segIntersect(li[i].st,li[i].ed,li[j].st,li[j].ed))
{
flag=true;
break;
}
}
} if(flag || n<)
{
printf("Figure %d: Impossible\n",cas++);
}
else
{
double ans=Area(n);
printf("Figure %d: %.2lf\n",cas++,ans);
}
printf("\n");
}
return ;
}
zoj 1010 (线段相交判断+多边形求面积)的更多相关文章
- POJ 1039 Pipe(直线和线段相交判断,求交点)
Pipe Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8280 Accepted: 2483 Description ...
- 计算几何基础——矢量和叉积 && 叉积、线段相交判断、凸包(转载)
转载自 http://blog.csdn.net/william001zs/article/details/6213485 矢量 如果一条线段的端点是有次序之分的话,那么这种线段就称为 有向线段,如果 ...
- POJ 3449 Geometric Shapes(判断几个不同图形的相交,线段相交判断)
Geometric Shapes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 1243 Accepted: 524 D ...
- ACM1558两线段相交判断和并查集
Segment set Problem Description A segment and all segments which are connected with it compose a seg ...
- POJ 1066 Treasure Hunt(线段相交判断)
Treasure Hunt Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4797 Accepted: 1998 Des ...
- POJ 3304 Segments (直线和线段相交判断)
Segments Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7739 Accepted: 2316 Descript ...
- Area---poj1265(皮克定理+多边形求面积)
题目链接:http://poj.org/problem?id=1265 题意是:有一个机器人在矩形网格中行走,起始点是(0,0),每次移动(dx,dy)的偏移量,已知,机器人走的图形是一个多边形,求这 ...
- HDU 1255 覆盖的面积(线段树:扫描线求面积并)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1255 题目大意:给你若干个矩形,让你求这些矩形重叠两次及以上的部分的面积. 解题思路:模板题,跟HDU ...
- poj2653线段相交判断
Stan has n sticks of various length. He throws them one at a time on the floor in a random way. Afte ...
随机推荐
- 图像处理工具包ImagXpress中如何定义查看器的属性
想要在图像处理控件ImagXpress中查看一个图像,首先需要创建一个查看器,之后你可以按照你自身的需要,来定义查看器的属性. 创建查看器 想要动态的创建一个查看器,需要先定义一个新的mageXVie ...
- 【python cookbook】【数据结构与算法】3.保存最后N个元素
问题:希望在迭代或是其他形式的处理过程中对最后几项记录做一个有限的历史记录统计 解决方案:选择collections.deque. 如下的代码对一系列文本行做简单的文本匹配操作,当发现有匹配时就输出当 ...
- 160919、使用AOP与注解记录Java日志
有些时候,我想要把每个运行过的方法接收到的参数.返回值和执行时间等信息记录(通过slf4j 和 log4j)下来.在AspectJ.jcabi-aspects和Java注解的帮助下我实现了这个想法. ...
- 调用css时,用link 和 @import url 有什么区别
加载css link与@import的区别: 其实 link 与 @import 在显示效果上还是有很大区别的,基本上来看 link 的加在会在页面显示之前全部加在完全,而 @import 会是读取完 ...
- linux cache and buffer【转】
转自:http://blog.csdn.net/turkeyzhou/article/details/6426738 版权声明:本文为博主原创文章,未经博主允许不得转载. Linux下对文件的访问和设 ...
- CSS修改方法
1.在IE中,大部分情况下默认margin = 0px padding = 0px,但在Chrome中需要写明 在css.css文件开头加上(要加在最上面) html,body,ul,ol,li,ta ...
- Java 门面模式 浅析
Java中的门面模式,一般来说他的用途是隐藏一些不希望用户看到的东西,比如方法,变量,并且这些变量是不能够设置成私有的,因为在系统内部有些地方需要调用.在Tomcat的HttpServletReque ...
- 这个Glance的界面该怎么看出问题,为什么状态是SOCKT?
这个glance的状态图有问题吗?
- 【Pro ASP.NET MVC 3 Framework】.学习笔记.5.SportsStore一个真实的程序
我们要建造的程序不是一个浅显的例子.我们要创建一个坚固的,现实的程序,坚持使它成为最佳实践.与Web Form中拖控件不同.一开始投入MVC程序付出利息,它给我们可维护的,可扩展的,有单元测试卓越支持 ...
- php中urlencode与rawurlencode的区别有那些呢
urlencode 函数: 返回字符串,此字符串中除了 -_. 之外的所有非字母数字字符都将被替换成百分号(%)后跟两位十六进制数,空格则编码为加号(+).此编码与 WWW 表单 POST 数据的编码 ...