protected void ConnectEdge(ShapeNode n, ref int index, List <Node> nl) { // Attempts to connect an edge(index) on a ShapeNode(n) against all edges in a list of Nodes(nl) // Subdivides matching edges accordingly if the intersection is less than the entire length of one of the edges. // Note: Subdivision code is fairly basic at the moment. Some edge cases are not covered. if (n.LinkList[index] != null) { return; } Vector2D edge = n.GetEdge(index); Vector2D norm1 = edge.Normalized(); foreach (ShapeNode n2 in nl) { if (n2 != n) { for (int index2 = 0; index2 < n2.points.Count; index2++) { Vector2D edge2 = n2.GetAntiClockwiseEdge(index2); Vector2D norm2 = edge2.Normalized(); if (edge.a == edge2.a && norm1 == norm2) { NodeLink nodeLink = ShapeNode.ConnectEdges(n, index, n2, index2); if (edge.Length() > edge2.Length()) { n.AddPoint(index + 1, edge2.b); index--; } if (edge2.Length() > edge.Length()) { n2.AddPoint(index2 + 1, edge.b); n2.LinkList[index2 + 1] = n2.LinkList[index2]; n2.LinkList[index2] = null; ConnectEdge(n2, ref index2, nl); } return; } } } } }
public void Initialize(int gridWidth, int gridHeight, int weavePercent, float weighting) { horizontalWeighting = weighting; verticalWeighting = 1.0f - weighting; // Create grid RectNode[,] basicGrid = new RectNode[gridWidth, gridHeight]; // Create square nodes SizeF cellSize = new SizeF(1, 1); Point p = new Point(); for (p.X = 0; p.X < gridWidth; p.X++) { for (p.Y = 0; p.Y < gridHeight; p.Y++) { RectNode node = new RectNode(p, cellSize); nodeDict.Add(node, node.LinkList); basicGrid[p.X, p.Y] = node; } } // Connect nodes for (p.X = 1; p.X < gridWidth; p.X++) { for (p.Y = 0; p.Y < gridHeight; p.Y++) { ShapeNode.ConnectEdges(basicGrid[p.X - 1, p.Y], RectNode.RightIndex, basicGrid[p.X, p.Y], RectNode.LeftIndex, horizontalWeighting); } } for (p.X = 0; p.X < gridWidth; p.X++) { for (p.Y = 1; p.Y < gridHeight; p.Y++) { ShapeNode.ConnectEdges(basicGrid[p.X, p.Y - 1], RectNode.BottomIndex, basicGrid[p.X, p.Y], RectNode.TopIndex, verticalWeighting); } } boundingBox.ptMin.X = 0; boundingBox.ptMin.Y = 0; boundingBox.ptMax.X = gridWidth; boundingBox.ptMax.Y = gridHeight; // Add Weaves int numWeaveNodes = (gridWidth - 2) * (gridHeight - 2); if (numWeaveNodes > 0) { for (p.X = 1; p.X < (gridWidth - 1); p.X++) { for (p.Y = 1; p.Y < (gridHeight - 1); p.Y++) { int chance = r.Next(0, 100); if (chance < weavePercent) { NodeLink link; Node gridNode = basicGrid[p.X, p.Y]; Node upperNode = gridNode.LinkList[0].Other(gridNode); Node leftNode = gridNode.LinkList[1].Other(gridNode); Node lowerNode = gridNode.LinkList[2].Other(gridNode); Node rightNode = gridNode.LinkList[3].Other(gridNode); RectNode weaveNode = new RectNode(p, cellSize); nodeDict.Add(weaveNode, weaveNode.LinkList); weaveNode.LinkList[0] = gridNode.LinkList[0]; gridNode.LinkList[0] = null; weaveNode.LinkList[2] = gridNode.LinkList[2]; gridNode.LinkList[2] = null; link = weaveNode.LinkList[0]; if (link.a == gridNode) { link.a = weaveNode; } else { link.b = weaveNode; } link = weaveNode.LinkList[2]; if (link.a == gridNode) { link.a = weaveNode; } else { link.b = weaveNode; } link = new NodeLink(upperNode, lowerNode); upperNode.LinkList[2] = link; lowerNode.LinkList[0] = link; link.visited = true; link = new NodeLink(leftNode, rightNode); leftNode.LinkList[3] = link; rightNode.LinkList[1] = link; link.visited = true; gridNode.LinkList[1].visited = true; gridNode.LinkList[3].visited = true; weaveNode.LinkList[0].visited = true; weaveNode.LinkList[2].visited = true; } } } } // Add start and end nodes Node startNode = basicGrid[0, 0]; startNode.LinkList[3] = new NodeLink(startNode, null); startNode.LinkList[3].visited = true; Node endNode = basicGrid[gridWidth - 1, gridHeight - 1]; endNode.LinkList[1] = new NodeLink(endNode, null); endNode.LinkList[1].visited = true;; }