链接:

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1010
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=29328#problem/B
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)

算法:计算几何 线段相交问题 面积问题

如果你不了解跨立实验判断线段相交问题,可以参考下我前面的一篇博客:ZOJ 1648 Circuit Board【跨立实验判断线段相交】

题意 + 思路:

给你 N  个点的坐标,点是按照顺序输入的。
每一个点都与它后面的那个点连成一条线段,最后一个点与起点相连。
求组成的多边形的面积。
注意:并不是直接求多边形的面积。
      首先要判断多边形的合法性:无论是凸多边形还是凹多边形都有可能合法。
                              这里的不合法指的是只要存在一条线段与不和它直接首尾相连的任意一条线段相交
                              就表示面积不可求,输出 impossible
            怎样算是相交?
            首先:直接两条线段两两跨立的当然算是相交了。
            其次:如果相于端点的也算是相交。
1.直接跨立相交,是肯定不行的。
2.相交于端点,也是非法的。
                     
关于线段相交于端点:画的详细点就是这样的图形也被认为是不可以求面积的
PS:图形来自 kuangbin 大神
所以这题的关键不是求面积,而是判断线段相交情况。
PS:本来第二次 WA 了马上就可以 AC 的了,判断线段相交的循环中 j < i 写错成了 j < 2
结果自己写这些简单题目的时候心态还是不行,最后就是纠结了一天多,然后又回到了开始的起点。
感谢 GJ 童鞋的提醒,要不然不知道乱改到什么时候去。

分析:

线段相交模板:自己画图理解下,还是推荐直接写自己的模板比较好了。
// = 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;
}

然后就是线段的比较判断问题:
注意: 是要与不和当前线段直接相连的每一条线段都比较。
          那么我们每次加入一个点,直接判断新形成的线段与前面所有出现过的不直接相连的比较就可以了
所以应该从第三条线段开始枚举。
因为第一条线段没有线段可以和它比较;
第二条线段前面只有一条线段,而这条线段又是和他直接相连的,所以不用比较。
这样一直到了第 N-1 条线段都可以这么判断下去。
然后注意到最后一条线段是终点和起点相连形成的,所以它不能和第一条线段比较,也不可以和第 n-1 条线段比较。2.
最后就是叉积求面积,不用解释了吧。
//叉积求面积 ,下标从 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 37
0 0
1 1
3 0
5 1
5 -1
3 0
1 -15
0 0
4 0
3 1
2 0
1 1*/

转载于:https://www.cnblogs.com/james1207/p/3275759.html

zoj 1010 Area【线段相交问题】相关推荐

  1. zoj 1010 (线段相交判断+多边形求面积)

    链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=10 Area Time Limit: 2 Seconds      Mem ...

  2. zoj 1010 Area (叉积求面积 与 跨立相交实验判断相交)

    题目链接:zoj 1010 题意:给你 N  个点的坐标,点是按照顺序输入的.每一个点都与它后面的那个点连成一条线段,最后一个点与起点相连. 求组成的多边形的面积. 参考博客:https://blog ...

  3. hdu 1558(线段相交+并查集)

    题意:给你一些操作,P后边输入四个值,分别代表一条线段的起点.终点坐标,当输入Q时,后边输入一个整形值K,输出第k条线段所在的集合中包含的线段的个数. 解题思路:线段相交+并查集,sum[i]表示i所 ...

  4. poj3304(线段相交问题)

    题意:是否存在这样一条直线,使这条直线和所有的线段相交. #include<iostream> #include<algorithm> #include<cstring& ...

  5. poj1410(线段相交问题判断)

    题意:给一个线段的两个端点坐标(确定线段),再给一个矩形的左上角的坐标和右下角的坐标(确定矩形),问判断该线段是否和矩形相交,在矩形内也算和矩形相交.直接用线段和矩形的四条边判断是否相交即可. #in ...

  6. 简单几何(线段相交+最短路) POJ 1556 The Doors

    题目传送门 题意:从(0, 5)走到(10, 5),中间有一些门,走的路是直线,问最短的距离 分析:关键是建图,可以保存所有的点,两点连通的条件是线段和中间的线段都不相交,建立有向图,然后用Dijks ...

  7. 叉乘(三)——线段与线段相交吗?

    用途3: 我们现在的任务就是判断线段P1P2和线段Q1Q2是否相交. 我们分两步确定两条线段是否相交: (1)快速排斥试验 设以线段 P1P2 为对角线的矩形为R, 设以线段 Q1Q2 为对角线的矩形 ...

  8. POJ 2653 Pick-up sticks (线段相交)

    题意:给你n条线段依次放到二维平面上,问最后有哪些没与前面的线段相交,即它是顶上的线段 题解:数据弱,正向纯模拟可过 但是有一个陷阱:如果我们从后面向前枚举,找与前面哪些相交,再删除前面那些相交的线段 ...

  9. 判断线段相交(hdu1558 Segment set 线段相交+并查集)

    先说一下题目大意:给定一些线段,这些线段顺序编号,这时候如果两条线段相交,则把他们加入到一个集合中,问给定一个线段序号,求在此集合中有多少条线段. 这个题的难度在于怎么判断线段相交,判断玩相交之后就是 ...

最新文章

  1. 线上日志分析与其他一些脚本
  2. 在网页输出10的阶乘.php,ASP网络程序设计实验报告和期末考试复习范围
  3. 电子信息工程班徽设计_蜻蜓AI说专业:与5G时代息息相关的电子信息工程专业怎么样?...
  4. IBASE的hierarchy结构
  5. Apache Spark 的设计与实现(job逻辑执行图)
  6. 设计模式:迪米特原则
  7. 常用机器学习算法优缺点及其应用领域
  8. js alert 封装 layui
  9. java log4j 相对路径_log4j中配置日志文件相对路径[续集]
  10. 1415120000,华为这个数字赞爆!
  11. idea中git替换,推送到新的github或者gitlab上面
  12. pyQT实现自动找茬游戏
  13. 鸿蒙能兼容java吗,鸿蒙系统能不能兼容windows的所有应用软件?
  14. epson r1900 清零软件_爱普生打印机清零软件
  15. win10配置oracle环境变量,win10环境下Oracle环境搭建过程
  16. 结构风荷载理论与matlab计算公式,结构风荷载理论与MATLAB计算
  17. ROS1云课→19仿真turtlebot(stage)
  18. 市场调研方法:焦点小组访谈法
  19. 两位数码管秒表c语言,单片机C语言编程实现双数码管可调秒表
  20. FLUENT中VOF模型的仿真流程

热门文章

  1. App邀请注册如何提高效率
  2. Beginning WF4读书笔计 - 第一章 03设计示图及xaml代码
  3. echarts水球图
  4. 3D坐标系中 点 的 平移、旋转和缩放
  5. 计算机图形学(四)几何变换_3_矩阵逆变换
  6. 基于asp.net319一嗨租车汽车租赁系统
  7. 惠普服务器SPP包制作方法
  8. 清晨,赶路,感觉像孤魂野鬼,没有归宿感
  9. python小程序抢购脚本怎么写_实战|抢购脚本编写
  10. 服务器放在北极什么位置,服务器放在北极和深海,是谁给了你勇气?