Beispiel #1
0
        //worst case O(nlogn)
        public void Solve(List <System.Drawing.PointF> pointList)
        {
            //sort points
            pointList = pointList.OrderByDescending(p => p.X).ToList();

            //call the recursive convexHull function, retrieve final list
            Hull finalHUll = convexHUll(pointList);

            pointList = finalHUll.getList();

            //prepare list and draw it
            PointF[] pointArray = pointList.ToArray();
            g.DrawPolygon(new Pen(Color.Red), pointArray);
        }
Beispiel #2
0
        //worst case O(n)
        public Hull combine(Hull left, Hull right)
        {
            //extract lists from hulls
            List <PointF> l = left.getList();
            List <PointF> r = right.getList();

            //set booleans, they come in handy
            bool bothDone  = false;
            bool rightDone = false;
            bool leftDone  = false;

            //those 4 ints abreviate topleftindex,toprightindex,bottomleftindex,and bottomrightindex
            int tli = left.getRightIndex();
            int tri = 0;
            int bli = left.getRightIndex();
            int bri = 0;


            //find top edge, repeat this loop until no change in left or right index
            while (!bothDone)
            {
                bothDone  = true;
                rightDone = false;
                leftDone  = false;

                //move right index "up" until no advantage in slope change
                while (!rightDone)
                {
                    int newIndex;

                    if (tri == 0)
                    {
                        newIndex = r.Count() - 1;
                    }
                    else
                    {
                        newIndex = tri - 1;
                    }

                    if (slope(l[tli], r[tri]) > slope(l[tli], r[newIndex]))
                    {
                        tri      = newIndex;
                        bothDone = false;
                    }
                    else
                    {
                        rightDone = true;
                    }
                }

                //move left index "up" until no advantage in slope change
                while (!leftDone)
                {
                    int newIndex;

                    if (tli == (l.Count() - 1))
                    {
                        newIndex = 0;
                    }
                    else
                    {
                        newIndex = tli + 1;
                    }

                    if (slope(l[tli], r[tri]) < slope(l[newIndex], r[tri]))
                    {
                        tli      = newIndex;
                        bothDone = false;
                    }
                    else
                    {
                        leftDone = true;
                    }
                }
            }



            bothDone = false;
            //find bottom edge, repeat this loop until no change in left or right index
            while (!bothDone)
            {
                bothDone  = true;
                rightDone = false;
                leftDone  = false;

                //move right index "down" until no advantage in slope change
                while (!rightDone)
                {
                    if (bri == (r.Count() - 1))
                    {
                        rightDone = true;
                    }
                    else
                    {
                        if (slope(l[bli], r[bri]) < slope(l[bli], r[bri + 1]))
                        {
                            bri++;
                            bothDone = false;
                        }
                        else
                        {
                            rightDone = true;
                        }
                    }
                }

                //move left index "down" until no advantage in slope change
                while (!leftDone)
                {
                    if (bli == 0)
                    {
                        leftDone = true;
                    }
                    else
                    {
                        if (slope(l[bli], r[bri]) > slope(l[bli - 1], r[bri]))
                        {
                            bli--;
                            bothDone = false;
                        }
                        else
                        {
                            leftDone = true;
                        }
                    }
                }
            }

            //pass in the indeces we found
            return(getNewHull(bli, bri, tri, tli, right.getRightIndex(), l, r));
        }