/** * heuristic function that caculate approximately the distance between * current point and the end point. * <p><strong>About distance : </strong>in AStar algorithm, the bigger is distance, * the better its performance can be. However, this appro. distance shouldn't beyond the * actual distance between the two points. Otherwise, it may can't get the best * solution. * <p>in 2D grid, the distance can set as width add length * <p>in 2D map, the distance should set as the segment's length between the * two points * * @param cur the current point * @param end the end point * @return the approximately distance between two points */ intheuristic(Point cur, Point end, int mode){ switch (mode) { //不进行估值,NONE是定义的常变量 case AStar.NONE: return0; //网格图 case AStar.GRID: return Math.abs(end.getRow() - cur.getRow()) + Math.abs(end.getCol() - cur.getCol()); //一般的2D平面 case AStar.EUCLID: doubledx= cur.getRow() - end.getRow(); doubledy= cur.getCol() - end.getCol(); return (int)Math.sqrt(dx * dx + dy * dy); default: return -1; } }