From c175c6a9bd619fc01d5db74fde1fbeea5cf7bf8f Mon Sep 17 00:00:00 2001 From: NathanSweet Date: Wed, 16 Dec 2020 11:12:19 -0800 Subject: [PATCH] QuadTreeFloat, fixed nearest() in some cases and use larger of the tree's width/height when the point is outside the tree. --- gdx/src/com/badlogic/gdx/utils/QuadTreeFloat.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/gdx/src/com/badlogic/gdx/utils/QuadTreeFloat.java b/gdx/src/com/badlogic/gdx/utils/QuadTreeFloat.java index 13d821de777..7637ea7cd92 100644 --- a/gdx/src/com/badlogic/gdx/utils/QuadTreeFloat.java +++ b/gdx/src/com/badlogic/gdx/utils/QuadTreeFloat.java @@ -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(); @@ -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(); @@ -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);