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.
 
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
4
0 0
0 1
1 1
1 0
0 0 1 1
4
0 0
0 1
1 1
1 0
0 0 1 0
9
0 0
0 2
1 1
2 2
3 1
4 2
5 1
6 2
6 0
0 1 6 1
 
Sample Output
1.414
1.000
6.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;
}

@执念  "@☆但求“❤”安★
下次我们做的一定会更好。。。。




为什么这次的题目是英文的。。。。QAQ...

计算机学院大学生程序设计竞赛(2015’12)Polygon的更多相关文章

  1. hdu 计算机学院大学生程序设计竞赛(2015’11)

    搬砖 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submissi ...

  2. 计算机学院大学生程序设计竞赛(2015’11)1005 ACM组队安排

    1005 ACM组队安排 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Pro ...

  3. 计算机学院大学生程序设计竞赛(2015’12) 1002 Polygon

    #include<iostream> #include<cstring> #include<cstdio> #include<cmath> #inclu ...

  4. 计算机学院大学生程序设计竞赛(2015’12)Study Words

    Study Words Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  5. 计算机学院大学生程序设计竞赛(2015’12)The Country List

    The Country List Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  6. 计算机学院大学生程序设计竞赛(2015’12) 1008 Study Words

    #include<cstdio> #include<cstring> #include<map> #include<string> #include&l ...

  7. 计算机学院大学生程序设计竞赛(2015’12) 1009 The Magic Tower

    #include<cmath> #include<cstdio> #include<cstring> #include<algorithm> using ...

  8. 计算机学院大学生程序设计竞赛(2015’12) 1006 01 Matrix

    #include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> ...

  9. 计算机学院大学生程序设计竞赛(2015’12) 1003 The collector’s puzzle

    #include<cstdio> #include<algorithm> using namespace std; using namespace std; +; int a[ ...

随机推荐

  1. Unable to instantiate Action...............

    当时找了好久都没搞明白,出现这样的问题,可能不是代码错了.将新添加的action不要添加在末尾,这样可能导致这样的问题出现,所以新添加的action最好放置在struts配置文件中间.

  2. 并发调用get请求

    http://zeusami.iteye.com/blog/1172864 package com.alibaba.xteam.web.travel.module.rpc; import java.i ...

  3. Java基础之线程——使用Runnable接口(JumbleNames)

    控制台程序. 除了定义Thread新的子类外,还可以在类中实现Runnable接口.您会发现这比从Thread类派生子类更方便,因为在实现Runnable接口时可以从不是Thread的类派生子类,并且 ...

  4. PostgreSQL数据导出导入COPY

    [postgres@DELL-R720 bin]$ ./psql -p 6432psql (9.4.5)Type "help" for help. postgres=# postg ...

  5. 有时候dfs可以简化各种组合的操作

    比如有时某些操作是组合起来的,你不用去模拟每一种,把其拆分为几种单个操作,就可以了,因为反正会枚举所有的,所以也反正会组合出那种...而且不易出错.. 当然以上只是一种思维方式,并不一定可行,还要考虑 ...

  6. 记录把方法添加到 JavaScript 对象不明白的地方

    <!DOCTYPE html> <html> <body> <script> function person(firstname,lastname,ag ...

  7. [转]WEB开发者必备的7个JavaScript函数

    我记得数年前,只要我们编写JavaScript,都必须用到几个常用的函数,比如,addEventListener 和 attachEvent,并不是为了很超前的技术和功能,只是一些基本的任务,原因是各 ...

  8. android 单例模式

    单例模式特点: 1.一个类只能有一个实例 2.自己创建这个实例 3.整个系统都要使用这个实例 单例模式的形式: 1.饿汉式单例类 public class Singleton { private Si ...

  9. HDU 4031 Attack(离线+线段树)(The 36th ACM/ICPC Asia Regional Chengdu Site —— Online Contest)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4031 Problem Description Today is the 10th Annual of ...

  10. Linux 配置双机SSH信任

    一.实现原理 使用一种被称为"公私钥"认证的方式来进行ssh登录."公私钥"认证方式简单的解释是: 首先在客户端上创建一对公私钥(公钥文件:~/.ssh/id_ ...