From 0cd630609ea14432751cf015467af181340c61ca Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Fri, 16 Apr 2021 17:09:54 +0200 Subject: Update draw_line to fix offset error --- src/main.c | 42 ++++++++++++++++-------------------------- 1 file changed, 16 insertions(+), 26 deletions(-) diff --git a/src/main.c b/src/main.c index b86a830..6dcbf64 100644 --- a/src/main.c +++ b/src/main.c @@ -102,17 +102,15 @@ put_text(int x, int y, Color clr, char *msg) { // Bresenham's line drawing algorithm using exclusively integer arithmetic. static void draw_line(int x0, int y0, int x1, int y1, Color clr) { - // The line length in color units. - int pitch = SCREEN_WIDTH; - // Pointer to the initial position of the screen buffer where we will start - // writing our data. We need to multiply by 2 because in mode 3 we have - // 2 bytes per pixel. - vu16 *destination = (u16*)(SCREEN_BUFFER + y0 * pitch * 2 + x0 * 2); + // writing our data. + vu16 *destination = (u16*)(SCREEN_BUFFER + y0 * SCREEN_WIDTH + x0); // Adjust the step direction and calculate deltas. int x_step; + int y_step; int dx; + int dy; if (x0 > x1) { x_step = -1; dx = x0 - x1; @@ -120,20 +118,14 @@ draw_line(int x0, int y0, int x1, int y1, Color clr) { x_step = 1; dx = x1 - x0; } - int y_step; - int dy; if (y0 > y1) { - y_step = -pitch; + y_step = -SCREEN_WIDTH; dy = y0 - y1; } else { - y_step = +pitch; + y_step = +SCREEN_WIDTH; dy = y1 - y0; } - // Precalculate 2 * deltas for x and y. - int ddx = dx + dx; - int ddy = dy + dy; - if(dy == 0) { // Horizontal line. for(int i = 0; i <= dx; i++) { @@ -146,27 +138,27 @@ draw_line(int x0, int y0, int x1, int y1, Color clr) { } } else if (dx >= dy){ // Positive slope. - int diff = ddy - dx; + int diff = 2 * dy - dx; for (int i = 0; i <= dx; ++i) { *destination = clr; if (diff >= 0) { destination += y_step; - diff -= ddx; + diff -= 2 * dx; } destination += x_step; - diff += ddy; + diff += 2 * dy; } } else { // Negative slope. - int diff = ddx - dy; + int diff = 2 * dx - dy; for (int i = 0; i <= dy; ++i) { *destination = clr; if (diff >= 0) { destination += x_step; - diff -= ddy; + diff -= 2 * dy; } destination += y_step; - diff += ddx; + diff += 2 * dx; } } } @@ -316,10 +308,10 @@ int main(void) { draw_fill_rect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, COLOR_GREY); int side = 60; - int x = 88; - int y = 70; int line = 35; - int height = sqrt(side * side - (side / 2) * (side / 2)); + int height = side * 0.5; + int x = SCREEN_WIDTH / 2 - height / 2; + int y = SCREEN_HEIGHT / 2; // Draw red triangle. draw_line(x + height - 1, y - side / 2, x, y - 1, COLOR_RED); @@ -335,8 +327,6 @@ int main(void) { // Draw white line at triangle tip. draw_line(x + height, y - side / 2, x + height, y + side / 2, COLOR_WHITE); draw_line(x + height + 1, y - side / 2, x + height + 1, y + side / 2, COLOR_WHITE); - draw_line(x + height + 2, y - side / 2, x + height + 2, y + side / 2, COLOR_WHITE); - // draw_line(x + height + 3, y - side / 2, x + height + 3, y + side / 2, COLOR_WHITE); // Double triangle line. draw_line(x - 1, y - side / 2, x - 1, y + side / 2, COLOR_WHITE); @@ -349,7 +339,7 @@ int main(void) { draw_line(x - line, y + 1, x, y + 1, COLOR_WHITE); draw_line(x + height, y + 1, x + height + line, y + 1, COLOR_WHITE); - put_text(77, 125, COLOR_RED, "0xbadd10de"); + put_text(SCREEN_WIDTH / 2 - 8 * 10 / 2, 125, COLOR_RED, "0xbadd10de"); int frame_counter = 0; while(true) { -- cgit v1.2.1