题目大意:给一个凸多边形(点不是按顺序给的),然后计算给出的线段在这个凸多边形里面的长度,如果在边界不计算。

分析:WA2..WA3...WA4..WA11...WA的无话可说,总之细节一定考虑清楚,重合的时候一定是0

代码如下:

=========================================================================================================

#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<iostream>
using namespace std; const int MAXN = ;
const double EPS = 1e-;
const double FarX = 4e4+; int Sign(double x)
{
if(x > EPS)return ;
if(fabs(x) < EPS)return ;
return -;
}
struct point
{
double x, y;
point(double x=, double y=):x(x),y(y){}
point operator - (const point &t)const{
return point(x-t.x, y-t.y);
}
bool operator == (const point &t)const{
return Sign(x-t.x) == && Sign(y-t.y) == ;
}
double operator ^(const point &t)const{
return x*t.y - y*t.x;
}
double operator *(const point &t)const{
return x*t.x + y*t.y;
}
};
double Dist(point t1, point t2)
{
return sqrt((t1-t2)*(t1-t2));
}
struct segment
{
point S, E;
double a, b, c;///ax + by = c; segment(point S=, point E=):S(S), E(E){
a = S.y - E.y;
b = E.x - S.x;
c = E.x*S.y - S.x*E.y;
}
int Inter(const segment &tmp)const{
int t1 = Sign((S-E)^(tmp.S-E));
int t2 = Sign((S-E)^(tmp.E-E)); if(t1 == && t2 == )
return -; if(t1*t2 == -)
return ;
if(abs(t1+t2) == )///如果完全相交或者有一点相交,不考虑重合
return ;
return false;
}
point crossNode(const segment &tmp)const
{///两条线段的交点
point result;
result.x = (c*tmp.b-tmp.c*b) / (a*tmp.b-tmp.a*b);
result.y = (c*tmp.a-tmp.c*a) / (b*tmp.a-tmp.b*a); return result;
}
bool OnSeg(const point &p)
{///判断点是否在线段上
if(Sign((S-E)^(p-E)) == )
if(Sign((p.x-S.x)*(p.x-E.x)) <= )
if(Sign((p.y-S.y)*(p.y-E.y)) <= )
return true;
return false;
}
};
struct Polygon
{
int N;///有N个点
point vertex[MAXN]; int Point_In_Poly(const point &p)
{///点在多边形里面1,还是外面-1,还是边上 0
segment ray(p, point(FarX, p.y));
int cnt = ; for(int i=; i<N; i++)
{
segment L(vertex[i], vertex[i+]); if(L.OnSeg(p))
return ; if(ray.OnSeg(L.S))
{
if(L.E.y-L.S.y > EPS)
cnt ++;
}
else if(ray.OnSeg(L.E))
{
if(L.S.y-L.E.y > EPS)
cnt ++;
}
else if(L.Inter(ray)== && ray.Inter(L)==)
cnt++;
} if(cnt % )
return ;
return -;
}
double Seg_In_Poly(const segment &L1)
{///线段与多边形相交的长度,先求出线段与多边形有几个交点
point p[];
int k=; for(int i=; i<N; i++)
{
segment L2(vertex[i], vertex[i+]); if(L1.Inter(L2) == - && L2.Inter(L1) == -)
return ; if(k< && L1.Inter(L2) && L2.Inter(L1))
{
point t = L1.crossNode(L2);
if(!k || !(p[] == t) )
p[k++] = t;
}
} double len=; if(k == )
{///有两个不同的交点
len = Dist(p[], p[]);
}
else if(k == )
{///有一个交点,判断哪个端点在多边形内
if(Point_In_Poly(L1.S) == )
len = Dist(p[], L1.S);
else if(Point_In_Poly(L1.E) == )
len = Dist(p[], L1.E);
}
else if(Point_In_Poly(L1.S) == )
{///没有交点的时候,判断线段是否在多边形内
len = Dist(L1.S, L1.E);
} return len;
}
};
Polygon poly; bool cmp(point t1, point t2)
{
return Sign((t2-poly.vertex[])^(t1-poly.vertex[])) < ;
} int main()
{
int i, M, ki=; scanf("%d", &poly.N); for(i=; i<poly.N; i++)
{
scanf("%lf%lf", &poly.vertex[i].x, &poly.vertex[i].y);
if(poly.vertex[ki].y > poly.vertex[i].y ||
(poly.vertex[ki].y == poly.vertex[i].y && poly.vertex[ki].x > poly.vertex[i].x) )
ki = i;
} swap(poly.vertex[], poly.vertex[ki]);
sort(poly.vertex+, poly.vertex+poly.N, cmp);
poly.vertex[poly.N] = poly.vertex[]; scanf("%d", &M); while(M--)
{
point A, B; scanf("%lf%lf%lf%lf", &A.x, &A.y, &B.x, &B.y);
segment L(A, B);
printf("%.6f\n",poly.Seg_In_Poly(L));
} return ;
}
/**
6
1 2
2 1
2 3
3 1
3 3
4 2
200
1 1 4 10
**/

