判断两条线段是否相交(三种算法)

算法1:

 

[cpp]  view plain copy
  1. ///----------alg 1------------  
  2. struct Point  
  3. {   
  4.     double x, y;  
  5. };   
  6.   
  7. bool between(double a, double X0, double X1)  
  8. {  
  9.     double temp1= a-X0;  
  10.     double temp2= a-X1;  
  11.     if ( ( temp1<1e-8 && temp2>-1e-8 ) || ( temp2<1e-6 && temp1>-1e-8 ) )  
  12.     {  
  13.         return true;  
  14.     }  
  15.     else  
  16.     {  
  17.         return false;  
  18.     }  
  19. }  
  20.   
  21.   
  22. // 判断两条直线段是否有交点,有则计算交点的坐标  
  23. // p1,p2是直线一的端点坐标  
  24. // p3,p4是直线二的端点坐标  
  25. bool detectIntersect(Point p1, Point p2, Point p3, Point p4)  
  26. {  
  27.     double line_x,line_y; //交点  
  28.     if ( (fabs(p1.x-p2.x)<1e-6) && (fabs(p3.x-p4.x)<1e-6) )  
  29.     {  
  30.         return false;  
  31.     }  
  32.     else if ( (fabs(p1.x-p2.x)<1e-6) ) //如果直线段p1p2垂直与y轴  
  33.     {  
  34.         if (between(p1.x,p3.x,p4.x))  
  35.         {  
  36.             double k = (p4.y-p3.y)/(p4.x-p3.x);  
  37.             line_x = p1.x;  
  38.             line_y = k*(line_x-p3.x)+p3.y;  
  39.   
  40.             if (between(line_y,p1.y,p2.y))  
  41.             {  
  42.                 return true;  
  43.             }  
  44.             else  
  45.             {  
  46.                 return false;  
  47.             }  
  48.         }  
  49.         else   
  50.         {  
  51.             return false;  
  52.         }  
  53.     }  
  54.     else if ( (fabs(p3.x-p4.x)<1e-6) ) //如果直线段p3p4垂直与y轴  
  55.     {  
  56.         if (between(p3.x,p1.x,p2.x))  
  57.         {  
  58.             double k = (p2.y-p1.y)/(p2.x-p1.x);  
  59.             line_x = p3.x;  
  60.             line_y = k*(line_x-p2.x)+p2.y;  
  61.   
  62.             if (between(line_y,p3.y,p4.y))  
  63.             {  
  64.                 return true;  
  65.             }  
  66.             else  
  67.             {  
  68.                 return false;  
  69.             }  
  70.         }  
  71.         else   
  72.         {  
  73.             return false;  
  74.         }  
  75.     }  
  76.     else  
  77.     {  
  78.         double k1 = (p2.y-p1.y)/(p2.x-p1.x);   
  79.         double k2 = (p4.y-p3.y)/(p4.x-p3.x);  
  80.   
  81.         if (fabs(k1-k2)<1e-6)  
  82.         {  
  83.             return false;  
  84.         }  
  85.         else   
  86.         {  
  87.             line_x = ((p3.y - p1.y) - (k2*p3.x - k1*p1.x)) / (k1-k2);  
  88.             line_y = k1*(line_x-p1.x)+p1.y;  
  89.         }  
  90.   
  91.         if (between(line_x,p1.x,p2.x)&&between(line_x,p3.x,p4.x))  
  92.         {  
  93.             return true;  
  94.         }  
  95.         else   
  96.         {  
  97.             return false;  
  98.         }  
  99.     }  
  100. }  
  101. ///------------alg 1------------  

 

 

算法2:

[cpp]  view plain copy
  1. ///------------alg 2------------  
  2. //叉积  
  3. double mult(Point a, Point b, Point c)  
  4. {  
  5.     return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);  
  6. }  
  7.   
  8. //aa, bb为一条线段两端点 cc, dd为另一条线段的两端点 相交返回true, 不相交返回false  
  9. bool intersect(Point aa, Point bb, Point cc, Point dd)  
  10. {  
  11.     if ( max(aa.x, bb.x)<min(cc.x, dd.x) )  
  12.     {  
  13.         return false;  
  14.     }  
  15.     if ( max(aa.y, bb.y)<min(cc.y, dd.y) )  
  16.     {  
  17.         return false;  
  18.     }  
  19.     if ( max(cc.x, dd.x)<min(aa.x, bb.x) )  
  20.     {  
  21.         return false;  
  22.     }  
  23.     if ( max(cc.y, dd.y)<min(aa.y, bb.y) )  
  24.     {  
  25.         return false;  
  26.     }  
  27.     if ( mult(cc, bb, aa)*mult(bb, dd, aa)<0 )  
  28.     {  
  29.         return false;  
  30.     }  
  31.     if ( mult(aa, dd, cc)*mult(dd, bb, cc)<0 )  
  32.     {  
  33.         return false;  
  34.     }  
  35.     return true;  
  36. }  
  37. ///------------alg 2------------  
  

 

 

