Loading...
Searching...
No Matches
bresenham_line.h
Go to the documentation of this file.
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2020 John Wellbelove
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/
#ifndef ETL_BRESENHAM_LINE_INCLUDED
#define ETL_BRESENHAM_LINE_INCLUDED
#include "platform.h"
#include "iterator.h"
#include "type_traits.h"
#include "utility.h"
#include <stddef.h>
#include <stdint.h>
namespace etl
{
//***************************************************************************
//***************************************************************************
template <typename T, typename TWork = int16_t>
class bresenham_line
{
public:
//***************************************************
//***************************************************
typedef etl::coordinate_2d<T> value_type;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef value_type* pointer;
typedef const value_type* const_pointer;
//***************************************************
//***************************************************
class const_iterator : public etl::iterator<ETL_OR_STD::forward_iterator_tag, const value_type>
{
public:
friend class bresenham_line;
//***************************************************
//***************************************************
const_iterator()
: p_bresenham_line(ETL_NULLPTR)
{
}
//***************************************************
//***************************************************
const_iterator(const const_iterator& other)
: p_bresenham_line(other.p_bresenham_line)
{
}
//***************************************************
//***************************************************
const_iterator& operator=(const const_iterator& rhs)
{
p_bresenham_line = rhs.p_bresenham_line;
return *this;
}
//***************************************************
//***************************************************
const_iterator& operator++()
{
// Has the end of the series has been reached?
if (p_bresenham_line->get_coordinate() == p_bresenham_line->back())
{
// Mark it as an end iterator.
p_bresenham_line = ETL_NULLPTR;
}
else
{
p_bresenham_line->next();
}
return *this;
}
//***************************************************
//***************************************************
value_type operator*() const
{
return p_bresenham_line->get_coordinate();
}
//***************************************************
//***************************************************
friend bool operator==(const const_iterator& lhs, const const_iterator& rhs)
{
return lhs.p_bresenham_line == rhs.p_bresenham_line;
}
//***************************************************
//***************************************************
friend bool operator!=(const const_iterator& lhs, const const_iterator& rhs)
{
return !(lhs == rhs);
}
private:
//***************************************************
//***************************************************
const_iterator(bresenham_line<T>* pb)
: p_bresenham_line(pb)
{
}
bresenham_line<T>* p_bresenham_line;
};
//***************************************************
//***************************************************
bresenham_line()
{
initialise(T(0), T(0), T(0), T(0));
}
//***************************************************
//***************************************************
bresenham_line(etl::coordinate_2d<T> first_, etl::coordinate_2d<T> last_)
{
initialise(first_.x, first_.y, last_.x, last_.y);
}
//***************************************************
//***************************************************
bresenham_line(T first_x, T first_y, T last_x, T last_y)
{
initialise(first_x, first_y, last_x, last_y);
}
//***************************************************
//***************************************************
void reset(etl::coordinate_2d<T> first_, etl::coordinate_2d<T> last_)
{
initialise(first_.x, first_.y, last_.x, last_.y);
}
//***************************************************
//***************************************************
void reset(T first_x, T first_y, T last_x, T last_y)
{
initialise(first_x, first_y, last_x, last_y);
}
//***************************************************
//***************************************************
const_iterator begin()
{
coordinate = first;
return const_iterator(this);
}
//***************************************************
//***************************************************
const_iterator end() const
{
return const_iterator();
}
//***************************************************
//***************************************************
const_reference front() const
{
return first;
}
//***************************************************
//***************************************************
const_reference back() const
{
return last;
}
//***************************************************
//***************************************************
size_t size() const
{
if (y_is_major_axis())
{
return static_cast<size_t>((dy / 2) + 1);
}
else
{
return static_cast<size_t>((dx / 2) + 1);
}
}
//***************************************************
//***************************************************
friend bool operator==(const bresenham_line& lhs, const bresenham_line& rhs)
{
return (lhs.front() == rhs.front()) && (lhs.back() == rhs.back());
}
//***************************************************
//***************************************************
friend bool operator!=(const bresenham_line& lhs, const bresenham_line& rhs)
{
return !(lhs == rhs);
}
private:
//***************************************************
//***************************************************
void initialise(T first_x, T first_y, T last_x, T last_y)
{
first = value_type(first_x, first_y);
last = value_type(last_x, last_y);
coordinate = first;
x_increment = (last_x < first_x) ? -1 : 1;
y_increment = (last_y < first_y) ? -1 : 1;
dx = (last_x < first_x) ? first_x - last_x : last_x - first_x;
dy = (last_y < first_y) ? first_y - last_y : last_y - first_y;
do_minor_increment = false;
if (y_is_major_axis())
{
dx *= 2;
balance = dx - dy;
dy *= 2;
}
else
{
dy *= 2;
balance = dy - dx;
dx *= 2;
}
}
//***************************************************
//***************************************************
bool y_is_major_axis() const
{
return dx < dy;
}
//***************************************************
//***************************************************
void next()
{
if (y_is_major_axis())
{
// Y is major axis.
if (do_minor_increment)
{
coordinate.x = T(coordinate.x + x_increment);
balance -= dy;
}
coordinate.y = T(coordinate.y + y_increment);
balance += dx;
}
else
{
// X is major axis.
if (do_minor_increment)
{
coordinate.y = T(coordinate.y + y_increment);
balance -= dx;
}
coordinate.x = T(coordinate.x + x_increment);
balance += dy;
}
do_minor_increment = (balance >= 0);
}
//***************************************************
//***************************************************
value_type get_coordinate() const
{
return coordinate;
}
typedef TWork work_t;
value_type first;
value_type last;
value_type coordinate;
work_t x_increment;
work_t y_increment;
work_t dx;
work_t dy;
work_t balance;
bool do_minor_increment;
};
} // namespace etl
#endif
etl::bresenham_line::const_iterator
Const Iterator.
Definition bresenham_line.h:70
etl::bresenham_line::const_iterator::operator!=
friend bool operator!=(const const_iterator &lhs, const const_iterator &rhs)
Inequality operator.
Definition bresenham_line.h:139
etl::bresenham_line::const_iterator::operator=
const_iterator & operator=(const const_iterator &rhs)
Assignment operator.
Definition bresenham_line.h:94
etl::bresenham_line::const_iterator::operator++
const_iterator & operator++()
Pre-increment operator.
Definition bresenham_line.h:104
etl::bresenham_line::const_iterator::const_iterator
const_iterator(const const_iterator &other)
Copy constructor.
Definition bresenham_line.h:86
etl::bresenham_line::const_iterator::operator==
friend bool operator==(const const_iterator &lhs, const const_iterator &rhs)
Equality operator.
Definition bresenham_line.h:131
etl::bresenham_line::const_iterator::const_iterator
const_iterator()
Default constructor.
Definition bresenham_line.h:78
etl::bresenham_line::const_iterator::operator*
value_type operator*() const
De-reference operator.
Definition bresenham_line.h:123
etl::bresenham_line
Definition bresenham_line.h:52
etl::bresenham_line::end
const_iterator end() const
Get a const_iterator to one past the last coordinate.
Definition bresenham_line.h:215
etl::bresenham_line::begin
const_iterator begin()
Definition bresenham_line.h:205
etl::bresenham_line::operator!=
friend bool operator!=(const bresenham_line &lhs, const bresenham_line &rhs)
Inequality operator.
Definition bresenham_line.h:262
etl::bresenham_line::bresenham_line
bresenham_line(T first_x, T first_y, T last_x, T last_y)
Definition bresenham_line.h:178
etl::bresenham_line::bresenham_line
bresenham_line(etl::coordinate_2d< T > first_, etl::coordinate_2d< T > last_)
Definition bresenham_line.h:169
etl::bresenham_line::reset
void reset(T first_x, T first_y, T last_x, T last_y)
Definition bresenham_line.h:196
etl::bresenham_line::value_type
etl::coordinate_2d< T > value_type
Standard container types.
Definition bresenham_line.h:58
etl::bresenham_line::back
const_reference back() const
Get the last coordinate.
Definition bresenham_line.h:231
etl::bresenham_line::operator==
friend bool operator==(const bresenham_line &lhs, const bresenham_line &rhs)
Equality operator.
Definition bresenham_line.h:254
etl::bresenham_line::bresenham_line
bresenham_line()
Constructor.
Definition bresenham_line.h:160
etl::bresenham_line::reset
void reset(etl::coordinate_2d< T > first_, etl::coordinate_2d< T > last_)
Definition bresenham_line.h:187
etl::bresenham_line::size
size_t size() const
Get the size of the series.
Definition bresenham_line.h:239
etl::bresenham_line::front
const_reference front() const
Get the first coordinate.
Definition bresenham_line.h:223
iterator.h
etl
Definition absolute.h:40
platform.h
etl::coordinate_2d
integer_sequence
Definition utility.h:908
etl::iterator
iterator
Definition iterator.h:482
type_traits.h
utility.h