zoj 1010 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
Source:
Asia 2001, Shanghai (Mainland China)
算法:计算几何 线段相交问题 面积问题
题意 + 思路:
分析:
// = 0 表示,相交于端点也是认定为相交
bool SegmentProperIntersection(Point s1, Point e1, Point s2, Point e2) {
double c1 = Cross(s2-s1, e1-s1), c2 = Cross(e1-s1, e2-s1),
c3 = Cross(s1-s2, e2-s2), c4 = Cross(e2-s2, e1-s2);
if(c1*c2 >= 0 && c3*c4 >= 0) return true;
return false;
}
//叉积求面积 ,下标从 0 开始
double Area() {
double area = 0;
for(int i = 1; i < n-1; i++)
area += Cross(p[i]-p[0], p[i+1]-p[0]);
return fabs(area) / 2.0;
}
code:
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<iostream>
using namespace std; const int maxn = 1000+10;
int n; struct Point{
double x,y;
Point() {} Point(double _x, double _y) {
x = _x;
y = _y;
}
Point operator - (const Point &b) const{
return Point(x-b.x, y-b.y);
}
}p[maxn]; double Cross(Point a, Point b) { //叉积
return a.x*b.y - a.y*b.x;
} // = 0 表示,相交于端点也是认定为相交
bool SegmentProperIntersection(Point s1, Point e1, Point s2, Point e2) {
double c1 = Cross(s2-s1, e1-s1), c2 = Cross(e1-s1, e2-s1),
c3 = Cross(s1-s2, e2-s2), c4 = Cross(e2-s2, e1-s2);
if(c1*c2 >= 0 && c3*c4 >= 0) return true;
return false;
} bool haveCross() { //从第 三 条线段开始, 一直到第 N-1 条线段
for(int i = 2; i < n-1; i++) { //检查 Lin(i,i+1) 与前面每一条不直接相连的线段
for(int j = 1; j < i; j++) {
if(SegmentProperIntersection(p[i], p[i+1], p[j-1], p[j])) {
return true;
}
}
} //判断最后一条线段p[0]_p[n-1]
//从第二条线段一直比较到第 n-2 条线段
for(int i = 1; i < n-2 ; i++) { //p[i]__p[i+1]
if(SegmentProperIntersection(p[0],p[n-1],p[i],p[i+1])) {
return true;
}
}
return false;
} //叉积求面积
double Area() {
double area = 0;
for(int i = 1; i < n-1; i++)
area += Cross(p[i]-p[0], p[i+1]-p[0]);
return fabs(area) / 2.0;
} int main()
{
int test = 0;
while(scanf("%d", &n) != EOF) {
if(n == 0) break; double x,y;
for(int i = 0; i < n; i++) {
scanf("%lf%lf", &x,&y);
p[i] = Point(x,y);
} if(n < 3) { //如果小于三个点, 肯定不能求面积
printf("Figure %d: Impossible\n", ++test);
printf("\n");
continue;
} if(haveCross()) { //如果有线段相交的情况
printf("Figure %d: Impossible\n", ++test);
printf("\n");
continue;
}
else printf("Figure %d: %.2lf", ++test,Area());
printf("\n");
}
return 0;
} /**
几组全为 impossible 的数据
*/
/* 7
0 0
4 0
2 1
4 3
3 3
3 2
1 3 7
0 0
1 1
3 0
5 1
5 -1
3 0
1 -1 5
0 0
4 0
3 1
2 0
1 1 */
zoj 1010 Area【线段相交问题】的更多相关文章
- zoj 1010 (线段相交判断+多边形求面积)
链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=10 Area Time Limit: 2 Seconds Mem ...
- [ZOJ 1010] Area (计算几何)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1010 题目大意:给你n个点,问你顺次连线能否连成多边形?如果能, ...
- [ZJU 1010] Area
ZOJ Problem Set - 1010 Area Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge Jer ...
- 判断线段相交 -- 51nod 1264 线段相交
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1264 三角形的有向面积:a.x*b.y+b.x*c.y+c.x*a.y ...
- hdu 1086(判断线段相交)
传送门:You can Solve a Geometry Problem too 题意:给n条线段,判断相交的点数. 分析:判断线段相交模板题,快速排斥实验原理就是每条线段代表的向量和该线段的一个端点 ...
- POJ 1127 Jack Straws (线段相交)
题意:给定一堆线段,然后有询问,问这两个线段是不是相交,并且如果间接相交也可以. 析:可以用并查集和线段相交来做,也可以用Floyd来做,相交就是一个模板题. 代码如下: #pragma commen ...
- HDU 1558 Segment set( 判断线段相交 + 并查集 )
链接:传送门 题意:输入一个数 n 代表有 n 组操作,P 是在平面内加入一条线段,Q x 是查询第 x 条线段所在相交集合的线段个数 例如:下图 5 与 1.2 相交,1 与 3 相交,2 与 4 ...
- POJ 1066 Treasure Hunt (线段相交)
题意:给你一个100*100的正方形,再给你n条线(墙),保证线段一定在正方形内且端点在正方形边界(外墙),最后给你一个正方形内的点(保证不再墙上) 告诉你墙之间(包括外墙)围成了一些小房间,在小房间 ...
- POJ 2653 Pick-up sticks (线段相交)
题意:给你n条线段依次放到二维平面上,问最后有哪些没与前面的线段相交,即它是顶上的线段 题解:数据弱,正向纯模拟可过 但是有一个陷阱:如果我们从后面向前枚举,找与前面哪些相交,再删除前面那些相交的线段 ...
随机推荐
- OS开发 touch事件的优先级和事件传递
界面类的对象一般都是可以接触点击事件的,只不过有的默认接受,有的需要设定属性. userInteractionEnabled 属性设置为YES的时候就可以接受点击事件了 - (void)touches ...
- [Angualr 2] Watch for changes
You can watch for form / control changes by using .valueChanges.observe({...}): this.sku.valueChange ...
- 玩转Web之servlet(三)---一张图看懂B/S架构
学WEB, 首先 要明确B/S架构,本文将简单说一下小编的愚见,若有不当,欢迎大家指正. 首先,什么是B/S架构?B/S是Broweser/Server的缩写,即浏览器/server模式,简单说就是c ...
- PHP安全编程:主机文件目录浏览(转)
除了能在共享服务器上读取任意文件之外,攻击者还能建立一个可以浏览文件系统的脚本.由于你的大多数敏感文件不会保存在网站主目录下,此类脚本一般用于找到你的源文件的所在位置.请看下例: 01 <?ph ...
- codevs1404字符串匹配
/* 无奈我改了那么久还是看的题解 首先跑一边kmp 几下ans[p]表示总共匹配到长度p的次数 这些不一定都是恰好到p 所以在处理一下 ans[p]通过处理变成 所有的匹配到长度p的次数 最后答案就 ...
- C#解leetcode 189. Rotate Array
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...
- (转)PHP中构造函数和析构函数解析
--http://www.jb51.net/article/56047.htm 构造函数 void __construct ([ mixed $args [, $... ]] ) PHP 5 允行开发 ...
- dedecms 使noflag参数及其过滤多个属性的修改方法
noflag='h' 是代表不包含头条属性的意思,其中flag就是属性, 自定义属性值:头条[h]推荐[c]图片[p]幻灯[f]滚动[s]跳转[j]图文[a]加粗[b]. noflag过滤多个属性的修 ...
- iOS开发: 向右滑动手势功能实现
在navigationController中实现向右滑动 返回功能 系统提供的backbarbuttonitem,不用添加任何代码即可实现向右滑动后退功能,但是往往要对按钮修改样式等时,就需要自定义l ...
- javascript基础学习(十三)
javascript之文档对象 学习要点: 文档对象 文档对象的应用 一.文档对象 Document对象是代表一个浏览器窗口或框架中的显示HTML文件的对象.javascript会为每个HTML文档自 ...