Browse Source

Fix incorrect Rect2i calculations: intersects and encloses
Clarify expand documentation

Markus Sauermann 3 years ago
parent
commit
23a4fe5b27
3 changed files with 13 additions and 7 deletions
  1. 6 6
      core/math/rect2.h
  2. 1 1
      doc/classes/Rect2i.xml
  3. 6 0
      tests/core/math/test_rect2.h

+ 6 - 6
core/math/rect2.h

@@ -382,16 +382,16 @@ struct _NO_DISCARD_ Rect2i {
 			ERR_PRINT("Rect2i size is negative, this is not supported. Use Rect2i.abs() to get a Rect2i with a positive size.");
 			ERR_PRINT("Rect2i size is negative, this is not supported. Use Rect2i.abs() to get a Rect2i with a positive size.");
 		}
 		}
 #endif
 #endif
-		if (position.x > (p_rect.position.x + p_rect.size.width)) {
+		if (position.x >= (p_rect.position.x + p_rect.size.width)) {
 			return false;
 			return false;
 		}
 		}
-		if ((position.x + size.width) < p_rect.position.x) {
+		if ((position.x + size.width) <= p_rect.position.x) {
 			return false;
 			return false;
 		}
 		}
-		if (position.y > (p_rect.position.y + p_rect.size.height)) {
+		if (position.y >= (p_rect.position.y + p_rect.size.height)) {
 			return false;
 			return false;
 		}
 		}
-		if ((position.y + size.height) < p_rect.position.y) {
+		if ((position.y + size.height) <= p_rect.position.y) {
 			return false;
 			return false;
 		}
 		}
 
 
@@ -405,8 +405,8 @@ struct _NO_DISCARD_ Rect2i {
 		}
 		}
 #endif
 #endif
 		return (p_rect.position.x >= position.x) && (p_rect.position.y >= position.y) &&
 		return (p_rect.position.x >= position.x) && (p_rect.position.y >= position.y) &&
-				((p_rect.position.x + p_rect.size.x) < (position.x + size.x)) &&
-				((p_rect.position.y + p_rect.size.y) < (position.y + size.y));
+				((p_rect.position.x + p_rect.size.x) <= (position.x + size.x)) &&
+				((p_rect.position.y + p_rect.size.y) <= (position.y + size.y));
 	}
 	}
 
 
 	_FORCE_INLINE_ bool has_no_area() const {
 	_FORCE_INLINE_ bool has_no_area() const {

+ 1 - 1
doc/classes/Rect2i.xml

@@ -70,7 +70,7 @@
 			<return type="Rect2i" />
 			<return type="Rect2i" />
 			<argument index="0" name="to" type="Vector2i" />
 			<argument index="0" name="to" type="Vector2i" />
 			<description>
 			<description>
-				Returns a copy of this [Rect2i] expanded to include a given point.
+				Returns a copy of this [Rect2i] expanded so that the borders align with the given point.
 				[codeblocks]
 				[codeblocks]
 				[gdscript]
 				[gdscript]
 				# position (-3, 2), size (1, 1)
 				# position (-3, 2), size (1, 1)

+ 6 - 0
tests/core/math/test_rect2.h

@@ -439,6 +439,9 @@ TEST_CASE("[Rect2i] Enclosing") {
 	CHECK_MESSAGE(
 	CHECK_MESSAGE(
 			!Rect2i(0, 100, 1280, 720).encloses(Rect2i(-4000, -4000, 100, 100)),
 			!Rect2i(0, 100, 1280, 720).encloses(Rect2i(-4000, -4000, 100, 100)),
 			"encloses() with non-contained Rect2i should return the expected result.");
 			"encloses() with non-contained Rect2i should return the expected result.");
+	CHECK_MESSAGE(
+			Rect2i(0, 100, 1280, 720).encloses(Rect2i(0, 100, 1280, 720)),
+			"encloses() with identical Rect2i should return the expected result.");
 }
 }
 
 
 TEST_CASE("[Rect2i] Expanding") {
 TEST_CASE("[Rect2i] Expanding") {
@@ -557,6 +560,9 @@ TEST_CASE("[Rect2i] Intersection") {
 	CHECK_MESSAGE(
 	CHECK_MESSAGE(
 			!Rect2i(0, 100, 1280, 720).intersects(Rect2i(-4000, -4000, 100, 100)),
 			!Rect2i(0, 100, 1280, 720).intersects(Rect2i(-4000, -4000, 100, 100)),
 			"intersects() with non-enclosed Rect2i should return the expected result.");
 			"intersects() with non-enclosed Rect2i should return the expected result.");
+	CHECK_MESSAGE(
+			!Rect2i(0, 0, 2, 2).intersects(Rect2i(2, 2, 2, 2)),
+			"intersects() with adjacent Rect2i should return the expected result.");
 }
 }
 
 
 TEST_CASE("[Rect2i] Merging") {
 TEST_CASE("[Rect2i] Merging") {