-
Notifications
You must be signed in to change notification settings - Fork 1
/
Draw2D.hpp
125 lines (110 loc) · 1.72 KB
/
Draw2D.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#pragma once
class Draw2D
{
private:
Stepper& x;
Stepper& y;
int16_t posX;
int16_t posY;
void (*setDrawing)(bool);
void rawMove(int16_t dx, int16_t dy)
{
if(dx == 0 && dy == 0)
{
return;
}
/*else if(dx == 0)
{
y.step(dy);
posY += dy;
return;
}
else if(dy == 0)
{
x.step(dx);
posX += dx;
return;
}*/
posX += dx;
posY += dy;
bool xReverse = dx < 0 ? true : false;
bool yReverse = dy < 0 ? true : false;
if(dx < 0)
dx = -dx;
if(dy < 0)
dy = -dy;
Stepper *a;
Stepper *b;
int16_t da;
int16_t db;
bool aReverse;
bool bReverse;
if(dx < dy)
{
a = &y;
b = &x;
da = dy;
db = dx;
aReverse = yReverse;
bReverse = xReverse;
}
else
{
a = &x;
b = &y;
da = dx;
db = dy;
aReverse = xReverse;
bReverse = yReverse;
}
//Bresenham algorithm
//constrains: 0 <= db/da <= 1 and da > 0 (thats why we put the smaller one in 'b' above)
int16_t D = 2 * db - da;
int16_t deltaE = 2 * db;
int16_t deltaNE = 2 * (db - da);
while(da > 0)
{
if(D < 0)
{
D += deltaE;
}
else
{
D += deltaNE;
b->singleStep(bReverse);
}
a->singleStep(aReverse);
da--;
}
}
public:
Draw2D(Stepper& _x, Stepper& _y,
int16_t _posX, int16_t _posY,
void (*switchFunc)(bool)
)
: x(_x), y(_y), posX(_posX), posY(_posY), setDrawing(switchFunc)
{
}
void moveTo(int16_t x, int16_t y)
{
setDrawing(false);
rawMove(x - posX, y - posY);
}
void move(int16_t dx, int16_t dy)
{
setDrawing(false);
rawMove(dx, dy);
}
void lineTo(int16_t x, int16_t y)
{
setDrawing(true);
rawMove(x - posX, y - posY);
}
void line(int16_t dx, int16_t dy)
{
setDrawing(true);
rawMove(dx, dy);
}
//TODO arcTo
//TODO curveTo
};