模板--判断两条线段是否相交--计算几何

模板--判断两条线段是否相交--计算几何;

1.两个结构体表示的是该条线段的A,B两个端点;

const int maxa=1e2+10;
const double pi=0.00000001;
struct nodea{
	int x,y;
}a[maxa];
struct nodeb{
	int x,y;
}b[maxa];
bool check(nodea a,nodeb b,nodea c,nodeb d){
    if(!(min(a.x,b.x)<=max(c.x,d.x)
	&&min(c.y,d.y)<=max(a.y,b.y)
	&&min(c.x,d.x)<=max(a.x,b.x)
	&&min(a.y,b.y)<=max(c.y,d.y)))	return false; 
    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<=pi&&w*z<=pi);
}

2.一个结构体表示的是该条线段的A,B两个端点,用于判断线段A,B是否相交;

const int maxa=1e2+10;
const double pi=1e-8;
const int inf=0x3f3f3f3f;
int n;
struct node{
	int x1,y1;
	int x2,y2;
}a[maxa];
bool check(node &a,node &b){
	if(!(min(a.x1,a.x2)<=max(b.x1,b.x2)
	&&min(b.y1,b.y2)<=max(a.y1,a.y2)
	&&min(b.x1,b.x2)<=max(a.x1,a.x2)
	&&min(a.y1,a.y2)<=max(b.y1,b.y2)))	return false;
	double u,v,w,z;
    u=(b.x1-a.x1)*(a.y2-a.y1)-(a.x2-a.x1)*(b.y1-a.y1);
    v=(b.x2-a.x1)*(a.y2-a.y1)-(a.x2-a.x1)*(b.y2-a.y1);
    w=(a.x1-b.x1)*(b.y2-b.y1)-(b.x2-b.x1)*(a.y1-b.y1);
    z=(a.x2-b.x1)*(b.y2-b.y1)-(b.x2-b.x1)*(a.y2-b.y1);
    return (u*v<=pi&&w*z<=pi);
}

题目链接:

传送门ZOJ--1648

传送门UVA--191

你可能感兴趣的:(#,新算法,#,常用模板代码,#,计算几何算法题目)