Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- draw_rectangle.c
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- */
- #include <stdio.h>
- #include <windows.h> // tracking cursor all the time, for cursor position functions
- // gotoxy(x,y) , wherex() , wherey(), showcursor(), hidecursor()
- #define MAX_COLUMNS 80 // of the console window
- #define MAX_ROWS 25 // of the console window
- // ----------CURSOR CONTROL FUNCTIONS------------------------------------------
- // make console cursor invisible
- void hidecursor()
- {
- HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
- CONSOLE_CURSOR_INFO info;
- info.dwSize = 20;
- info.bVisible = FALSE;
- SetConsoleCursorInfo(consoleHandle, &info);
- }
- // make console cursor invisible
- void showcursor()
- {
- HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
- CONSOLE_CURSOR_INFO info;
- info.dwSize = 20;
- info.bVisible = TRUE;
- SetConsoleCursorInfo(consoleHandle, &info);
- }
- // place cursor at position ( x, y ) = ( column, row )
- void gotoxy( int column, int row )
- {
- COORD coord;
- coord.X = column;
- coord.Y = row;
- SetConsoleCursorPosition( GetStdHandle(STD_OUTPUT_HANDLE), coord );
- }
- // return x coordinate (column) of current cursor position
- // on failure return -1
- int wherex()
- {
- CONSOLE_SCREEN_BUFFER_INFO csbi;
- if (!GetConsoleScreenBufferInfo( GetStdHandle(STD_OUTPUT_HANDLE), &csbi ) )
- return -1;
- return csbi.dwCursorPosition.X;
- }
- // return y coordinate (row) of current cursor position
- // on failure return -1
- int wherey()
- {
- CONSOLE_SCREEN_BUFFER_INFO csbi;
- if (!GetConsoleScreenBufferInfo( GetStdHandle( STD_OUTPUT_HANDLE ), &csbi ) )
- return -1;
- return csbi.dwCursorPosition.Y;
- }
- // END OF-----CURSOR CONTROL FUNCTIONS------------------------------------------
- // Function to print hollow rectangle with char ch, from cursor position row, column.
- // rows must be between 2 and MAX_ROWS-2 and
- // columns must be between 2 and MAX_COLUMNS-2 because
- // the smallest rectangle has no interior space, only the sides of the frame.
- void print_hollow_rectangle(char ch, int column, int row, int howMuchColumns, int howMuchRows)
- {
- int i;
- char str1[MAX_COLUMNS+1]; // str1 is string for first and last line of rectangle
- char str2[MAX_COLUMNS+1]; // str2 is string for middle lines of rectangle
- if( column + howMuchColumns < 1 ) // 2 for two vertical edges of the rectangle
- howMuchColumns = 2;
- if( row + howMuchRows < 2 ) // 2 for two horizontal edges of the rectangle
- howMuchRows = 2;
- if( column + howMuchColumns > MAX_COLUMNS ) // if right side of rectangle greater then
- howMuchColumns = MAX_COLUMNS - column; // MAX_COLUMNS, then truncate it
- if( row + howMuchRows > MAX_ROWS - 1 ) // if bottom side of rectangle greater then
- howMuchRows = MAX_ROWS - row; // MAX_ROWS, then truncate it
- for (i = 0; i < howMuchColumns; i++) // creating string for first and last line of rectangle
- str1[i] = ch;
- str1[i] = '\0'; // end of first and last line string
- // creating string for middle lines of rectangle
- str2[0] = ch; // first left character of middle lines string
- for (i = 1; i < howMuchColumns-1; i++) // middle characters of middle lines are blanko (space, ' ')
- str2[i] = ' ';
- str2[i] = ch; // last right character of middle lines string
- str2[i+1] = '\0'; // end of middle lines string
- // print rectangle
- gotoxy( column, row ); // move cursor to (column, row)
- fputs(str1, stdout); // print string for first line of rectangle without '\n'
- for (i = 0; i < howMuchRows-2; i++)
- {
- gotoxy( column, row+i+1 ); // move cursor to (column, row+i+1)
- fputs(str2, stdout); // print string for middle lines of rectangle without '\n'
- }
- gotoxy( column, row+i+1 ); // move cursor to (column, row+i+1)
- fputs(str1, stdout); // print string for last line of rectangle without '\n'
- } // print_hollow_rectangle()
- // Draws a rectangle with a single line
- void draw_rectangle_1( int x, int y, int width, int height )
- {
- int i;
- if( x + width < 1 ) // 2 for two vertical edges of the rectangle
- width = 2;
- if( y + height < 2 ) // 2 for two horizontal edges of the rectangle
- height = 2;
- if( x + width > MAX_COLUMNS ) // if right side of rectangle greater then
- width = MAX_COLUMNS - x; // MAX_COLUMNS, then truncate it
- if( y + height > MAX_ROWS - 1 ) // if bottom side of rectangle greater then
- height = MAX_ROWS - y; // MAX_ROWS, then truncate it
- gotoxy(x,y); // UL, upper left corner
- putchar(218);
- for(i=x+1; i<x+width;i++) { // UH, upper horizontal line
- gotoxy(i,y);
- putchar(196);
- }
- gotoxy(i-1,y); // UR, upper right corner
- putchar(191);
- for(i=y+1; i<y+height-1;i++) { // VH, left and right vertical line
- gotoxy(x,i); // left vertical line
- putchar(179);
- gotoxy(x+width-1,i); // right vertical line
- putchar(179);
- }
- gotoxy(x,y+height-1); // DL, down left corner
- putchar(192);
- for(i=x+1; i<x+width-1;i++) { // DH, down horizontal line
- gotoxy(i,y+height-1);
- putchar(196);
- }
- gotoxy(x+width-1,y+height-1); // DR, down right corner
- putchar(217);
- } // draw_rectangle_1()
- // Draws a rectangle with a double line
- void draw_rectangle_2( int x, int y, int width, int height )
- {
- int i;
- if( x + width < 1 ) // 2 for two vertical edges of the rectangle
- width = 2;
- if( y + height < 2 ) // 2 for two horizontal edges of the rectangle
- height = 2;
- if( x + width > MAX_COLUMNS ) // if right side of rectangle greater then
- width = MAX_COLUMNS - x; // MAX_COLUMNS, then truncate it
- if( y + height > MAX_ROWS - 1 ) // if bottom side of rectangle greater then
- height = MAX_ROWS - y; // MAX_ROWS, then truncate it
- gotoxy(x,y); // UL, upper left corner
- putchar(201);
- for(i=x+1; i<x+width;i++) { // UH, upper horizontal line
- gotoxy(i,y);
- putchar(205);
- }
- gotoxy(i-1,y); // UR, upper right corner
- putchar(187);
- for(i=y+1; i<y+height-1;i++) { // VH, left and right vertical line
- gotoxy(x,i); // left vertical line
- putchar(186);
- gotoxy(x+width-1,i); // right vertical line
- putchar(186);
- }
- gotoxy(x,y+height-1); // DL, down left corner
- putchar(200);
- for(i=x+1; i<x+width-1;i++) { // DH, down horizontal line
- gotoxy(i,y+height-1);
- putchar(205);
- }
- gotoxy(x+width-1,y+height-1); // DR, down right corner
- putchar(188);
- } // draw_rectangle_2()
- int main(void)
- {
- int i, x, y, width=20, height=10;
- system("MODE CON: COLS=80 LINES=30"); // select MODE (size) of Console Window
- x=3;
- y=1;
- print_hollow_rectangle( 254, x, y, width, height );
- x=27;
- y=5;
- draw_rectangle_1(x,y,width,height);
- x=51;
- y=10;
- draw_rectangle_2(x,y,width,height);
- gotoxy(0,20);
- return 0;
- } // main()
Add Comment
Please, Sign In to add comment