private Node[,] generateNodes(int step) { Node[,] nodes = new Node[image_mask.Width / step + 1, image_mask.Height / step + 1]; for (int i = 0; i < nodes.GetLength(0); i++) { for (int j = 0; j < nodes.GetLength(1); j++) { nodes[i, j] = new Node(new Point(i * step, j * step)); } } return nodes; }
private Dictionary<Point, Node> generateMap(Node[,] nodes) { Dictionary<Point, Node> dict = new Dictionary<Point, Node>(); // Build the graph and dictionary for (int i = 1; i < nodes.GetLength(0) - 1; i++) { for (int j = 1; j < nodes.GetLength(1) - 1; j++) { nodes[i, j].addLink(nodes[i - 1, j - 1]); nodes[i, j].addLink(nodes[i + 1, j + 1]); nodes[i, j].addLink(nodes[i - 1, j + 1]); nodes[i, j].addLink(nodes[i + 1, j - 1]); nodes[i, j].addLink(nodes[i + 1, j]); nodes[i, j].addLink(nodes[i - 1, j]); nodes[i, j].addLink(nodes[i, j + 1]); nodes[i, j].addLink(nodes[i, j - 1]); dict.Add(nodes[i, j].getPoint(), nodes[i, j]); } } // Find points inside unallowed regions List<Point> removeList = new List<Point>(); foreach (KeyValuePair<Point, Node> d in dict) { int x = (int)d.Value.getPoint().X; int y = (int)d.Value.getPoint().Y; Color color = image_mask.GetPixel(x, y); if (color.R < 10 && color.G < 10 && color.B < 10) { removeList.Add(d.Key); } } // Remove points from graph and dictionary foreach (Point p in removeList) { Node node = dict[p]; foreach (Node n in node.getLinks()) { n.removeLink(node); } dict.Remove(p); } return dict; }