/// <summary> /// 计算多边形和折线(包括线段)是否相交,(未考虑“回”字型) /// </summary> /// <param name="pd"></param> /// <param name="line"></param> /// <returns></returns> public static bool CalcIfPolygonIntersectPolyline(PolygonD pd, PointDArray line) { if (!pd.Bounds.IntersectsWith(line.Bounds)) { return(false); } PointD[] plg1 = pd.Points[0].Points; PointD[] plg2 = line.Points; PointD start1, end1, start2, end2; for (int i = 0; i < plg1.Length; i++) { start1 = plg1[i]; end1 = (i == plg1.Length - 1) ? plg1[0] : plg1[i + 1]; for (int j = 0; j < plg2.Length - 1; j++) { start2 = plg2[j]; end2 = plg2[j + 1]; if (CrossLine(start1, end1, start2, end2) != 0) { return(true); } } } //补充判断多边形完全包含折线或线段的情况 if (PtInPolygon(line.Points[0].X, line.Points[0].Y, pd)) { return(true); } return(false); }
/// <summary> /// 计算两个多边形是否相交。(未考虑“回”字型) /// </summary> /// <param name="pd1"></param> /// <param name="pd2"></param> /// <returns></returns> public static bool CalcIfPolygonsIntersect(PolygonD pd1, PolygonD pd2) { if (!pd1.Bounds.IntersectsWith(pd2.Bounds)) { return(false); } PointD[] plg1 = pd1.Points[0].Points; PointD[] plg2 = pd2.Points[0].Points; PointD start1, end1, start2, end2; for (int i = 0; i < plg1.Length; i++) { start1 = plg1[i]; end1 = (i == plg1.Length - 1) ? plg1[0] : plg1[i + 1]; for (int j = 0; j < plg2.Length; j++) { start2 = plg2[j]; end2 = (j == plg2.Length - 1) ? plg2[0] : plg2[j + 1]; if (CrossLine(start1, end1, start2, end2) != 0) { return(true); } } } return(CalcPolygonContainsPolygon(pd1, pd2) || CalcPolygonContainsPolygon(pd2, pd1)); }
/// <summary> /// 判断多边形是否和一个矩形相交 /// </summary> /// <param name="pd"></param> /// <param name="rd"></param> /// <returns></returns> public static bool CalcIfPolygonIntersectRectangle(PolygonD pd, RectangleD rd) { if (!pd.Bounds.IntersectsWith(rd)) { return(false); } if (CalcPolygonIntersect(pd.Points[0].Points, rd).Length == 0) { return(false); } return(true); }
/// <summary> /// 计算多边形(不含“回”字型)是否包含一个矩形 /// </summary> /// <param name="pd"></param> /// <param name="rd"></param> /// <returns></returns> public static bool CalcPolygonContainsRectangle(PolygonD pd, RectangleD rd) { if (!pd.Bounds.IntersectsWith(rd)) { return(false); } PointD[] rc = CalcPolygonIntersect(pd.Points[0].Points, rd); if (rc.Length > 2) { byte result = 0x00; for (int i = 0; i < rc.Length; i++) { if ((rc[i].X != rd.Left && rc[i].X != rd.Right) || (rc[i].Y != rd.Top && rc[i].Y != rd.Bottom)) { return(false); } if (rc[i].X == rd.Left && rc[i].Y == rd.Top) { result |= 0x01; } if (rc[i].X == rd.Right && rc[i].Y == rd.Top) { result |= 0x02; } if (rc[i].X == rd.Right && rc[i].Y == rd.Bottom) { result |= 0x04; } if (rc[i].X == rd.Left && rc[i].Y == rd.Bottom) { result |= 0x08; } } if (result == 0x0f) { return(true); } else { return(false); } } else { return(false); } }
/// <summary> /// 计算点是否在多边形内(包括“回”字型) /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <param name="pd"></param> /// <returns></returns> public static bool PtInPolygon(double x, double y, PolygonD pd) { if (!pd.Bounds.Contains(x, y)) { return(false); } int count = 0; for (int i = 0; i < pd.Points.Count; i++) { if (ptInRgn(x, y, pd.Points[i].Points)) { count++; } } return(count % 2 == 1); }
public static bool CalcPolygonContainsRectangle2(PolygonD pd, RectangleD rd) { bool result = CalcRegionContainsRectangle2(pd.Points[0], rd); if (pd.Points.Count > 1 && result) { for (int i = 1; i < pd.Points.Count; i++) { if (CalcRegionContainsRectangle2(pd.Points[i], rd) || calcIfPolygonIntersect(pd.Points[i].Points, rd))//CalcPolygonIntersect(pd.Points[i].Points,rd).Length>0) { return(false); } } return(true); } else { return(result); } }
/// <summary> /// 计算多边形(含“回”字型)是否包含另一个多边形 /// </summary> /// <param name="pdOutter"></param> /// <param name="pdInner"></param> /// <returns></returns> public static bool CalcPolygonContainsPolygon(PolygonD pdOutter, PolygonD pdInner) { if (!pdOutter.Bounds.IntersectsWith(pdInner.Bounds)) { return(false); } if (!CalcRegionContainsRegion(pdOutter.Points[0], pdInner.Points[0])) { return(false); } if (pdOutter.Points.Count > 1) { for (int i = 1; i < pdOutter.Points.Count; i++) { if (CalcRegionContainsRegion(pdInner.Points[0], pdOutter.Points[i])) { return(false); } } } return(true); }
public static bool CalcIfPolygonIntersect(PolygonD pd1, PolygonD pd2) { return(pd1.Bounds.IntersectsWith(pd2.Bounds)); }