예제 #1
0
        static void Main()
        {
            // example structure as binary heap
            // -4444 parent root at the beginning with children -5 and 1
            // -5 with children 5 and 50, 1 with children 4 and 8
            // 5 with children 999 and 19
            //                                   -4444
            //                                -5        1
            //                             5      50    4  8
            //                         999   19                     

            PriorityQueue<int> queue = new PriorityQueue<int>();

            queue.Enqueue(5);
            queue.Enqueue(19);
            queue.Enqueue(-4444);
            queue.Enqueue(1);
            queue.Enqueue(50);
            queue.Enqueue(4);
            queue.Enqueue(8);
            queue.Enqueue(999);
            queue.Enqueue(-5);

            Print(queue);


            Console.WriteLine(queue.Dequeue());

            // all are for testing
            // Console.WriteLine();
            // Print(queue);

            Console.WriteLine(queue.Dequeue());

            // Console.WriteLine();
            // Print(queue);

            Console.WriteLine(queue.Dequeue());
            Console.WriteLine("----------------");

            // Console.WriteLine();
            // Print(queue);

            Console.WriteLine("Peek: " + queue.Peek());

            Console.WriteLine(queue.Count);

            queue.Clear();

            Console.WriteLine(queue.Count);
        }
        /// <summary>
        /// Computes the voronoi graph.
        /// </summary>
        /// <returns>
        /// The voronoi graph.
        /// </returns>
        /// <param name='Datapoints'>
        /// Datapoints.
        /// </param>
        /// <exception cref='Exception'>
        /// Represents errors that occur during application execution.
        /// </exception>
        public static VoronoiGraph ComputeVoronoiGraph(IEnumerable Datapoints)
        {
            PriorityQueue<VEvent> PQ = new PriorityQueue<VEvent>(PriorityQueueType.Minimum);
            Hashtable CurrentCircles = new Hashtable();
            VoronoiGraph VG = new VoronoiGraph();
            VNode RootNode = null;

            foreach(Vector2D V in Datapoints)
            {
                PQ.Enqueue(new VDataEvent(V));
            }

            while(PQ.Count>0)
            {
                VEvent VE = PQ.Dequeue() as VEvent;
                VDataNode[] CircleCheckList;

                if(VE is VDataEvent)
                {
                    RootNode = VNode.ProcessDataEvent(VE as VDataEvent,RootNode,VG,VE.Y,out CircleCheckList);
                }
                else if(VE is VCircleEvent)
                {
                    CurrentCircles.Remove(((VCircleEvent)VE).NodeN);
                    if(!((VCircleEvent)VE).Valid)
                        continue;
                    RootNode = VNode.ProcessCircleEvent(VE as VCircleEvent,RootNode,VG,VE.Y,out CircleCheckList);
                }
                else {
                    throw new Exception("Got event of type "+VE.GetType().ToString()+"!");
                }

                foreach(VDataNode VD in CircleCheckList)
                {
                    if(CurrentCircles.ContainsKey(VD))
                    {
                        ((VCircleEvent)CurrentCircles[VD]).Valid=false;
                        CurrentCircles.Remove(VD);
                    }

                    VCircleEvent VCE = VNode.CircleCheckDataNode(VD,VE.Y);
                    if(VCE!=null)
                    {
                        PQ.Enqueue(VCE);
                        CurrentCircles[VD]=VCE;
                    }
                }

                if(VE is VDataEvent)
                {
                    Vector2D DP = ((VDataEvent)VE).DataPoint;
                    foreach(VCircleEvent VCE in CurrentCircles.Values)
                    {
                        if(MathTools.Dist(DP.X,DP.Y,VCE.Center.X,VCE.Center.Y)<VCE.Y-VCE.Center.Y && Math.Abs(MathTools.Dist(DP.X,DP.Y,VCE.Center.X,VCE.Center.Y)-(VCE.Y-VCE.Center.Y))>1e-10) {
                            VCE.Valid = false;
                        }
                    }
                }
            }

            return VG;
        }