[RGEOS]空间拓扑关系

-1.判断两个线段是否平行

 1 inline bool parallel_seg_seg(Segment_2 S1, Segment_2 S2)
 2 {
 3     Vector_2 u(S1);
 4     Vector_2 v(S2);
 5     Vector_2 w = S1.source() - S2.source();
 6     float D = perp(u, v);
 7     if (abs(D)<SMALL_NUM)
 8     {
 9         return true;
10     }
11     return false;
12 }
View Code

0.线段的拐向:已知向量P0P1,向量P1P2

(1)判断点P2在直线P0P1的左边还是在右边,还是在直线上

 1 // 判断点P2在直线P0P1的左边还是在右边,还是在直线上
 2 //isLeft(): tests if a point is Left|On|Right of an infinite line.
 3 //    Input:  three points P0, P1, and P2
 4 //    Return: >0 for P2 left of the line through P0 and P1
 5 //            =0 for P2 on the line
 6 //            <0 for P2 right of the line
 7 inline int isLeft( Point P0, Point P1, Point P2 )
 8 {
 9     return ( (P1.x - P0.x) * (P2.y - P0.y)
10             - (P2.x - P0.x) * (P1.y - P0.y) );
11 }
View Code

1.点在线段上

(1)点是否在共线的线段上

 1 /// <summary>
 2         /// 点是否在共线的线段上
 3         /// 1 = P is inside S;
 4         /// 0 = P is not inside S
 5         /// </returns> 
 6         /// </summary>
 7         /// <param name="P">a point P</param>
 8         /// <param name="S">a collinear segment S</param>
 9         /// <returns></returns>
10         public static int InSegment(RPoint P, RSegment S)
11         {
12             if (S.P0.X != S.P1.X)
13             {    // S is not vertical
14                 if (S.P0.X <= P.X && P.X <= S.P1.X)
15                     return 1;
16                 if (S.P0.X >= P.X && P.X >= S.P1.X)
17                     return 1;
18             }
19             else
20             {    // S is vertical, so test y coordinate
21                 if (S.P0.Y <= P.Y && P.Y <= S.P1.Y)
22                     return 1;
23                 if (S.P0.Y >= P.Y && P.Y >= S.P1.Y)
24                     return 1;
25             }
26             return 0;
27         }
点是否在共线的线段上

(2)点是否包含在任意线段内

 1         /// <summary>
 2         /// 点是否在线段上
 3       /// </summary>
 4         /// <param name="P">任意的点</param>
 5         /// <param name="S">任意线段</param>
 6         /// <returns>1=P点在线段S上;0=P点不在线段S上</returns>
 7         public static int Inside2D_Point_Segment(RPoint P, RSegment S)
 8         {
 9     Vector3d u = S.P1 - S.P0;
10                 Vector3d v = P - S.P0;
11                 double D = RMath.perp(u, v);
12     //判断u和v是否平行
13              if (Math.Abs(D) < RMath.SMALL_NUM)
14                {
15         if (InSegment(P, S) == 1) 
16        {
17        return 1;
18        }
19                }
20                return 0;
21         }
View Code

2.点在矩形内

 1 // 点在矩形内
 2 // 1 = P is inside E;
 3 // 0 = P is not inside E
 4 public static int Inside2D_Point_Envelope(RPoint P, REnvelope E)
 5         {
 6     if(P.X>E.LowerLeft.X && P.X>E.TopRight.X && P.Y>E.LowerLeft.Y && P.Y<E.TopRight.Y)
 7     {
 8                     return 1;
 9     }
10     else
11     {
12         return 0;
13     }
14         }
View Code

3.点在圆内

  点到圆心的距离小于半径

4.点在2D多边形内

  转角方法

  射线方法

5.2D线段在矩形内

6.2D多边形与多边形是否相交

   一种笨方法:首先判断包围盒是否相交,再判断一个多边形的点在另外一个多边形内。

原文地址:https://www.cnblogs.com/yhlx125/p/3438847.html