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