您的位置:首页 > 其它

线段是否相交

2015-09-29 17:02 127 查看
给定两条线段,判断是否相交

#include<iostream>
#include<algorithm>//use sort
#include<cmath>//use fabs
using namespace std;
struct Point//点坐标
{
double x;
double y;
};
struct Line//线段属性
{
double k;
double b;
Point p1;
Point p2;
};
void IsIntersect(Line &line1, Line &line2);
int main()
{
Line l1, l2;
cin >> l1.p1.x >> l1.p1.y;
cin >> l1.p2.x >> l1.p2.y;
cin >> l2.p1.x >> l2.p1.y;
cin >> l2.p2.x >> l2.p2.y;
IsIntersect(l1, l2);
return 0;
}

void GetLine(Line &line)//求出线段的k和b
{
//不考虑两点横坐标相等
line.k = (line.p1.y - line.p2.y) / (line.p1.x - line.p2.x);
line.b = line.p1.y - line.k*line.p1.x;
}
bool JudgeIsIntersect(Line &line,double &pointY)//判断斜率存在的线段的两个纵坐标与直线交点的纵坐标,如果相交,那么交点的纵坐标是在line2两点纵坐标的中间
{
if (line.p1.y >= line.p2.y)
{
if (pointY <= line.p1.y&&pointY >= line.p2.y)
return true;
else
return false;
}
if (line.p1.y < line.p2.y)
{
if (pointY <= line.p2.y&&pointY >= line.p1.y)
return true;
else
return false;
}
}
void IsIntersect(Line &line1, Line &line2)
{
//两个变量判断斜率是否存在
double line1X = line1.p1.x - line1.p2.x;
double line2X = line2.p1.x - line2.p2.x;
/*
分四种情况
1.line1,line2斜率都不存在
2.line1斜率不存在,line2存在
3.line1存在,line2的斜率不存在
4.line1,line2的斜率都存在
*/

//1.
//line1,line2斜率都不存在
if (line1X == 0 && line2X == 0)
{
if (line1.p1.x == line2.p1.x)//共线:分为两个,相交(重合)与不相交
{
//如何判断是重合呢还是没有相交呢?
//两条线段,四个点,一条直线上,取两条线段横坐标之和即fabs(line1.p1.x - line1.p2.x) + fabs(line2.p1.x - line2.p2.x)
//这四个横坐标,有最大也有最小,那么最大到最小的距离double length = a[4] - a[0]
//通过比较这两个数值即可判断
double a[4] = { line1.p1.y,line1.p2.y,line2.p1.y,line2.p2.y };
sort(a, a + 4);
double length = a[3] - a[0];
double lengthSum = fabs(line1.p1.y - line1.p2.y) + fabs(line2.p1.y - line2.p2.y);
if (length > lengthSum)
cout << "不相交\n";
else
cout << "相交\n";
}
else
cout << "不想交\n";
}

//2.
//line1斜率不存在,line2存在
else if (line1X == 0 && line2X != 0)
{
GetLine(line2);
double intersectionY = line2.k*line1.p1.x + line2.b;//直线交点的纵坐标,如果相交,那么交点的纵坐标是在line2两点纵坐标的中间
if (JudgeIsIntersect(line2, intersectionY))
cout << "相交\n";
else
cout << "不相交\n";
}
//3.
//line1存在,line2的斜率不存在
else if (line1X != 0 && line2X == 0)
{
GetLine(line1);
double intersectionY = line1.k*line2.p1.x + line1.b;
if (JudgeIsIntersect(line1, intersectionY))
cout << "相交\n";
else
cout << "不相交\n";
}
//4.
//line1,line2的斜率都存在
else
{
GetLine(line1);
GetLine(line2);
if (line1.k == line2.k)//斜率相同
{
if (line1.b == line2.b)//共线:分为两个,相交(重合)与不相交
{
double a[4] = { line1.p1.x,line1.p2.x,line2.p1.x,line2.p2.x };
sort(a, a + 4);
double length = a[3] - a[0];
double lengthSum = fabs(line1.p1.x - line1.p2.x) + fabs(line2.p1.x - line2.p2.x);
if (length > lengthSum)
cout << "不相交\n";
else
cout << "相交\n";
}
else
cout << "不相交\n";
}
else//斜率不同
{
double intersectionX = (line2.b - line1.b) / (line1.k - line2.k);

double line1XLength = fabs(line1.p1.x - line1.p2.x);
double line1Length = fabs(line1.p1.x - intersectionX) + fabs(line1.p2.x - intersectionX);
double line2XLength = fabs(line2.p1.x - line2.p2.x);
double line2Length = fabs(line2.p1.x - intersectionX) + fabs(line2.p2.x - intersectionX);
if ((line1XLength == line1Length) && (line2XLength == line2Length))
cout << "相交\n";
else
cout << "不相交\n";
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: