Window.cpp: Fixed the line drawing code and improved it's speed.

This commit is contained in:
Fabian Schlenz 2019-10-24 06:31:52 +02:00
parent 66c0124072
commit b5343b59e5
2 changed files with 34 additions and 31 deletions

View File

@ -29,9 +29,9 @@ public:
void setSubPixel(accum88 x, accum88 y, CRGB* color, SubpixelRenderingMode m = SUBPIXEL_RENDERING_ADD);
void setPixelByIndex(uint16_t index, CRGB* color);
void raisePixel(uint8_t x, uint8_t y, CRGB* color);
void line(accum88 x1, accum88 y1, accum88 x2, accum88 y2, CRGB* color);
void lineWithAngle(uint8_t x, uint8_t y, uint8_t angle, uint8_t length, CRGB* color);
void lineWithAngle(uint8_t x, uint8_t y, uint8_t angle, uint8_t startdist, uint8_t length, CRGB* color);
void line(saccum78 x1, saccum78 y1, saccum78 x2, saccum78 y2, CRGB* color);
void lineWithAngle(uint8_t x, uint8_t y, uint16_t angle, uint8_t length, CRGB* color);
void lineWithAngle(uint8_t x, uint8_t y, uint16_t angle, uint8_t startdist, uint8_t length, CRGB* color);
void circle(uint8_t x, uint8_t y, uint8_t r, CRGB* color);
uint16_t coordsToGlobalIndex(uint8_t x, uint8_t y);
uint16_t localToGlobalIndex(uint16_t);

View File

@ -150,27 +150,29 @@ void Window::fadeToBlackBy(fract8 speed) {
}
}
void Window::line(accum88 x1, accum88 y1, accum88 x2, accum88 y2, CRGB* color) {
void Window::line(saccum78 x1, saccum78 y1, saccum78 x2, saccum78 y2, CRGB* color) {
// Bresenham algorithm
const uint8_t stepsize = 32;
saccum78 dx = x2-x1;
saccum78 dy = y2-y1;
accum88 x = x1;
accum88 y = y1;
saccum78 p = 2*dy - dx;
while (x < x2) {
if (p >= 0) {
setSubPixel(x, y, color, SUBPIXEL_RENDERING_RAISE);
y+=stepsize;
p = p + 2*dy - 2*dx;
} else {
setSubPixel(x, y, color, SUBPIXEL_RENDERING_RAISE);
p = p + 2*dy;
const uint8_t stepsize = 64;
saccum78 dx = abs(x2 - x1);
saccum78 dy = -abs(y2 - y1);
int8_t sx = x1<x2 ? 1 : -1;
int8_t sy = y1<y2 ? 1 : -1;
saccum78 err = dx + dy;
saccum78 e2;
uint8_t step = 0;
while (1) {
if (step == 0) setSubPixel(x1, y1, color, SUBPIXEL_RENDERING_RAISE);
if (++step >= stepsize) step=0;
if (x1>>8==x2>>8 && y1>>8==y2>>8) break;
e2 = 2*err;
if (e2 > dy) {
err += dy;
x1 += sx;
}
if (e2 < dx) {
err += dx;
y1 += sy;
}
x+=stepsize;
}
}
@ -202,22 +204,23 @@ void Window::circle(uint8_t x0, uint8_t y0, uint8_t radius, CRGB* color) {
}
}
void Window::lineWithAngle(uint8_t x, uint8_t y, uint8_t angle, uint8_t length, CRGB* color) {
void Window::lineWithAngle(uint8_t x, uint8_t y, uint16_t angle, uint8_t length, CRGB* color) {
lineWithAngle(x, y, angle, 0, length, color);
}
void Window::lineWithAngle(uint8_t x, uint8_t y, uint8_t angle, uint8_t startdist, uint8_t length, CRGB* color) {
accum88 x1 = x<<8;
accum88 y1 = y<<8;
void Window::lineWithAngle(uint8_t x, uint8_t y, uint16_t angle, uint8_t startdist, uint8_t length, CRGB* color) {
//LOGln("lineWithAngle called. x: %d.%03d, y: %d.%03d, angle: %d", x>>8, x&0xFF, y>>8, y&0xFF, angle);
saccum78 x1 = x<<8;
saccum78 y1 = y<<8;
if (startdist > 0) {
x1 = (x<<8) + (startdist<<8) * cos8(angle) / 256;
y1 = (y<<8) + (startdist<<8) * sin8(angle) / 256;
x1 = (x<<8) + (startdist<<8) * cos16(angle) / 0x10000;
y1 = (y<<8) + (startdist<<8) * sin16(angle) / 0x10000;
}
accum88 x2 = (x<<8) + (startdist + length) * cos16(angle);
accum88 y2 = (y<<8) + (startdist + length) * sin16(angle);
saccum78 x2 = (x<<8) + ((startdist + length)<<8) * cos16(angle) / 0x10000;
saccum78 y2 = (y<<8) + ((startdist + length)<<8) * sin16(angle) / 0x10000;
//LOGln("x1: %d.%03d, y1: %d.%03d, x2: %d.%03d, y2: %d.%03d", x1>>8, x1&0xFF, y1>>8, y1&0xFF, x2>>8, x2&0xFF, y2>>8, y2&0xFF);
line(x1, y1, x2, y2, color);
}