Skip to content

Commit

Permalink
Implement Bresenham line algorithm in Geometry2D
Browse files Browse the repository at this point in the history
  • Loading branch information
groud committed Nov 27, 2020
1 parent 0b0b3d9 commit ba70e1a
Showing 1 changed file with 44 additions and 0 deletions.
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

0 comments on commit ba70e1a

Please sign in to comment.