Example #1
0
        private void GenerateGraph()
        {
            var start = Stopwatch.GetTimestamp();
            var rand  = new Random((int)nudSeed.Value);
            var w     = splitPanel.Panel2.ClientSize.Width;
            var h     = splitPanel.Panel2.ClientSize.Height;

            var numSites = (int)nudNumRegions.Value;
            var sites    = new List <PointF>();

            for (int i = 0; i < numSites; i++)
            {
                var p = new Point(rand.Next(w), rand.Next(h));
                sites.Add(p);
            }

            if (nudRelax.Value > 0)
            {
                sites = RelaxPoints((int)nudRelax.Value, sites);
            }


            _graph = VoronoiGraph.ComputeVoronoiGraph(sites, w, h, chDebug.Checked);

            var elapsed = new TimeSpan(Stopwatch.GetTimestamp() - start);


            Console.WriteLine("Voronois done!");
            Console.WriteLine("{0} sites, {1} relaxations, time elapsed: {2}", numSites, nudRelax.Value, elapsed);
        }
Example #2
0
        private List <PointF> RelaxPoints(int times, List <PointF> points)
        {
            var ret        = new List <PointF>();
            var w          = splitPanel.Panel2.ClientSize.Width;
            var h          = splitPanel.Panel2.ClientSize.Height;
            var tempPoints = points;

            for (int i = 0; i < times; i++)
            {
                var voronoi = VoronoiGraph.ComputeVoronoiGraph(tempPoints, w, h);
                tempPoints.Clear();
                foreach (var site in voronoi.Sites)
                {
                    var region = site.Region(splitPanel.Panel2.ClientRectangle);
                    var p      = new PointF(0, 0);
                    foreach (var q in region)
                    {
                        p.X += q.X;
                        p.Y += q.Y;
                    }
                    p.X /= region.Count;
                    p.Y /= region.Count;
                    tempPoints.Add(p);
                }
                ret = tempPoints;
            }

            return(ret);
        }