From 40ebb56ea750200db47bca1884bcb5ca4fea9ed1 Mon Sep 17 00:00:00 2001 From: AceMouse Date: Sat, 27 Aug 2022 11:01:43 +0200 Subject: [PATCH] fix staircase lines --- olive.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/olive.c b/olive.c index 92dc16e..121debb 100644 --- a/olive.c +++ b/olive.c @@ -112,7 +112,6 @@ void olivec_draw_line(uint32_t *pixels, size_t pixels_width, size_t pixels_heigh int x1, int y1, int x2, int y2, uint32_t color) { - // TODO: fix the olivec_draw_line stairs int dx = x2 - x1; int dy = y2 - y1; @@ -122,16 +121,23 @@ void olivec_draw_line(uint32_t *pixels, size_t pixels_width, size_t pixels_heigh if (x1 > x2) OLIVEC_SWAP(int, x1, x2); for (int x = x1; x <= x2; ++x) { if (0 <= x && x < (int) pixels_width) { - int sy1 = dy*x/dx + c; - int sy2 = dy*(x + 1)/dx + c; - if (sy1 > sy2) OLIVEC_SWAP(int, sy1, sy2); - for (int y = sy1; y <= sy2; ++y) { - if (0 <= y && y < (int) pixels_height) { - pixels[y*pixels_width + x] = color; - } + int y = dy*x/dx + c; + if (0 <= y && y < (int) pixels_height) { + pixels[y*pixels_width + x] = color; } } } + if (dy != 0){ + if (y1 > y2) OLIVEC_SWAP(int, y1, y2); + for (int y = y1; y <= y2; ++y) { + if (0 <= y && y < (int) pixels_height) { + int x = ((y-c)*dx)/dy; + if (0 <= x && x < (int) pixels_width) { + pixels[y*pixels_width + x] = color; + } + } + } + } } else { int x = x1; if (0 <= x && x < (int) pixels_width) {