diff options
author | Rémi Verschelde <remi@verschelde.fr> | 2021-05-07 20:46:06 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-07 20:46:06 +0200 |
commit | c3f7465b7efe233eaa8945e41f4029840c1aa153 (patch) | |
tree | 9cdd0f36cc124ee47214e0a0f454b97fa82f6d37 /core/math | |
parent | 8976594f4b90edd42a926e483815c829a540d8d2 (diff) | |
parent | a3dda2df85bf3e3ef82dbe1c2377640b9f3fd9c0 (diff) |
Merge pull request #48535 from groud/tiles_squashed
TileSet and TileMap rework (squashed)
Diffstat (limited to 'core/math')
-rw-r--r-- | core/math/geometry_2d.h | 39 | ||||
-rw-r--r-- | core/math/vector2.h | 8 |
2 files changed, 47 insertions, 0 deletions
diff --git a/core/math/geometry_2d.h b/core/math/geometry_2d.h index 4b5aef352f..4958b5ac6a 100644 --- a/core/math/geometry_2d.h +++ b/core/math/geometry_2d.h @@ -395,6 +395,45 @@ public: H.resize(k); return H; } + + static Vector<Point2i> bresenham_line(const Point2i &p_start, const Point2i &p_end) { + Vector<Point2i> points; + + Vector2i delta = (p_end - p_start).abs() * 2; + Vector2i step = (p_end - p_start).sign(); + Vector2i current = p_start; + + if (delta.x > delta.y) { + int err = delta.x / 2; + + for (; current.x != p_end.x; current.x += step.x) { + points.push_back(current); + + err -= delta.y; + if (err < 0) { + current.y += step.y; + err += delta.x; + } + } + } else { + int err = delta.y / 2; + + for (; current.y != p_end.y; current.y += step.y) { + points.push_back(current); + + err -= delta.x; + if (err < 0) { + current.x += step.x; + err += delta.y; + } + } + } + + points.push_back(current); + + 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); diff --git a/core/math/vector2.h b/core/math/vector2.h index 81bc71d590..edc6e3a3ef 100644 --- a/core/math/vector2.h +++ b/core/math/vector2.h @@ -280,6 +280,14 @@ struct Vector2i { return p_idx ? y : x; } + Vector2i min(const Vector2i &p_vector2i) const { + return Vector2(MIN(x, p_vector2i.x), MIN(y, p_vector2i.y)); + } + + Vector2i max(const Vector2i &p_vector2i) const { + return Vector2(MAX(x, p_vector2i.x), MAX(y, p_vector2i.y)); + } + Vector2i operator+(const Vector2i &p_v) const; void operator+=(const Vector2i &p_v); Vector2i operator-(const Vector2i &p_v) const; |