Top

线段判相交及点与是否在点上


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
struct node{
double x;
double y;
};
bool judge(node a,node b,node c,node d) //两线段ab,cd是否相交
{
if(min(a.x,b.x) <= max(c.x,d.x) && min(c.x,d.x) <= max(a.x,b.x) && min(a.y,b.y) <= max(c.y,d.y) &&min(c.y,d.y)<=max(a.y,b.y))
{

double u,v,w,z;//保存叉乘
u=(c.x-a.x)*(b.y-a.y)-(b.x-a.x)*(c.y-a.y);
v=(d.x-a.x)*(b.y-a.y)-(b.x-a.x)*(d.y-a.y);
w=(a.x-c.x)*(d.y-c.y)-(d.x-c.x)*(a.y-c.y);
z=(b.x-c.x)*(d.y-c.y)-(d.x-c.x)*(b.y-c.y);
return (u*v<=0.00000001 && w*z<=0.00000001); //浮点数判断大小
}
return false;
}
bool onsegment(node pi,node pj,node Q) //Q点是否是在pipj线段上
{
if((Q.x-pi.x)*(pj.y-pi.y)==(pj.x-pi.x)*(Q.y-pi.y)&&min(pi.x,pj.x)<=Q.x&&Q.x<=max(pi.x,pj.x)&&min(pi.y,pj.y)<=Q.y&&Q.y<=max(pi.y,pj.y)){
return true;
}else{
return false;
}
}
bool check(node a,node b,node c,node d){ //连线段是否平行
double len=(a.x-b.x)*(c.y-d.y)-(c.x-d.x)*(a.y-b.y);
if(len==0)return 1;
else return 0;
}
文章版权为Anoyer博客所有,转载请以链接形式标明本文地址