-
-
Notifications
You must be signed in to change notification settings - Fork 21.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix incorrect Rect2i calculations: intersects and encloses #57469
Conversation
6de2c85
to
822a875
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me.
Just to note that this exclusive definition is the usual way of handling integer bounds in my experience. It is a matter of definition of course, but it's the same as an integer range in a for loop being:
includes 0 to 9, but not 10. One fun thing to mention with the plus one in the |
core/math/rect2.h
Outdated
if (p_vector.x > end.x) { | ||
end.x = p_vector.x; | ||
if (p_vector.x >= end.x) { | ||
end.x = p_vector.x + 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That seems very weird to me. IMO the current behavior was correct, and if the documentation makes it sound like it should be different, then the documentation should be changed.
I would definitely be confused if Rect2i(0, 0, 10, 10).expand(Vector2i(20, 10))
returns Rect2i(0, 0, 21, 11)
. To me expand
means "grow until the borders line up with the target point".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From your comment and from #57468 (comment) I find that the best way would be to keep the functionality of expand
as it is and adjust the documentation accordingly.
In order to optimize this, iterate over all points while setting minx, miny, maxx, maxy and afterwards call |
822a875
to
c681dd9
Compare
Clarify expand documentation
c681dd9
to
23a4fe5
Compare
Thanks! |
Just to note that my review approval was based on the PR having the
Thus in this common usage, by definition, Problems with the non +1 versionThe non +1 version of expand is sometimes used, particularly for optimization, but will not work well in the general case, there are a number of situations it can't deal with well. Some examplesFor instance, if you have an existing bound say in 1 dimension, (0 position, 10 size) and you want to expand that bound to include a point:
You immediately have a problem, because the user needs to add code to deal with these two different situations.
However, there is a bigger problem.
This is one of a number of cases where this non +1 definition of +1 definition "just works"With the standard definition of
And it works in all situations. Why the confusion?I have a feeling there may be a confusion because using inclusive bounds (i.e. position 0, size 1, includes 0 and 1) which we do not use, then you don't need to apply the +1. But with exclusive, you do. We should also consider the possibility that the existing implementation of the function from 3.x is simply a bug. We would have to ask @reduz (and he may not remember! 😁 ). It was only used from one place, Maybe a compromise could be to have two functions? (1) You could then simply go through the codebase with find_in_files, and see the intended usage, and call the appropriate function. I've seen this exact situation come up before, and the solution used then was to fix the |
For reference, here are the locations in the code base, that use godot/editor/editor_atlas_packer.cpp Line 103 in 2aee84c
Here point+(1,1) is explicitly included (and would benefit from the +1 version): godot/editor/plugins/tiles/tile_map_editor.cpp Lines 1559 to 1560 in 2aee84c
godot/editor/plugins/tiles/tile_map_editor.cpp Line 2979 in 2aee84c
Here godot/editor/plugins/tiles/tile_map_editor.cpp Lines 3855 to 3858 in 2aee84c
Line 3377 in 2aee84c
|
Correction of some border cases for Rect2i calculations.
Clarify expand documentation.
Added Unit tests for these cases.
resolves #57468