Inheritance - SGU 129(线段与多边形相交的长度)的更多相关文章

  1. dtIntersectSegmentPoly2D 2D上的线段与多边形相交计算 产生结果:是否相交,线段跨越的开始和结束百分比,相交的边

    dtIntersectSegmentPoly2D(startPos, endPos, verts, nv, tmin, tmax, segMin, segMax): http://geomalgori ...

  2. Snake - SGU 128(构造多边形)

    题目大意:有N个点,如果可以使用这N个点连接,连接的时候任意两条边要成直角,任意边都要平行于x轴或者y轴,并且不能出现跨立相交,最终组成一个闭合的多边形,求出来这个多边形的最小长度. 分析:容易证明这 ...

  3. HDU 1558 Segment set (并查集+线段非规范相交)

    题目链接 题意 : 如果两个线段相交就属于同一集合,查询某条线段所属集合有多少线段,输出. 思路 : 先判断与其他线段是否相交,然后合并. #include <cstdio> #inclu ...

  4. 线段和矩形相交 POJ 1410

    // 线段和矩形相交 POJ 1410 // #include <bits/stdc++.h> #include <iostream> #include <cstdio& ...

  5. 判断线段和直线相交 POJ 3304

    // 判断线段和直线相交 POJ 3304 // 思路: // 如果存在一条直线和所有线段相交,那么平移该直线一定可以经过线段上任意两个点,并且和所有线段相交. #include <cstdio ...

  6. Geometric Shapes (poj3449多边形相交)

    题意:给你一些多边形的点,判断每个多边形和那些多边形相交,编号按照字典序输出 思路:枚举每个多边形的每条边看是否相交,这里的相交是包括端点的,关键是给你正方形不相邻两个点求另外两个点怎么求,长方形给你 ...

  7. hdu 5130(2014广州 圆与多边形相交模板)

    题意:一个很多个点p构成的多边形,pb <= pa * k时p所占区域与多边形相交面积 设p(x,y),       (x - xb)^2+(y - yb)^2 / (x - xa)^2+(y ...

  8. hdu3340 线段树+多边形

    Rain in ACStar Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  9. POJ_3304_Segments_线段判断是否相交

    POJ_3304_Segments_线段判断是否相交 Description Given n segments in the two dimensional space, write a progra ...

随机推荐

  1. .NET 互操作

    首先推荐一本书<精通.NET 互操作> ,这本书是目前中文资料里讲 互操作最详尽的书了. 做系统集成项目的同学应该都和设备打过交道(如视频设备:海康.大华等),在大多数情况下这些设备厂商会 ...

  2. macbook pro retina 编程字体推荐

    使用VS2010.VS2012.Qt Creator编译工具首推等宽字体,等宽字体中consolas. 首先大家都知道等宽对于编码来说的直观性不言而喻,其次retina屏幕的特殊性,整天用特别小的字体 ...

  3. 中文翻译:pjsip教程(三)之ICE stream transport的使用

    1:pjsip教程(一)之PJNATH简介 2:pjsip教程(二)之ICE穿越打洞:Interactive Connectivity Establishment简介 3:pjsip教程(三)之ICE ...

  4. 我的接口框架---框架函数文件common.php

    <?php defined('JDHU') OR die('no allow access'); /** * 加载配置文件 */ function &get_config($replac ...

  5. centos下的lnmp环境搭建

    1.配置centos的第三方yum源,因为原始的yum是无法安装nginx的 wget http://www.atomicorp.com/installers/atomic  下载atomic yum ...

  6. leetcode power(x,n)

    class Solution { public: double pow(double x, int n) { double a=1; if(n==0)return 1; if(x==1)return ...

  7. C#反射(一) 【转】

    在还不太熟悉反射的昨天,以为反射很神秘,在网上到处找答案.今天找了段代码敲了一下,茅塞顿开!其实反射也就那么简单的一回事!    反射是一种机制,通过这种机制我们可以知道一个未知类型的类型信息.比如, ...

  8. [BZOJ 2049] [Sdoi2008] Cave 洞穴勘测 【LCT】

    题目链接:BZOJ - 2049 题目分析 LCT的基本模型,包括 Link ,Cut 操作和判断两个点是否在同一棵树内. Link(x, y) : Make_Root(x); Splay(x); F ...

  9. 【Tools】Chrome 控制台不完全指南

    Chrome的开发者工具已经强大到没朋友的地步了,特别是其功能丰富界面友好的console,使用得当可以有如下功效: 更高「逼格」更快「开发调试」更强「进阶级的Frontender」 Bug无处遁形「 ...

  10. 【Database】MySQL各版本的区别

    MySQL 的官网下载地址:http://www.mysql.com/downloads/ 在这个下载界面会有几个版本的选择. 1. MySQL Community Server 社区版本,开源免费, ...