[RGEOS]空间拓扑关系

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

 inline bool parallel_seg_seg(Segment_2 S1, Segment_2 S2)
{
Vector_2 u(S1);
Vector_2 v(S2);
Vector_2 w = S1.source() - S2.source();
float D = perp(u, v);
if (abs(D)<SMALL_NUM)
{
return true;
}
return false;
}

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

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

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

1.点在线段上

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

 /// <summary>
/// 点是否在共线的线段上
/// 1 = P is inside S;
/// 0 = P is not inside S
/// </returns>
/// </summary>
/// <param name="P">a point P</param>
/// <param name="S">a collinear segment S</param>
/// <returns></returns>
public static int InSegment(RPoint P, RSegment S)
{
if (S.P0.X != S.P1.X)
{ // S is not vertical
if (S.P0.X <= P.X && P.X <= S.P1.X)
return ;
if (S.P0.X >= P.X && P.X >= S.P1.X)
return ;
}
else
{ // S is vertical, so test y coordinate
if (S.P0.Y <= P.Y && P.Y <= S.P1.Y)
return ;
if (S.P0.Y >= P.Y && P.Y >= S.P1.Y)
return ;
}
return ;
}

点是否在共线的线段上

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

         /// <summary>
/// 点是否在线段上
/// </summary>
/// <param name="P">任意的点</param>
/// <param name="S">任意线段</param>
/// <returns>1=P点在线段S上;0=P点不在线段S上</returns>
public static int Inside2D_Point_Segment(RPoint P, RSegment S)
{
Vector3d u = S.P1 - S.P0;
Vector3d v = P - S.P0;
double D = RMath.perp(u, v);
//判断u和v是否平行
if (Math.Abs(D) < RMath.SMALL_NUM)
{
if (InSegment(P, S) == )
{
return ;
}
}
return ;
}

2.点在矩形内

 // 点在矩形内
// 1 = P is inside E;
// 0 = P is not inside E
public static int Inside2D_Point_Envelope(RPoint P, REnvelope E)
{
if(P.X>E.LowerLeft.X && P.X>E.TopRight.X && P.Y>E.LowerLeft.Y && P.Y<E.TopRight.Y)
{
return ;
}
else
{
return ;
}
}

3.点在圆内

  点到圆心的距离小于半径

4.点在2D多边形内

  转角方法

  射线方法

5.2D线段在矩形内

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

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

上一篇:(转)R空间数据处理与可视化


下一篇:[RGEOS]绘制多边形Polygon