链接: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了好多次,要注意与当前线段相邻接的线段不判断

主要就是第一个线段,要跳过与下一条线段的相交性,以及最后一条线段的相交性,其他线段只需要向下跳过一个线段判断相交即可

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 #include <iostream>
  5 #include <algorithm>
  6 #include <math.h>
  7
  8 #define MAXX 1005
  9 #define eps 1e-8
 10 using namespace std;
 11
 12 typedef struct
 13 {
 14     double x;
 15     double y;
 16 }point;
 17
 18 typedef struct
 19 {
 20     point st;
 21     point ed;
 22 }line;
 23
 24 point p[MAXX];
 25 line li[MAXX];
 26
 27 double crossProduct(point a,point b,point c)
 28 {
 29     return (c.x-a.x)*(b.y-a.y)-(c.y-a.y)*(b.x-a.x);
 30 }
 31
 32 double dist(point a,point b)
 33 {
 34     return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
 35 }
 36
 37 bool  xy(double x,double y){ return x < y - eps; }
 38 bool  dy(double x,double y){ return x > y + eps; }
 39 bool xyd(double x,double y){ return x < y + eps; }
 40 bool dyd(double x,double y){ return x > y - eps; }
 41 bool  dd(double x,double y){ return fabs(x-y)<eps; }
 42
 43 bool onSegment(point a,point b,point c)
 44 {
 45     double maxx=max(a.x,b.x);
 46     double maxy=max(a.y,b.y);
 47     double minx=min(a.x,b.x);
 48     double miny=min(a.y,b.y);
 49
 50     if(dd(crossProduct(a,b,c),0.0)&&xyd(c.x,maxx)&&dyd(c.x,minx)
 51        &&xyd(c.y,maxy)&&dyd(c.y,miny))
 52         return true;
 53     return false;
 54 }
 55
 56 bool segIntersect(point p1,point p2,point p3,point p4)
 57 {
 58     double d1=crossProduct(p3,p4,p1);
 59     double d2=crossProduct(p3,p4,p2);
 60     double d3=crossProduct(p1,p2,p3);
 61     double d4=crossProduct(p1,p2,p4);
 62
 63     if(xy(d1*d2,0.0)&&xy(d3*d4,0.0))
 64         return true;
 65     if(dd(d1,0.0)&&onSegment(p3,p4,p1))
 66         return true;
 67     if(dd(d2,0.0)&&onSegment(p3,p4,p2))
 68         return true;
 69     if(dd(d3,0.0)&&onSegment(p1,p2,p3))
 70         return true;
 71     if(dd(d4,0.0)&&onSegment(p1,p2,p4))
 72         return true;
 73     return false;
 74 }
 75
 76 double Area(int n)
 77 {
 78     double ans=0.0;
 79
 80     for(int i=2; i<n; i++)
 81     {
 82         ans+=crossProduct(p[0],p[i-1],p[i]);
 83     }
 84     return fabs(ans)/2.0;
 85 }
 86
 87 int main()
 88 {
 89     int n,m,i,j;
 90     double x,y;
 91     int cas=1;
 92     while(scanf("%d",&n)!=EOF&&n)
 93     {
 94         for(i=0; i<n; i++)
 95         {
 96             scanf("%lf%lf",&p[i].x,&p[i].y);
 97         }
 98
 99         for(i=0; i<n-1; i++)
100         {
101             li[i].st.x=p[i].x;
102             li[i].st.y=p[i].y;
103             li[i].ed.x=p[i+1].x;
104             li[i].ed.y=p[i+1].y;
105         }
106         li[n-1].st.x=p[n-1].x;
107         li[n-1].st.y=p[n-1].y;
108         li[n-1].ed.x=p[0].x;
109         li[n-1].ed.y=p[0].y;
110         bool flag=false;
111         for(i=0; i<n; i++)
112         {
113             for(j=i+2; j<n; j++)
114             {
115                     if(i == 0 && j == n-1)continue;
116                     /*if((li[i].st.x == li[j].st.x && li[i].st.y == li[j].st.y)
117                        || li[i].st.x == li[j].ed.x && li[i].st.y == li[j].ed.y
118                        || li[i].ed.x == li[j].st.x && li[i].ed.y == li[j].st.y
119                        || li[i].ed.x == li[j].ed.x && li[i].ed.y == li[j].ed.y)
120                         continue;*/
121                     if(segIntersect(li[i].st,li[i].ed,li[j].st,li[j].ed))
122                     {
123                         flag=true;
124                         break;
125                     }
126             }
127         }
128
129         if(flag || n<3)
130         {
131             printf("Figure %d: Impossible\n",cas++);
132         }
133         else
134         {
135             double ans=Area(n);
136             printf("Figure %d: %.2lf\n",cas++,ans);
137         }
138         printf("\n");
139     }
140     return 0;
141 }