算法3:http://dec3.jlu.edu.cn/webcourse/t000096/graphics/chapter5/01_1.

html

[c-sharp]  view plain copy
  1. ///------------alg 3------------  
  2. double determinant(double v1, double v2, double v3, double v4)  // 行列式  
  3. {  
  4.     return (v1*v3-v2*v4);  
  5. }  
  6.   
  7. bool intersect3(Point aa, Point bb, Point cc, Point dd)  
  8. {  
  9.     double delta = determinant(bb.x-aa.x, cc.x-dd.x, bb.y-aa.y, cc.y-dd.y);  
  10.     if ( delta<=(1e-6) && delta>=-(1e-6) )  // delta=0,表示两线段重合或平行  
  11.     {  
  12.         return false;  
  13.     }  
  14.     double namenda = determinant(cc.x-aa.x, cc.x-dd.x, cc.y-aa.y, cc.y-dd.y) / delta;  
  15.     if ( namenda>1 || namenda<0 )  
  16.     {  
  17.         return false;  
  18.     }  
  19.     double miu = determinant(bb.x-aa.x, cc.x-aa.x, bb.y-aa.y, cc.y-aa.y) / delta;  
  20.     if ( miu>1 || miu<0 )  
  21.     {  
  22.         return false;  
  23.     }  
  24.     return true;  
  25. }  
  26. ///------------alg 3------------  

 

main函数测试:

 

[cpp]  view plain copy
  1. int main()  
  2. {  
  3.     Point p1, p2, p3, p4;  
  4.     p1.x = 1;  
  5.     p1.y = 4;  
  6.     p2.x = 3;  
  7.     p2.y = 0;  
  8.     p3.x = 0;  
  9.     p3.y = 1;  
  10.     p4.x = 4;  
  11.     p4.y = 3;  
  12.   
  13.   
  14.     int i=0, j=0;  
  15.     bool flag = false;  
  16.     flag = intersect3(p1, p2, p3, p4);  
  17.   
  18.     // alg 2  
  19.     time_t seconds1 = time (NULL);  
  20.     for ( ; i!=20000; ++i )  
  21.     {  
  22.         for ( j=0; j!=60000; ++j )  
  23.         {  
  24.             flag = detectIntersect(p1, p2, p3, p4);  
  25.         }  
  26.     }  
  27.     time_t seconds2 = time (NULL);  
  28.   
  29.     cout << "Time used in alg 1:" << seconds2-seconds1 << " seconds." << endl;  
  30.   
  31.     // alg 2  
  32.     time_t seconds3 = time (NULL);  
  33.     i=0;  
  34.     j=0;  
  35.     for ( ; i!=20000; ++i )  
  36.     {  
  37.         for ( j=0; j!=60000; ++j )  
  38.         {  
  39.             flag = intersect(p1, p2, p3, p4);  
  40.         }  
  41.     }  
  42.     time_t seconds4 = time (NULL);  
  43.     cout << "Time used in alg 2:" << seconds4-seconds3 << " seconds." << endl;  
  44.   
  45.     // alg 3  
  46.     time_t seconds5 = time (NULL);  
  47.     i=0;  
  48.     j=0;  
  49.     for ( ; i!=20000; ++i )  
  50.     {  
  51.         for ( j=0; j!=60000; ++j )  
  52.         {  
  53.             flag = intersect3(p1, p2, p3, p4);  
  54.         }  
  55.     }  
  56.     time_t seconds6 = time (NULL);  
  57.     cout << "Time used in alg 3:" << seconds6-seconds5 << " seconds." << endl;  
  58.     return 0;  
  59. }  
  

 VS2008编译器环境下测试结果:

Debug模式下: 

alg 1: 315 seconds;

