예제 #1
0
        public void LloydRelaxation(int nbIterations)
        {
            // Reapeat the whole process for the number of iterations asked
            for (int i = 0; i < nbIterations; i++)
            {
                List <Vector2f> newPoints = new List <Vector2f>();
                // Go thourgh all sites
                sites.ResetListIndex();
                Site site = sites.Next();

                while (site != null)
                {
                    // Loop all corners of the site to calculate the centroid
                    List <Vector2f> region     = site.Region(plotBounds);
                    Vector2f        centroid   = Vector2f.zero;
                    float           signedArea = 0;
                    float           x0         = 0;
                    float           y0         = 0;
                    float           x1         = 0;
                    float           y1         = 0;
                    float           a          = 0;
                    // For all vertices except last
                    for (int j = 0; j < region.Count - 1; j++)
                    {
                        x0          = region[j].x;
                        y0          = region[j].y;
                        x1          = region[j + 1].x;
                        y1          = region[j + 1].y;
                        a           = x0 * y1 - x1 * y0;
                        signedArea += a;
                        centroid.x += (x0 + x1) * a;
                        centroid.y += (y0 + y1) * a;
                    }
                    // Do last vertex
                    x0          = region[region.Count - 1].x;
                    y0          = region[region.Count - 1].y;
                    x1          = region[0].x;
                    y1          = region[0].y;
                    a           = x0 * y1 - x1 * y0;
                    signedArea += a;
                    centroid.x += (x0 + x1) * a;
                    centroid.y += (y0 + y1) * a;

                    signedArea *= 0.5f;
                    centroid.x /= (6 * signedArea);
                    centroid.y /= (6 * signedArea);
                    // Move site to the centroid of its Voronoi cell
                    newPoints.Add(centroid);
                    site = sites.Next();
                }

                // Between each replacement of the cendroid of the cell,
                // we need to recompute Voronoi diagram:
                Rectf origPlotBounds = this.plotBounds;
                Dispose();
                Init(newPoints, origPlotBounds);
            }
        }
예제 #2
0
        public void LloydRelaxation(int nbIterations)
        {
            // Reapeat the whole process for the number of iterations asked.
            for (int i = 0; i < nbIterations; i++)
            {
                List <Vector2f> newPoints = new List <Vector2f>();
                // Go thourgh all sites
                this.sitelist.ResetListIndex();
                Site site = this.sitelist.Next();

                while (site != null)
                {
                    // Loop all corners of the site to calculate the centroid.
                    List <Vector2f> region = site.Region(this.PlotBounds);
                    if (region.Count < 1)
                    {
                        site = sitelist.Next();
                        continue;
                    }

                    Vector2f centroid   = Vector2f.ZERO;
                    float    signedArea = 0;
                    float    x0         = 0;
                    float    y0         = 0;
                    float    x1         = 0;
                    float    y1         = 0;
                    float    a          = 0;
                    // For all vertices except last.
                    for (int j = 0; j < region.Count - 1; j++)
                    {
                        x0          = region[j].x;
                        y0          = region[j].y;
                        x1          = region[j + 1].x;
                        y1          = region[j + 1].y;
                        a           = x0 * y1 - x1 * y0;
                        signedArea += a;
                        centroid.x += (x0 + x1) * a;
                        centroid.y += (y0 + y1) * a;
                    }
                    // Do last vertex.
                    x0          = region[region.Count - 1].x;
                    y0          = region[region.Count - 1].y;
                    x1          = region[0].x;
                    y1          = region[0].y;
                    a           = x0 * y1 - x1 * y0;
                    signedArea += a;
                    centroid.x += (x0 + x1) * a;
                    centroid.y += (y0 + y1) * a;

                    signedArea *= 0.5f;
                    centroid.x /= (6 * signedArea);
                    centroid.y /= (6 * signedArea);
                    // Move site to the centroid of its Voronoi cell.
                    newPoints.Add(centroid);
                    site = this.sitelist.Next();
                }

                // Recompute the Voronoi diagram.
                Rectf origPlotBounds = this.PlotBounds;
                this.Dispose();
                this.Init(newPoints, origPlotBounds);
            }
        }