计算机学院大学生程序设计竞赛(2015’12)Polygon
来源:互联网 发布:大华pss监控软件 编辑:程序博客网 时间:2024/05/19 06:17
Polygon
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 195 Accepted Submission(s): 41
Problem Description
Give you a simple polygon and a line, and your task is to calculate the length of the line which is covered by the polygon.
Input
There are several test cases.
Each test case starts with one non-negative integer n (3 <= n <= 1000) giving the number of the vertices of the polygon. Then following n lines, each line contains two real numbers standing for the x and y coordinates of a vertex. The vertices are given either in clockwise or counterclockwise order. Then four real numbers (sx, sy, tx, ty), standing for the coordinates of the two points of the line.
All real numbers are between -100000 and 100000.
Each test case starts with one non-negative integer n (3 <= n <= 1000) giving the number of the vertices of the polygon. Then following n lines, each line contains two real numbers standing for the x and y coordinates of a vertex. The vertices are given either in clockwise or counterclockwise order. Then four real numbers (sx, sy, tx, ty), standing for the coordinates of the two points of the line.
All real numbers are between -100000 and 100000.
Output
For each case print the total length of the line that covered by the polygon, with 3 digits after the decimal point.
Sample Input
40 0 0 11 11 00 0 1 140 0 0 11 11 00 0 1 090 00 21 12 23 14 25 16 26 00 1 6 1
Sample Output
1.4141.0006.000
#include<iostream>#include<cstring>#include<cstdio>#include<cmath>#include<algorithm>using namespace std;struct Point{ double x; double y;} p[1001], px[10001];int n;double eps=1e-8;int cmp(Point a, Point b){ if(abs(a.x-b.x)<eps) return a.y<b.y; return a.x<b.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));}double direction(Point pi,Point pj,Point pk){ return (pk.x-pi.x)*(pj.y-pi.y)-(pj.x-pi.x)*(pk.y-pi.y);}bool on_segment(Point pi,Point pj,Point pk){ if(direction(pi, pj, pk)==0) { if(pk.x>=min(pi.x,pj.x)&&pk.x<=max(pi.x,pj.x)&&pk.y>=min(pi.y,pj.y)&&pk.y<=max(pi.y,pj.y)) return true; } return false;}bool segments_intersect(Point p1,Point p2,Point p3,Point p4){ double d1=direction(p3,p4,p1); double d2=direction(p3,p4,p2); double d3=direction(p1,p2,p3); double d4=direction(p1,p2,p4); if(d1*d2<0&&d3*d4<0) return true; else if(d1==0&&on_segment(p3,p4,p1)) return true; else if(d2==0&&on_segment(p3,p4,p2)) return true; else if(d3==0&&on_segment(p1,p2,p3)) return true; else if(d4==0&&on_segment(p1,p2,p4)) return true; return false;}Point intersection(Point a1, Point a2, Point b1, Point b2){ Point ret = a1; double t = ((a1.x - b1.x) * (b1.y - b2.y) - (a1.y - b1.y) * (b1.x - b2.x)) / ((a1.x - a2.x) * (b1.y - b2.y) - (a1.y - a2.y) * (b1.x - b2.x)); ret.x += (a2.x - a1.x) * t; ret.y += (a2.y - a1.y) * t; return ret;}int InPolygon(Point a){ int i; Point b,c,d; b.y=a.y; b.x=1e15; int count=0; for(i=0; i<n; i++) { c = p[i]; d = p[i + 1]; if(on_segment(c,d,a)) return 1; if(abs(c.y-d.y)<eps) continue; if(on_segment(a,b,c)) { if(c.y>d.y) count++; } else if(on_segment(a,b,d)) { if(d.y>c.y) count++; } else if(segments_intersect(a,b,c,d)) count++; } return count%2;}bool Intersect(Point s,Point e,Point a,Point b){ return direction(e,a,s)*direction(e,b,s)<=0;}double calculate(Point s,Point e){ int i,k=0; double sum; Point a,b,temp; for(i=0; i<n; i++) { a=p[i]; b=p[i+1]; if(abs(direction(e,a,s))<eps&&abs(direction(e,b,s))<eps) { px[k++]=a; px[k++]=b; } else if(Intersect(s,e,a,b)) { px[k++]=intersection(s,e,a,b); } } if(k==0) return 0.0; sort(px,px+k,cmp); px[k]=px[0]; sum=0; for(i=0; i<k-1; i++) { a=px[i]; b=px[i+1]; temp.x=(a.x+b.x)/2.0; temp.y=(a.y+b.y)/2.0; if(InPolygon(temp)) sum+=dist(a,b); } return sum;}int main(){ int i; double sum; Point s,e; while(~scanf("%d",&n)&&n) { for(i=0; i<n; i++) scanf("%lf%lf",&p[i].x,&p[i].y); p[n]=p[0]; scanf("%lf%lf%lf%lf",&s.x,&s.y,&e.x,&e.y); sum=calculate(s,e); printf("%.3lf\n",sum); } return 0;}
1 0
- 计算机学院大学生程序设计竞赛(2015’12)Polygon
- 计算机学院大学生程序设计竞赛(2015’12)(STL )
- 计算机学院大学生程序设计竞赛(2015’12)Happy Value
- 计算机学院大学生程序设计竞赛(2015’12)Bitwise Equations
- 计算机学院大学生程序设计竞赛(2015’12)01 Matrix
- 计算机学院大学生程序设计竞赛(2015’12)Pick Game
- 计算机学院大学生程序设计竞赛(2015’12)Study Words
- 计算机学院大学生程序设计竞赛(2015’12)Bitwise Equations
- hdu 计算机学院大学生程序设计竞赛(2015’11)质方数
- hdu 计算机学院大学生程序设计竞赛(2015’11)游乐场
- 计算机学院大学生程序设计竞赛(2015’11)
- hdu--计算机学院大学生程序设计竞赛(2015’11)
- hdu-- 计算机学院大学生程序设计竞赛(2015’11)
- hdu-- 计算机学院大学生程序设计竞赛(2015’11)
- hdu-- 计算机学院大学生程序设计竞赛(2015’11)
- hdu-- 计算机学院大学生程序设计竞赛(2015’11)
- 计算机学院大学生程序设计竞赛(2015’11)
- 计算机学院大学生程序设计竞赛(2015’11)1004 质方数
- HDU 1495 非常可乐
- 虚拟化三剑客专题-VMware(下)
- 触发器有两个特殊的表:插入表(instered表)和删除表(deleted表)
- 为OLED屏增加GUI支持2:2D图形库
- 对onreadystatechange属性的理解
- 计算机学院大学生程序设计竞赛(2015’12)Polygon
- 虚拟化三剑客专题-VMware(上)
- C++中string、int常见类型转换(含代码演示)
- 移动互联网长连接方案实例
- install devstack (liberty) in ubuntu14.04(完整解决)
- Objective C运行时(runtime)技术的几个要点总结
- SDWebImage
- php链接mysql数据库
- Gradle导入jar