Line Drawing Toolkit

Iterator-based line drawing

#include <libtcod.h>

void main() {
  TCOD_bresenham_data_t bresenham_data;
  int x=5, y=8;
  TCOD_line_init_mt(x, y, 13, 14, &bresenham_data);
  do {
    printf("%d %d\n", x, y);
  } while (!TCOD_line_step_mt(&x, &y, &bresenham_data));
}
struct TCOD_bresenham_data_t

A struct used for computing a bresenham line.

void TCOD_line_init_mt(int xFrom, int yFrom, int xTo, int yTo, TCOD_bresenham_data_t *data)
bool TCOD_line_step_mt(int *xCur, int *yCur, TCOD_bresenham_data_t *data)

Callback-based line drawing

#include <libtcod.h>

void main() {
  bool my_listener(int x,int y) {
    printf("%d %d\n", x, y);
    return true;
  }
  TCOD_line(5, 8, 13, 4, my_listener);
}
typedef bool (*TCOD_line_listener_t)(int x, int y)

A callback to be passed to TCOD_line.

The points given to the callback include both the starting and ending positions.

Param x:

Param y:

Return:

As long as this callback returns true it will be called with the next x,y point on the line.

bool TCOD_line(int xFrom, int yFrom, int xTo, int yTo, TCOD_line_listener_t listener)
class BresenhamLine

Encapsulates a Bresenham line drawing algorithm.

New in version 1.17.

Public Functions

inline explicit BresenhamLine(Point2 begin, Point2 end) noexcept

Construct a new Bresenham line from begin to end.

Iterating over this instance will include both endpoints.

inline explicit BresenhamLine(Point2 begin, Point2 end, int error) noexcept

Construct a new Bresenham line with a manually given error value.

inline value_type operator[](int index) noexcept

Return the world position of the Bresenham at the index relative to the current index.

BresenhamLine is not restricted by any bounds so you can freely give a index past the end or before zero.

The internal state must always seek to the position being indexed, this will affect performance depending on if successive indexes are close together or far apart.

inline value_type operator*() noexcept

Return the world position of the Bresenham at the current index.

inline BresenhamLine adjust_range(int shift_begin, int shift_end) const noexcept

  Return a new version of this BresenhamLine with an adjusted range.

  `shift_begin` and `shift_end` change the beginning and ending of the line
  when iterators over.

  Example::
Remove the endpoints of a bresenham line. auto line = tcod::BresenhamLine(from, to).adjust_range(1, -1);

inline BresenhamLine without_start() const noexcept

  Remove the staring endpoint of a line.

  Example::

    for (auto&& [x, y] : tcod::BresenhamLine(from, to).without_start()) {
All positions excluding from. }

inline BresenhamLine without_end() const noexcept

  Remove the final endpoint of a line.

  Example::

    for (auto&& [x, y] : tcod::BresenhamLine(from, to).without_end()) {
All positions excluding to. }

inline BresenhamLine without_endpoints() const noexcept

  Remove both endpoints of a line.

  Example::

    for (auto&& [x, y] : tcod::BresenhamLine(from, to).without_endpoints()) {
All positions between and excluding from and to. }

inline BresenhamLine begin() const noexcept

Return the beginning iterator, which is a copy of the current object.

inline BresenhamLine end() const noexcept

Return the past-the-end iterator.

Deprecated functions

bool TCOD_line_mt(int xFrom, int yFrom, int xTo, int yTo, TCOD_line_listener_t listener, TCOD_bresenham_data_t *data)
void TCOD_line_init(int xFrom, int yFrom, int xTo, int yTo)
bool TCOD_line_step(int *xCur, int *yCur)

advance one step.

returns true if we reach destination