Exemple #1
0
        void performLloyd()
        {
            VoronoiStruct.Voronoi newMap = new VoronoiStruct.Voronoi(vmap.width, vmap.height);
            Rectangle             regin  = new Rectangle(0, 0, vmap.width, vmap.height);

            foreach (var poly in vmap.polygons)
            {
                List <VoronoiStruct.Point> points = new List <VoronoiStruct.Point>();
                foreach (var edge in poly.edges)
                {
                    VoronoiStruct.Point a = edge.line.a;
                    VoronoiStruct.Point b = edge.line.b;
                    if (!points.Contains(a))
                    {
                        points.Add(a);
                    }
                    if (!points.Contains(b))
                    {
                        points.Add(b);
                    }
                }
                int cx = points.Sum((point) => { return(point.x); }) / points.Count;
                int cy = points.Sum((point) => { return(point.y); }) / points.Count;
                newMap.polygons.Add(new VoronoiStruct.Polygon(new VoronoiStruct.Point(cx, cy)));
            }
            vmap      = newMap;
            sweepLine = null;
            drawVoronoi(vmap);
            ++mapIterations;
            label_iterations.Text = string.Format("{0}-th iterations", mapIterations);
        }
Exemple #2
0
        void initMap(string json, out VoronoiStruct.Voronoi vmap)
        {
            var map = JsonConvert.DeserializeObject <VoronoiStruct.Voronoi>(json);

            foreach (var poly in map.polygons)
            {
                foreach (var edge in poly.edges)
                {
                    edge.deAbstract();
                }
            }
            vmap                  = map;
            sweepLine             = null;
            mapIterations         = 0;
            label_iterations.Text = string.Format("{0}-th iterations", mapIterations);
        }
Exemple #3
0
        private void button_perform_fortune_Click(object sender, EventArgs e)
        {
            if (vmap == null)
            {
                return;
            }
            if (sweepLine == null)
            {
                sweepLine = new VoronoiStruct.SweepLine(ref vmap);
            }
            double L = sweepLine.nextEvent();

            if (L == double.MaxValue)
            {
                sweepLine.finishEdges();
            }
            drawVoronoi(vmap);
            drawCurrentStep();
        }
Exemple #4
0
        private void button_get_result_fortune_Click(object sender, EventArgs e)
        {
            if (vmap == null)
            {
                return;
            }
            if (sweepLine == null)
            {
                sweepLine = new VoronoiStruct.SweepLine(ref vmap);
            }
            while (sweepLine.nextEvent() != double.MaxValue)
            {
                ;
            }
            sweepLine.finishEdges();
            Rectangle regin = new Rectangle(0, 0, vmap.width, vmap.height);

            foreach (var poly in vmap.polygons)
            {
                poly.optimize(regin);
            }

            drawVoronoi(vmap);
        }