Skip to content

Commit 02088ab

Browse files
committed
Merge pull request #74714 from dalexeev/expose-bresenham-line
Expose `Geometry2D.bresenham_line()` method
2 parents a2e3473 + 9f0ae21 commit 02088ab

File tree

5 files changed

+38
-7
lines changed

5 files changed

+38
-7
lines changed

core/core_bind.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,19 @@ Dictionary Geometry2D::make_atlas(const Vector<Size2> &p_rects) {
920920
return ret;
921921
}
922922

923+
TypedArray<Point2i> Geometry2D::bresenham_line(const Point2i &p_from, const Point2i &p_to) {
924+
Vector<Point2i> points = ::Geometry2D::bresenham_line(p_from, p_to);
925+
926+
TypedArray<Point2i> result;
927+
result.resize(points.size());
928+
929+
for (int i = 0; i < points.size(); i++) {
930+
result[i] = points[i];
931+
}
932+
933+
return result;
934+
}
935+
923936
void Geometry2D::_bind_methods() {
924937
ClassDB::bind_method(D_METHOD("is_point_in_circle", "point", "circle_position", "circle_radius"), &Geometry2D::is_point_in_circle);
925938
ClassDB::bind_method(D_METHOD("segment_intersects_circle", "segment_from", "segment_to", "circle_position", "circle_radius"), &Geometry2D::segment_intersects_circle);
@@ -954,6 +967,8 @@ void Geometry2D::_bind_methods() {
954967

955968
ClassDB::bind_method(D_METHOD("make_atlas", "sizes"), &Geometry2D::make_atlas);
956969

970+
ClassDB::bind_method(D_METHOD("bresenham_line", "from", "to"), &Geometry2D::bresenham_line);
971+
957972
BIND_ENUM_CONSTANT(OPERATION_UNION);
958973
BIND_ENUM_CONSTANT(OPERATION_DIFFERENCE);
959974
BIND_ENUM_CONSTANT(OPERATION_INTERSECTION);

core/core_bind.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,8 @@ class Geometry2D : public Object {
324324

325325
Dictionary make_atlas(const Vector<Size2> &p_rects);
326326

327+
TypedArray<Point2i> bresenham_line(const Point2i &p_from, const Point2i &p_to);
328+
327329
Geometry2D() { singleton = this; }
328330
};
329331

core/math/geometry_2d.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -451,17 +451,17 @@ class Geometry2D {
451451
return H;
452452
}
453453

454-
static Vector<Point2i> bresenham_line(const Point2i &p_start, const Point2i &p_end) {
454+
static Vector<Point2i> bresenham_line(const Point2i &p_from, const Point2i &p_to) {
455455
Vector<Point2i> points;
456456

457-
Vector2i delta = (p_end - p_start).abs() * 2;
458-
Vector2i step = (p_end - p_start).sign();
459-
Vector2i current = p_start;
457+
Vector2i delta = (p_to - p_from).abs() * 2;
458+
Vector2i step = (p_to - p_from).sign();
459+
Vector2i current = p_from;
460460

461461
if (delta.x > delta.y) {
462462
int err = delta.x / 2;
463463

464-
for (; current.x != p_end.x; current.x += step.x) {
464+
for (; current.x != p_to.x; current.x += step.x) {
465465
points.push_back(current);
466466

467467
err -= delta.y;
@@ -473,7 +473,7 @@ class Geometry2D {
473473
} else {
474474
int err = delta.y / 2;
475475

476-
for (; current.y != p_end.y; current.y += step.y) {
476+
for (; current.y != p_to.y; current.y += step.y) {
477477
points.push_back(current);
478478

479479
err -= delta.x;

doc/classes/Geometry2D.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,20 @@
99
<tutorials>
1010
</tutorials>
1111
<methods>
12+
<method name="bresenham_line">
13+
<return type="Vector2i[]" />
14+
<param index="0" name="from" type="Vector2i" />
15+
<param index="1" name="to" type="Vector2i" />
16+
<description>
17+
Returns the [url=https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm]Bresenham line[/url] between the [param from] and [param to] points. A Bresenham line is a series of pixels that draws a line and is always 1-pixel thick on every row and column of the drawing (never more, never less).
18+
Example code to draw a line between two [Marker2D] nodes using a series of [method CanvasItem.draw_rect] calls:
19+
[codeblock]
20+
func _draw():
21+
for pixel in Geometry2D.bresenham_line($MarkerA.position, $MarkerB.position):
22+
draw_rect(Rect2(pixel, Vector2.ONE), Color.WHITE)
23+
[/codeblock]
24+
</description>
25+
</method>
1226
<method name="clip_polygons">
1327
<return type="PackedVector2Array[]" />
1428
<param index="0" name="polygon_a" type="PackedVector2Array" />

doc/classes/InputEventMouseMotion.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
</brief_description>
66
<description>
77
Stores information about a mouse or a pen motion. This includes relative position, absolute position, and velocity. See [method Node._input].
8-
[b]Note:[/b] By default, this event is only emitted once per frame rendered at most. If you need more precise input reporting, set [member Input.use_accumulated_input] to [code]false[/code] to make events emitted as often as possible. If you use InputEventMouseMotion to draw lines, consider implementing [url=https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm]Bresenham's line algorithm[/url] as well to avoid visible gaps in lines if the user is moving the mouse quickly.
8+
[b]Note:[/b] By default, this event is only emitted once per frame rendered at most. If you need more precise input reporting, set [member Input.use_accumulated_input] to [code]false[/code] to make events emitted as often as possible. If you use InputEventMouseMotion to draw lines, consider using [method Geometry2D.bresenham_line] as well to avoid visible gaps in lines if the user is moving the mouse quickly.
99
[b]Note:[/b] This event may be emitted even when the mouse hasn't moved, either by the operating system or by Godot itself. If you really need to know if the mouse has moved (e.g. to suppress displaying a tooltip), you should check that [code]relative.is_zero_approx()[/code] is [code]false[/code].
1010
</description>
1111
<tutorials>

0 commit comments

Comments
 (0)