public List <Vector2> Region(Vector2 p) { Site site = _sitesIndexedByLocation [p]; if (site == null) { return(new List <Vector2> ()); } return(site.GenerateRegion(_plotBounds)); }
public void LloydRelaxation(int nbIterations) { // Reapeat the whole process for the number of iterations asked for (int i = 0; i < nbIterations; i++) { List <Vector2> newPoints = new List <Vector2>(); // Go thourgh all sites SitesList.ResetListIndex(); Site site = SitesList.Next(); while (site != null) { // Loop all corners of the site to calculate the centroid List <Vector2> region = site.GenerateRegion(plotBounds); if (region.Count < 1) { site = SitesList.Next(); continue; } Vector2 centroid = Vector2.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 = SitesList.Next(); } // Between each replacement of the cendroid of the cell, // we need to recompute Voronoi diagram: Rect origPlotBounds = this.plotBounds; Dispose(); Init(newPoints, origPlotBounds); } }