View Code

转载于:https://www.cnblogs.com/ccccnzb/p/3903291.html

zoj 1010 (线段相交判断+多边形求面积)相关推荐

  1. POJ3348 Cows【凸包+多边形求面积】

    POJ3348Cows 凸包+多边形求面积 个人分类: 计算几何凸包 Language: Default Cows Time Limit: 2000MS   Memory Limit: 65536K ...

  2. 七巧板复原算法——计算机图形学基本算法之二,线段相交判断

    判断线段相交,朴素的方法(初中直线方程的判断方法),就是先计算两条直线的交点,然后再判断交点是否在其中一条的线段上.这也是笔者能唯一想到的方法,后来抱着试试看有没有更好方法的想法,搜了一下网络.哦, ...

  3. 线段树扫描线模板(求面积和周长) Picture

    求面积: #include <iostream> #include <stdio.h> #include <algorithm> using namespace s ...

  4. 计算几何中的线段相交判断问题

    完整代码如下: /*点的加减,标量乘除操作*/ struct Point {double x, y;Point(double _x = 0, double _y = 0):x(_x), y(_y){} ...

  5. HDOJ2036改革春风吹满地笔记——任意多边形求面积

    题目地址 学习了任意多边形的计算,通过向量叉乘来进行计算. 计算公式 如果逆时针给出坐标,求得是正的,就是答案.如果顺时针给出坐标,求得是负,需要变正 具体推导过程 博客地址1 还学了海伦公式求三角形 ...

  6. ZOJ 1648 线段相交

    题意: 是否存在规范相交 View Code 1 #include <iostream> 2 #include <cstdlib> 3 #include <cstdio& ...

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

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

  8. zoj 1010 Area【线段相交问题】

    链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1010 http://acm.hust.edu.cn/vjudge/ ...

  9. 几何常用算法与判断线段相交【转】

    下面这个函数在我写的计算几何库函数里面有,那个库可以在http://algorithm.126.com/的资源中心   -   代码角   找到. 算法简单说明: 首先判断以两条线段为对角线的矩形是否 ...

最新文章

  1. 报错整理:ImportError: cannot import name ‘mean_absolute_percentage_error‘ from ‘sklearn.metrics‘
  2. Python爬虫-HTMLSession的使用
  3. linux下面jmeter对百度进行压力测试
  4. Gearman 启动日志文件提示协议出错的BUG
  5. 图文详解linux/windows mysql忘记root密码解决方案
  6. TechEd 2007 HOL分享
  7. java年月日时分秒格式_Java 日期时间 LocalDate LocalTime LocalDateTime类
  8. 【实践】网易云音乐推荐中用户行为序列深度建模.pdf(附下载链接)
  9. dbcc_DBCC FREEPROCCACHE命令介绍和概述
  10. Guava学习笔记:Immutable(不可变)集合
  11. java连接mysql数据库的详细步骤
  12. Arduino白泽四足机器人——matlab逆运动学求解
  13. 计算机无法登录到你的账户,win10电脑无法登陆到你的账户怎么办?win10电脑无法登陆到你的账户相关讲解...
  14. python分词统计词频_-用python找出一篇文章中词频最高的20个单词
  15. 根据前序遍历和[中序遍历]
  16. [C++] 获取IE代理服务器的账号密码
  17. 嵌入式项目实战——基于QT的视频监控系统设计(二)
  18. DVDFab Photo Enhancer AI一款声称可以将图片无损放大到夸张的40倍大小的人工智能软件
  19. 网络营销不是收费删帖:为网络营销正本清源
  20. 产品经理职业发展路径

热门文章

  1. Linux通过端口号杀死指定进程
  2. 2022-2028年中国加密货币交易所市场研究及前瞻分析报告
  3. 解决谷歌浏览器在非https下限制获取多媒体对象(音视频)的解决方式
  4. 2022-2028年中国汽车橡胶件行业市场调查研究及前瞻分析报告
  5. 浅显易懂 Makefile 入门 (08)— 默认 shell (/bin/sh)、命令回显、make参数(-n 只显示命令但不执行,-s 禁止所有回显)、单行命令、多行命令、并发执行
  6. TVM优化c++部署实践
  7. ALD对照CVD淀积技术的优势
  8. 适用于AMD ROC GPU的Numba概述
  9. 无人驾驶汽车开发平台,加速无人驾驶汽车的商业化
  10. 模型压缩95%:Lite Transformer,MIT韩松等人