Skip to content
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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions core/math/geometry_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,50 @@ class Geometry2D {
H.resize(k);
return H;
}

static Vector<Point2i> bresenham_line(const Point2i &p_start, const Point2i &p_end) {
Vector<Point2i> points;

float dx = ABS(p_end.x - p_start.x);
float dy = ABS(p_end.y - p_start.y);

int x = p_start.x;
int y = p_start.y;

int sx = p_start.x > p_end.x ? -1 : 1;
int sy = p_start.y > p_end.y ? -1 : 1;

if (dx > dy) {
float err = dx / 2;

for (; x != p_end.x; x += sx) {
points.push_back(Point2i(x, y));

err -= dy;
if (err < 0) {
y += sy;
err += dx;
}
}
} else {
float err = dy / 2;

for (; y != p_end.y; y += sy) {
points.push_back(Point2i(x, y));

err -= dx;
if (err < 0) {
x += sx;
err += dy;
}
}
}

points.push_back(Vector2(x, y));

return points;
}

static Vector<Vector<Vector2>> decompose_polygon_in_convex(Vector<Point2> polygon);

static void make_atlas(const Vector<Size2i> &p_rects, Vector<Point2i> &r_result, Size2i &r_size);
Expand Down