Пример #1
0
        private bool SearchForOutstandingVertex(Vertices hullArea, out TSVector2 outstanding)
        {
            TSVector2 tSVector = TSVector2.zero;
            bool      result   = false;
            bool      flag     = hullArea.Count > 2;

            if (flag)
            {
                int       num       = hullArea.Count - 1;
                TSVector2 tSVector2 = hullArea[0];
                TSVector2 tSVector3 = hullArea[num];
                for (int i = 1; i < num; i++)
                {
                    TSVector2 tSVector4 = hullArea[i];
                    bool      flag2     = LineTools.DistanceBetweenPointAndLineSegment(ref tSVector4, ref tSVector2, ref tSVector3) >= this._hullTolerance;
                    if (flag2)
                    {
                        tSVector = hullArea[i];
                        result   = true;
                        break;
                    }
                }
            }
            outstanding = tSVector;
            return(result);
        }
Пример #2
0
        public bool IsSimple()
        {
            bool flag = base.Count < 3;
            bool result;

            if (flag)
            {
                result = false;
            }
            else
            {
                for (int i = 0; i < base.Count; i++)
                {
                    TSVector2 tSVector  = base[i];
                    TSVector2 tSVector2 = this.NextVertex(i);
                    for (int j = i + 1; j < base.Count; j++)
                    {
                        TSVector2 tSVector3 = base[j];
                        TSVector2 tSVector4 = this.NextVertex(j);
                        TSVector2 tSVector5;
                        bool      flag2 = LineTools.LineIntersect2(ref tSVector, ref tSVector2, ref tSVector3, ref tSVector4, out tSVector5);
                        if (flag2)
                        {
                            result = false;
                            return(result);
                        }
                    }
                }
                result = true;
            }
            return(result);
        }
Пример #3
0
        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);
        }
Пример #4
0
        /// <summary>
        /// Checks if the vertices forms an simple polygon by checking for edge crossings.
        /// </summary>
        public bool IsSimple()
        {
            //The simplest polygon which can exist in the Euclidean plane has 3 sides.
            if (Count < 3)
            {
                return(false);
            }

            for (int i = 0; i < Count; ++i)
            {
                TSVector2 a1 = this[i];
                TSVector2 a2 = NextVertex(i);
                for (int j = i + 1; j < Count; ++j)
                {
                    TSVector2 b1 = this[j];
                    TSVector2 b2 = NextVertex(j);

                    TSVector2 temp;

                    if (LineTools.LineIntersect2(ref a1, ref a2, ref b1, ref b2, out temp))
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
Пример #5
0
        private bool DistanceToHullAcceptable(Vertices polygon, TSVector2 point, bool higherDetail)
        {
            bool flag = polygon == null;

            if (flag)
            {
                throw new ArgumentNullException("polygon", "'polygon' can't be null.");
            }
            bool flag2 = polygon.Count < 3;

            if (flag2)
            {
                throw new ArgumentException("'polygon.Count' can't be less then 3.");
            }
            TSVector2 tSVector = polygon[polygon.Count - 1];
            bool      result;

            if (higherDetail)
            {
                for (int i = 0; i < polygon.Count; i++)
                {
                    TSVector2 value = polygon[i];
                    bool      flag3 = LineTools.DistanceBetweenPointAndLineSegment(ref point, ref value, ref tSVector) <= this._hullTolerance || TSVector2.Distance(point, value) <= this._hullTolerance;
                    if (flag3)
                    {
                        result = false;
                        return(result);
                    }
                    tSVector = polygon[i];
                }
                result = true;
            }
            else
            {
                for (int j = 0; j < polygon.Count; j++)
                {
                    TSVector2 value = polygon[j];
                    bool      flag4 = LineTools.DistanceBetweenPointAndLineSegment(ref point, ref value, ref tSVector) <= this._hullTolerance;
                    if (flag4)
                    {
                        result = false;
                        return(result);
                    }
                    tSVector = polygon[j];
                }
                result = true;
            }
            return(result);
        }
Пример #6
0
        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);
        }
Пример #8
0
        private static void SimplifySection(Vertices vertices, int i, int j, bool[] usePoint, FP distanceTolerance)
        {
            if ((i + 1) == j)
            {
                return;
            }

            TSVector2 a = vertices[i];
            TSVector2 b = vertices[j];

            FP  maxDistance = -1.0;
            int maxIndex    = i;

            for (int k = i + 1; k < j; k++)
            {
                TSVector2 point = vertices[k];

                FP distance = LineTools.DistanceBetweenPointAndLineSegment(ref point, ref a, ref b);

                if (distance > maxDistance)
                {
                    maxDistance = distance;
                    maxIndex    = k;
                }
            }

            if (maxDistance <= distanceTolerance)
            {
                for (int k = i + 1; k < j; k++)
                {
                    usePoint[k] = false;
                }
            }
            else
            {
                SimplifySection(vertices, i, maxIndex, usePoint, distanceTolerance);
                SimplifySection(vertices, maxIndex, j, usePoint, distanceTolerance);
            }
        }
Пример #9
0
        private static void SimplifySection(Vertices vertices, int i, int j, bool[] usePoint, FP distanceTolerance)
        {
            bool flag = i + 1 == j;

            if (!flag)
            {
                TSVector2 tSVector  = vertices[i];
                TSVector2 tSVector2 = vertices[j];
                FP        fP        = -1.0;
                int       num       = i;
                for (int k = i + 1; k < j; k++)
                {
                    TSVector2 tSVector3 = vertices[k];
                    FP        fP2       = LineTools.DistanceBetweenPointAndLineSegment(ref tSVector3, ref tSVector, ref tSVector2);
                    bool      flag2     = fP2 > fP;
                    if (flag2)
                    {
                        fP  = fP2;
                        num = k;
                    }
                }
                bool flag3 = fP <= distanceTolerance;
                if (flag3)
                {
                    for (int l = i + 1; l < j; l++)
                    {
                        usePoint[l] = false;
                    }
                }
                else
                {
                    SimplifyTools.SimplifySection(vertices, i, num, usePoint, distanceTolerance);
                    SimplifyTools.SimplifySection(vertices, num, j, usePoint, distanceTolerance);
                }
            }
        }
Пример #10
0
 public static Vertices LineSegmentAABBIntersect(ref TSVector2 point1, ref TSVector2 point2, AABB aabb)
 {
     return(LineTools.LineSegmentVerticesIntersect(ref point1, ref point2, aabb.Vertices));
 }
Пример #11
0
 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));
 }
Пример #12
0
 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));
 }
Пример #13
0
        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);
        }
Пример #14
0
        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);
        }
        /// <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       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);
        }
Пример #17
0
 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--;
         }
     }
 }