private static bool CanSee(int i, int j, Vertices vertices) { bool flag = BayazitDecomposer.Reflex(i, vertices); bool result; if (flag) { bool flag2 = BayazitDecomposer.LeftOn(BayazitDecomposer.At(i, vertices), BayazitDecomposer.At(i - 1, vertices), BayazitDecomposer.At(j, vertices)) && BayazitDecomposer.RightOn(BayazitDecomposer.At(i, vertices), BayazitDecomposer.At(i + 1, vertices), BayazitDecomposer.At(j, vertices)); if (flag2) { result = false; return(result); } } else { bool flag3 = BayazitDecomposer.RightOn(BayazitDecomposer.At(i, vertices), BayazitDecomposer.At(i + 1, vertices), BayazitDecomposer.At(j, vertices)) || BayazitDecomposer.LeftOn(BayazitDecomposer.At(i, vertices), BayazitDecomposer.At(i - 1, vertices), BayazitDecomposer.At(j, vertices)); if (flag3) { result = false; return(result); } } bool flag4 = BayazitDecomposer.Reflex(j, vertices); if (flag4) { bool flag5 = BayazitDecomposer.LeftOn(BayazitDecomposer.At(j, vertices), BayazitDecomposer.At(j - 1, vertices), BayazitDecomposer.At(i, vertices)) && BayazitDecomposer.RightOn(BayazitDecomposer.At(j, vertices), BayazitDecomposer.At(j + 1, vertices), BayazitDecomposer.At(i, vertices)); if (flag5) { result = false; return(result); } } else { bool flag6 = BayazitDecomposer.RightOn(BayazitDecomposer.At(j, vertices), BayazitDecomposer.At(j + 1, vertices), BayazitDecomposer.At(i, vertices)) || BayazitDecomposer.LeftOn(BayazitDecomposer.At(j, vertices), BayazitDecomposer.At(j - 1, vertices), BayazitDecomposer.At(i, vertices)); if (flag6) { result = false; return(result); } } for (int k = 0; k < vertices.Count; k++) { bool flag7 = (k + 1) % vertices.Count == i || k == i || (k + 1) % vertices.Count == j || k == j; if (!flag7) { TSVector2 tSVector; bool flag8 = LineTools.LineIntersect(BayazitDecomposer.At(i, vertices), BayazitDecomposer.At(j, vertices), BayazitDecomposer.At(k, vertices), BayazitDecomposer.At(k + 1, vertices), out tSVector); if (flag8) { result = false; return(result); } } } result = true; return(result); }
public static Vertices LineSegmentVerticesIntersect(ref TSVector2 point1, ref TSVector2 point2, Vertices vertices) { Vertices vertices2 = new Vertices(); for (int i = 0; i < vertices.Count; i++) { TSVector2 item; bool flag = LineTools.LineIntersect(vertices[i], vertices[vertices.NextIndex(i)], point1, point2, true, true, out item); if (flag) { vertices2.Add(item); } } return(vertices2); }
private static bool CanSee(int i, int j, Vertices vertices) { if (Reflex(i, vertices)) { if (LeftOn(At(i, vertices), At(i - 1, vertices), At(j, vertices)) && RightOn(At(i, vertices), At(i + 1, vertices), At(j, vertices))) { return(false); } } else { if (RightOn(At(i, vertices), At(i + 1, vertices), At(j, vertices)) || LeftOn(At(i, vertices), At(i - 1, vertices), At(j, vertices))) { return(false); } } if (Reflex(j, vertices)) { if (LeftOn(At(j, vertices), At(j - 1, vertices), At(i, vertices)) && RightOn(At(j, vertices), At(j + 1, vertices), At(i, vertices))) { return(false); } } else { if (RightOn(At(j, vertices), At(j + 1, vertices), At(i, vertices)) || LeftOn(At(j, vertices), At(j - 1, vertices), At(i, vertices))) { return(false); } } for (int k = 0; k < vertices.Count; ++k) { if ((k + 1) % vertices.Count == i || k == i || (k + 1) % vertices.Count == j || k == j) { continue; // ignore incident edges } TSVector2 intersectionPoint; if (LineTools.LineIntersect(At(i, vertices), At(j, vertices), At(k, vertices), At(k + 1, vertices), out intersectionPoint)) { return(false); } } return(true); }
private static List <Vertices> TriangulatePolygon(Vertices vertices) { List <Vertices> list = new List <Vertices>(); TSVector2 lowerInt = new TSVector2(); TSVector2 upperInt = new TSVector2(); // intersection points int lowerIndex = 0, upperIndex = 0; Vertices lowerPoly, upperPoly; for (int i = 0; i < vertices.Count; ++i) { if (Reflex(i, vertices)) { FP upperDist; FP lowerDist = upperDist = FP.MaxValue; for (int j = 0; j < vertices.Count; ++j) { // if line intersects with an edge FP d; TSVector2 p; if (Left(At(i - 1, vertices), At(i, vertices), At(j, vertices)) && RightOn(At(i - 1, vertices), At(i, vertices), At(j - 1, vertices))) { // find the point of intersection p = LineTools.LineIntersect(At(i - 1, vertices), At(i, vertices), At(j, vertices), At(j - 1, vertices)); if (Right(At(i + 1, vertices), At(i, vertices), p)) { // make sure it's inside the poly d = SquareDist(At(i, vertices), p); if (d < lowerDist) { // keep only the closest intersection lowerDist = d; lowerInt = p; lowerIndex = j; } } } if (Left(At(i + 1, vertices), At(i, vertices), At(j + 1, vertices)) && RightOn(At(i + 1, vertices), At(i, vertices), At(j, vertices))) { p = LineTools.LineIntersect(At(i + 1, vertices), At(i, vertices), At(j, vertices), At(j + 1, vertices)); if (Left(At(i - 1, vertices), At(i, vertices), p)) { d = SquareDist(At(i, vertices), p); if (d < upperDist) { upperDist = d; upperIndex = j; upperInt = p; } } } } // if there are no vertices to connect to, choose a point in the middle if (lowerIndex == (upperIndex + 1) % vertices.Count) { TSVector2 p = ((lowerInt + upperInt) / 2); lowerPoly = Copy(i, upperIndex, vertices); lowerPoly.Add(p); upperPoly = Copy(lowerIndex, i, vertices); upperPoly.Add(p); } else { FP highestScore = 0, bestIndex = lowerIndex; while (upperIndex < lowerIndex) { upperIndex += vertices.Count; } for (int j = lowerIndex; j <= upperIndex; ++j) { if (CanSee(i, j, vertices)) { FP score = 1 / (SquareDist(At(i, vertices), At(j, vertices)) + 1); if (Reflex(j, vertices)) { if (RightOn(At(j - 1, vertices), At(j, vertices), At(i, vertices)) && LeftOn(At(j + 1, vertices), At(j, vertices), At(i, vertices))) { score += 3; } else { score += 2; } } else { score += 1; } if (score > highestScore) { bestIndex = j; highestScore = score; } } } lowerPoly = Copy(i, (int)bestIndex, vertices); upperPoly = Copy((int)bestIndex, i, vertices); } list.AddRange(TriangulatePolygon(lowerPoly)); list.AddRange(TriangulatePolygon(upperPoly)); return(list); } } // polygon is already convex if (vertices.Count > Settings.MaxPolygonVertices) { lowerPoly = Copy(0, vertices.Count / 2, vertices); upperPoly = Copy(vertices.Count / 2, 0, vertices); list.AddRange(TriangulatePolygon(lowerPoly)); list.AddRange(TriangulatePolygon(upperPoly)); } else { list.Add(vertices); } return(list); }
public static bool LineIntersect(TSVector2 point1, TSVector2 point2, TSVector2 point3, TSVector2 point4, out TSVector2 intersectionPoint) { return(LineTools.LineIntersect(ref point1, ref point2, ref point3, ref point4, true, true, out intersectionPoint)); }
public static bool LineIntersect(TSVector2 point1, TSVector2 point2, TSVector2 point3, TSVector2 point4, bool firstIsSegment, bool secondIsSegment, out TSVector2 intersectionPoint) { return(LineTools.LineIntersect(ref point1, ref point2, ref point3, ref point4, firstIsSegment, secondIsSegment, out intersectionPoint)); }
/// <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 List <Vertices> TriangulatePolygon(Vertices vertices) { List <Vertices> list = new List <Vertices>(); TSVector2 value = default(TSVector2); TSVector2 value2 = default(TSVector2); int num = 0; int i = 0; List <Vertices> result; for (int j = 0; j < vertices.Count; j++) { bool flag = BayazitDecomposer.Reflex(j, vertices); if (flag) { FP y2; FP y = y2 = FP.MaxValue; for (int k = 0; k < vertices.Count; k++) { bool flag2 = BayazitDecomposer.Left(BayazitDecomposer.At(j - 1, vertices), BayazitDecomposer.At(j, vertices), BayazitDecomposer.At(k, vertices)) && BayazitDecomposer.RightOn(BayazitDecomposer.At(j - 1, vertices), BayazitDecomposer.At(j, vertices), BayazitDecomposer.At(k - 1, vertices)); if (flag2) { TSVector2 tSVector = LineTools.LineIntersect(BayazitDecomposer.At(j - 1, vertices), BayazitDecomposer.At(j, vertices), BayazitDecomposer.At(k, vertices), BayazitDecomposer.At(k - 1, vertices)); bool flag3 = BayazitDecomposer.Right(BayazitDecomposer.At(j + 1, vertices), BayazitDecomposer.At(j, vertices), tSVector); if (flag3) { FP fP = BayazitDecomposer.SquareDist(BayazitDecomposer.At(j, vertices), tSVector); bool flag4 = fP < y2; if (flag4) { y2 = fP; value = tSVector; num = k; } } } bool flag5 = BayazitDecomposer.Left(BayazitDecomposer.At(j + 1, vertices), BayazitDecomposer.At(j, vertices), BayazitDecomposer.At(k + 1, vertices)) && BayazitDecomposer.RightOn(BayazitDecomposer.At(j + 1, vertices), BayazitDecomposer.At(j, vertices), BayazitDecomposer.At(k, vertices)); if (flag5) { TSVector2 tSVector = LineTools.LineIntersect(BayazitDecomposer.At(j + 1, vertices), BayazitDecomposer.At(j, vertices), BayazitDecomposer.At(k, vertices), BayazitDecomposer.At(k + 1, vertices)); bool flag6 = BayazitDecomposer.Left(BayazitDecomposer.At(j - 1, vertices), BayazitDecomposer.At(j, vertices), tSVector); if (flag6) { FP fP = BayazitDecomposer.SquareDist(BayazitDecomposer.At(j, vertices), tSVector); bool flag7 = fP < y; if (flag7) { y = fP; i = k; value2 = tSVector; } } } } bool flag8 = num == (i + 1) % vertices.Count; Vertices vertices2; Vertices vertices3; if (flag8) { TSVector2 item = (value + value2) / 2; vertices2 = BayazitDecomposer.Copy(j, i, vertices); vertices2.Add(item); vertices3 = BayazitDecomposer.Copy(num, j, vertices); vertices3.Add(item); } else { FP y3 = 0; FP value3 = num; while (i < num) { i += vertices.Count; } for (int l = num; l <= i; l++) { bool flag9 = BayazitDecomposer.CanSee(j, l, vertices); if (flag9) { FP fP2 = 1 / (BayazitDecomposer.SquareDist(BayazitDecomposer.At(j, vertices), BayazitDecomposer.At(l, vertices)) + 1); bool flag10 = BayazitDecomposer.Reflex(l, vertices); if (flag10) { bool flag11 = BayazitDecomposer.RightOn(BayazitDecomposer.At(l - 1, vertices), BayazitDecomposer.At(l, vertices), BayazitDecomposer.At(j, vertices)) && BayazitDecomposer.LeftOn(BayazitDecomposer.At(l + 1, vertices), BayazitDecomposer.At(l, vertices), BayazitDecomposer.At(j, vertices)); if (flag11) { fP2 += 3; } else { fP2 += 2; } } else { fP2 += 1; } bool flag12 = fP2 > y3; if (flag12) { value3 = l; y3 = fP2; } } } vertices2 = BayazitDecomposer.Copy(j, (int)((long)value3), vertices); vertices3 = BayazitDecomposer.Copy((int)((long)value3), j, vertices); } list.AddRange(BayazitDecomposer.TriangulatePolygon(vertices2)); list.AddRange(BayazitDecomposer.TriangulatePolygon(vertices3)); result = list; return(result); } } bool flag13 = vertices.Count > Settings.MaxPolygonVertices; if (flag13) { Vertices vertices2 = BayazitDecomposer.Copy(0, vertices.Count / 2, vertices); Vertices vertices3 = BayazitDecomposer.Copy(vertices.Count / 2, 0, vertices); list.AddRange(BayazitDecomposer.TriangulatePolygon(vertices2)); list.AddRange(BayazitDecomposer.TriangulatePolygon(vertices3)); } else { list.Add(vertices); } result = list; 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--; } } }