public static int FindNumberOfObjectsIterative(bool[,] picture) { List <BitmapObject> bitmaps = new List <BitmapObject>(); for (int i = 0; i < picture.GetLength(0); i++) { for (int j = 0; j < picture.GetLength(1); j++) { //skip false values if (!picture[i, j]) { continue; } var currentCoordinate = new Tuple <int, int>(i, j); BitmapObject existingObject = bitmaps.FirstOrDefault(x => x.IsAdjacent(currentCoordinate)); if (existingObject != null) { existingObject.AddCoordinate(currentCoordinate); } else { BitmapObject bitmapObject = new BitmapObject(i, j); bitmaps.Add(bitmapObject); } } } var bitmapsToRemove = NumberOfAdjacentBitmaps(bitmaps); return(bitmaps.Count - bitmapsToRemove); }
public static void FindAdjacentNodes(Tuple<int,int> currentNode, BitmapObject obj, bool[,] picture, bool[,] visited) { visited[currentNode.Item1, currentNode.Item2] = true; if (!picture[currentNode.Item1, currentNode.Item2]) return; if (obj.ContainsCoordinate(currentNode)) return; obj.AddCoordinate(currentNode); var top = currentNode; var bottom = currentNode; var left = currentNode; var right = currentNode; if (currentNode.Item1 - 1 >= 0) { top = new Tuple<int, int>(currentNode.Item1 - 1, currentNode.Item2); FindAdjacentNodes(top, obj, picture, visited); } if (currentNode.Item1 + 1 < picture.GetLength(0)) { bottom = new Tuple<int, int>(currentNode.Item1 + 1, currentNode.Item2); FindAdjacentNodes(bottom, obj, picture, visited); } if (currentNode.Item2 - 1 >= 0) { left = new Tuple<int, int>(currentNode.Item1, currentNode.Item2 - 1); FindAdjacentNodes(left, obj, picture, visited); } if (currentNode.Item2 + 1 < picture.GetLength(1)) { right = new Tuple<int, int>(currentNode.Item1, currentNode.Item2 + 1); FindAdjacentNodes(right, obj, picture, visited); } }
public static void FindAdjacentNodes(Tuple <int, int> currentNode, BitmapObject obj, bool[,] picture, bool[,] visited) { visited[currentNode.Item1, currentNode.Item2] = true; if (!picture[currentNode.Item1, currentNode.Item2]) { return; } if (obj.ContainsCoordinate(currentNode)) { return; } obj.AddCoordinate(currentNode); var top = currentNode; var bottom = currentNode; var left = currentNode; var right = currentNode; if (currentNode.Item1 - 1 >= 0) { top = new Tuple <int, int>(currentNode.Item1 - 1, currentNode.Item2); FindAdjacentNodes(top, obj, picture, visited); } if (currentNode.Item1 + 1 < picture.GetLength(0)) { bottom = new Tuple <int, int>(currentNode.Item1 + 1, currentNode.Item2); FindAdjacentNodes(bottom, obj, picture, visited); } if (currentNode.Item2 - 1 >= 0) { left = new Tuple <int, int>(currentNode.Item1, currentNode.Item2 - 1); FindAdjacentNodes(left, obj, picture, visited); } if (currentNode.Item2 + 1 < picture.GetLength(1)) { right = new Tuple <int, int>(currentNode.Item1, currentNode.Item2 + 1); FindAdjacentNodes(right, obj, picture, visited); } }