--- dao/FuldDaekningWorker/src/geocode/kdtree/KDTree.java 2015/06/14 14:28:21 2585 +++ dao/FuldDaekningWorker/src/geocode/kdtree/KDTree.java 2015/07/13 10:16:41 2597 @@ -56,7 +56,28 @@ int currentIndex = items.size()/2; return new KDNode(createKDTree(new ArrayList(items.subList(0, currentIndex)), depth+1), createKDTree(new ArrayList(items.subList(currentIndex + 1, items.size())), depth+1), items.get(currentIndex)); } + + + private KDNode findNearest(KDNode currentNode, T search, int depth) { + int direction = search.getComparator(depth % 3).compare( search, currentNode.location ); + KDNode next = (direction < 0) ? currentNode.left : currentNode.right; + KDNode other = (direction < 0) ? currentNode.right : currentNode.left; + KDNode best = (next == null) ? currentNode : findNearest(next, search, depth + 1); // Go to a leaf + if ( currentNode.location.squaredDistance(search) < best.location.squaredDistance(search) ) { + best = currentNode; // Set best as required + } + if ( other != null ) { + if ( currentNode.location.axisSquaredDistance(search, depth % 3) < best.location.squaredDistance(search) ) { + KDNode possibleBest = findNearest( other, search, depth + 1 ); + if ( possibleBest.location.squaredDistance(search) < best.location.squaredDistance(search) ) { + best = possibleBest; + } + } + } + return best; // Work back up + } + /* backup af udgave med isNodeCompatible private KDNode findNearest(KDNode currentNode, T search, int depth) { int direction = search.getComparator(depth % 3).compare( search, currentNode.location ); KDNode next = (direction < 0) ? currentNode.left : currentNode.right; @@ -74,5 +95,5 @@ } } return best; // Work back up - } + }*/ }