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

分析: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. C++关于编译器合成的默认构造函数

    有两个常见的误解: 1.任何类如果没有定义默认构造函数,就会被合成出一个来. 2.编译器合成的默认构造函数会显式地设定类内每一个数据成员的默认值. 对于第一个误解,并不是任何类在没有显式定义默认构造函 ...

  2. Fedora 21 设置开机启动脚本

    sudo touch /etc/rc.d/rc.localsudo vim /etc/rc.d/rc.local 在/etc/rc.d/rc.local文件中写入, 然后使用:wq命令 保存并退出. ...

  3. jQuery动态添加的元素中处理字符串溢出后在指定字符数后添加省略号

    "+[jsonData[i].questitle.lenth>40?jsonData[i].questitle.substring(0,40)+"...":json ...

  4. C#连接、访问MySQL数据库

    一.准备工具 visual stuido(本示例使用visual studio 2010) MySql.Data.dll mysql_installer_community_V5.6.21.1_set ...

  5. 利用def生成dll文件

    DLL中导出函数的声明有两种方式:一种为在函数声明中加上__declspec(dllexport),这里不再举例说明:另外一种方式是采用模块定义(.def) 文件声明,.def文件为链接器提供了有关被 ...

  6. 2016022605 - redis订阅发布

    Redis的实现消息队列功能,消息发布者发送的消息,消息接收者接收发布者的消息.由该消息传送的链路被称为通道.在Redis客户端可以订阅任何数目的通道. 案例:给出一个客户端订阅一个通道名为redis ...

  7. MATLAB灰度图、中值滤波图

    x=imread(‘x.jpg’); x=rbg2gray(x);  %转成灰度图像 k=medfilt2(x);   %中值滤波,默认为3X3矩阵 figure, imshow(k); medfil ...

  8. Multilingual App Toolkit v2.2 release

    Multilingual App Toolkit v2.2 release Today we released Multilingual App Toolkit v2.2. This release ...

  9. go 语言与循环

    package main import "fmt" type Employee struct{name string; age int} func displayName(e *E ...

  10. MVC架构学习

    作为一名小小的GreenBird,学习MVC呢,已经花费了2天了,期间得到了美丽的学姐的帮助,初步整理了一下. 首先,学习MVC呢就先以一个标准的MVC的简单的例子来入手,然后根据学姐的PPT,我用v ...