private void DoVoronoiAndRelax() { Voronoi = new Voronoi(Delaunay); Voronoi.Build(); Voronoi.TrimSitesToBndry(Bounds); Voronoi.LloydRelax(Bounds); }
public static GameMap Generate() { var nameGen = new NameGenerator(); GameMap gameRes = new GameMap(); int n = 50; var points = new List<Vector2>(); var colors = new List<uint>(); var lines = new List<Tuple<Vector2, Vector2>>(); for (int i = 0; i < n; i++) { points.Add(new Vector2(UnityEngine.Random.Range(-10.0F, 10.0F), UnityEngine.Random.Range(-10.0F, 10.0F))); colors.Add(0); } Delaunay.Voronoi v = new Delaunay.Voronoi(points, colors, new Rect(-10.0F, -10.0F, 20.0F, 20.0F)); var regions = v.SiteCoords(); foreach (var pts in v.Regions()) { var prov = new Province(); prov.Border = pts.Reverse<Vector2>().ToList(); prov.Name = nameGen.getName(); gameRes.AddProvince(prov); } return gameRes; }
private static void makeNeighbors() { Stopwatch sw = new Stopwatch(); sw.Start(); float x, y, maxx = nodes[0].x, maxy = nodes[0].y, minx = nodes[0].x, miny = nodes[0].y; for (int i = 0; i < nodes.Count; i++) { x = nodes[i].x; y = nodes[i].y; if (float.IsNaN(x) || float.IsNaN(y) || float.IsInfinity(x) || float.IsInfinity(y)) { if (UnityEngine.Debug.isDebugBuild) { UnityEngine.Debug.Log("ERROR: makeNeighbors Nan or Inf " + i); } nodes[i].x = nodes[i].y = 33; //something! } if (maxx < x) { maxx = x; } if (minx > x) { minx = x; } if (maxy < y) { maxy = y; } if (miny > y) { miny = y; } } preVoronoi += sw.ElapsedTicks; sw.Reset(); sw.Start(); //if (voronoi != null) voronoi.Dispose(); slowed things down! pools not efficiently implemented Delaunay.Voronoi voronoi = new Delaunay.Voronoi(Engine.nodes, new Rect(minx - 1, miny - 1, 2 + maxx - minx, 2 + maxy - miny)); inVoronoi += sw.ElapsedTicks; sw.Reset(); sw.Start(); postVoronoi += sw.ElapsedTicks; sw.Reset(); }
/// <summary> /// Find minium amount of connecting line segments between supplied room center points. /// </summary> /// <param name="percentOfSegementsAboveMinThreshold">Percent of extra connecting line segments to return. If 0% then only returns min spanning tree.</param> /// <returns></returns> public List <Line> FindConnectingLineSegments(List <Vector2> centerPoints, float percentOfSegementsAboveMinThreshold) { Delaunay.Voronoi v = CreateGraph(centerPoints); List <LineSegment> dTriangulation = v.DelaunayTriangulation(); List <LineSegment> tree = v.SpanningTree(Delaunay.KruskalType.MINIMUM); int numOfExtraSegments = Mathf.RoundToInt((float)dTriangulation.Count * percentOfSegementsAboveMinThreshold); List <LineSegment> finalSegments = new List <LineSegment>(); int extraSegCount = 0; foreach (LineSegment segment in dTriangulation) { bool foundInTree = false; // always add the min spanning tree segments to final. foreach (LineSegment treeSeg in tree) { if (segment.p0.Value == treeSeg.p0.Value && segment.p1.Value == treeSeg.p1.Value) { finalSegments.Add(segment); foundInTree = true; break; } } // Add in the extra segements. if (!foundInTree && extraSegCount < numOfExtraSegments) { finalSegments.Add(segment); extraSegCount++; } } List <Line> foundLines = new List <Line>(); foreach (LineSegment seg in finalSegments) { foundLines.Add(new Line(seg.p0, seg.p1)); } return(foundLines); }