[几何]判断两个线段是否相交(多语言实现)

转载自:http://blog.csdn.net/shao941122/article/details/51488639

本文主要讲怎么判断两个线段是否相交


参考博客:

http://www.geeksforgeeks.org/check-if-two-given-line-segments-intersect/

http://www.cise.ufl.edu/~sitharam/COURSES/CG/kreveldintrolinesegment.pdf

http://geomalgorithms.com/a09-_intersect-3.html

http://hsfzxjy.github.io/the-simplest-way-to-find-out-if-two-segments-are-intersected/


How to check if two given line segments intersect?

Given two line segments (p1, q1) and (p2, q2), find if the given line segments intersect with each other.

Before we discuss solution, let us define notion of orientationOrientation of an ordered triplet of points in the plane can be
–counterclockwise
–clockwise
–colinear
The following diagram shows different possible orientations of (a, bc)


How is Orientation useful here?
Two segments (p1,q1) and (p2,q2) intersect if and only if one of the following two conditions is verified

1. General Case:
– (p1q1p2) and (p1q1q2) have different orientations and
– (p2q2p1) and (p2q2q1) have different orientations.

Examples:


2. Special Case 
– (p1q1p2), (p1q1q2), (p2q2p1), and (p2q2q1) are all collinear and
– the x-projections of (p1q1) and (p2q2) intersect
– the y-projections of (p1q1) and (p2q2) intersect

Examples:



