void VoronoiTesselation() { bool usesUserDefinedSites = false; if (voronoiSites != null && voronoiSites.Count > 0) { numProvinces = voronoiSites.Count; usesUserDefinedSites = true; } if (centers == null || centers.Length != numProvinces) { centers = new Point[numProvinces]; } for (int k = 0; k < centers.Length; k++) { if (usesUserDefinedSites) { Vector2 p = voronoiSites[k]; centers[k] = new Point(p.x, p.y); } else { centers[k] = new Point(UnityEngine.Random.Range(-0.49f, 0.49f), UnityEngine.Random.Range(-0.49f, 0.49f)); } } if (voronoi == null) { voronoi = new VoronoiFortune(); } for (int k = 0; k < goodGridRelaxation; k++) { voronoi.AssignData(centers); voronoi.DoVoronoi(); if (k < goodGridRelaxation - 1) { for (int j = 0; j < numProvinces; j++) { Point centroid = voronoi.cells[j].centroid; centers[j] = (centers[j] + centroid) / 2; } } } // Make cell regions: we assume cells have only 1 region but that can change in the future for (int k = 0; k < voronoi.cells.Length; k++) { VoronoiCell voronoiCell = voronoi.cells[k]; Vector2 center = voronoiCell.center.vector3; MapProvince cell = new MapProvince(center); MapRegion cr = new MapRegion(cell); if (edgeNoise > 0) { cr.polygon = voronoiCell.GetPolygon(voronoiCell.center, edgeMaxLength, edgeNoise); } else { cr.polygon = voronoiCell.GetPolygon(); } if (cr.polygon != null) { // Add segments int segmentsCount = voronoiCell.segments.Count; for (int i = 0; i < segmentsCount; i++) { Segment s = voronoiCell.segments[i]; if (!s.deleted) { if (edgeNoise > 0) { cr.segments.AddRange(s.subdivisions); } else { cr.segments.Add(s); } } } cell.region = cr; mapProvinces.Add(cell); } } }
void SetupIrregularGrid() { Point[] centers = new Point[_numCells]; for (int k=0;k<centers.Length;k++) { centers[k] = new Point(UnityEngine.Random.Range (-0.49f, 0.49f), UnityEngine.Random.Range (-0.49f, 0.49f)); } VoronoiFortune voronoi = new VoronoiFortune (); for (int k=0;k<goodGridRelaxation;k++) { voronoi.AssignData (centers); voronoi.DoVoronoi (); if (k <goodGridRelaxation-1) { for (int j=0;j<_numCells;j++) { Point centroid = voronoi.cells[j].centroid; centers[j] = (centers[j] + centroid)/2; } } } // Make cell regions: we assume cells have only 1 region but that can change in the future float curvature = goodGridCurvature; for (int k=0; k<voronoi.cells.Length; k++) { VoronoiCell voronoiCell = voronoi.cells[k]; Cell cell = new Cell(k.ToString(), voronoiCell.center.vector3); Region cr = new Region (cell); if (curvature>0) { cr.polygon = voronoiCell.GetPolygon(3, curvature); } else { cr.polygon = voronoiCell.GetPolygon(1, 0); } if (cr.polygon!=null) { // Add segments for (int i=0;i<voronoiCell.segments.Count;i++) { Segment s = voronoiCell.segments[i]; if (!s.deleted) { if (curvature>0) { cr.segments.AddRange(s.subdivisions); } else { cr.segments.Add (s); } } } cell.polygon = cr.polygon.Clone(); cell.region = cr; cells.Add (cell); } } }