Skip to content

Commit

Permalink
QuadTreeFloat, fixed nearest() in some cases and use larger of the tr…
Browse files Browse the repository at this point in the history
…ee's width/height when the point is outside the tree.
  • Loading branch information
NathanSweet committed Dec 16, 2020
1 parent 9cd2a0b commit c175c6a
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions gdx/src/com/badlogic/gdx/utils/QuadTreeFloat.java
Expand Up @@ -158,8 +158,8 @@ private void query (float centerX, float centerY, float radiusSqr, float rectX,

/** @param result For the entry nearest to the specified point, the value, x, y, and square of the distance to the value are
* added to this array after it is cleared. See {@link #VALUE}, {@link #X}, {@link #Y}, and {@link #DISTSQR}.
* @return false if no entry was found because the quad tree was empty or the specified point is farther than twice the quad
* tree's width from an entry. If false is returned the result array is empty. */
* @return false if no entry was found because the quad tree was empty or the specified point is farther than the larger of the
* quad tree's width or height from an entry. If false is returned the result array is empty. */
public boolean nearest (float x, float y, FloatArray result) {
// Find nearest value in a cell that contains the point.
result.clear();
Expand All @@ -169,7 +169,11 @@ public boolean nearest (float x, float y, FloatArray result) {
result.add(Float.POSITIVE_INFINITY);
findNearestInternal(x, y, result);
float nearValue = result.first(), nearX = result.get(1), nearY = result.get(2), nearDist = result.get(3);
if (nearDist == Float.POSITIVE_INFINITY) nearDist = width * width;
boolean found = nearDist != Float.POSITIVE_INFINITY;
if (!found) {
nearDist = Math.max(width, height);
nearDist *= nearDist;
}

// Check for a nearer value in a neighboring cell.
result.clear();
Expand All @@ -183,7 +187,7 @@ public boolean nearest (float x, float y, FloatArray result) {
nearY = result.get(i - 1);
}
}
if (result.isEmpty()) return false;
if (!found && result.isEmpty()) return false;
result.clear();
result.add(nearValue);
result.add(nearX);
Expand Down

0 comments on commit c175c6a

Please sign in to comment.