Ejemplo n.º 1
0
        void UpdateTriangulation(Vector2 p)
        {
            var tmpT = new List <Triangle2D>();
            var tmpS = new List <Segment2D>();

            var v = CheckAndAddVertex(p);

            tmpT = T.FindAll(t => t.ContainsInExternalCircle(v));
            tmpT.ForEach(t => {
                tmpS.Add(t.s0);
                tmpS.Add(t.s1);
                tmpS.Add(t.s2);

                AddTriangle(t.a, t.b, v);
                AddTriangle(t.b, t.c, v);
                AddTriangle(t.c, t.a, v);
                RemoveTriangle(t);
            });

            while (tmpS.Count != 0)
            {
                var s = tmpS.Last();
                tmpS.RemoveAt(tmpS.Count - 1);

                var commonT = T.FindAll(t => t.HasSegment(s));
                if (commonT.Count <= 1)
                {
                    continue;
                }

                var abc = commonT[0];
                var abd = commonT[1];

                if (abc.Equals(abd))
                {
                    RemoveTriangle(abc);
                    RemoveTriangle(abd);
                    continue;
                }

                var a = s.a;
                var b = s.b;
                var c = abc.ExcludePoint(s);
                var d = abd.ExcludePoint(s);

                var ec = Circle2D.GetCircumscribedCircle(abc);
                if (ec.Contains(d.Coordinate))
                {
                    RemoveTriangle(abc);
                    RemoveTriangle(abd);

                    AddTriangle(a, c, d);                     // add acd
                    AddTriangle(b, c, d);                     // add bcd

                    var segments0 = abc.ExcludeSegment(s);
                    tmpS.Add(segments0[0]);
                    tmpS.Add(segments0[1]);

                    var segments1 = abd.ExcludeSegment(s);
                    tmpS.Add(segments1[0]);
                    tmpS.Add(segments1[1]);
                }
            }
        }