35#ifndef TCOD_BRESENHAM_HPP_
36#define TCOD_BRESENHAM_HPP_
50 virtual bool putPoint(
int x,
int y) = 0;
71 static void init(
int xFrom,
int yFrom,
int xTo,
int yTo);
112 static bool step(
int *xCur,
int *yCur);
169 using Point2 = std::array<int, 2>;
170 using iterator_category = std::random_access_iterator_tag;
171 using value_type = Point2;
172 using difference_type = int;
173 using pointer = void;
174 using reference = value_type;
184 index_end_{get_delta_x() + 1},
186 y_error_{-get_delta_x() / 2} {}
194 index_end_{get_delta_x() + 1},
196 y_error_{error > 0 ? error % get_delta_x() - get_delta_x() : error % get_delta_x()} {}
224 inline value_type
operator[](
int index)
noexcept {
return bresenham_get(index_ + index); }
228 inline value_type
operator*() noexcept {
return (*
this)[0]; }
229 inline constexpr bool operator==(
const BresenhamLine& rhs)
const noexcept {
return index_ == rhs.index_; }
230 inline constexpr bool operator!=(
const BresenhamLine& rhs)
const noexcept {
return !(*
this == rhs); }
231 inline constexpr difference_type operator-(
const BresenhamLine& rhs)
const noexcept {
return index_ - rhs.index_; }
246 new_data.index_ += shift_begin;
247 new_data.index_end_ += shift_end;
248 new_data.index_end_ = std::max(new_data.index_, new_data.index_end_);
289 return BresenhamLine{origin_, dest_, index_end_, index_end_, cursor_, y_error_};
300 inline Point2 transform(
const Point2& cursor)
const noexcept {
301 return {ax + cursor.at(0) * xx + cursor.at(1) * yx, ay + cursor.at(0) * xy + cursor.at(1) * yy};
313 inline Matrix get_matrix() const noexcept {
return get_matrix(origin_, dest_); }
314 static Matrix get_matrix(
const Point2& origin,
const Point2& dest)
noexcept {
315 const int delta_x = dest.at(0) - origin.at(0);
316 const int delta_y = dest.at(1) - origin.at(1);
325 if (delta_x < 0) matrix.xx = -1;
326 if (delta_y < 0) matrix.yy = -1;
327 if (std::abs(delta_y) > std::abs(delta_x)) {
328 std::swap(matrix.xx, matrix.yx);
329 std::swap(matrix.xy, matrix.yy);
333 explicit BresenhamLine(Point2
begin, Point2
end,
int index_begin,
int index_end, Point2 cursor,
int error) noexcept
334 : origin_{
begin}, dest_{
end}, index_{index_begin}, index_end_{index_end}, cursor_{cursor}, y_error_{error} {}
340 inline Point2 get_normalized_delta() const noexcept {
return get_normalized_delta(origin_, dest_); }
341 static Point2 get_normalized_delta(
const Point2& origin,
const Point2& dest)
noexcept {
342 return std::abs(dest.at(0) - origin.at(0)) > std::abs(dest.at(1) - origin.at(1))
343 ? Point2{std::abs(dest.at(0) - origin.at(0)), std::abs(dest.at(1) - origin.at(1))}
344 : Point2{std::abs(dest.at(1) - origin.at(1)), std::abs(dest.at(0) - origin.at(0))};
351 inline int get_delta_x() const noexcept {
return get_normalized_delta().at(0); }
356 const Point2 delta = get_normalized_delta();
357 y_error_ += delta.at(1);
360 y_error_ -= delta.at(0);
369 const Point2 delta = get_normalized_delta();
370 y_error_ -= delta.at(1);
371 if (y_error_ <= -delta.at(0)) {
373 y_error_ += delta.at(0);
381 inline Point2 bresenham_get(
int index) {
382 while (cursor_.at(0) < index) bresenham_next();
383 while (cursor_.at(0) > index) bresenham_prev();
384 return get_matrix().transform(cursor_);
Definition bresenham.hpp:54
static void init(int xFrom, int yFrom, int xTo, int yTo)
First, you have to initialize the toolkit with your starting and ending coordinates.
static bool line(int xFrom, int yFrom, int xTo, int yTo, TCODLineListener *listener)
The function returns false if the line has been interrupted by the callback (it returned false before...
static bool step(int *xCur, int *yCur)
You can then step through each cell with this function.
Definition bresenham.hpp:48
Encapsulates a Bresenham line drawing algorithm.
Definition bresenham.hpp:167
BresenhamLine without_endpoints() const noexcept
Definition bresenham.hpp:280
value_type operator*() noexcept
Return the world position of the Bresenham at the current index.
Definition bresenham.hpp:228
BresenhamLine begin() const noexcept
Return the beginning iterator, which is a copy of the current object.
Definition bresenham.hpp:284
BresenhamLine without_end() const noexcept
Definition bresenham.hpp:270
BresenhamLine(Point2 begin, Point2 end) noexcept
Construct a new Bresenham line from begin to end.
Definition bresenham.hpp:180
value_type operator[](int index) noexcept
Return the world position of the Bresenham at the index relative to the current index.
Definition bresenham.hpp:224
BresenhamLine(Point2 begin, Point2 end, int error) noexcept
Construct a new Bresenham line with a manually given error value.
Definition bresenham.hpp:190
BresenhamLine adjust_range(int shift_begin, int shift_end) const noexcept
Definition bresenham.hpp:244
BresenhamLine end() const noexcept
Return the past-the-end iterator.
Definition bresenham.hpp:288
BresenhamLine without_start() const noexcept
Definition bresenham.hpp:260
A template container for holding a multi-dimensional array of items.
Definition matrix.hpp:139
The libtcod namespace.
Definition bresenham.hpp:157