Skip to content

Instantly share code, notes, and snippets.

@Robijnvogel
Created April 17, 2018 13:13
Show Gist options
  • Save Robijnvogel/eafd64cc61bd9f105a29f2361b1a7c4a to your computer and use it in GitHub Desktop.
Save Robijnvogel/eafd64cc61bd9f105a29f2361b1a7c4a to your computer and use it in GitHub Desktop.
public List<Cell> runBFS2D(Vec2D startCoords) {
List<Cell> positiveMatches = new ArrayList();
final String queryProperty = getProperty(startCoords);
Cell firstCell = new Cell(startCoords);
positiveMatches.add(firstCell);
for (int i = 0; i < positiveMatches.size(); i++) {
Cell thisCell = positiveMatches.get(i);
for (int direction = 0; direction < 8; direction++) {
if (!thisCell.hasNeighbour(direction)) {
Cell newCell = new Cell(newCoords);
thisCell.setNeighbour(newCell, direction);
newCell.setNeighbour(thisCell, (direction + 4) % 8);
if (direction > 0) {
Cell oldCell = thisCell.getNeighbour(direction - 1);
int oldNewDir = (((direction + 1) / 2) * 2 + 1) % 8; //since we are dealing with integers, the "/ 2" gets rounded down.
oldCell.setNeighbour(newCell, oldNewDir);
newCell.setNeighbour(oldCell, (oldNewDir + 4) % 8);
if (direction == 7) {
Cell veryOldCell = thisCell.getNeighbour(0);
veryOldCell.setNeighbour(newCell, 5);
newCell.setNeighbour(veryOldCell, 1);
}
}
int xDiff;
int yDiff;
if (direction < 3) {
xDiff = -1;
} else if (direction % 4 == 3) {
xDiff = 0;
} else {
xDiff = 1;
}
int yCounter = (direction + 2) % 8;
if (yCounter < 3) {
yDiff = -1;
} else if (yCounter % 4 == 3) {
yDiff = 0;
} else {
yDiff = 1;
}
Vec2D newCoords = thisCell.getCoords().transform(xDiff, yDiff);
if (queryProperty.equals(getProperty(newCoords))) {
positiveMatches.add(newCell);
}
}
}
}
return positiveMatches;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment