-
-
Notifications
You must be signed in to change notification settings - Fork 21.6k
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
Implement Bresenham line algorithm in Geometry2D #43916
Conversation
4a22387
to
45a5490
Compare
CC @Xrayez |
The method is not exposed to scripting in TEST_CASE("[Geometry2D] Bresenham line") {
Vector<Point2i> line = Geometry2D::bresenham_line(Point2i(25, 0), Point2i(49, 49));
Ref<Image> image;
image.instance();
image->create(50, 50, false, Image::FORMAT_RGBA8);
image->fill(Color(0, 0, 0));
for (int i = 0; i < line.size(); ++i) {
image->set_pixelv(line[i], Color(1, 1, 1));
}
image->save_png("bresenham.png");
} ResultSo works! Regarding code style, I'd also use And I'd expose the method to scripting. The algorithm can be used for bitmap-based (raycast) collision detection, and I know at least one person who uses such method in Unity. That said, it's not restricted to painting. |
I don't mind either way, I'll change it to Point2i. We could eventually expose it I suppose, but performance isn't great. |
45a5490
to
ba70e1a
Compare
I updated the PR to use Point2i. I also discussed with @reduz about the possibility to expose it, but he thinks, and I agree on this, that the use case stays relatively limited. Considering the performances are not the best, it might be better to keep it internal for now. If it is needed, I could possibly expose the line drawing algorithm in the TileMap class. It should be enough to cover most use cases. |
I accept the decision but I'm curious why there's so much opposition to exposing this to scripting? The algorithm is fairly classic so there should be no ambiguity. If performance is concern, then it can be always improved later, and exposing this to scripting may in fact inspire people to make it more efficient. In either case, we'll get either a feature proposal or a bug report out of this. Every new solution creates a problem, and every problem demands solution. My rationale is that is something is added to core, it should be useful for a lot of use cases, and I interpreted this PR implies this. But to be honest, if this implementation is only useful for tile/grid map editor painting, it should stay there, I'm mostly trying to grasp/adhere to Godot's development principles. Referring to Solutions must be local:
If this change is an exception from the best practices, I'm curious what makes it so? |
I am not sure. But I'd say that the lack of demand for it is likely the first reason.
This would lead the same code to be duplicated at least 4 times (maybe more, as I am trying to split the TileSet editor into smaller modules). As I don't want to make all parts depend on a generic "tiles_helpers.h" files with only one function, I think it's better to have it in Geometry2D. In any case, we often add things to the core that are needed internally in several places. That does not mean it's always a good idea to expose everything, as some part of it might not be performance optimal, user friendly or even needed. |
I think, oftentimes, implementation tend to diverge over time due to changing requirements, so what seems to be a code duplication at first may result in quite different result in the future. According to IRC discussion:
But don't get me wrong, I'm not saying that this shouldn't be implemented in core. But as someone who tends to work on C++ side of things with Godot via modules, I sometimes find myself in a weird/disapponting situation that the C++ has a particular method, but when I switch to GDScript, I cannot find one, which creates more inconsistency and confusion, in my opinion. I see two possible solutions to this:
Something similar to I understand that this is outside the scope of this PR, but this is something which would improve code quality (which would also prevent this discussion from happening in the first place). 😛 Also see previous changes such as #15892. I'd like to draw attention to "missing" part there. So, missing bindings could as well be considered as a bug to someone. But yeah lets see whether users would find this useful at some point, I'm certainly not against these changes in this PR. |
For those who are interested, I've implemented Bresenham line in Goost: goostengine/goost#75, which is exposed to scripting. So, I guess it's fine to keep it internal in Godot indeed, especially if/when the implementation may end up being different depending on the exact use cases. |
Looks like this method is added as part of another recently merged PR #48535 available in Lines 399 to 435 in a3dda2d
Due to this, I think it's safe to close this PR, then. |
I need it to paint on TileSet, Tilemaps, and it should also be useful for the Gridmap editor.
Note: This implementation is taken from the one in TileMap : https://github.com/godotengine/godot/blob/master/editor/plugins/tile_map_editor_plugin.cpp#L970