判断能否找到一条直线使得与每个线段有交点
先枚举2条线段,再枚举出2条线段上的各一个端点,来当做直线。
判断直线相交直接利用向量线段表示法,然后用叉积去算
a是直线的一个点,v是方向向量,b是线段的一个点,w是方向向量
假设a+tv是交点
cj(a + tv − b,w) = 0
cj(a − b,w) + cj(tv,w) = 0
t ∗ cj(v,w) = cj(b − a,w)
t = cj(b − a,w)/cj(v,w)
a+tv的x,y坐标就知道了
还要特别注意如果w是垂直的话,就要判断y坐标是否在b线段内
不然直接用x坐标判断
找到了一条这样的直线直接返回
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define maxl 110
#define eps 1e-8
using namespace std;
int n;
bool ans;
struct point
{
double x,y;
point(double a=0,double b=0)
{
x=a;y=b;
}
point operator - (const point &b)const
{
return point(x-b.x,y-b.y);
}
double operator * (const point &b)const
{
return x*b.x+y*b.y;
}
double operator ^ (const point &b)const
{
return x*b.y-y*b.x;
}
double operator == (const point &b)const
{
return x>b.x-eps && x<b.x+eps &&
y>b.y-eps && y<b.y+eps;
}
double operator != (const point &b)const
{
return x<b.x-eps || x>b.x+eps || y<b.y-eps || y>b.y+eps;
}
};
struct line
{
point s,e;
double k;
line (point a=point(),point b=point())
{
s=a;e=b;
k=atan2(a.y-b.y,a.x-b.x);
}
}a[maxl];
inline void prework()
{
scanf("%d",&n);
point p1,p2;
for(int i=1;i<=n;i++)
{
scanf("%lf%lf",&p1.x,&p1.y);
scanf("%lf%lf",&p2.x,&p2.y);
a[i].s=p1;a[i].e=p2;
}
}
inline bool jug(line vec)
{
double t;
point p;
for(int i=1;i<=n;i++)
{
t=point(a[i].e-vec.s) ^ point(a[i].e-a[i].s);
t/=point(vec.e) ^ point(a[i].e-a[i].s);
p.x=vec.s.x+t*vec.e.x;
p.y=vec.s.y+t*vec.e.y;
if(a[i].s.x==a[i].e.x)
{
if(p.y<min(a[i].s.y,a[i].e.y)-eps ||
p.y>max(a[i].s.y,a[i].e.y)+eps)
return false;
}
else
{
if(p.x<min(a[i].s.x,a[i].e.x)-eps ||
p.x>max(a[i].s.x,a[i].e.x)+eps)
return false;
}
}
return true;
}
inline void mainwork()
{
ans=false;
line vec;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
if(a[i].s!=a[j].s && !ans)
{
vec.s=a[i].s;vec.e=a[j].s-a[i].s;
ans=jug(vec);
}
if(a[i].s!=a[j].e && !ans)
{
vec.s=a[i].s;vec.e=a[j].e-a[i].s;
ans=jug(vec);
}
if(a[i].e!=a[j].s && !ans)
{
vec.s=a[i].e;vec.e=a[j].s-a[i].e;
ans=jug(vec);
}
if(a[i].e!=a[j].e && !ans)
{
vec.s=a[i].e;vec.e=a[j].e-a[i].e;
ans=jug(vec);
}
if(ans)
return;
}
}
inline void print()
{
if(ans)
puts("Yes!");
else
puts("No!");
}
int main()
{
int t;
scanf("%d",&t);
for(int i=1;i<=t;i++)
{
prework();
mainwork();
print();
}
return 0;
}
(2)假设AB直线在线段CD中间,那么AC X AB和AB X AD必须是同号的
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define maxl 110
#define eps 1e-8
using namespace std;
int n;
bool ans;
struct point
{
double x,y;
point(double a=0,double b=0)
{
x=a;y=b;
}
point operator - (const point &b)const
{
return point(x-b.x,y-b.y);
}
double operator * (const point &b)const
{
return x*b.x+y*b.y;
}
double operator ^ (const point &b)const
{
return x*b.y-y*b.x;
}
double operator == (const point &b)const
{
return x>b.x-eps && x<b.x+eps &&
y>b.y-eps && y<b.y+eps;
}
double operator != (const point &b)const
{
return x<b.x-eps || x>b.x+eps || y<b.y-eps || y>b.y+eps;
}
};
struct line
{
point s,e;
double k;
line (point a=point(),point b=point())
{
s=a;e=b;
k=atan2(a.y-b.y,a.x-b.x);
}
}a[maxl];
inline void prework()
{
scanf("%d",&n);
point p1,p2;
for(int i=1;i<=n;i++)
{
scanf("%lf%lf",&p1.x,&p1.y);
scanf("%lf%lf",&p2.x,&p2.y);
a[i].s=p1;a[i].e=p2;
}
}
inline bool jug(line vec)
{
double t;
point p;
for(int i=1;i<=n;i++)
if((point(a[i].s-vec.s)^vec.e)*(vec.e^point(a[i].e-vec.s))<0-eps)
return false;
return true;
}
inline void mainwork()
{
ans=false;
line vec;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
if(a[i].s!=a[j].s && !ans)
{
vec.s=a[i].s;vec.e=a[j].s-a[i].s;
ans=jug(vec);
}
if(a[i].s!=a[j].e && !ans)
{
vec.s=a[i].s;vec.e=a[j].e-a[i].s;
ans=jug(vec);
}
if(a[i].e!=a[j].s && !ans)
{
vec.s=a[i].e;vec.e=a[j].s-a[i].e;
ans=jug(vec);
}
if(a[i].e!=a[j].e && !ans)
{
vec.s=a[i].e;vec.e=a[j].e-a[i].e;
ans=jug(vec);
}
if(ans)
return;
}
}
inline void print()
{
if(ans)
puts("Yes!");
else
puts("No!");
}
int main()
{
int t;
scanf("%d",&t);
for(int i=1;i<=t;i++)
{
prework();
mainwork();
print();
}
return 0;
}
2019.2.11板子更新
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define maxl 110
#define eps 1e-8
using namespace std;
int n;
bool ans;
inline int sgn(double x)
{
if(fabs(x)<eps) return 0;
if(x<0)
return -1;
else
return 1;
}
struct point
{
double x,y;
point(double a=0,double b=0)
{
x=a;y=b;
}
point operator - (const point &b)const
{
return point(x-b.x,y-b.y);
}
//按照x坐标排序
bool operator < (const point &b)const
{
return x<b.x-eps;
}
bool operator == (const point &b)const
{
return sgn(x-b.x)==0 && sgn(y-b.y)==0;
}
bool operator != (const point &b)const
{
return sgn(x-b.x)!=0 || sgn(y-b.y)!=0;
}
friend point operator * (const point &a,const double &b) //数乘
{
return point(b*a.x,b*a.y);
}
friend point operator * (const double &a,const point &b)
{
return point(a*b.x,a*b.y);
}
void transxy(double &sinb,double &cosb)
{ //逆时针旋转,给出sin,cos
double tx=x,ty=y;
x=tx*cosb-ty*sinb;
y=tx*sinb+ty*cosb;
}
void transxy(double &b)//逆时针旋转b弧度
{
double tx=x,ty=y;
x=tx*cos(b)-ty*sin(b);
y=tx*sin(b)+ty*cos(b);
}
double norm()
{
return sqrt(x*x+y*y);
}
};
inline double det(const point &a,const point &b)
{ //叉积
return a.x*b.y-a.y*b.x;
}
inline double dot(const point &a,const point &b)
{ //点积
return a.x*b.x+a.y*b.y;
}
inline double dist(const point &a,const point &b)
{
return (a-b).norm();
}
struct line
{
point s,e;
double k;
line (point a=point(),point b=point())
{
s=a;e=b;
k=atan2(a.y-b.y,a.x-b.x);
}
}a[maxl];
inline void prework()
{
scanf("%d",&n);
point p1,p2;
for(int i=1;i<=n;i++)
{
scanf("%lf%lf",&p1.x,&p1.y);
scanf("%lf%lf",&p2.x,&p2.y);
a[i].s=p1;a[i].e=p2;
}
}
inline bool jug(line vec)
{
double t;
point p;
for(int i=1;i<=n;i++)
if(det(point(a[i].s-vec.s),vec.e)*det(vec.e,point(a[i].e-vec.s))<0-eps)
return false;
return true;
}
inline void mainwork()
{
ans=false;
line vec;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
if(a[i].s!=a[j].s && !ans)
{
vec.s=a[i].s;vec.e=a[j].s-a[i].s;
ans=jug(vec);
}
if(a[i].s!=a[j].e && !ans)
{
vec.s=a[i].s;vec.e=a[j].e-a[i].s;
ans=jug(vec);
}
if(a[i].e!=a[j].s && !ans)
{
vec.s=a[i].e;vec.e=a[j].s-a[i].e;
ans=jug(vec);
}
if(a[i].e!=a[j].e && !ans)
{
vec.s=a[i].e;vec.e=a[j].e-a[i].e;
ans=jug(vec);
}
if(ans)
return;
}
}
inline void print()
{
if(ans)
puts("Yes!");
else
puts("No!");
}
int main()
{
int t;
scanf("%d",&t);
for(int i=1;i<=t;i++)
{
prework();
mainwork();
print();
}
return 0;
}

本文介绍了一种算法,用于判断是否存在一条直线能够与一组给定的线段都有交点。通过枚举线段及其端点构建直线,并利用向量叉积计算交点,确保直线与每条线段相交。适用于计算机图形学和算法竞赛。
2307

被折叠的 条评论
为什么被折叠?



