两直线四坐标点判断相交并求交点
2017-11-17 17:39
399 查看
当判定两条线段相交后,可以进行交点的求解。
bool Intersection(double x1, double y1, double x2, double y2,
double x3, double y3, double x4, double y4,
double& dbX, double& dbY)
{
double a=x2-x1;
double b=x3-x4;
double c=y2-y1;
double d=y3-y4;
double g=x3-x1;
double h=y3-y1;
double f=a*d-b*c; //行列式
if(fabs(f)<1.0e-06) // delta=0,表示两线段重合或平行:delta<=(1e-6) && delta>=-(1e-6)
{
//Inverse matrix cannot be computed.
return false;
}
double t=(d*g-b*h)/f;
double s=(-c*g+a*h)/f;
if((0>t)||(t>1))
{
//tow line do not intersect.
return false;
}
if((0>s)||(s>1))
{
//tow line do not intersect.
return false;
}
dbX=x1+t*(x2-x1);
dbY=y1+t*(y2-y1);
return true;
}
bool Intersection(double x1, double y1, double x2, double y2,
double x3, double y3, double x4, double y4,
double& dbX, double& dbY)
{
double a=x2-x1;
double b=x3-x4;
double c=y2-y1;
double d=y3-y4;
double g=x3-x1;
double h=y3-y1;
double f=a*d-b*c; //行列式
if(fabs(f)<1.0e-06) // delta=0,表示两线段重合或平行:delta<=(1e-6) && delta>=-(1e-6)
{
//Inverse matrix cannot be computed.
return false;
}
double t=(d*g-b*h)/f;
double s=(-c*g+a*h)/f;
if((0>t)||(t>1))
{
//tow line do not intersect.
return false;
}
if((0>s)||(s>1))
{
//tow line do not intersect.
return false;
}
dbX=x1+t*(x2-x1);
dbY=y1+t*(y2-y1);
return true;
}
相关文章推荐
- 判断直线与线段是否相交,相交则输出交点x轴坐标
- 判断线段是否相交的函数和求直线交点的函数
- poj 1269 Intersecting Lines(直线相交判断,求相交交点)
- POJ 1269 Intersecting Lines 判断直线关系(相交(求交点),重合,平行)
- poj 1269 Intersecting Lines(判断直线相交 求交点)
- 两条线段知道端点line1(x1,y1)(x2,y2)line2(x3,y3)(x4,y4),判断两条线段是否相交,交点坐标(x,y)
- POJ 1269 Intersecting Lines(直线相交判断,求交点)
- poj 1269 判断直线相交,并求交点
- POJ 1039 Pipe(直线和线段相交判断,求交点)
- POJ 1039 Pipe(直线和线段相交判断,求交点)
- poj1269Intersecting Lines【直线相交平行共线的判断+求交点】
- poj 2074 Line of Sight(视线问题,求直线与线段的交点及判断相交)
- 判断两线段是否相交并计算交点坐标
- POJ-1269 直线相交的判断及交点
- 判断两条线段/直线相交,并求交点
- poj 1269 Intersecting Lines(判断两直线关系,并求交点坐标)
- Intersecting Lines(直线相交判断,求相交交点)
- poj 1269Intersecting Lines (直线相交判断,求相交交点)
- 判断两条线段/直线相交,并求交点
- POJ 1269 Intersecting Lines(直线相交判断,求交点)