Skip to content

Commit

Permalink
added line drawing in all 8 octants with unoptimized bresenham's alg
Browse files Browse the repository at this point in the history
  • Loading branch information
DeveloperGY committed Dec 22, 2022
1 parent f4865e0 commit 013e28c
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 9 deletions.
Binary file modified bin/libtermDisplay.a
Binary file not shown.
Binary file modified bin/out
Binary file not shown.
Binary file modified obj/draw.o
Binary file not shown.
42 changes: 34 additions & 8 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,44 @@ int main(void)
return -1;
}

td_drawStrokeRect(2, 2, 5, 5, '#', TD_COLOR_WHITE, TD_COLOR_WHITE);
// td_drawFillRect(3,3, 3, 3, '.', TD_COLOR_WHITE, TD_COLOR_DEFAULT);
td_drawPoint(3, 3, '@', TD_COLOR_YELLOW, TD_COLOR_DEFAULT);
td_drawRect(10, 7, 22, 12, 's', 'f', TD_COLOR_YELLOW, TD_COLOR_MAGENTA, TD_COLOR_WHITE, TD_COLOR_RED);
td_display();
char ch;

td_terminate();
int x = 3;
int y = 3;

while (getch() != 'e')
while (ch != 'q')
{

td_drawStrokeRect(2, 2, 5, 5, '#', TD_COLOR_WHITE, TD_COLOR_WHITE);
td_drawFillRect(3,3, 3, 3, '.', TD_COLOR_WHITE, TD_COLOR_DEFAULT);
td_drawRect(10, 7, 22, 12, 's', 'f', TD_COLOR_YELLOW, TD_COLOR_MAGENTA, TD_COLOR_WHITE, TD_COLOR_RED);
td_drawLine(0, 0, 10, 4, '*', TD_COLOR_RED, TD_COLOR_CYAN);
td_drawLine(0, 4, 10, 0, '*', TD_COLOR_BLACK, TD_COLOR_BLUE);
td_drawLine(0, 0, 4, 10, '*', TD_COLOR_CYAN, TD_COLOR_RED);
td_drawLine(0, 10, 4, 0, '*', TD_COLOR_BLUE, TD_COLOR_BLACK);
td_drawPoint(x, y, '@', TD_COLOR_YELLOW, TD_COLOR_DEFAULT);
td_display();

ch = getch();

if (ch == 'd')
{
x++;
}
if (ch == 'a')
{
x--;
}
if (ch == 'w')
{
y--;
}
if (ch == 's')
{
y++;
}
}

td_terminate();

return 0;
}
125 changes: 125 additions & 0 deletions src/termDisplay/draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ void td_display()

printf("\e[1;1H\e[2J"); // Clear the terminal using escape sequences

// change to create a string with a singular print call

for (int i=0; i<TD_HEIGHT; i++)
{
for (int j=0; j<TD_WIDTH; j++)
Expand All @@ -29,6 +31,17 @@ void td_display()
}
printf("\n");
}

for (int i=0; i<TD_HEIGHT; i++)
{
for (int j=0; j<TD_WIDTH; j++)
{
TD_CHAR_BUFFER[i][j] = ' ';
TD_FG_COLOR_BUFFER[i][j] = TD_COLOR_DEFAULT;
TD_BG_COLOR_BUFFER[i][j] = TD_COLOR_DEFAULT + TD_FG_TO_BG_OFFSET;
}
}

return;
}

Expand Down Expand Up @@ -94,5 +107,117 @@ void td_drawRect(int x, int y, unsigned int width, unsigned int height, char str
}
}
}
return;
}

void td_drawLine(int x1, int y1, int x2, int y2, char c, int fgColor, int bgColor)
{
float slope = (double)(y2 - y1) / (double)(x2 - x1);

int positiveSlope = slope >= 0;
int majorSlope = fabs(slope) > 1;

if (positiveSlope && !majorSlope)
{
if (x2 < x1) // swap to make the line always go left to right
{
int temp = x2;
x2 = x1;
x1 = temp;

temp = y2;
y2 = y1;
y1 = temp;
}

int yOffset = 0;

for (int i=0; i<=x2-x1; i++)
{
double y = slope * (i + x1);
if (y - 0.5 >= yOffset)
{
yOffset++;
}
td_drawPoint(x1 + i, yOffset + y1, c, fgColor, bgColor);
}
}
else if (!positiveSlope && !majorSlope)
{
if (x2 < x1) // swap to make the line always go left to right
{
int temp = x2;
x2 = x1;
x1 = temp;

temp = y2;
y2 = y1;
y1 = temp;
}
int yOffset = 0;

for (int i=0; i<=x2-x1; i++)
{
double y = slope * (i + x1);
if (y + 0.5 <= yOffset)
{
yOffset--;
}
td_drawPoint(x1 + i, yOffset + y1, c, fgColor, bgColor);
}
}
else if (positiveSlope && majorSlope)
{
if (y2 < y1) // swap to make the line always go left to right
{
int temp = x2;
x2 = x1;
x1 = temp;

temp = y2;
y2 = y1;
y1 = temp;
}

int xOffset = 0;

for (int i=0; i<=y2-y1; i++)
{
double x = (double)(i+y1)/slope;

if (x - 0.5 >= xOffset)
{
xOffset++;
}
td_drawPoint(xOffset + x1, i + y1, c, fgColor, bgColor);
}
}
else if (!positiveSlope && majorSlope)
{
if (y2 < y1) // swap to make the line always go left to right
{
int temp = x2;
x2 = x1;
x1 = temp;

temp = y2;
y2 = y1;
y1 = temp;
}

int xOffset = 0;

for (int i=0; i<=y2-y1; i++)
{
double x = (double)(i+y1)/slope;

if (x + 0.5 <= xOffset)
{
xOffset--;
}
td_drawPoint(xOffset + x1, i + y1, c, fgColor, bgColor);
}
}

return;
}
17 changes: 16 additions & 1 deletion src/termDisplay/termDisplay.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <math.h>

#include <termios.h>

Expand Down Expand Up @@ -163,7 +164,21 @@ void td_drawFillRect(int x, int y, unsigned int width, unsigned int height, char
*/
void td_drawRect(int x, int y, unsigned int width, unsigned int height, char stroke, char fill, int strokeFGColor, int strokeBGColor, int fillFGColor, int fillBGColor);

// draw line
/**
* @brief Draws a line between the selected coordinates with the specified character and colors
*
* @param x1 X Coordinate of the first point
* @param y1 Y Coordinate of the first point
*
* @param x2 X Coordinate of the second point
* @param y2 Y Coordinate of the second point
*
* @param c The character to draw
*
* @param fgColor The foreground color
* @param bgColor The background color
*/
void td_drawLine(int x1, int y1, int x2, int y2, char c, int fgColor, int bgColor);

#ifdef __cplusplus
}
Expand Down

0 comments on commit 013e28c

Please sign in to comment.