-
-
Notifications
You must be signed in to change notification settings - Fork 96
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
Add method get_enclosing_rect
to Geometry2D
#7333
Comments
why close? |
I assumed it was something only wanted by me. but apparently not, I will reopen. 😉 |
This is so trivial to work around that I don't think it's worth to add it to core, could document it in However the solution here is inefficient, a better solution is to use min/max, describing how to do that in the documentation could be done, but again as above I think it would make more sense to add it to Edit: Suggested code for documentation (haven't tested) func get_enclosing_rect(points: PackedVector2Array) -> Rect2:
if points.is_empty():
return Rect2()
var start := points[0]
var end := points[0]
for i in range(1, points.size()):
start = Vector2(min(start.x, points[i].x), min(start.y, points[i].y))
end = Vector2(max(end.x, points[i].x), max(end.y, points[i].y))
return Rect2(start, end - start) The c++ implementation would be something like: Rect2 get_enclosing_rect(const Vector<Point2> &p_points) {
if (p_points.is_empty()) {
return Rect2();
}
Vector2 start = p_points[0];
Vector2 end = p_points[0];
for (int i = 1; i < p_points.size(); ++i) {
start = start.min(p_points[i]);
end = end.max(p_points[i]);
}
return Rect2(start, end - start);
} |
Either this or document in Rect2 |
Doing it manually won't change much in efficiency, |
Sure, but this method does it without having to call through that function and just does it directly, and avoids doing subtractions and assignments before being done |
I also think it's trivial, my intent with the proposal was mainly about the performance gain from letting the C++ side do all the "loop work" (could gain a few milliseconds in heavy loops). But in reality, if we start doing these kind of things, I'll close the proposal again. |
Describe the project you are working on
N/A
Describe the problem or limitation you are having in your project
Performance issues regarding the loop intensive task to get a
Rect2
enclosing a cloud of points.Describe the feature / enhancement and how it helps to overcome the problem or limitation
It's a common method and easy to implement, but loop intensive.
Geometry2D::get_enclosing_rect
can be implemented in the core for performance.Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams
If this enhancement will not be used often, can it be worked around with a few lines of script?
Yes.
Is there a reason why this should be core and not an add-on in the asset library?
Performance.
The text was updated successfully, but these errors were encountered: