/// <summary> /// 获取外包矩形 /// </summary> /// <returns></returns> public moRectangle GetEnvelope() { moRectangle sRect = null; if (_ShapeType == moGeometryTypeConstant.Point) { moPoint sPoint = (moPoint)_Geometry; sRect = new moRectangle(sPoint.X, sPoint.X, sPoint.Y, sPoint.Y); } else if (_ShapeType == moGeometryTypeConstant.MultiPolyline) { moMultiPolyline sMultiPolyline = (moMultiPolyline)_Geometry; sRect = sMultiPolyline.GetEnvelope(); } else { moMultiPolygon sMultiPolygon = (moMultiPolygon)_Geometry; sRect = sMultiPolygon.GetEnvelope(); } return(sRect); }
/// <summary> /// 指示指定点是否位于指定复合多边形内 /// </summary> /// <param name="point"></param> /// <param name="multiPolygon"></param> /// <returns></returns> public static bool IsPointWithinMultiPolygon(moPoint point, moMultiPolygon multiPolygon) { //(1)判断点是否位于外包矩形内,如否则返回否 moRectangle sExtent = multiPolygon.GetEnvelope(); if (IsPointWithinBox(point, sExtent) == false) { return(false); } //(2)射线法求交点个数 Int32 sIntersectionCount = 0; //交点个数 Int32 sPartCount = multiPolygon.Parts.Count; for (Int32 i = 0; i <= sPartCount - 1; i++) { sIntersectionCount = sIntersectionCount + GetIntersectionCountBetweenRayAndPolygon(point, multiPolygon.Parts.GetItem(i)); } if (sIntersectionCount % 2 == 1) { //奇数个,位于多边形内 return(true); } return(false); }
/// <summary> /// 指示指定复合多边形是否部分或完全位于指定矩形盒内 /// </summary> /// <param name="multipolygon"></param> /// <param name="box"></param> /// <returns></returns> public static bool IsMultiPolygonPartiallyWithinBox(moMultiPolygon multipolygon, moRectangle box) { //思路:先判断矩形盒是否相交,如是,按如下顺序,满足任何一个条件,则返回True //(1)复合多边形任何一个点位于矩形盒内; //(2)矩形盒任何一个顶点位于复合多边形内 //(3)矩形盒与复合多边形有交点 moRectangle sBox = multipolygon.GetEnvelope(); if (AreBoxesCross(sBox, box) == false) { return(false); } //(1)多边形任何一个点位于矩形盒内 Int32 sPartCount = multipolygon.Parts.Count; for (Int32 i = 0; i <= sPartCount - 1; i++) { Int32 sPointCount = multipolygon.Parts.GetItem(i).Count; for (Int32 j = 0; j <= sPointCount - 1; j++) { moPoint sCurPoint = multipolygon.Parts.GetItem(i).GetItem(j); if (IsPointWithinBox(sCurPoint, box) == true) { return(true); } } } //(2)矩形盒任何一个顶点位于多边形内 moPoint sRectPoint = new moPoint(box.MinX, box.MinY); //左下点 if (IsPointWithinMultiPolygon(sRectPoint, multipolygon) == true) { return(true); } sRectPoint = new moPoint(box.MinX, box.MaxY); //左上点 if (IsPointWithinMultiPolygon(sRectPoint, multipolygon) == true) { return(true); } sRectPoint = new moPoint(box.MaxX, box.MaxY); //右上点 if (IsPointWithinMultiPolygon(sRectPoint, multipolygon) == true) { return(true); } sRectPoint = new moPoint(box.MaxX, box.MinY); //右下点 if (IsPointWithinMultiPolygon(sRectPoint, multipolygon) == true) { return(true); } //(3)矩形盒与复合多边形有交点 for (Int32 i = 0; i <= sPartCount - 1; i++) { moPoints sPoints = multipolygon.Parts.GetItem(i); Int32 sPointCount = sPoints.Count; for (Int32 j = 0; j <= sPointCount - 2; j++) { if (IsSegmentCrossBox(sPoints.GetItem(j), sPoints.GetItem(j + 1), box) == true) { return(true); } } if (IsSegmentCrossBox(sPoints.GetItem(sPointCount - 1), sPoints.GetItem(0), box) == true) { return(true); } } //(4)都不满足,返回false return(false); }