Skip to content

Commit be0b647

Browse files
author
Jason Dorie
committed
Fixed weird line issues
Previous algorithm was buggy - sets of lines drawn end-to-end did not match, and were not straight, likely due to rounding issues.
1 parent d3e3c7e commit be0b647

File tree

1 file changed

+39
-43
lines changed

1 file changed

+39
-43
lines changed

Learn/Simple Libraries/Social/libbadgetools/oled_line.c

Lines changed: 39 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,54 +6,50 @@ volatile screen *self;
66

77
void line(int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32_t c)
88
{
9-
int32_t _parm__0015[12];
10-
_parm__0015[0] = x0;
11-
_parm__0015[1] = y0;
12-
_parm__0015[2] = x1;
13-
_parm__0015[3] = y1;
14-
_parm__0015[4] = c;
15-
// Draws a line on the screen
16-
// Adapted/converted from psuedo-code found on Wikipedia:
17-
// http://en.wikipedia.org/wiki/Bresenham's_line_algorithm
18-
_parm__0015[5] = -((abs((_parm__0015[3] - _parm__0015[1]))) > (abs((_parm__0015[2] - _parm__0015[0]))));
19-
if (_parm__0015[5]) {
20-
screen_swap((int32_t)(&_parm__0015[0]), (int32_t)(&_parm__0015[1]));
21-
screen_swap((int32_t)(&_parm__0015[2]), (int32_t)(&_parm__0015[3]));
22-
}
23-
if (_parm__0015[0] > _parm__0015[2]) {
24-
screen_swap((int32_t)(&_parm__0015[0]), (int32_t)(&_parm__0015[2]));
25-
screen_swap((int32_t)(&_parm__0015[1]), (int32_t)(&_parm__0015[3]));
26-
}
27-
_parm__0015[6] = _parm__0015[2] - _parm__0015[0];
28-
_parm__0015[7] = abs((_parm__0015[3] - _parm__0015[1]));
29-
_parm__0015[8] = _parm__0015[6] << 1;
30-
_parm__0015[10] = _parm__0015[1];
31-
if (_parm__0015[1] < _parm__0015[3]) {
32-
_parm__0015[9] = 1;
33-
} else {
34-
_parm__0015[9] = -1;
35-
}
9+
int dx = x1-x0;
10+
signed char ix = (dx > 0) - (dx < 0);
11+
dx = abs(dx) << 1;
12+
13+
int dy = y1 - y0;
14+
signed char iy = (dy > 0) - (dy < 0);
15+
dy = abs(dy) << 1;
16+
17+
point( x0, y0, c);
18+
19+
if( dx >= dy )
3620
{
37-
int32_t _limit__0041 = _parm__0015[2];
38-
int32_t _step__0042 = 1;
39-
_parm__0015[11] = _parm__0015[0];
40-
if (_parm__0015[11] >= _limit__0041) _step__0042 = -_step__0042;
41-
do {
42-
if (_parm__0015[5]) {
43-
point(_parm__0015[10], _parm__0015[11], _parm__0015[4]);
44-
} else {
45-
point(_parm__0015[11], _parm__0015[10], _parm__0015[4]);
21+
int err = dy - (dx >> 1);
22+
while(x0 != x1)
23+
{
24+
if( (err >= 0) && (err || (ix > 0)) )
25+
{
26+
err -= dx;
27+
y0 += iy;
4628
}
47-
_parm__0015[8] = _parm__0015[8] - _parm__0015[7];
48-
if (_parm__0015[8] < 0) {
49-
_parm__0015[10] = _parm__0015[10] + _parm__0015[9];
50-
_parm__0015[8] = _parm__0015[8] + _parm__0015[6];
29+
30+
err += dy;
31+
x0 += ix;
32+
point( x0, y0, c );
33+
}
34+
}
35+
else
36+
{
37+
int err = dx - (dy >> 1);
38+
while(y0 != y1)
39+
{
40+
if( (err >= 0) && (err || (iy > 0)) )
41+
{
42+
err -= dy;
43+
x0 += ix;
5144
}
52-
_parm__0015[11] = _parm__0015[11] + _step__0042;
53-
} while (((_step__0042 > 0) && (_parm__0015[11] <= _limit__0041)) || ((_step__0042 < 0) && (_parm__0015[11] >= _limit__0041)));
45+
46+
err += dx;
47+
y0 += iy;
48+
point( x0, y0, c );
49+
}
5450
}
51+
5552
if (self->AutoUpdate) screen_update();
56-
//return 0;
5753
}
5854

5955
/*

0 commit comments

Comments
 (0)