Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        public static int FindNumberOfObjectsRecursive(bool[,] picture)
        {
            List <BitmapObject> bitmaps = new List <BitmapObject>();

            bool[,] visited = new bool[picture.GetLength(0), picture.GetLength(1)];

            for (int i = 0; i < picture.GetLength(0); i++)
            {
                for (int j = 0; j < picture.GetLength(1); j++)
                {
                    //skip false and visited values
                    if (!picture[i, j] || visited[i, j])
                    {
                        continue;
                    }

                    var          currentCoordinate = new Tuple <int, int>(i, j);
                    BitmapObject bitmapObject      = new BitmapObject();
                    FindAdjacentNodes(currentCoordinate, bitmapObject, picture, visited);
                    bitmaps.Add(bitmapObject);
                }
            }


            return(bitmaps.Count);
        }
Ejemplo n.º 3
0
        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;
        }
Ejemplo n.º 4
0
        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);
            }
        }
Ejemplo n.º 5
0
 public bool IsAdjacent(BitmapObject obj)
 {
     foreach (var c in obj.GetCoordinates())
     {
         if (this.IsAdjacent(new Tuple<int, int>(c.Item1, c.Item2)))
             return true;
     }
     return false;
 }
Ejemplo n.º 6
0
 public bool IsAdjacent(BitmapObject obj)
 {
     foreach (var c in obj.GetCoordinates())
     {
         if (this.IsAdjacent(new Tuple <int, int>(c.Item1, c.Item2)))
         {
             return(true);
         }
     }
     return(false);
 }
Ejemplo n.º 7
0
        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);
            }
        }
Ejemplo n.º 8
0
        public static int FindNumberOfObjectsRecursive(bool[,] picture)
        {
            List<BitmapObject> bitmaps = new List<BitmapObject>();
            bool[,] visited = new bool[picture.GetLength(0), picture.GetLength(1)];

            for (int i = 0; i < picture.GetLength(0); i++)
            {
                for (int j = 0; j < picture.GetLength(1); j++)
                {
                    //skip false and visited values
                    if (!picture[i, j] || visited[i, j]) continue;

                    var currentCoordinate = new Tuple<int, int>(i, j);
                    BitmapObject bitmapObject = new BitmapObject();
                    FindAdjacentNodes(currentCoordinate, bitmapObject, picture, visited);
                    bitmaps.Add(bitmapObject);
                }
            }

            return bitmaps.Count;
        }