private void AddCirularInclusion(Range tempRange, Point center) { var pointsInside = GetPointsInsideCircle(Size / 2, center); foreach (var point in pointsInside) { if (point.X < tempRange.Width && point.X > 0 && point.Y < tempRange.Height && point.Y > 0) { if (!SpecialId.IsIdSpecial(tempRange.GrainsArray[point.X, point.Y].Id) || tempRange.GrainsArray[point.X, point.Y].Id == 0) { tempRange.GrainsArray[point.X, point.Y].Color = Color.Black; tempRange.GrainsArray[point.X, point.Y].Id = Convert.ToInt32(SpecialId.Id.Inclusion); } } } }
private void AddSquareInclusion(Range tempRange, Point center) { int halfA = (Size / 2); for (int x = center.X - halfA; (x <= center.X + halfA && x < tempRange.Width && x > 0); x++) { for (int y = center.Y - halfA; (y <= center.Y + halfA && y < tempRange.Height && y > 0); y++) { if (!SpecialId.IsIdSpecial(tempRange.GrainsArray[x, y].Id) || tempRange.GrainsArray[x, y].Id == 0) { tempRange.GrainsArray[x, y].Color = Color.Black; tempRange.GrainsArray[x, y].Id = Convert.ToInt32(SpecialId.Id.Inclusion); } } } }
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); }
public Range Grow(Range prevRange, MainProperties properties) { var currRange = new Range(prevRange.Width, prevRange.Height); StructureHandler.GrainsArrayInit(currRange); StructureHandler.UpdateInsideCircle(currRange); foreach (var p in StructureHandler.PointsArea) { if (prevRange.GrainsArray[p.X, p.Y].Id != (int)SpecialId.Id.Empty) { // change if there is already some color (not white) currRange.GrainsArray[p.X, p.Y].Id = prevRange.GrainsArray[p.X, p.Y].Id; currRange.GrainsArray[p.X, p.Y].Color = prevRange.GrainsArray[p.X, p.Y].Color; } else { if (properties.NeighbourhoodType != NeighbourhoodType.Moore2) { if (properties.NeighbourhoodType == NeighbourhoodType.Moore) { NeighborhoodList = TakeMooreNeighbourhood(p.X, p.Y, prevRange.GrainsArray); } else { NeighborhoodList = TakeNeumannNeighbourhood(p.X, p.Y, prevRange.GrainsArray); } var MostOfCells = NeighborhoodList.Where(g => !SpecialId.IsSpecialId(g.Id)); if (MostOfCells.Any()) { // assign grain which are the most in the list of neighborhood currRange.GrainsArray[p.X, p.Y] = MostOfCells.GroupBy(g => g.Id).OrderByDescending(g => g.Count()) .Select(g => g.First()).First(); } } else { // MOORE 2 IsGrainUpdate = false; // rule 1 - ordinary Moore NeighborhoodList = TakeMooreNeighbourhood(p.X, p.Y, prevRange.GrainsArray); MostOfCells = NeighborhoodList.Where(g => !SpecialId.IsSpecialId(g.Id)) .GroupBy(g => g.Id).OrderByDescending(g => g.Count()); if (MostOfCells.Any()) { if (MostOfCells.First().Count() >= 5 && MostOfCells.First().Count() <= 8) { currRange.GrainsArray[p.X, p.Y] = MostOfCells.Select(g => g.First()).First(); } else { // rule 2 - nearest Moore (Von Neumann) NeighborhoodList = TakeNearestMooreNeighbourhood(p.X, p.Y, prevRange.GrainsArray); MostOfCells = NeighborhoodList.Where(g => !SpecialId.IsSpecialId(g.Id)) .GroupBy(g => g.Id).OrderByDescending(g => g.Count()); if (MostOfCells.Any()) { if (MostOfCells.First().Count() == 3) { currRange.GrainsArray[p.X, p.Y] = MostOfCells.Select(g => g.First()).First(); IsGrainUpdate = true; } } if (!IsGrainUpdate) { // rule 3 - further Moore NeighborhoodList = TakeFurtherMooreNeighbourhood(p.X, p.Y, prevRange.GrainsArray); MostOfCells = NeighborhoodList.Where(g => !SpecialId.IsSpecialId(g.Id)) .GroupBy(g => g.Id).OrderByDescending(g => g.Count()); if (MostOfCells.Any()) { if (MostOfCells.First().Count() == 3) { currRange.GrainsArray[p.X, p.Y] = MostOfCells.Select(g => g.First()).First(); IsGrainUpdate = true; } } if (!IsGrainUpdate) { // rule 4 - ordinary Moore with probability NeighborhoodList = TakeMooreNeighbourhood(p.X, p.Y, prevRange.GrainsArray); MostOfCells = NeighborhoodList.Where(g => !SpecialId.IsSpecialId(g.Id)) .GroupBy(g => g.Id).OrderByDescending(g => g.Count()); if (MostOfCells.Any() && Random.Next(0, 100) <= properties.CurrGrowthProbability) { currRange.GrainsArray[p.X, p.Y] = MostOfCells.Select(g => g.First()).First(); IsGrainUpdate = true; } } } } } } } } return(currRange); }
public Range Grow(Range tempRange, InitNucleons nucleons) { List <Grain> neighbourhood = new List <Grain>(); List <Point> pointsCoordinates = new List <Point>(); List <Point> neighbourhoodPoints = new List <Point>(); Grain centerGrain; int EnergyAfter, EnergyBefore, EnergyDelta, randomNumberOfList; if (nucleons.IsEnable == true) { for (int i = 1; i < tempRange.Width - 1; i++) { for (int j = 1; j < tempRange.Height - 1; j++) { neighbourhood = TakeNeumannNeighbourhood(i, j, tempRange.GrainsArray); if (neighbourhood.Where(g => g.Id == -5).Select(g => g.Id).Count() > 0 && tempRange.GrainsArray[i, j].Id != -5) { pointsCoordinates.Add(new Point(i, j)); } } } pointsCoordinates.OrderBy(a => Random.Next()); while (pointsCoordinates.Count > 0) { EnergyAfter = 0; EnergyBefore = 0; EnergyDelta = 0; //step 1 centerGrain = tempRange.GrainsArray[pointsCoordinates.First().X, pointsCoordinates.First().Y]; //step 2 neighbourhood = TakeMooreNeighbourhood(pointsCoordinates.First().X, pointsCoordinates.First().Y, tempRange.GrainsArray); EnergyBefore = neighbourhood.Where(g => !SpecialId.IsIdSpecial(g.Id) && g.Id != centerGrain.Id) .Select(g => g.Id).Count() + centerGrain.Energy_H; //step 3 randomNumberOfList = Random.Next(InitStructures.AllGrainsTypes.Length); centerGrain = InitStructures.AllGrainsTypes[randomNumberOfList]; EnergyAfter = neighbourhood.Where(g => !SpecialId.IsIdSpecial(g.Id) && g.Id != centerGrain.Id) .Select(g => g.Id).Count(); //step 4 EnergyDelta = EnergyAfter - EnergyBefore; //step 5 if (EnergyDelta <= 0) { tempRange.GrainsArray[pointsCoordinates.First().X, pointsCoordinates.First().Y] = neighbourhood.Where(g => g.Id == (int)SpecialId.Id.Nucleon).OrderBy(a => Random.Next()).First(); } pointsCoordinates.RemoveAt(0); } } else { for (int i = 1; i < tempRange.Width - 1; i++) { for (int j = 1; j < tempRange.Height - 1; j++) { pointsCoordinates.Add(new Point(i, j)); } } pointsCoordinates.OrderBy(a => Random.Next()); while (pointsCoordinates.Count > 0) { EnergyAfter = 0; EnergyBefore = 0; EnergyDelta = 0; //step 1 centerGrain = tempRange.GrainsArray[pointsCoordinates.First().X, pointsCoordinates.First().Y]; //step 2 neighbourhood = TakeMooreNeighbourhood(pointsCoordinates.First().X, pointsCoordinates.First().Y, tempRange.GrainsArray); neighbourhoodPoints = TakeMooreNeighbourhood(pointsCoordinates.First().X, pointsCoordinates.First().Y); EnergyBefore = neighbourhood.Where(g => (g.Id != centerGrain.Id)) .Select(g => g.Id).Count(); //step 3 randomNumberOfList = Random.Next(InitStructures.AllGrainsTypes.Length); centerGrain = InitStructures.AllGrainsTypes[randomNumberOfList]; EnergyAfter = neighbourhood.Where(g => (g.Id != centerGrain.Id)) .Select(g => g.Id).Count(); //step 4 EnergyDelta = EnergyAfter - EnergyBefore; //step 5 if (EnergyDelta <= 0 && !SpecialId.IsIdSpecial(tempRange.GrainsArray[pointsCoordinates.First().X, pointsCoordinates.First().Y].Id)) { tempRange.GrainsArray[pointsCoordinates.First().X, pointsCoordinates.First().Y] = centerGrain; } pointsCoordinates.RemoveAt(0); } } UpdateBitmap(tempRange); return(tempRange); }