private int[] findNearestArea(int cellX,int cellY,int spanX,int spanY,int[] direction,boolean[][] occupied,boolean blockOccupied[][],int[] result){
  final int[] bestXY=result != null ? result : new int[2];
  float bestDistance=Float.MAX_VALUE;
  int bestDirectionScore=Integer.MIN_VALUE;
  final int countX=mCountX;
  final int countY=mCountY;
  for (int y=0; y < countY - (spanY - 1); y++) {
    inner:     for (int x=0; x < countX - (spanX - 1); x++) {
      for (int i=0; i < spanX; i++) {
        for (int j=0; j < spanY; j++) {
          if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
            continue inner;
          }
        }
      }
      float distance=(float)Math.hypot(x - cellX,y - cellY);
      int[] curDirection=mTmpPoint;
      computeDirectionVector(x - cellX,y - cellY,curDirection);
      int curDirectionScore=direction[0] * curDirection[0] + direction[1] * curDirection[1];
      boolean exactDirectionOnly=false;
      boolean directionMatches=direction[0] == curDirection[0] && direction[0] == curDirection[0];
      if ((directionMatches || !exactDirectionOnly) && Float.compare(distance,bestDistance) < 0 || (Float.compare(distance,bestDistance) == 0 && curDirectionScore > bestDirectionScore)) {
        bestDistance=distance;
        bestDirectionScore=curDirectionScore;
        bestXY[0]=x;
        bestXY[1]=y;
      }
    }
  }
  if (bestDistance == Float.MAX_VALUE) {
    bestXY[0]=-1;
    bestXY[1]=-1;
  }
  return bestXY;
}

private int [ ] findNearestArea ( int cellX , int cellY , int spanX , int spanY , int [ ] direction , boolean [ ] [ ] occupied , boolean blockOccupied [ ] [ ] , int [ ] result ) { final int [ ] bestXY = result != null ? result : new int [ 2 ] ; float bestDistance = Float . MAX_VALUE ; int bestDirectionScore = Integer . MIN_VALUE ; final int countX = mCountX ; final int countY = mCountY ; for ( int y = 0 ; y < countY - ( spanY - 1 ) ; y ++ ) { inner : for ( int x = 0 ; x < countX - ( spanX - 1 ) ; x ++ ) { for ( int i = 0 ; i < spanX ; i ++ ) { for ( int j = 0 ; j < spanY ; j ++ ) { if ( occupied [ x + i ] [ y + j ] && ( blockOccupied == null || blockOccupied [ i ] [ j ] ) ) { continue inner ; } } } float distance = ( float ) Math . hypot ( x - cellX , y - cellY ) ; int [ ] curDirection = mTmpPoint ; computeDirectionVector ( x - cellX , y - cellY , curDirection ) ; int curDirectionScore = direction [ 0 ] * curDirection [ 0 ] + direction [ 1 ] * curDirection [ 1 ] ; boolean exactDirectionOnly = false ; boolean directionMatches = direction [ 0 ] == curDirection [ 0 ] && direction [ 0 ] == curDirection [ 0 ] ; if ( ( directionMatches || ! exactDirectionOnly ) && Float . compare ( distance , bestDistance ) < 0 || ( Float . compare ( distance , bestDistance ) == 0 && curDirectionScore > bestDirectionScore ) ) { bestDistance = distance ; bestDirectionScore = curDirectionScore ; bestXY [ 0 ] = x ; bestXY [ 1 ] = y ; } } } if ( bestDistance == Float . MAX_VALUE ) { bestXY [ 0 ] = - 1 ; bestXY [ 1 ] = - 1 ; } return bestXY ; }
find a vacant area that will fit the given bounds nearest the requested cell location , and will also weigh in a suggested direction vector of the desired location . this method computers distance based on unit grid distances , not pixel distances .
