Exemple #1
0
        private void InsertPoint(EndPoint p)
        {
            if (p.Begin)
            {
                bool inserted = false;
                for (int index = 0; index < Open.Count; index++)
                {
                    Segment test = Open[index];
                    if (Segment.IsInFrontOf(p.Segment, test, Center) != Segment.IntersectionResult.InFront)
                    {
                        Open.Insert(index, p.Segment);
                        inserted = true;
                        break;
                    }
                }

                if (!inserted)
                {
                    Open.Add(p.Segment);
                }
            }
            else
            {
                Open.Remove(p.Segment);
            }
        }
Exemple #2
0
        private void SplitSegmentsThatOverlap()
        {
            // O(N^2)
            Queue <Segment> open = new Queue <Segment>(Segments);

            Segments.Clear();
            while (open.Any())
            {
                if (Segments.Count > 300)
                {
                    Debug.Log("Too many segment splits!");
                    return;
                }
                Segment segment   = open.Dequeue();
                bool    splitFree = true;
                foreach (Segment testing in Segments)
                {
                    Segment.IntersectionResult intersection = Segment.IsInFrontOf(segment, testing, Center);

                    if (intersection == Segment.IntersectionResult.Intersects)
                    {
                        // Do they *really* overlap, there seems to be some issues with the IsInFrontOf method.
                        if (ShadowMathUtils.Approximately(testing.Start.Point, segment.Start.Point) ||
                            ShadowMathUtils.Approximately(testing.End.Point, segment.End.Point) ||
                            ShadowMathUtils.Approximately(testing.End.Point, segment.Start.Point) ||
                            ShadowMathUtils.Approximately(testing.Start.Point, segment.End.Point))
                        {
                            //intersection = Segment.IsInFrontOf(segment, testing, Center);
                            //Debug.Log("They're the same point!"+intersection);
                            continue;
                        }

                        // segment && testing overlap, we must split one of them but both must go back on the queue
                        Segments.Remove(testing);
                        open.Enqueue(testing);

                        Vector2 position =
                            ShadowMathUtils.LineIntersection(
                                segment.Start.Point,
                                segment.End.Point,
                                testing.Start.Point,
                                testing.End.Point);

                        if (_drawGizmos)
                        {
                            Gizmos.color = Color.red;
                            Gizmos.DrawSphere(position, 0.2f);
                        }

                        // Split segments and add both parts back to the queue
                        Split(segment, position, open);
                        splitFree = false;
                        break;
                    }
                }
                if (splitFree)
                {
                    Segments.Add(segment);
                }
            }
        }