Example #1
0
        public InitBoundaries(MainProperties properties)
        {
            ClearBoundaries        = new Range(properties.RangeWidth, properties.RangeHeight);
            BoundariesAll          = new Range(properties.RangeWidth, properties.RangeHeight);
            BoundariesSelected     = new Range(properties.RangeWidth, properties.RangeHeight);
            BoundariesSingleSelect = new Range(properties.RangeWidth, properties.RangeHeight);

            InitStructures.AddBlackBorder(ClearBoundaries);
            InitStructures.AddBlackBorder(BoundariesAll);
            InitStructures.AddBlackBorder(BoundariesSelected);
            InitStructures.AddBlackBorder(BoundariesSingleSelect);

            // init grains array by default values
            for (int i = 1; i < properties.RangeWidth - 1; i++)
            {
                for (int j = 1; j < properties.RangeHeight - 1; j++)
                {
                    ClearBoundaries.GrainsArray[i, j] = new Grain()
                    {
                        Id       = 0,
                        Color    = Color.White,
                        Energy_H = 0
                    };
                    BoundariesAll.GrainsArray[i, j] = new Grain()
                    {
                        Id       = 0,
                        Color    = Color.White,
                        Energy_H = 0
                    };
                    BoundariesSelected.GrainsArray[i, j] = new Grain()
                    {
                        Id       = 0,
                        Color    = Color.White,
                        Energy_H = 0
                    };
                    BoundariesSingleSelect.GrainsArray[i, j] = new Grain()
                    {
                        Id       = 0,
                        Color    = Color.White,
                        Energy_H = 0
                    };
                }
            }
        }
Example #2
0
        public Range Grow(Range prevRange, MainProperties properties)
        {
            var currRange = new Range(prevRange.Width, prevRange.Height, true);

            InitStructures.AddBlackBorder(currRange);

            var isGrowthMoore2 = properties.NeighbourhoodType == NeighbourhoodType.Moore2 ? true : false;

            List <Grain> neighbourhood = new List <Grain>();

            for (int i = 1; i < prevRange.Width - 1; i++)
            {
                for (int j = 1; j < prevRange.Height - 1; j++)
                {
                    if (prevRange.GrainsArray[i, j].Id != (int)SpecialId.Id.Empty)
                    {
                        // just init if there is already some color (not white)
                        currRange.GrainsArray[i, j] = prevRange.GrainsArray[i, j];
                    }
                    else
                    {
                        if (!isGrowthMoore2)
                        {
                            // ordinary types of growth - list of Moore or Neuman neighbourhood
                            switch (properties.NeighbourhoodType)
                            {
                            case NeighbourhoodType.Moore:
                                neighbourhood = TakeMooreNeighbourhood(i, j, prevRange.GrainsArray);
                                break;

                            case NeighbourhoodType.Neumann:
                                neighbourhood = TakeNeumannNeighbourhood(i, j, prevRange.GrainsArray);
                                break;
                            }

                            var most = neighbourhood.Where(g => (!SpecialId.IsIdSpecial(g.Id)))
                                       .GroupBy(g => g.Id);

                            if (most.Any())
                            {
                                // assign grain which are the most in the list of neighborhoods
                                currRange.GrainsArray[i, j] = most.OrderByDescending(g => g.Count())
                                                              .Select(g => g.First()).First();
                            }
                            else
                            {
                                currRange.GrainsArray[i, j] = new Grain()
                                {
                                    Id    = (int)SpecialId.Id.Empty,
                                    Color = Color.White
                                };
                                currRange.IsFull = false;
                            }
                        }
                        else
                        {
                            // MOORE 2

                            var grainGrowth = false;

                            // rule 1 - ordinary moore
                            neighbourhood = TakeMooreNeighbourhood(i, j, prevRange.GrainsArray);

                            var most = neighbourhood.Where(g => (!SpecialId.IsIdSpecial(g.Id)))
                                       .GroupBy(g => g.Id);

                            if (most.Any())
                            {
                                most = most.OrderByDescending(g => g.Count());

                                if (most.First().Count() >= 5 && most.First().Count() <= 8)
                                {
                                    currRange.GrainsArray[i, j] = most.Select(g => g.First()).First();
                                    grainGrowth = true;
                                }
                                else
                                {
                                    // rule 2 - nearest moore
                                    neighbourhood = TakeNearestMooreNeighbourhood(i, j, prevRange.GrainsArray);

                                    most = neighbourhood.Where(g => (!SpecialId.IsIdSpecial(g.Id)))
                                           .GroupBy(g => g.Id);

                                    if (most.Any())
                                    {
                                        most = most.OrderByDescending(g => g.Count());
                                        if (most.First().Count() == 3)
                                        {
                                            currRange.GrainsArray[i, j] = most.Select(g => g.First()).First();
                                            grainGrowth = true;
                                        }
                                    }
                                    if (!grainGrowth)
                                    {
                                        // rule 3 - further moore
                                        neighbourhood = TakeFurtherMooreNeighbourhood(i, j, prevRange.GrainsArray);

                                        most = neighbourhood.Where(g => (!SpecialId.IsIdSpecial(g.Id)))
                                               .GroupBy(g => g.Id);

                                        if (most.Any())
                                        {
                                            most = most.OrderByDescending(g => g.Count());
                                            if (most.First().Count() == 3)
                                            {
                                                currRange.GrainsArray[i, j] = most.Select(g => g.First()).First();
                                                grainGrowth = true;
                                            }
                                        }
                                    }
                                    if (!grainGrowth)
                                    {
                                        // rule 4 - ordinary moore with probability
                                        neighbourhood = TakeMooreNeighbourhood(i, j, prevRange.GrainsArray);

                                        most = neighbourhood.Where(g => (!SpecialId.IsIdSpecial(g.Id)))
                                               .GroupBy(g => g.Id);

                                        var randomProbability = Random.Next(0, 100);
                                        if (most.Any() && (randomProbability <= properties.GrowthProbability))
                                        {
                                            currRange.GrainsArray[i, j] = most.OrderByDescending(g => g.Count())
                                                                          .Select(g => g.First()).First();
                                            grainGrowth = true;
                                        }
                                    }
                                }
                            }
                            if (!grainGrowth)
                            {
                                // if grain not exist
                                currRange.GrainsArray[i, j] = new Grain()
                                {
                                    Id    = (int)SpecialId.Id.Empty,
                                    Color = Color.White
                                };
                                currRange.IsFull = false;
                            }
                        }
                    }
                }
            }
            UpdateBitmap(currRange);
            return(currRange);
        }