Fixed line and circle drawing code in Window. But it looks bad ans some form of dithering is definitely required.

This commit is contained in:
Fabian Schlenz 2019-09-04 05:58:36 +02:00
parent 5b70511570
commit 1122546853
3 changed files with 42 additions and 60 deletions

View File

@ -4,6 +4,8 @@
#include "prototypes.h"
class Window {
private:
void _circle_point(int x0, int y0, int x1, int y1, CRGB* color);
public:
const uint8_t x, y;
const uint8_t width, height;

View File

@ -68,54 +68,52 @@ void Window::fadeToBlackBy(fract8 speed) {
void Window::line(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, CRGB* color) {
// Bresenham algorithm
int16_t dx = abs(x2-x1);
int16_t dy = abs(y2-y1);
int8_t sx = x1<x2 ? 1 : -1;
int8_t sy = y1<y2 ? 1 : -1;
int16_t err = dx + dy;
int16_t e2;
while (1) {
setPixel(x1, y1, color);
if (x1==x2 && y1==y2) break;
e2 = 2*err;
if (e2 > dy) { err += dy; x1 += sx; }
if (e2 < dx) { err += dx; y1 += sy; }
int dx = x2-x1;
int dy = y2-y1;
int x = x1;
int y = y1;
int p = 2*dy - dx;
while (x < x2) {
if (p >= 0) {
setPixel(x, y, color);
y++;
p = p + 2*dy - 2*dx;
} else {
setPixel(x, y, color);
p = p + 2*dy;
}
x++;
}
}
void Window::_circle_point(int x0, int y0, int x1, int y1, CRGB* color) {
setPixel(x0+x1, y0+y1, color);
setPixel(x0-x1, y0+y1, color);
setPixel(x0+x1, y0-y1, color);
setPixel(x0-x1, y0-y1, color);
setPixel(x0+y1, y0+x1, color);
setPixel(x0-y1, y0+x1, color);
setPixel(x0+y1, y0-x1, color);
setPixel(x0-y1, y0-x1, color);
}
void Window::circle(uint8_t x0, uint8_t y0, uint8_t radius, CRGB* color) {
// Again, Bresenham
uint8_t f = 1 - radius;
int16_t ddF_x = 0;
int16_t ddF_y = -2 * radius;
uint8_t x = 0;
uint8_t y = radius;
setPixel(x0, y0 + radius, color);
setPixel(x0, y0 - radius, color);
setPixel(x0 + radius, y0, color);
setPixel(x0 - radius, y0, color);
while (x < y) {
if (f >= 0) {
y--;
ddF_y += 2;
f += ddF_y;
}
int x=0, y=radius;
int d=3 - 2*radius;
_circle_point(x0, y0, x, y, color);
while (y >= x) {
x++;
ddF_x += 2;
f += ddF_x + 1;
setPixel(x0 + x, y0 + y, color);
setPixel(x0 - x, y0 + y, color);
setPixel(x0 + x, y0 - y, color);
setPixel(x0 - x, y0 - y, color);
setPixel(x0 + y, y0 + x, color);
setPixel(x0 - y, y0 + x, color);
setPixel(x0 + y, y0 - x, color);
setPixel(x0 - y, y0 - x, color);
if (d>0) {
y--;
d = d + 4*(x-y) + 10;
} else {
d = d + 4*x + 6;
}
_circle_point(x0, y0, x, y, color);
}
}

View File

@ -1,18 +0,0 @@
Original:
DATA: [==== ] 40.5% (used 33164 bytes from 81920 bytes)
PROGRAM: [==== ] 36.0% (used 375664 bytes from 1044464 bytes)
lots of compares:
DATA: [==== ] 38.6% (used 31640 bytes from 81920 bytes)
PROGRAM: [==== ] 35.6% (used 371740 bytes from 1044464 bytes)
crc32:
DATA: [==== ] 38.5% (used 31532 bytes from 81920 bytes)
PROGRAM: [==== ] 35.6% (used 371456 bytes from 1044464 bytes)
original:
DATA: [==== ] 39.4% (used 32256 bytes from 81920 bytes) │pitrix_dev/log MQTT * Received data for topic pitrix_dev/uptime with payload 391
PROGRAM: [==== ] 37.4% (used 390380 bytes from 1044464 bytes)
DATA: [==== ] 37.4% (used 30608 bytes from 81920 bytes) │pitrix_dev/free_heap 43624
PROGRAM: [==== ] 37.4% (used 390556 bytes from 1044464 bytes)