/// <summary> /// /// </summary> /// <param name="poly1"></param> /// <param name="poly2"></param> /// <param name="poly"></param> /// <param name="curedge"></param> private static void SetInitData(Polygon2D poly1, Polygon2D poly2, ref Polygon2D poly, ref Double2Bool curPoint, ref int curedge) { if (poly1 == null || poly2 == null || poly1.GetEdgeNum() < 3 || poly2.GetEdgeNum() < 3) { return; } // 先找poly1 最小的。 poly = poly1; curedge = 0; Double2Bool min = poly1.GetPointPlus(0); for (int i = 1; i < poly1.GetEdgeNum(); i++) { Double2Bool point = poly1.GetPointPlus(i); if (point.y < min.y || (point.y == min.y && point.x < min.x)) { min = point; curedge = i; } } // for (int i = 0; i < poly2.GetEdgeNum(); i++) { Double2Bool point = poly2.GetPointPlus(i); if (point.y < min.y || (point.y == min.y && point.x < min.x)) { min = point; curedge = i; poly = poly2; } } curPoint = min; }
/// <summary> /// 获取顶点数组 /// </summary> /// <returns></returns> public override Double2Bool[] GetPointsPlus() { Double2Bool[] points = new Double2Bool[3]; points[0] = new Double2Bool(this.p1, true); points[1] = new Double2Bool(this.p2, true); points[2] = new Double2Bool(this.p3, true); return(points); }
/// <summary> /// 多边形决策。 /// </summary> /// <param name="ls2d"></param> /// <param name="dir"></param> /// <param name="targetPoint"></param> /// <param name="middlePoint"></param> private static bool GetNearPointInEdge(Point2D ls2d, bool dir, Double2Bool curPoint, List <Double3> listMiddlePoint, ref Double3 nextPoint) { Double2 targetPos = new Double2(curPoint.x, curPoint.y); if (dir == true) { if (listMiddlePoint == null || listMiddlePoint.Count == 0) { return(false); } else { if (targetPos == ls2d.startPoint) { nextPoint = listMiddlePoint[0]; return(true); } else { for (int i = 0; i < listMiddlePoint.Count; i++) { if (targetPos == new Double2(listMiddlePoint[i].x, listMiddlePoint[i].y) == true && i < listMiddlePoint.Count - 1) { nextPoint = listMiddlePoint[i + 1]; return(true); } } } } } else { if (listMiddlePoint == null || listMiddlePoint.Count == 0) { return(false); } else { if (targetPos == ls2d.endPoint) { nextPoint = listMiddlePoint[listMiddlePoint.Count - 1]; return(true); } else { for (int i = listMiddlePoint.Count - 1; i >= 0; i--) { if (targetPos == new Double2(listMiddlePoint[i].x, listMiddlePoint[i].y) == true && i > 0) { nextPoint = listMiddlePoint[i - 1]; return(true); } } } } } return(false); }
/// <summary> /// 获取顶点数组 /// </summary> /// <returns></returns> public override Double2Bool[] GetPointsPlus() { Double2Bool[] points = new Double2Bool[this.pointArr.Length]; for (int i = 0; i < points.Length; i++) { points[i] = GetPointPlus(i); } return(points); }
/// <summary> /// 加入顶点 /// </summary> /// <param name="listPoint"></param> /// <param name="point"></param> /// <returns></returns> private static bool AddPoint(ref List <Double2Bool> listPoint, Double2Bool point) { if (listPoint.Contains(point) == true) { return(false); } else { listPoint.Add(point); return(true); } }
public override bool Equals(System.Object obj) { if (obj == null) { return(false); } Double2Bool p = (Double2Bool)obj; if ((System.Object)p == null) { return(false); } return((x == p.x) && (y == p.y) && (isCross == p.isCross)); }
/// <summary> /// 求多边形的并。 /// </summary> /// <param name="polygon1"></param> /// <param name="polygon2"></param> /// <returns></returns> private static Double2Bool[] CombinePolygon(Double2Bool[] polygon1, Double2Bool[] polygon2, ref bool isCombine) { isCombine = true; if (polygon1 == null || polygon1.Length < 3) { return(null); } if (polygon2 == null || polygon2.Length < 3) { return(polygon1); } // Polygon2D poly1 = new Polygon2D(polygon1); Polygon2D poly2 = new Polygon2D(polygon2); // 获取poly1每条线段上的交点。 List <Double3>[] Poly1IntersectArray = new List <Double3> [poly1.GetEdgeNum()]; List <Double3>[] Poly2IntersectArray = new List <Double3> [poly2.GetEdgeNum()]; GetAllEdgeInterSectPoint(poly1, poly2, ref Poly1IntersectArray, ref Poly2IntersectArray); bool CheckIntersect = false; foreach (List <Double3> list in Poly1IntersectArray) { if (list != null && list.Count > 0) { CheckIntersect = true; break; } } if (CheckIntersect == false) { isCombine = false; ClearPolyIntersectArray(ref Poly1IntersectArray, ref Poly2IntersectArray); return(polygon1); } // List <Double2Bool> listPoint = new List <Double2Bool>(); Polygon2D poly = null; int curedge = 0; Double2Bool curPoint = new Double2Bool(0, 0, true); bool SearchDir = true; List <Double3>[] curPolyIntersectArray = null; SetInitData(poly1, poly2, ref poly, ref curPoint, ref curedge); if (poly == poly1) { curPolyIntersectArray = Poly1IntersectArray; } else if (poly == poly2) { curPolyIntersectArray = Poly2IntersectArray; } while (poly != null && curedge >= 0 && curedge < poly.GetEdgeNum()) { Point2D ls2d = poly.GetSimpleEdge(curedge); Double2 normalDir = poly.GetNormal(curedge); if (AddPoint(ref listPoint, curPoint) == false) { break; } Double3 nextPoint = Double3.zero; bool ret = GetNearPointInEdge(ls2d, SearchDir, curPoint, curPolyIntersectArray[curedge], ref nextPoint); if (ret == false) { if (SearchDir == true) { curedge++; if (curedge >= poly.GetEdgeNum()) { curedge = 0; } curPoint = poly.GetPointPlus(curedge); } else { curedge--; if (curedge < 0) { curedge = poly.GetEdgeNum() - 1; } curPoint = poly.GetPointPlus(curedge + 1); } } else // 则需要交换了。换到另一个多边形。 { // 肯定是边上的点了, 强制设置为true,可通过 curPoint = new Double2Bool(nextPoint.x, nextPoint.y, true); curedge = (int)nextPoint.z; ExChangePoly(ref poly, poly1, poly2, ref curPolyIntersectArray, Poly1IntersectArray, Poly2IntersectArray); Point2D ls = poly.GetSimpleEdge(curedge); if (Double2.Dot(ls.endPoint - ls.startPoint, normalDir) > 0) { SearchDir = true; } else { SearchDir = false; } } } ClearPolyIntersectArray(ref Poly1IntersectArray, ref Poly2IntersectArray); return(listPoint.ToArray()); }