【计算几何】 POJ 1127 Jack Straws 判断线段是否相交
给出n个线段
相交有传递关系
(当个模板)
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <cmath>
using namespace std;
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#define cler(arr, val) memset(arr, val, sizeof(arr))
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define IN freopen ("in.txt" , "r" , stdin);
#define OUT freopen ("out.txt" , "w" , stdout);
typedef long long LL;
const int MAXN = 10+5;
const int MAXM = 550000;
const int INF = 0x3f3f3f3f;
const int mod = 1000000007;
const double eps= 1e-8;
double add(double a,double b)//去除误差
{
if(abs(a+b)<eps*(abs(a)+abs(b)))
return 0;
else return a+b;
}
struct point
{
double x,y;
point(){}
point(double x,double y): x(x),y(y){}
point operator + (point p)
{
return point(add(x,p.x),add(y, p.y));
}
point operator - (point p)
{
return point(add(x,-p.x),add(y,-p.y));
}
point operator * (double d)
{
return point(x*d,y*d);
}
double dot(point p)//点积
{
return add(x*p.x ,y*p.y);
}
double det(point p)//叉积
{
return add(x*p.y ,-y*p.x);
}
};
bool onseg(point p1,point p2,point q)
{
return (p1-q).det(p2-q)==0 && (p1-q).dot(p2-q) <=0;
}
point intersection(point p1,point p2,point q1,point q2)
{
return p1 + (p2-p1)*((q2-q1).det(q1-p1)/(q2-q1).det(p2-p1));
}
point p[MAXN],q[MAXN];
bool g[MAXN][MAXN];
void solve(int n)
{
for(int i=1;i<=n;i++)
{
g[i][i]=true;
for(int j=1;j<i;j++)
{
if((p[i]-q[i]).det((p[j]-q[j]))==0)//平行时
{
g[i][j]=g[j][i]=onseg(p[i],q[i],p[j])
|onseg(p[i],q[i],q[j])
|onseg(p[j],q[j],p[i])
|onseg(p[j],q[j],q[i]);
}
else
{
point r =intersection(p[i],q[i],p[j],q[j]);
g[i][j]=g[j][i]=onseg(p[i],q[i],r)&&onseg(p[j],q[j],r);
}
}
}
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
g[i][j]|=g[i][k]&&g[k][j];
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif
int n,a,b;
while(scanf("%d",&n),n)
{
for(int i=1;i<=n;i++)
cin>>p[i].x>>p[i].y>>q[i].x>>q[i].y;
cler(g,false);
solve(n);
while(scanf("%d%d",&a,&b),a+b)
{
g[a][b]?cout<<"CONNECTED"<<endl:cout<<"NOT CONNECTED"<<endl;
}
}
return 0;
}

浙公网安备 33010602011771号