可以使用 java.awt.geom.Line2D 的朋友,请直接使用系统API: intersectsLine  (http://docs.oracle.com/javase/7/docs/api/java/awt/geom/Line2D.html)
[java]  view plain  copy
 print ?
  1. // intersectsLine  
  2.  public boolean intersectsLine(double x1, double y1, double x2, double y2)  
  3.  Tests if the line segment from (x1,y1) to (x2,y2) intersects this line segment.  
  4. // Parameters:  
  5.  x1 - the X coordinate of the start point of the specified line segment  
  6.  y1 - the Y coordinate of the start point of the specified line segment  
  7.  x2 - the X coordinate of the end point of the specified line segment  
  8.  y2 - the Y coordinate of the end point of the specified line segment  
  9. // Returns:  
  10.  if this line segment and the specified line segment intersect each other; false otherwise.  
  11. // Since:  
  12.  1.2  

不能使用系统API的小伙伴,我们只能自己造轮子了。

C++语言实现:

[cpp]  view plain  copy
 print ?
  1. // A C++ program to check if two given line segments intersect  
  2. #include <iostream>  
  3. using namespace std;  
  4.   
  5. struct Point  
  6. {  
  7.     int x;  
  8.     int y;  
  9. };  
  10.   
  11. // Given three colinear points p, q, r, the function checks if point q lies on line segment 'pr'  
  12. bool onSegment(Point p, Point q, Point r)  
  13. {  
  14.     if (q.x <= max(p.x, r.x) && q.x >= min(p.x, r.x) &&  
  15.             q.y <= max(p.y, r.y) && q.y >= min(p.y, r.y))  
  16.         return true;  
  17.     return false;  
  18. }  
  19.   
  20. // To find orientation of ordered triplet (p, q, r).  
  21. // The function returns following values  
  22. // 0 --> p, q and r are colinear  
  23. // 1 --> Clockwise  
  24. // 2 --> Counterclockwise  
  25. int orientation(Point p, Point q, Point r)  
  26. {  
  27.     // See http://www.geeksforgeeks.org/orientation-3-ordered-points/  
  28.     // for details of below formula.  
  29.     int val = (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);  
  30.     if (val == 0) return 0;  // colinear  
  31.     return (val > 0)? 1: 2; // clock or counterclock wise  
  32. }  
  33.   
  34. // The main function that returns true if line segment 'p1q1' and 'p2q2' intersect.  
  35. bool doIntersect(Point p1, Point q1, Point p2, Point q2)  
  36. {  
  37.     // Find the four orientations needed for general and special cases  
  38.     int o1 = orientation(p1, q1, p2);  
  39.     int o2 = orientation(p1, q1, q2);  
  40.     int o3 = orientation(p2, q2, p1);  
  41.     int o4 = orientation(p2, q2, q1);  
  42.   
  43.     // General case  
  44.     if (o1 != o2 && o3 != o4)  
  45.         return true;  
  46.   
  47.     // Special Cases  
  48.     // p1, q1 and p2 are colinear and p2 lies on segment p1q1  
  49.     if (o1 == 0 && onSegment(p1, p2, q1)) return true;  
  50.   
  51.     // p1, q1 and p2 are colinear and q2 lies on segment p1q1  
  52.     if (o2 == 0 && onSegment(p1, q2, q1)) return true;  
  53.   
  54.     // p2, q2 and p1 are colinear and p1 lies on segment p2q2  
  55.     if (o3 == 0 && onSegment(p2, p1, q2)) return true;  
  56.   
  57.     // p2, q2 and q1 are colinear and q1 lies on segment p2q2  
  58.     if (o4 == 0 && onSegment(p2, q1, q2)) return true;  
  59.   
  60.     return false// Doesn't fall in any of the above cases  
  61. }  
  62.   
  63. // Driver program to test above functions  
  64. int main()  
  65. {  
  66.     struct Point p1 = {1, 1}, q1 = {10, 1};  
  67.     struct Point p2 = {1, 2}, q2 = {10, 2};  
  68.   
  69.     doIntersect(p1, q1, p2, q2)? cout << "Yes\n": cout << "No\n";  
  70.   
  71.     p1 = {10, 0}, q1 = {0, 10};  
  72.     p2 = {0, 0}, q2 = {10, 10};  
  73.     doIntersect(p1, q1, p2, q2)? cout << "Yes\n": cout << "No\n";  
  74.   
  75.     p1 = {-5, -5}, q1 = {0, 0};  
  76.     p2 = {1, 1}, q2 = {10, 10};  
  77.     doIntersect(p1, q1, p2, q2)? cout << "Yes\n": cout << "No\n";  
  78.   
  79.     return 0;  
  80. }  

输出结果:

[cpp]  view plain  copy
 print ?
  1. No  
  2. Yes  
  3. No  

Java语言实现:


[java]  view plain  copy
 print ?
  1. // Given three colinear points p, q, r, the function checks if point q lies on line segment 'pr'  
  2. boolean onSegment(Point p, Point q, Point r) {  
  3.     if (q.x <= Math.max(p.x, r.x) && q.x >= Math.min(p.x, r.x) &&  
  4.             q.y <= Math.max(p.y, r.y) && q.y >= Math.min(p.y, r.y))  
  5.         return true;  
  6.     return false;  
  7. }  
  8.   
  9.   
  10. /** 
  11.  * To find orientation of ordered triplet (p, q, r). 
  12.  * The function returns following values 
  13.  * 
  14.  * @param p 
  15.  * @param q 
  16.  * @param r 
  17.  * @return 0 --> p, q and r are colinear, 1 --> 顺时针方向, 2 --> 逆时钟方向 
  18.  */  
  19. int orientation(Point p, Point q, Point r) {  
  20.     // See http://www.geeksforgeeks.org/orientation-3-ordered-points/  for details of below formula.  
  21.     int val = (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);  
  22.     if (val == 0return 0;  // colinear  
  23.     return (val > 0) ? 1 : 2// clock or counterclock wise  
  24. }  
  25.   
  26. // The main function that returns true if line segment 'p1q1' and 'p2q2' intersect.  
  27. boolean doIntersect(Point p1, Point q1, Point p2, Point q2) {  
  28.   
  29.     // Find the four orientations needed for general and special cases  
  30.     int o1 = orientation(p1, q1, p2);  
  31.     int o2 = orientation(p1, q1, q2);  
  32.     int o3 = orientation(p2, q2, p1);  
  33.     int o4 = orientation(p2, q2, q1);  
  34.     // General case  
  35.     if (o1 != o2 && o3 != o4) {  
  36.         return true;  
  37.     }  
  38.     // Special Cases  
  39.     // p1, q1 and p2 are colinear and p2 lies on segment p1q1  
  40.     if (o1 == 0 && onSegment(p1, p2, q1)) return true;  
  41.     // p1, q1 and p2 are colinear and q2 lies on segment p1q1  
  42.     if (o2 == 0 && onSegment(p1, q2, q1)) return true;  
  43.     // p2, q2 and p1 are colinear and p1 lies on segment p2q2  
  44.     if (o3 == 0 && onSegment(p2, p1, q2)) return true;  
  45.     // p2, q2 and q1 are colinear and q1 lies on segment p2q2  
  46.     if (o4 == 0 && onSegment(p2, q1, q2)) return true;  
  47.   
  48.     return false// Doesn't fall in any of the above cases  
  49. }  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值