Control Surface stm32
MIDI Control Surface library for Arduino
Bresenham.hpp
Go to the documentation of this file.
1#pragma once
2
4
5#include <AH/STL/cmath>
6#include <AH/STL/limits>
7#include <Printable.h>
8#include <Settings/SettingsWrapper.hpp>
9
11
12template <typename T>
13static 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)
48 start,
49 BresenhamLine::cos(angle),
50 BresenhamLine::sin(angle),
51 } {}
52
53 pint getCurrentLength() const { return length; }
54
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(float angle) {
77 return std::round(static_cast<float>(errorScalingFactor) *
78 std::cos(angle));
79 }
80
81 static int sin(float angle) {
82 return std::round(static_cast<float>(errorScalingFactor) *
83 std::sin(angle));
84 }
85
86 private:
89 int dx, dy;
90 int adx, ady;
91 int xinc, yinc;
92 bool steep;
93 int error;
95};
96
static constexpr int sgn(T val)
Definition: Bresenham.hpp:13
#define END_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Line rasterization algorithm for drawing lines to the display.
Definition: Bresenham.hpp:18
Pixel next()
Definition: Bresenham.hpp:55
int16_t pint
Definition: Bresenham.hpp:20
BresenhamLine(Pixel start, float angle)
Definition: Bresenham.hpp:46
static const int errorScalingFactor
Definition: Bresenham.hpp:87
static int sin(float angle)
Definition: Bresenham.hpp:81
static int cos(float angle)
Definition: Bresenham.hpp:76
BresenhamLine(Pixel start, int cos, int sin)
Definition: Bresenham.hpp:33
pint getCurrentLength() const
Definition: Bresenham.hpp:53
constexpr auto max(const T &a, const U &b) -> decltype(a< b ? b :a)
Return the larger of two numbers/objects.
Definition: MinMaxFix.hpp:22
Pixel(pint x, pint y)
Definition: Bresenham.hpp:23
unsigned int distanceSquared(Pixel other) const
Definition: Bresenham.hpp:26
size_t printTo(Print &p) const override
Definition: Bresenham.cpp:5