#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std; struct Point
{
double x;
double y;
} p[], px[];
int n;
double eps=1e-; 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)//判断点pk时候在线段pi, pj上
{
if(direction(pi, pj, pk)==)
{
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<&&d3*d4<)
return true;
else if(d1==&&on_segment(p3,p4,p1))
return true;
else if(d2==&&on_segment(p3,p4,p2))
return true;
else if(d3==&&on_segment(p1,p2,p3))
return true;
else if(d4==&&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 flag=;
int count=;
for(i=; i<n; i++)
{
c = p[i];
d = p[i + ];
if(on_segment(c,d,a))//该点在多边形的一条边上
return ;
if(abs(c.y-d.y)<eps)
continue;
if(on_segment(a,b,c))//和顶点相交的情况,如果y值较大则取
{
if(c.y>d.y)
count++;
}
else if(on_segment(a,b,d))//和顶点相交的情况,如果y值较大则取
{
if(d.y>c.y)
count++;
}
else if(segments_intersect(a,b,c,d))//和边相交
count++;
}
return count%;//当L和多边形的交点数目C是奇数的时候,P在多边形内,是偶数的话P在多边形外。
} bool Intersect(Point s,Point e,Point a,Point b)
{
return direction(e,a,s)*direction(e,b,s)<=;
}//所在直线相交 double calculate(Point s,Point e)
{
int i,j,k=;
double sum;
Point a,b,temp;
for(i=; i<n; i++) //遍历所有点计算交点
{
a=p[i];
b=p[i+];
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==)
return 0.0;
sort(px,px+k,cmp); // 排序,由于割线是直线,所以交点必定线性分布
px[k]=px[];
sum=;
for(i=; i<k-; i++)
{
a=px[i];
b=px[i+];
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 q,i,j;
double sum;
Point s,e;
while(~scanf("%d",&n))
{
if(n==)
break;
for(i=; i<n; i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
p[n]=p[];
scanf("%lf%lf%lf%lf",&s.x,&s.y,&e.x,&e.y);
sum=calculate(s,e);
printf("%.3f\n",sum);
}
return ;
}

计算机学院大学生程序设计竞赛(2015’12) 1002 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)Study Words

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

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

    Polygon Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  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. 严格递增类的dp Codeforces Round #371 (Div. 1) C dp

    http://codeforces.com/contest/713 题目大意:给你一个长度为n的数组,每次有+1和-1操作,在该操作下把该数组变成严格递增所需要的最小修改值是多少 思路:遇到这类题型, ...

  2. 第一次安装ubuntu要设置的东西

    1. 安装网卡驱动 lscpi 查看网卡型号 根据型号找到驱动源码 下载下来并编译 安装 2. 编译安卓源码的时候出现jdk型号不对的情况 把/usr/bin/java 删除,就可以了.

  3. keepalived 健康检测

    1.TCP方式 详见:http://www.cnblogs.com/tengpan-cn/p/5776574.html 以下内容,都是基于此进行修改 2.HTTP_GET 根据返回状态判断服务器是否正 ...

  4. (转)Tomcat内存设置详解

    Java内存溢出详解 一.常见的Java内存溢出有以下三种: 1. java.lang.OutOfMemoryError: Java heap space ----JVM Heap(堆)溢出JVM在启 ...

  5. on使用详解

    on()是bind(),live(),delegate()的替代品,1.7及1.7以后使用on() bind() 绑定元素 live() 为元素附加事件,匹配选择器的当前及未来的元素(比如由脚本创建的 ...

  6. MYSQL和ORACLE的触发器与存储过程语法差异

    整改了一番脚本,遇到了一些两种数据库之间的差异,记录一下: 触发器: 差异 MYSQL ORACLE 说明 创建语句不同 create trigger `AA` BEFORE INSERT on `B ...

  7. hiho#1128 : 二分·二分查找

    input 1<=n<=1e6 1<=k<=2*1e9 a1 a2 ... an 1<=an<=2*1e9 output k存在则输出k是第几大的数,否则输出-1 ...

  8. UVA 571 Jugs ADD18 小白书10 数学Part1 专题

    只能往一个方向倒,如c1=3,c2=5,a b从0 0->0 5->3 2->0 2->2 0->2 5->3 4->0 4->3 1->0 1- ...

  9. Struts2中的验证框架

    通过注解的方式,可以让方法不用验证 @SkipValidation public String toRegView() { System.out.println("toRegView&quo ...

  10. SCRIPT7002: XMLHttpRequest: 网络错误 0x2ef3, 由于出现错误 00002ef3&nbsp

    项目使用的是tomcat7 ,浏览器是ie11 突然打开浏览器发现不能获取数据了 tomcat conf server.xml 文件 <Connector port="8080&quo ...