From 65d27473a29d29ebf7e05204f4d43805d490a317 Mon Sep 17 00:00:00 2001 From: kdx Date: Mon, 8 May 2023 19:23:25 +0200 Subject: line drawing --- src/main.c | 1 + src/px.c | 31 +++++++++++++++++++++++++++++++ src/px.h | 3 +++ 3 files changed, 35 insertions(+) diff --git a/src/main.c b/src/main.c index c3110a4..0e5e502 100644 --- a/src/main.c +++ b/src/main.c @@ -61,6 +61,7 @@ main(void) pxPal(3, 11); pxPalt(3, true); pxSpr(&spr, x, y + 8, .flip_x=true, .flip_y=true); + pxLine(0, 0, x, y, 2);; pxPset(0, 0, 10); pxPset(1, 0, 10); diff --git a/src/px.c b/src/px.c index 267b2e0..7458bcd 100644 --- a/src/px.c +++ b/src/px.c @@ -180,6 +180,37 @@ pxRectfill(int x0, int y0, int x1, int y1, PxCol col) pxPset(x, y, col); } +void +pxLine(int x0, int y0, int x1, int y1, PxCol col) +{ + int dx, dy, sx, sy, err, e2; + + dx = x1 - x0; + dx = (dx < 0) ? (-dx) : (dx); + dy = y1 - y0; + dy = (dy < 0) ? (dy) : (-dy); + sx = (x0 < x1) ? (1) : (-1); + sy = (y0 < y1) ? (1) : (-1); + err = dx + dy; + + for (;;) { + pxPset(x0, y0, col); + + if (x0 == x1 && y0 == y1) + break; + + e2 = 2 * err; + if (e2 >= dy) { + err += dy; + x0 += sx; + } + if (e2 <= dx) { + err += dx; + y0 += sy; + } + } +} + void _pxSpr(const PxSprArgs *args) { diff --git a/src/px.h b/src/px.h index 713f245..9eb9610 100644 --- a/src/px.h +++ b/src/px.h @@ -88,6 +88,9 @@ void pxRect(int x0, int y0, int x1, int y1, PxCol col); /* Draw filled rectangle. */ void pxRectfill(int x0, int y0, int x1, int y1, PxCol col); +/* Draw line. */ +void pxLine(int x0, int y0, int x1, int y1, PxCol col); + /* Draw sprite. */ #define pxSpr(...) _pxSpr(&(const PxSprArgs){ \ .x=0, .y=0, .ix=0, .iy=0, .w=-1, .h=-1, .flip_x=false, .flip_x=false, \ -- cgit v1.2.3