private bool AreRegionsAdjacent(NavigationRegion a, NavigationRegion b) { return(a == b || a.Above(b) || a.Below(b) || a.LeftOf(b) || a.RightOf(b)); }
public List <NavigationRegion> ConstructNavigationRegionList(int[,] gridMap, int numRow, int numColumn) { // List of regions created according to gridMap List <NavigationRegion> result = GenerateInitialNavigationRegions(gridMap, numRow, numColumn); // The key is the region to split. Value is the region to split by. List <KeyValuePair <int, int> > splitsToDo = new List <KeyValuePair <int, int> >(); // Use this to give unique id's to each new region we add. int count = result.Count; // We assume there will always be splits to do. while (true) { // If a is adjacent to b, add a split that we want to do. foreach (NavigationRegion a in result) { foreach (NavigationRegion b in result) { if (a == b) { continue; } if ((a.RightOf(b) || a.LeftOf(b)) && (a.Row != b.Row || a.Height != b.Height)) { if (a.Height > b.Height) { splitsToDo.Add(new KeyValuePair <int, int>(a.Id, b.Id)); } else { splitsToDo.Add(new KeyValuePair <int, int>(b.Id, a.Id)); } } else if ((a.Above(b) || a.Below(b)) && (a.Column != b.Column || a.Width != b.Width)) { if (a.Width > b.Width) { splitsToDo.Add(new KeyValuePair <int, int>(a.Id, b.Id)); } else { splitsToDo.Add(new KeyValuePair <int, int>(b.Id, a.Id)); } } } } // Since there are no more splits to do, we exit the loop. if (splitsToDo.Count == 0) { break; } else { // Keep popping splits off the top of our splits to do list. // If both regions are still in the list, the split can be done (it is // possible we request a split to a region that was already split). // If the split is possible, we do it, remove the split region from // the list, and add the resulting components to the final list of regions. while (splitsToDo.Count > 0) { KeyValuePair <int, int> split = splitsToDo[0]; splitsToDo.RemoveAt(0); NavigationRegion toSplit = GetRegion(split.Key, result); NavigationRegion splitBy = GetRegion(split.Value, result); if (toSplit != null && splitBy != null) { List <NavigationRegion> splitResult; if (toSplit.Above(splitBy) || toSplit.Below(splitBy)) { splitResult = VerticalSplit(toSplit, splitBy); } else { splitResult = HorizontalSplit(toSplit, splitBy); } result.Remove(toSplit); foreach (NavigationRegion component in splitResult) { component.Id = count; count++; result.Add(component); } } } } } // Fix the id's to be zero-based. This makes them much easier to manage when // building an adjacency matrix for them. for (int i = 0; i < result.Count; i++) { result[i].Id = i; } return(result); }