DSA/Lab_3/code.c

91 lines
2.0 KiB
C
Raw Permalink Normal View History

2024-03-09 17:32:59 +02:00
#include <ncurses.h>
struct vec2 {
int x;
int y;
};
struct borders {
int top;
int bottom;
int left;
int right;
};
int main()
{
initscr(); // initialise screen
clear(); // clear the empty buffer just in case
noecho(); // disable the direct typing to the terminal so it doesn't mess with the application
// here we define utility variables
// borders for a pen point
struct borders b;
b.bottom = LINES;
b.top = -1;
b.left = -1;
b.right = COLS;
// pen point position
struct vec2 position;
position.x = COLS;
position.y = LINES - 1;
// pen point direction
struct vec2 direction;
direction.x = -1;
direction.y = 0;
// here we start the drawing
for (int i = 0; i < COLS*LINES; i++)
{
// move & paint
position.x += direction.x;
position.y += direction.y;
mvaddch(position.y, position.x, '*');
// flush the buffer to show result on the screen
refresh();
// check if we are about to meet the border
if (position.x + direction.x <= b.left) { // if yes, then...
// turn right
direction.x = 0;
direction.y = -1;
// grab the border and pull it towards us as bit
b.bottom--;
}
// and then repeat for every single border we have...
else if (position.y + direction.y <= b.top) {
direction.x = 1;
direction.y = 0;
b.left++;
}
else if (position.x + direction.x >= b.right) {
direction.x = 0;
direction.y = 1;
b.top++;
}
else if (position.y + direction.y >= b.bottom) {
direction.x = -1;
direction.y = 0;
b.right--;
}
// and we wait... wait... wait...
napms(20);
}
// hold the buffer until the keypress
getch();
// finish & cleanup
endwin();
return 0;
}