/// <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 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--; } } }