Exemplo n.º 1
0
        public bool Split(Vector2 start, Vector2 end)
        {
            if ((Front != null) && (Back != null))
            {
                return(Front.Split(start, end) | Back.Split(start, end));
            }

            bool allInside   = true;
            int  insideCount = 0;

            for (int index = 0, count = Polygon.Indices.Length; index < count; index++)
            {
                var firstVertex  = Polygon.Vertices[Polygon.Indices[index]];
                var secondVertex = Polygon.Vertices[Polygon.Indices[(index + 1) % count]];

                var first  = LineSegment.Classify(firstVertex, secondVertex, start);
                var second = LineSegment.Classify(firstVertex, secondVertex, end);

                first  = (first == LineSegment.PointClassification.Colinear) ? second : first;
                second = (second == LineSegment.PointClassification.Colinear) ? first : second;

                if (first != LineSegment.PointClassification.Colinear)
                {
                    allInside = allInside && (first == second) && (first == LineSegment.PointClassification.Inside);

                    if ((first == second) && (first == LineSegment.PointClassification.Outside))
                    {
                        return(LineSegment.Classify(start, end, firstVertex) == LineSegment.PointClassification.Inside);
                    }

                    ++insideCount;

                    if (first != second)
                    {
                        return(SplitSegmentAndRetry(firstVertex, secondVertex, start, end));
                    }
                }
            }

            if (allInside && (insideCount > 0))
            {
                DividePolygon(start, end);
                return(true);
            }

            return(false);
        }