Control Surface
main
MIDI Control Surface library for Arduino
Toggle main menu visibility
Loading...
Searching...
No Matches
src
Display
Helpers
Bresenham.hpp
Go to the documentation of this file.
1
#pragma once
2
3
#include <
AH/Arduino-Wrapper.h
>
4
5
#include <AH/STL/cmath>
6
#include <AH/STL/limits>
7
#include <Printable.h>
8
#include <
Settings/SettingsWrapper.hpp
>
9
10
BEGIN_CS_NAMESPACE
11
12
template
<
typename
T>
13
static
constexpr
inline
int
sgn
(T val) {
14
return
(T(0) < val) - (val < T(0));
15
}
16
18
class
BresenhamLine
{
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
(std::abs(
dx
)),
37
ady
(std::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
(
float
angle) {
78
return
std::round(
static_cast<
float
>
(
errorScalingFactor
) *
79
std::cos(angle));
80
}
81
82
static
int
sin
(
float
angle) {
83
return
std::round(
static_cast<
float
>
(
errorScalingFactor
) *
84
std::sin(angle));
85
}
86
87
private
:
88
const
static
int
errorScalingFactor
= std::numeric_limits<int>::max() / 2;
89
Pixel
px
;
90
int
dx
,
dy
;
91
int
adx
,
ady
;
92
int
xinc
,
yinc
;
93
bool
steep
;
94
int
error
;
95
pint
length
= 0;
96
};
97
98
END_CS_NAMESPACE
Arduino-Wrapper.h
sgn
static constexpr int sgn(T val)
Definition
Bresenham.hpp:13
END_CS_NAMESPACE
#define END_CS_NAMESPACE
Definition
Settings/NamespaceSettings.hpp:14
BEGIN_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Definition
Settings/NamespaceSettings.hpp:11
SettingsWrapper.hpp
BresenhamLine::error
int error
Definition
Bresenham.hpp:94
BresenhamLine::adx
int adx
Definition
Bresenham.hpp:91
BresenhamLine::next
Pixel next()
Definition
Bresenham.hpp:56
BresenhamLine::pint
int16_t pint
Definition
Bresenham.hpp:20
BresenhamLine::steep
bool steep
Definition
Bresenham.hpp:93
BresenhamLine::dy
int dy
Definition
Bresenham.hpp:90
BresenhamLine::dx
int dx
Definition
Bresenham.hpp:90
BresenhamLine::xinc
int xinc
Definition
Bresenham.hpp:92
BresenhamLine::BresenhamLine
BresenhamLine(Pixel start, float angle)
Definition
Bresenham.hpp:47
BresenhamLine::ady
int ady
Definition
Bresenham.hpp:91
BresenhamLine::errorScalingFactor
static const int errorScalingFactor
Definition
Bresenham.hpp:88
BresenhamLine::sin
static int sin(float angle)
Definition
Bresenham.hpp:82
BresenhamLine::cos
static int cos(float angle)
Definition
Bresenham.hpp:77
BresenhamLine::BresenhamLine
BresenhamLine(Pixel start, int cos, int sin)
Definition
Bresenham.hpp:33
BresenhamLine::length
pint length
Definition
Bresenham.hpp:95
BresenhamLine::yinc
int yinc
Definition
Bresenham.hpp:92
BresenhamLine::px
Pixel px
Definition
Bresenham.hpp:89
BresenhamLine::getCurrentLength
pint getCurrentLength() const
Definition
Bresenham.hpp:54
BresenhamLine::Pixel
Definition
Bresenham.hpp:21
BresenhamLine::Pixel::Pixel
Pixel()=default
BresenhamLine::Pixel::Pixel
Pixel(pint x, pint y)
Definition
Bresenham.hpp:23
BresenhamLine::Pixel::y
pint y
Definition
Bresenham.hpp:22
BresenhamLine::Pixel::distanceSquared
unsigned int distanceSquared(Pixel other) const
Definition
Bresenham.hpp:26
BresenhamLine::Pixel::x
pint x
Definition
Bresenham.hpp:22
BresenhamLine::Pixel::printTo
size_t printTo(Print &p) const override
Definition
Bresenham.cpp:5
Generated by
1.17.0