public List <PointF> Region(PointF p) { Site site = _sitesIndexedByLocation[p]; if (site == null) { return(new List <PointF>()); } return(site.Region(_plotBounds)); }
public List <Vector2> Region(Vector2 p) { Site site = _sitesIndexedByLocation [p]; if (site == null) { return(new List <Vector2> ()); } return(site.Region(_plotBounds)); }
public List <List <Vector2> > Regions(Polygon plotBounds) { List <List <Vector2> > list = new List <List <Vector2> >(); for (int i = 0; i < _sites.Count; i++) { Site site = _sites[i]; list.Add(site.Region(plotBounds)); } return(list); }
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 _sites.ResetListIndex(); Site site = _sites.Next(); while (site != null) { // Loop all corners of the site to calculate the centroid List <Vector2> region = site.Region(plotBounds); if (region.Count < 1) { site = _sites.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 = _sites.Next(); } // Between each replacement of the cendroid of the cell, // we need to recompute Voronoi diagram: Rect origPlotBounds = this.plotBounds; Dispose(); _points.Clear(); _points = newPoints; Init(newPoints, _colors, origPlotBounds); } }