Ejemplo n.º 1
0
        /*---------------------------------------------------------------------
        *  Performs the Graham scan on an array of angularly sorted points P.
        *  ---------------------------------------------------------------------*/

        private cVertexList Graham()
        {
            cVertexList top;
            int         i;

            //  cVertex p1, p2;  /* Top two points on stack. */


            /* Initialize stack. */
            top = new cVertexList();
            cVertex v1 = new cVertex(list.head.Point.X, list.head.Point.Y);

            v1.IndexInModel = list.head.IndexInModel;
            v1.IsProcessed  = list.head.IsProcessed;

            cVertex v2 = new cVertex(list.head.NextVertex.Point.X, list.head.NextVertex.Point.Y);

            v2.IndexInModel = list.head.NextVertex.IndexInModel;
            v2.IsProcessed  = list.head.NextVertex.IsProcessed;


            Push(v1, top);
            Push(v2, top);

            // Bottom two elements will never be removed.
            i = 2;

            while (i < list.n)
            {
                cVertex v3 = new cVertex(list.GetElement(i).Point.X, list.GetElement(i).Point.Y);
                v3.IsProcessed  = list.GetElement(i).IsProcessed;
                v3.IndexInModel = list.GetElement(i).IndexInModel;

                if (v1.Point.Left(top.head.PrevVertex.Point, top.head.PrevVertex.PrevVertex.Point, v3.Point))
                {
                    Push(v3, top);
                    i++;
                }
                else
                {
                    if (top.n > 2)
                    {
                        Pop(top);
                    }
                }
            }

            return(top);
        }
Ejemplo n.º 2
0
        private void Sort(cVertexList a, int lo0, int hi0)
        {
            if (lo0 >= hi0)
            {
                return;
            }
            cVertex mid = new cVertex();

            mid = a.GetElement(hi0);
            int lo = lo0;
            int hi = hi0 - 1;

            while (lo <= hi)
            {
                while (lo <= hi && ((Compare(a.GetElement(lo), mid) == 1) || (Compare(a.GetElement(lo), mid) == 0)))
                {
                    lo++;
                }

                while (lo <= hi && ((Compare(a.GetElement(hi), mid) == -1) || (Compare(a.GetElement(hi), mid) == 0)))
                {
                    hi--;
                }

                if (lo < hi)
                {
                    Swap(a.GetElement(lo), a.GetElement(hi));
                }
            }
            Swap(a.GetElement(lo), a.GetElement(hi0));
            Sort(a, lo0, lo - 1);
            Sort(a, lo + 1, hi0);
        }
Ejemplo n.º 3
0
        private int ReadVertices()
        {
            cVertex v = P.head;
            int     i = 0;

            do
            {
                v.IndexInModel = i++;
                v.IsProcessed  = true;
                v = v.NextVertex;
            } while (v != P.head);

            v = B.head;
            do
            {
                cVertex temp = new cVertex(v.Point.X, v.Point.Y);
                P.InsertBeforeHead(temp);
                v = v.NextVertex;
            } while (v != B.head);

            v = P.GetElement(n); i = 0;
            do
            {
                /* Reflect secondary polygon */
                v.Point.X      = -v.Point.X;
                v.Point.Y      = -v.Point.Y;
                v.IndexInModel = i++;
                v.IsProcessed  = false;
                v = v.NextVertex;
            } while (v != P.head);

            double xmin, ymin, xmax, ymax;     /* Primary min & max */
            double sxmin, symin, sxmax, symax; /* Secondary min & max */
            int    mp, ms;                     /* i index of max (u-r) primary and secondary points */

            xmin  = ymin = xmax = ymax = 0;
            sxmin = symin = sxmax = symax = 0;
            mp    = ms = 0; v = P.head;
            xmin  = xmax = v.Point.X;
            ymin  = ymax = v.Point.Y;
            mp    = 0; i = 1;
            v     = v.NextVertex;
            cVertex startB = P.GetElement(n);

            do
            {
                if (v.Point.X > xmax)
                {
                    xmax = v.Point.X;
                }
                else if (v.Point.X < xmin)
                {
                    xmin = v.Point.X;
                }
                if (v.Point.Y > ymax)
                {
                    ymax = v.Point.Y; mp = i;
                }
                else if (v.Point.Y == ymax && (v.Point.X > P.GetElement(mp).Point.X))
                {
                    mp = i;
                }
                else if (v.Point.Y < ymin)
                {
                    ymin = v.Point.Y;
                }
                v = v.NextVertex; i++;
            } while (v != startB);
            /*System.Diagnostics.Debug.WriteLine("Index of upper rightmost primary, i=mp = "+mp);*/
            v     = startB;
            sxmin = sxmax = v.Point.X;
            symin = symax = v.Point.Y;
            ms    = n; v = v.NextVertex; i = 1;
            do
            {
                if (v.Point.X > sxmax)
                {
                    sxmax = v.Point.X;
                }
                else if (v.Point.X < sxmin)
                {
                    sxmin = v.Point.X;
                }
                if (v.Point.Y > symax)
                {
                    symax = v.Point.Y; ms = i;
                }
                else if (v.Point.Y == symax && (v.Point.X > P.GetElement(ms).Point.X))
                {
                    ms = i;
                }
                else if (v.Point.Y < symin)
                {
                    symin = v.Point.Y;
                }
                v = v.NextVertex; i++;
            } while (v != P.head.NextVertex);
            /*System.Diagnostics.Debug.WriteLine("Index of upper rightmost secondary, i=ms = "+ms);*/

            /* Compute the start point: upper rightmost of both. */
            System.Diagnostics.Debug.WriteLine("p0:");
            p0.PrintPoint();
            System.Diagnostics.Debug.WriteLine("mp is: " + mp);
            System.Diagnostics.Debug.WriteLine("mp element:" + P.GetElement(mp).Point.X + "," + P.GetElement(mp).Point.Y);
            AddVec(p0, P.GetElement(mp).Point, p0);
            System.Diagnostics.Debug.WriteLine("p0 after addvec:");
            p0.PrintPoint();
            System.Diagnostics.Debug.WriteLine("ms is: " + ms);
            System.Diagnostics.Debug.WriteLine("ms element:" + P.GetElement(ms).Point.X + "," + P.GetElement(ms).Point.Y);
            //   AddVec( p0, P.GetElement(ms).v, p0 );
            System.Diagnostics.Debug.WriteLine("p0 after another addvec:");
            p0.PrintPoint();
            return(mp);
        }