/// <summary> /// Calculates all intersections between two polygons. /// </summary> /// <param name="polygon1">The first polygon.</param> /// <param name="polygon2">The second polygon.</param> /// <param name="slicedPoly1">Returns the first polygon with added intersection points.</param> /// <param name="slicedPoly2">Returns the second polygon with added intersection points.</param> private static void CalculateIntersections(Vertices polygon1, Vertices polygon2, out Vertices slicedPoly1, out Vertices slicedPoly2) { slicedPoly1 = new Vertices(polygon1); slicedPoly2 = new Vertices(polygon2); // Iterate through polygon1's edges for (int i = 0; i < polygon1.Count; i++) { // Get edge vertices TSVector2 a = polygon1[i]; TSVector2 b = polygon1[polygon1.NextIndex(i)]; // Get intersections between this edge and polygon2 for (int j = 0; j < polygon2.Count; j++) { TSVector2 c = polygon2[j]; TSVector2 d = polygon2[polygon2.NextIndex(j)]; TSVector2 intersectionPoint; // Check if the edges intersect if (LineTools.LineIntersect(a, b, c, d, out intersectionPoint)) { // calculate alpha values for sorting multiple intersections points on a edge FP alpha; // Insert intersection point into first polygon alpha = GetAlpha(a, b, intersectionPoint); if (alpha > 0f && alpha < 1f) { int index = slicedPoly1.IndexOf(a) + 1; while (index < slicedPoly1.Count && GetAlpha(a, b, slicedPoly1[index]) <= alpha) { ++index; } slicedPoly1.Insert(index, intersectionPoint); } // Insert intersection point into second polygon alpha = GetAlpha(c, d, intersectionPoint); if (alpha > 0f && alpha < 1f) { int index = slicedPoly2.IndexOf(c) + 1; while (index < slicedPoly2.Count && GetAlpha(c, d, slicedPoly2[index]) <= alpha) { ++index; } slicedPoly2.Insert(index, intersectionPoint); } } } } // Check for very small edges for (int i = 0; i < slicedPoly1.Count; ++i) { int iNext = slicedPoly1.NextIndex(i); //If they are closer than the distance remove vertex if ((slicedPoly1[iNext] - slicedPoly1[i]).LengthSquared() <= ClipperEpsilonSquared) { slicedPoly1.RemoveAt(i); --i; } } for (int i = 0; i < slicedPoly2.Count; ++i) { int iNext = slicedPoly2.NextIndex(i); //If they are closer than the distance remove vertex if ((slicedPoly2[iNext] - slicedPoly2[i]).LengthSquared() <= ClipperEpsilonSquared) { slicedPoly2.RemoveAt(i); --i; } } }
private bool SplitPolygonEdge(Vertices polygon, TSVector2 coordInsideThePolygon, out int vertex1Index, out int vertex2Index) { int num = 0; int index = 0; bool flag = false; FP y = FP.MaxValue; bool flag2 = false; TSVector2 zero = TSVector2.zero; List <FP> list = this.SearchCrossingEdges(polygon, (int)((long)coordInsideThePolygon.y)); vertex1Index = 0; vertex2Index = 0; zero.y = coordInsideThePolygon.y; bool flag3 = list != null && list.Count > 1 && list.Count % 2 == 0; bool result; if (flag3) { for (int i = 0; i < list.Count; i++) { bool flag4 = list[i] < coordInsideThePolygon.x; if (flag4) { FP fP = coordInsideThePolygon.x - list[i]; bool flag5 = fP < y; if (flag5) { y = fP; zero.x = list[i]; flag2 = true; } } } bool flag6 = flag2; if (flag6) { y = FP.MaxValue; int num2 = polygon.Count - 1; for (int j = 0; j < polygon.Count; j++) { TSVector2 tSVector = polygon[j]; TSVector2 tSVector2 = polygon[num2]; FP fP = LineTools.DistanceBetweenPointAndLineSegment(ref zero, ref tSVector, ref tSVector2); bool flag7 = fP < y; if (flag7) { y = fP; num = j; index = num2; flag = true; } num2 = j; } bool flag8 = flag; if (flag8) { TSVector2 value = polygon[index] - polygon[num]; value.Normalize(); TSVector2 value2 = polygon[num]; FP fP = TSVector2.Distance(value2, zero); vertex1Index = num; vertex2Index = num + 1; polygon.Insert(num, fP * value + polygon[vertex1Index]); polygon.Insert(num, fP * value + polygon[vertex2Index]); result = true; return(result); } } } result = false; return(result); }
private static void CalculateIntersections(Vertices polygon1, Vertices polygon2, out Vertices slicedPoly1, out Vertices slicedPoly2) { slicedPoly1 = new Vertices(polygon1); slicedPoly2 = new Vertices(polygon2); for (int i = 0; i < polygon1.Count; i++) { TSVector2 tSVector = polygon1[i]; TSVector2 tSVector2 = polygon1[polygon1.NextIndex(i)]; for (int j = 0; j < polygon2.Count; j++) { TSVector2 tSVector3 = polygon2[j]; TSVector2 tSVector4 = polygon2[polygon2.NextIndex(j)]; TSVector2 tSVector5; bool flag = LineTools.LineIntersect(tSVector, tSVector2, tSVector3, tSVector4, out tSVector5); if (flag) { FP alpha = YuPengClipper.GetAlpha(tSVector, tSVector2, tSVector5); bool flag2 = alpha > 0f && alpha < 1f; if (flag2) { int num = slicedPoly1.IndexOf(tSVector) + 1; while (num < slicedPoly1.Count && YuPengClipper.GetAlpha(tSVector, tSVector2, slicedPoly1[num]) <= alpha) { num++; } slicedPoly1.Insert(num, tSVector5); } alpha = YuPengClipper.GetAlpha(tSVector3, tSVector4, tSVector5); bool flag3 = alpha > 0f && alpha < 1f; if (flag3) { int num2 = slicedPoly2.IndexOf(tSVector3) + 1; while (num2 < slicedPoly2.Count && YuPengClipper.GetAlpha(tSVector3, tSVector4, slicedPoly2[num2]) <= alpha) { num2++; } slicedPoly2.Insert(num2, tSVector5); } } } } for (int k = 0; k < slicedPoly1.Count; k++) { int index = slicedPoly1.NextIndex(k); bool flag4 = (slicedPoly1[index] - slicedPoly1[k]).LengthSquared() <= YuPengClipper.ClipperEpsilonSquared; if (flag4) { slicedPoly1.RemoveAt(k); k--; } } for (int l = 0; l < slicedPoly2.Count; l++) { int index2 = slicedPoly2.NextIndex(l); bool flag5 = (slicedPoly2[index2] - slicedPoly2[l]).LengthSquared() <= YuPengClipper.ClipperEpsilonSquared; if (flag5) { slicedPoly2.RemoveAt(l); l--; } } }