alg 2: 832 seconds;

alg 3: 195 seconds;

 

Release模式下: 

alg 1: 157 seconds;

alg 2: 169 seconds;

alg 3: 122 seconds;

 

 

结论: 使用算法3,时间复杂度最低。

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 首先,我们需要分别获取这两条线段的端点坐标。假设这两条线段分别为线段A和线段B,则线段A有两个端点坐标(x1,y1)和(x2,y2),线段B有两个端点坐标(x3,y3)和(x4,y4)。 然后,我们可以使用叉积来判断两条线段是否相交。我们可以计算出线段A的向量(x2-x1,y2-y1)和线段B的向量(x4-x3,y4-y3)的叉积。如果两条线段相交,则这两个向量的叉积一定不为零。 具体代码如下: ``` bool isIntersect(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) { double v1 = (x4-x3)*(y1-y3) - (y4-y3)*(x1-x3); double v2 = (x4-x3)*(y2-y3) - (y4-y3)*(x2-x3); double v3 = (x2-x1)*(y3-y1) - (y2-y1)*(x3-x1); double v4 = (x2-x1)*(y4-y1) - (y2-y1)*(x4-x1); return (v1*v2 < 0) && (v3*v4 < 0); } ``` 这段代码中,函数isIntersect()接收两条线段的端点坐标作为参数,并返回一个布尔值,表示这两条线段是否相交。 ### 回答2: 要判断两条线段是否相交可以使用QGIS中的C++代码。以下是一个简单的示例代码: ```cpp #include <qgsgeometry.h> bool areLinesIntersecting(const QgsPointXY& p1, const QgsPointXY& p2, const QgsPointXY& p3, const QgsPointXY& p4) { QgsGeometry g1(QgsWkbTypes::LineString); QgsGeometry g2(QgsWkbTypes::LineString); QgsPointXY points[] = { p1, p2 }; g1.addPart(QgsLineString(points, sizeof(points) / sizeof(points[0]))); QgsPointXY points2[] = { p3, p4 }; g2.addPart(QgsLineString(points2, sizeof(points2) / sizeof(points2[0]))); return g1.intersects(g2); } int main() { QgsPointXY p1(0, 0); QgsPointXY p2(10, 10); QgsPointXY p3(5, 0); QgsPointXY p4(5, 10); bool intersecting = areLinesIntersecting(p1, p2, p3, p4); if (intersecting) { qDebug() << "The lines are intersecting."; } else { qDebug() << "The lines are not intersecting."; } return 0; } ``` 以上代码定义了一个`areLinesIntersecting`函数,它接受四个点作为参数来表示两条线段。使用QgsGeometry创建两个线段,然后通过`intersects`函数来判断是否相交。在`main`函数中,我们定义了四个点作为示例输入,并调用`areLinesIntersecting`函数进行判断。根据返回结果,打印出相应的信息。 请注意,代码中使用了QGIS的相关类和函数,所以确保已正确安装并配置了QGIS库。另外,此代码只是一个简单示例,实际使用时可能需要考虑更多的边界情况和错误处理。 ### 回答3: 首先,QGIS是一个开源的地理信息系统软件,支持Python编程语言。要判断两条线段是否相交,可以利用QGIS的Geometry类来进行计算。下面是一个简单的示例代码,演示如何使用QGIS判断两条线段是否相交: ```python from qgis.core import * # 创建两条线段的几何对象 line1 = QgsGeometry.fromPolyline([QgsPointXY(0, 0), QgsPointXY(2, 2)]) line2 = QgsGeometry.fromPolyline([QgsPointXY(1, 0), QgsPointXY(1, 3)]) # 判断线段是否相交 if line1.intersects(line2): print("线段相交") else: print("线段相交") ``` 在上面的代码中,我们首先导入了`qgis.core`模块,该模块提供了与QGIS核心功能相关的功能。然后,我们使用`QgsGeometry`类创建了两条线段的几何对象。其中,`fromPolyline`方法用于创建线段的几何对象,传入的参数是一系列地理坐标点。接着,我们使用`intersects`方法判断两条线段是否相交。如果相交,则输出"线段相交",否则输出"线段相交"。 请注意,上述代码仅仅是一个示例,只适用于简单的线段相交判断。如果需要处理复杂的情况,例如线段重叠、线段线等,可能需要使用更复杂的算法或者调用其他库来完成。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值