A C++ template library for embedded applications
MIT licensed
Designed and
maintained by
John Wellbelove
bresenham_line

A 'pseudo' container that generates coordinates on a line between two points using the Bresenham line algorithm.
The class has an STL-like API and is a forward iterator type container.

Note: The iterator only supports pre-increment.

etl::bresenham_line<typename T>
Where T is the coordinate element type.
___________________________________________________________________________________________________
Member types

value_type              etl::coordinate_2d<T>
size_type               std::size_t
difference_type         std::ptrdiff_t
const_reference         const value_type&
const_pointer           const value_type*
const_iterator          Constant forward iterator
____________________________________________________________________________________________________
Constructor

etl::bresenham_line<T>;
etl::bresenham_line<T>(T first_x, T first_y, T last_x, T last_y);
etl::bresenham_line<T>(const etl::coordinate_2d<T>& first, const etl::coordinate_2d<T>& last);
____________________________________________________________________________________________________
Initialization

void reset(T first_x, T first_y, T last_x, T last_y);
void reset(const etl::coordinate_2d<T>& first, const etl::coordinate_2d<T>& last);
____________________________________________________________________________________________________
Element access

const_reference front() const
Returns a const reference to the first coordinate in the line.
____________________________________________________________________________________________________
const_reference back() const
Returns a const reference to the last coordinate in the line.
____________________________________________________________________________________________________
Iterators

const_iterator begin()
Returns an iterator to the beginning of the coordinate series.
This will reset the Bresenham line algorithm to the first coordinate.
____________________________________________________________________________________________________
const_iterator end() const
Returns an iterator to the end of the coordinate series.
____________________________________________________________________________________________________
Capacity

size_t size() const
Returns the number of coordinates in the series.
____________________________________________________________________________________________________
Non-member functions

== true if the two lines are equal, otherwise false.
!= true if the two lines are not equal, otherwise false.
____________________________________________________________________________________________________
Examples

Plot pixels on a line
std::ostream& operator << (std::ostream& os, const etl::coordinate_2d<int>& coordinate)
{
  os << "(" << coordinate.x << "," << coordinate.y << ")";
  return os;
}

etl::coordinate_2d<int> first = { -3,  5 };
etl::coordinate_2d<int> last  = {  3, -5 };

etl::bresenham_line<int> line(first, last);

std::cout << "There are " << line.size() << " coordinates between " << line.front() << " and "
<< line.back();

// Plot the pixels between first and last.
std::for_each(line.begin(), line.end(), PlotPixel);


Create a vector of pixels on a line
etl::coordinate_2d<int> first = { -3,  5 };
etl::coordinate_2d<int> last  = {  3, -5 };

etl::bresenham_line<int> line(first, last);

std::vector<etl::coordinate_2d<int>> coordinates;

// Create the vector of points on the line between first and last.
std::copy(line.begin(), line.end(), std::back_inserter(coordinates));
bresenham_line.h