Program: 140918
Project: 140918
Buggy source:
/** 
 * Computes the branch length of grid point 1 at x1/y1 with point 2 at x2/y2. The branch length is the distance along the flow path starting at point 1 to the point of intersection with the flow path starting at point 2. The length of the flow path of point 2 does not influence the branch length. If point 1 is on the flow path starting at point 2, then the branch length is 0. If the flow path of point 1 ends in a sink or at the border of the grid, then the branch length is the length of the flow line to this end point. If the flow path starting at point 2 ends in a sink or at the border of the grid, then the flow line of point 1 is followed until it ends in a sink or at the border of the grid, and the branch length is the length of this flow line.
 * @param travelled an empty set for storing the points of both flow paths
 * @param dir the directions of flow (using D8 implementation)
 * @param x1 x of point 1 for which the branch length is computed
 * @param y1 y of point 1 for which the branch length is computed
 * @param x2 x of point 2
 * @param y2 y of point 2
 * @return branch length for vertex point 1 at x1/y1. Units are cells.
 */
private float branchLength(HashMap<Integer,Float> travelled,Grid dir,int x1,int y1,int x2,int y2){
}

Fixed version (ground truth):
/** 
 * Computes the branch length of grid point 1 at x1/y1 with point 2 at x2/y2. The branch length is the distance along the flow path starting at point 1 to the point of intersection with the flow path starting at point 2. The length of the flow path of point 2 does not influence the branch length. If point 1 is on the flow path starting at point 2, then the branch length is 0. If the flow path of point 1 ends in a sink or at the border of the grid, then the branch length is the length of the flow line to this end point. If the flow path starting at point 2 ends in a sink or at the border of the grid, then the flow line of point 1 is followed until it ends in a sink or at the border of the grid, and the branch length is the length of this flow line.
 * @param travelled an empty set for storing the points of both flow paths
 * @param dir the directions of flow (using D8 implementation)
 * @param x1 x of point 1 for which the branch length is computed
 * @param y1 y of point 1 for which the branch length is computed
 * @param x2 x of point 2
 * @param y2 y of point 2
 * @return branch length for vertex point 1 at x1/y1. Units are cells.
 */
private float branchLength(HashMap<Integer,Float> travelled,Grid dir,int x1,int y1,int x2,int y2){
  float length=0;
  final int nCols=dir.getCols();
  final int nRows=dir.getRows();
  int direction1, direction2;
  boolean path2Ended=false;
  int idx1, idx2;
  while (true) {
    idx1=x1 + y1 * nCols;
    if (travelled.containsKey(idx1)) {
      return length;
    }
    travelled.put(idx1,length);
    if (!path2Ended) {
      idx2=x2 + y2 * nCols;
      if (travelled.containsKey(idx2)) {
        return travelled.get(idx2);
      }
      travelled.put(idx2,null);
    }
    direction1=(int)dir.getValue(x1,y1);
    x1+=this.dX[direction1];
    y1+=this.dY[direction1];
    if (direction1 % 2 == 0) {
      length+=1.4142136;
    }
 else {
      length+=1;
    }
    if (direction1 == 4 || x1 == 0 || y1 == 0 || x1 == nCols - 1 || y1 == nRows - 1) {
      return length;
    }
    if (!path2Ended) {
      direction2=(int)dir.getValue(x2,y2);
      x2+=this.dX[direction2];
      y2+=this.dY[direction2];
      path2Ended=direction2 == 4;
      path2Ended|=x2 == 0 || y2 == 0 || x2 == nCols - 1 || y2 == nRows - 1;
    }
  }
}