Control Surface  1.2.0
MIDI Control Surface library for Arduino
Bresenham.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <AH/Arduino-Wrapper.h>
4 
5 #include <Printable.h>
6 #include <Settings/SettingsWrapper.hpp>
7 #include <limits.h>
8 #include <stdint.h>
9 
11 
12 template <typename T>
13 static constexpr inline int sgn(T val) {
14  return (T(0) < val) - (val < T(0));
15 }
16 
19  public:
20  using pint = int16_t;
21  struct Pixel : public Printable {
22  pint x = 0, y = 0;
23  Pixel(pint x, pint y) : x(x), y(y) {}
24  Pixel() = default;
25  size_t printTo(Print &p) const override;
26  unsigned int distanceSquared(Pixel other) const {
27  int dx = this->x - other.x;
28  int dy = this->y - other.y;
29  return dx * dx + dy * dy;
30  }
31  };
32 
33  BresenhamLine(Pixel start, int cos, int sin)
34  : px(start), // Starting point
35  dx(cos), dy(sin), // cosine and sine of the slope of the line (scaled)
36  adx(abs(dx)), ady(abs(dy)), // absolute values of cos and sin
37  xinc(sgn(dx)), yinc(sgn(dy)), // increment steps for x and y
38  steep(ady > adx) // whether to increment x or y on each iteration
39  {
40  if (steep)
41  error = adx - ady + adx;
42  else
43  error = ady - adx + ady;
44  }
45 
46  BresenhamLine(Pixel start, float angle)
47  : BresenhamLine{
48  start,
49  BresenhamLine::cos(angle),
50  BresenhamLine::sin(angle),
51  } {}
52 
53  pint getCurrentLength() const { return length; }
54 
55  Pixel next() {
56  Pixel result = px;
57  if (steep) {
58  if (error >= 0) {
59  px.x += xinc;
60  error -= 2 * ady;
61  }
62  px.y += yinc;
63  error += 2 * adx;
64  } else {
65  if (error >= 0) {
66  px.y += yinc;
67  error -= 2 * adx;
68  }
69  px.x += xinc;
70  error += 2 * ady;
71  }
72  ++length;
73  return result;
74  }
75 
76  static int cos(double angle) {
77  return round(errorScalingFactor * ::cos(angle));
78  }
79 
80  static int sin(double angle) {
81  return round(errorScalingFactor * ::sin(angle));
82  }
83 
84  private:
85  const static int errorScalingFactor = INT_MAX / 2;
87  int dx, dy;
88  int adx, ady;
89  int xinc, yinc;
90  bool steep;
91  int error;
92  pint length = 0;
93 };
94 
BresenhamLine::Pixel::printTo
size_t printTo(Print &p) const override
Definition: Bresenham.cpp:5
BresenhamLine::error
int error
Definition: Bresenham.hpp:91
BresenhamLine::yinc
int yinc
Definition: Bresenham.hpp:89
BresenhamLine::Pixel::Pixel
Pixel(pint x, pint y)
Definition: Bresenham.hpp:23
sgn
static constexpr int sgn(T val)
Definition: Bresenham.hpp:13
BresenhamLine::cos
static int cos(double angle)
Definition: Bresenham.hpp:76
BresenhamLine::sin
static int sin(double angle)
Definition: Bresenham.hpp:80
BresenhamLine::length
pint length
Definition: Bresenham.hpp:92
BresenhamLine::dy
int dy
Definition: Bresenham.hpp:87
BresenhamLine::pint
int16_t pint
Definition: Bresenham.hpp:20
BEGIN_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:9
BresenhamLine::BresenhamLine
BresenhamLine(Pixel start, float angle)
Definition: Bresenham.hpp:46
BresenhamLine::getCurrentLength
pint getCurrentLength() const
Definition: Bresenham.hpp:53
END_CS_NAMESPACE
#define END_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:10
BresenhamLine::xinc
int xinc
Definition: Bresenham.hpp:89
BresenhamLine::next
Pixel next()
Definition: Bresenham.hpp:55
Arduino-Wrapper.h
BresenhamLine::Pixel
Definition: Bresenham.hpp:21
BresenhamLine::dx
int dx
Definition: Bresenham.hpp:87
BresenhamLine::Pixel::Pixel
Pixel()=default
BresenhamLine
Line rasterization algorithm for drawing lines to the display.
Definition: Bresenham.hpp:18
BresenhamLine::BresenhamLine
BresenhamLine(Pixel start, int cos, int sin)
Definition: Bresenham.hpp:33
BresenhamLine::adx
int adx
Definition: Bresenham.hpp:88
BresenhamLine::Pixel::y
pint y
Definition: Bresenham.hpp:22
BresenhamLine::px
Pixel px
Definition: Bresenham.hpp:86
BresenhamLine::Pixel::distanceSquared
unsigned int distanceSquared(Pixel other) const
Definition: Bresenham.hpp:26
BresenhamLine::errorScalingFactor
static const int errorScalingFactor
Definition: Bresenham.hpp:85
BresenhamLine::steep
bool steep
Definition: Bresenham.hpp:90
BresenhamLine::ady
int ady
Definition: Bresenham.hpp:88
BresenhamLine::Pixel::x
pint x
Definition: Bresenham.hpp:22