private void InitGraph() { int cellCount = rowLengthsAccumulated[rowCount - 1] + rowLengths[rowCount - 1]; if (graph == null) { graph = new PositionGraph(cellCount); Debug.Log("polarGrid: creating graph (unity doesnt initialize it)"); } else { graph.Size = cellCount; } }
//[Range(1,10)] //public int nearJoint = 2; public void GenerateRandomGraph() { //平面にグラフを敷き、ボロノイ図を作成した後に //ボロノイ図のブロックをいくつかクラスタにし、 //クラスタの辺を道路とする var result = new GameObject($"GroundGraph-{count}"); var graph = result.AddComponent <BolonoiGraphBehaviour>(); List <Graph> all = graph.all; for (int i = 0; i < count; i++) { all.Add(PositionGraph.From(RandomPos())); } }
public void GenerateRandomGraph() { var result = new GameObject($"GeneratedGraph-{count}-{nearJoint}"); var graph = result.AddComponent <GraphBehaviour>(); graph.graph = PositionGraph.From(Vector3.Zero); Graph zero = graph.graph; List <Graph> all = graph.all; for (int i = 0; i < count; i++) { Graph g = PositionGraph.From(RandomPos()); zero += g; all.Add(g); } //ループしつつ最近点2個のみの接続とする for (int i = count - 1; 0 <= i; i--) { //pick var origin = zero[i]; //find2 var picks = zero.Where(A => A != origin && A != zero ) .OrderBy(B => { var d = Vector3.Distance( origin.Position, B.Position); return(d); } ) .Take(nearJoint) .ToList(); Debug.Log($"{origin.id} =>{string.Join(",", picks.Select(pick => pick.id).ToArray())}"); foreach (Graph pick in picks) { origin += pick; } } //zeroと縁を切る Debug.Log($"Zeroと縁切り {zero.Count}"); //zeroと近傍のみ接続 zero .Where(A => A != zero) .OrderByDescending(B => { var d = Vector3.Distance( zero.Position, B.Position); return(d); }) //近傍以外 .Take(zero.Count - nearJoint) .ToList() .ForEach(g => zero -= g); }
protected override void DrawWalls(WeaveRectGrid grid) { for (int i = 0; i != grid.height; ++i) { int north = tex.height - (i * cellSize); int south = north - cellSize; for (int j = 0; j < grid.width; ++j) { int west = j * cellSize; int east = west + cellSize; int x1 = west; int x2 = x1 + inset; int x4 = east; int x3 = x4 - inset; int y1 = north; int y2 = y1 - inset; int y4 = south; int y3 = y4 + inset; int vertex = grid.PositionToVertex(i, j); List <KeyValuePair <int, Line[]> > links = new List <KeyValuePair <int, Line[]> >(4); links.Add( new KeyValuePair <int, Line[]>( grid.NorthOf(vertex), new Line[3] { new Line(x2, y1, x2, y2), new Line(x3, y1, x3, y2), new Line(x2, y2, x3, y2) } ) ); links.Add( new KeyValuePair <int, Line[]>( grid.SouthOf(vertex), new Line[3] { new Line(x2, y3, x2, y4), new Line(x3, y3, x3, y4), new Line(x2, y3, x3, y3) } ) ); links.Add( new KeyValuePair <int, Line[]>( grid.WestOf(vertex), new Line[3] { new Line(x1, y3, x2, y3), new Line(x1, y2, x2, y2), new Line(x2, y2, x2, y3) } ) ); links.Add( new KeyValuePair <int, Line[]>( grid.EastOf(vertex), new Line[3] { new Line(x3, y3, x4, y3), new Line(x3, y2, x4, y2), new Line(x3, y2, x3, y3) } ) ); foreach (var dir_lines in links) { int neighbor = dir_lines.Key; int linkedVertex = -1; Line[] lines = dir_lines.Value; if (grid.Graph.AreLinked(vertex, neighbor)) { linkedVertex = neighbor; } else if (neighbor != -1) { List <int> vLinks = grid.Graph.LinksOf(vertex); System.Predicate <int> isTunnelLink = linkd => linkd != neighbor && grid.IsTunnel(linkd) && grid.Graph[neighbor] == grid.Graph[linkd]; int tunnelIndex = vLinks.FindIndex(isTunnelLink); if (tunnelIndex != -1) { linkedVertex = vLinks[tunnelIndex]; } } if (linkedVertex != -1) { tex.Line(lines[0].a, lines[0].b, wallColor); tex.Line(lines[1].a, lines[1].b, wallColor); } else { tex.Line(lines[2].a, lines[2].b, wallColor); } } } } PositionGraph graph = grid.Graph; for (int tunnelVertex = grid.width * grid.height; tunnelVertex != grid.Graph.Size; ++tunnelVertex) { Position position = grid.Graph[tunnelVertex]; int north = tex.height - (position.row * cellSize); int south = north - cellSize; int west = position.col * cellSize; int east = west + cellSize; int x1 = west; int x2 = x1 + inset; int x4 = east; int x3 = x4 - inset; int y1 = north; int y2 = y1 - inset; int y4 = south; int y3 = y4 + inset; bool isVerticalPassage = graph.AreLinked(tunnelVertex, grid.NorthOf(tunnelVertex)) || graph.AreLinked(tunnelVertex, grid.SouthOf(tunnelVertex)); if (isVerticalPassage) { // north tex.Line(new Vector2Int(x2, y3), new Vector2Int(x2, y4), wallColor); tex.Line(new Vector2Int(x3, y3), new Vector2Int(x3, y4), wallColor); // south tex.Line(new Vector2Int(x2, y1), new Vector2Int(x2, y2), wallColor); tex.Line(new Vector2Int(x3, y1), new Vector2Int(x3, y2), wallColor); } else // horizontal passage { // w tex.Line(new Vector2Int(x1, y3), new Vector2Int(x2, y3), wallColor); tex.Line(new Vector2Int(x1, y2), new Vector2Int(x2, y2), wallColor); // e tex.Line(new Vector2Int(x3, y3), new Vector2Int(x4, y3), wallColor); tex.Line(new Vector2Int(x3, y2), new Vector2Int(x4, y2), wallColor); } } }