Exemple #1
0
        /// <summary>
        /// The Graham Scan algorithm determines the points belonging to the convex hull in O(n lg n).
        /// </summary>
        /// <param name="points">set of points</param>
        /// <returns>points in the convex hull</returns>
        /// <seealso href="http://en.wikipedia.org/wiki/Graham_scan">Graham scan, Wikipedia</seealso>
        public static List <Point> GrahamScan(List <Point> points)
        {
            if (points.Count <= 3)
            {
                return(new List <Point>(points));
            }

            var ps = new List <Point>(points);

            ps.Sort(new CompareYThenX());
            ps.Sort(new PolarComparator(ps[0]));

            Deque <Point> hull = new ArrayDeque <Point>();

            hull.Push(ps[0]);
            hull.Push(ps[1]);
            hull.Push(ps[2]);

            for (int i = 3; i < points.Count; i++)
            {
                var top = hull.Pop();
                while (hull.Any() && !IsLeftTurn(hull.Peek(), top, ps[i]))
                {
                    top = hull.Pop();
                }
                hull.Push(top);
                hull.Push(ps[i]);
            }

            return(new List <Point>(hull));
        }
Exemple #2
0
        /// <summary>
        /// Returns the next Event or {@code null} if there are no more events or
        /// the walker is closed.
        /// </summary>
        internal virtual Event Next()
        {
            DirectoryNode top = Stack.Peek();

            if (top == null)
            {
                return(null);                // stack is empty, we are done
            }

            // continue iteration of the directory at the top of the stack
            Event ev;

            do
            {
                Path        entry = null;
                IOException ioe   = null;

                // get next entry in the directory
                if (!top.Skipped())
                {
                    IEnumerator <Path> iterator = top.Iterator();
                    try
                    {
//JAVA TO C# CONVERTER TODO TASK: Java iterators are only converted within the context of 'while' and 'for' loops:
                        if (iterator.hasNext())
                        {
//JAVA TO C# CONVERTER TODO TASK: Java iterators are only converted within the context of 'while' and 'for' loops:
                            entry = iterator.next();
                        }
                    }
                    catch (DirectoryIteratorException x)
                    {
                        ioe = x.InnerException;
                    }
                }

                // no next entry so close and pop directory, creating corresponding event
                if (entry == null)
                {
                    try
                    {
                        top.Stream().Close();
                    }
                    catch (IOException e)
                    {
                        if (ioe != null)
                        {
                            ioe = e;
                        }
                        else
                        {
                            ioe.AddSuppressed(e);
                        }
                    }
                    Stack.Pop();
                    return(new Event(EventType.END_DIRECTORY, top.Directory(), ioe));
                }

                // visit the entry
                ev = Visit(entry, true, true);                 // canUseCached -  ignoreSecurityException
            } while (ev == null);

            return(ev);
        }