From dfe85bc6a74b03fafc90fae74b34eb918b15ff13 Mon Sep 17 00:00:00 2001 From: knolax <1339802534.kk@gmail.com> Date: Thu, 22 Feb 2018 08:24:05 -0500 Subject: added line drawing --- wsim.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 72 insertions(+), 1 deletion(-) diff --git a/wsim.c b/wsim.c index b369b9d..cfaa089 100644 --- a/wsim.c +++ b/wsim.c @@ -123,6 +123,71 @@ void setpixel(uint8_t x, uint8_t y,uint8_t set) { } return; } +/*takes two points and draws a line between them, dot specifies that every nth x + * point be drawn, if dot != 1, then a dotted line is drawn + */ +void drawline(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, uint8_t dot) { + /*temporary x and y for switching x1,y1 and x2,y1 if x1>x2*/ + uint8_t tv; + /*number of x values to translate*/ + uint8_t dx; + /*number of y values to translate*/ + uint8_t dy; + /* for whether dy should be positive or negative, 1 pos, 0 neg*/ + uint8_t dir; + /*drawin index*/ + uint8_t xi = 0; + /*makes sure that x1 < x2, otherwise switch them arround*/ + if (x1 > x2) { + tv = x1; + x1 = x2; + x2 = tv; + tv = y1; + y1 = y2; + y2 = tv; + } + dx = x2 - x1; + /*find absolute + * dir is used since i'm planning to port to a platform that may not + * have signed ints. + */ + if (y1 > y2) { + dir = 0; + dy = y1 - y2; + } else { + dir = 1; + dy = y2 - y1; + } + /* + *For vertical Lines + * Uses xi as a y index now + */ + if (x1 == x2) { + while (xi <= dy) { + if (dir) { + setpixel(x1, y1 + xi, 1); + } else { + setpixel(x1, y1 - xi, 1); + } + xi++; + } + } + /* + * loop through x and draws y in line, dots done with modulus. + */ + while (xi <= (dx)) { + if ((xi % dot) == 0) { + if (dir) { + setpixel(x1 + xi, y1 + ((dy/dx * xi)),1); + } else { + setpixel(x1 + xi, y1 - ((dy/dx * xi)) ,1); + } + } + xi++; + } + + return; +} int main (int argc, char * argv[]) { /*holds keypress*/ char input; @@ -130,8 +195,14 @@ int main (int argc, char * argv[]) { memset(map, 0, sizeof(map[0][0]) * 100 * 50); printmap(); while (state & STATE_LOOP) { - setpixel(0,0,2); + setpixel(10,20,2); + drawline(0,0,20,20,1); + drawline(0,40,10,20,1); + drawline(10,50,0,40,2); + drawline(10,0,10,200,1); + drawline(10,100,100,100,4); printmap(); + /* checks input*/ input = getc(stdin); /*haven't set the terminal to raw mode so will still need -- cgit v1.1