/*---------------------------------------------------------------------
         * Prints out n-3 diagonals (as pairs of integer indices)
         * which form a triangulation of P.  This algorithm is O(n^2).
         * See Chapter 1 for a full explanation.
         * Triangulate operates on listcopy rather than list, so that
         * the original polygon is not destroyed.
         */
        public void Triangulate()
        {
            cVertex   v0, v1, v2, v3, v4;    // five consecutive vertices
            cDiagonal diag;
            int       n        = listcopy.n; //number of vertices; shrinks to 3
            bool      earfound = false;      //to prevent infinite loop on improper input

            EarInit();

            /* Each step of outer loop removes one ear. */
            while (n > 3)
            {
                /* Inner loop searches for an ear. */
                v2 = listcopy.head;
                do
                {
                    if (v2.IsEar)
                    {
                        /* Ear found. Fill variables. */
                        v3 = v2.NextVertex; v4 = v3.NextVertex;
                        v1 = v2.PrevVertex; v0 = v1.PrevVertex;

                        /* (v1,v3) is a diagonal */
                        earfound = true;
                        diag     = new cDiagonal(v1, v3);
                        diag.PrintDiagonal(listcopy.n - n);
                        diaglist.InsertBeforeHead(diag);

                        /* Update earity of diagonal endpoints */
                        v1.IsEar = Diagonal(v0, v3);
                        v3.IsEar = Diagonal(v1, v4);

                        /* Cut off the ear v2 */
                        v1.NextVertex = v3;
                        v3.PrevVertex = v1;
                        listcopy.head = v3; /* In case the head was v2. */
                        n--;
                        break;              /* out of inner loop; resume outer loop */
                    } /* end if ear found */
                    v2 = v2.NextVertex;
                } while (v2 != listcopy.head);
                if (!earfound)
                {
                    System.Diagnostics.Debug.WriteLine("Polygon is nonsimple: cannot triangulate");
                    break;
                }
                else
                {
                    earfound = false;
                }
                diagdrawn = false;
            } /* end outer while loop */
        }
Пример #2
0
        public void PrintDiagonals()
        {
            cDiagonal temp = head;
            int       i    = 0;

            do
            {
                temp.PrintDiagonal(i);
                temp = temp.next;
                i++;
            } while (temp != head);
        }