dmilicev

draw_rectangle.c

Apr 15th, 2021 (edited)
593
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.25 KB | None | 0 0
  1. /*
  2.  
  3.     draw_rectangle.c
  4.  
  5.  
  6.     You can find all my C programs at Dragan Milicev's pastebin:
  7.  
  8.     https://pastebin.com/u/dmilicev
  9.  
  10. */
  11.  
  12. #include <stdio.h>
  13. #include <windows.h>    // tracking cursor all the time, for cursor position functions
  14.                         // gotoxy(x,y) , wherex() , wherey(), showcursor(), hidecursor()
  15.  
  16. #define MAX_COLUMNS 80      // of the console window
  17. #define MAX_ROWS    25      // of the console window
  18.  
  19. // ----------CURSOR CONTROL FUNCTIONS------------------------------------------
  20.  
  21. // make console cursor invisible
  22. void hidecursor()
  23. {
  24.    HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
  25.    CONSOLE_CURSOR_INFO info;
  26.    info.dwSize = 20;
  27.    info.bVisible = FALSE;
  28.    SetConsoleCursorInfo(consoleHandle, &info);
  29. }
  30.  
  31. // make console cursor invisible
  32. void showcursor()
  33. {
  34.    HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
  35.    CONSOLE_CURSOR_INFO info;
  36.    info.dwSize = 20;
  37.    info.bVisible = TRUE;
  38.    SetConsoleCursorInfo(consoleHandle, &info);
  39. }
  40.  
  41. // place cursor at position ( x, y ) = ( column, row )
  42. void gotoxy( int column, int row )
  43. {
  44.     COORD coord;
  45.  
  46.     coord.X = column;
  47.     coord.Y = row;
  48.  
  49.     SetConsoleCursorPosition( GetStdHandle(STD_OUTPUT_HANDLE), coord );
  50. }
  51.  
  52. // return x coordinate (column) of current cursor position
  53. // on failure return -1
  54. int wherex()
  55. {
  56.     CONSOLE_SCREEN_BUFFER_INFO csbi;
  57.  
  58.     if (!GetConsoleScreenBufferInfo( GetStdHandle(STD_OUTPUT_HANDLE), &csbi ) )
  59.         return -1;
  60.  
  61.     return csbi.dwCursorPosition.X;
  62. }
  63.  
  64. // return y coordinate (row) of current cursor position
  65. // on failure return -1
  66. int wherey()
  67. {
  68.     CONSOLE_SCREEN_BUFFER_INFO csbi;
  69.  
  70.     if (!GetConsoleScreenBufferInfo( GetStdHandle( STD_OUTPUT_HANDLE ), &csbi ) )
  71.         return -1;
  72.  
  73.     return csbi.dwCursorPosition.Y;
  74. }
  75.  
  76. // END OF-----CURSOR CONTROL FUNCTIONS------------------------------------------
  77.  
  78. // Function to print hollow rectangle with char ch, from cursor position row, column.
  79. // rows     must be between 2 and MAX_ROWS-2 and
  80. // columns  must be between 2 and MAX_COLUMNS-2 because
  81. // the smallest rectangle has no interior space, only the sides of the frame.
  82. void print_hollow_rectangle(char ch, int column, int row, int howMuchColumns, int howMuchRows)
  83. {
  84.     int i;
  85.     char str1[MAX_COLUMNS+1];                   // str1 is string for first and last line of rectangle
  86.     char str2[MAX_COLUMNS+1];                   // str2 is string for middle lines of rectangle
  87.  
  88.     if( column + howMuchColumns < 1 )           // 2 for two vertical edges of the rectangle
  89.         howMuchColumns = 2;
  90.  
  91.     if( row + howMuchRows < 2 )                 // 2 for two horizontal edges of the rectangle
  92.         howMuchRows = 2;
  93.  
  94.     if( column + howMuchColumns > MAX_COLUMNS ) // if right side of rectangle greater then
  95.         howMuchColumns = MAX_COLUMNS - column;  // MAX_COLUMNS, then truncate it
  96.  
  97.     if( row + howMuchRows > MAX_ROWS - 1 )      // if bottom side of rectangle greater then
  98.         howMuchRows = MAX_ROWS - row;           // MAX_ROWS, then truncate it
  99.  
  100.  
  101.     for (i = 0; i < howMuchColumns; i++)        // creating string for first and last line of rectangle
  102.         str1[i] = ch;
  103.  
  104.     str1[i] = '\0';                             // end of first and last line string
  105.  
  106.                                                 // creating string for middle lines of rectangle
  107.     str2[0] = ch;                               // first left character of middle lines string
  108.     for (i = 1; i < howMuchColumns-1; i++)      // middle characters of middle lines are blanko (space, ' ')
  109.         str2[i] = ' ';
  110.     str2[i] = ch;                               // last right character of middle lines string
  111.     str2[i+1] = '\0';                           // end of middle lines string
  112.  
  113.                                                 // print rectangle
  114.     gotoxy( column, row );                      // move cursor to (column, row)
  115.     fputs(str1, stdout);                        // print string for first line of rectangle without '\n'
  116.  
  117.     for (i = 0; i < howMuchRows-2; i++)
  118.     {
  119.         gotoxy( column, row+i+1 );              // move cursor to (column, row+i+1)
  120.         fputs(str2, stdout);                    // print string for middle lines of rectangle without '\n'
  121.     }
  122.  
  123.     gotoxy( column, row+i+1 );                  // move cursor to (column, row+i+1)
  124.     fputs(str1, stdout);                        // print string for last line of rectangle without '\n'
  125. } // print_hollow_rectangle()
  126.  
  127. // Draws a rectangle with a single line
  128. void draw_rectangle_1( int x, int y, int width, int height )
  129. {
  130.     int i;
  131.  
  132.     if( x + width < 1 )                         // 2 for two vertical edges of the rectangle
  133.         width = 2;
  134.  
  135.     if( y + height < 2 )                        // 2 for two horizontal edges of the rectangle
  136.         height = 2;
  137.  
  138.     if( x + width > MAX_COLUMNS )               // if right side of rectangle greater then
  139.         width = MAX_COLUMNS - x;                // MAX_COLUMNS, then truncate it
  140.  
  141.     if( y + height > MAX_ROWS - 1 )             // if bottom side of rectangle greater then
  142.         height = MAX_ROWS - y;                  // MAX_ROWS, then truncate it
  143.  
  144.     gotoxy(x,y);                                // UL, upper left corner
  145.     putchar(218);
  146.  
  147.     for(i=x+1; i<x+width;i++) {                 // UH, upper horizontal line
  148.         gotoxy(i,y);
  149.         putchar(196);
  150.     }
  151.  
  152.     gotoxy(i-1,y);                              // UR, upper right corner
  153.     putchar(191);
  154.  
  155.     for(i=y+1; i<y+height-1;i++) {              // VH, left and right vertical line
  156.         gotoxy(x,i);                            // left vertical line
  157.         putchar(179);
  158.         gotoxy(x+width-1,i);                    // right vertical line
  159.         putchar(179);
  160.     }
  161.  
  162.     gotoxy(x,y+height-1);                       // DL, down left corner
  163.     putchar(192);
  164.  
  165.     for(i=x+1; i<x+width-1;i++) {               // DH, down horizontal line
  166.         gotoxy(i,y+height-1);
  167.         putchar(196);
  168.     }
  169.  
  170.     gotoxy(x+width-1,y+height-1);               // DR, down right corner
  171.     putchar(217);
  172. } // draw_rectangle_1()
  173.  
  174. // Draws a rectangle with a double line
  175. void draw_rectangle_2( int x, int y, int width, int height )
  176. {
  177.     int i;
  178.  
  179.     if( x + width < 1 )                         // 2 for two vertical edges of the rectangle
  180.         width = 2;
  181.  
  182.     if( y + height < 2 )                        // 2 for two horizontal edges of the rectangle
  183.         height = 2;
  184.  
  185.     if( x + width > MAX_COLUMNS )               // if right side of rectangle greater then
  186.         width = MAX_COLUMNS - x;                // MAX_COLUMNS, then truncate it
  187.  
  188.     if( y + height > MAX_ROWS - 1 )             // if bottom side of rectangle greater then
  189.         height = MAX_ROWS - y;                  // MAX_ROWS, then truncate it
  190.  
  191.     gotoxy(x,y);                                // UL, upper left corner
  192.     putchar(201);
  193.  
  194.     for(i=x+1; i<x+width;i++) {                 // UH, upper horizontal line
  195.         gotoxy(i,y);
  196.         putchar(205);
  197.     }
  198.  
  199.     gotoxy(i-1,y);                              // UR, upper right corner
  200.     putchar(187);
  201.  
  202.     for(i=y+1; i<y+height-1;i++) {              // VH, left and right vertical line
  203.         gotoxy(x,i);                            // left vertical line
  204.         putchar(186);
  205.         gotoxy(x+width-1,i);                    // right vertical line
  206.         putchar(186);
  207.     }
  208.  
  209.     gotoxy(x,y+height-1);                       // DL, down left corner
  210.     putchar(200);
  211.  
  212.     for(i=x+1; i<x+width-1;i++) {               // DH, down horizontal line
  213.         gotoxy(i,y+height-1);
  214.         putchar(205);
  215.     }
  216.  
  217.     gotoxy(x+width-1,y+height-1);               // DR, down right corner
  218.     putchar(188);
  219. } // draw_rectangle_2()
  220.  
  221. int main(void)
  222. {
  223.     int i, x, y, width=20, height=10;
  224.  
  225.     system("MODE CON: COLS=80 LINES=30");       // select MODE (size) of Console Window
  226.  
  227.     x=3;
  228.     y=1;
  229.     print_hollow_rectangle( 254, x, y, width, height );
  230.  
  231.     x=27;
  232.     y=5;
  233.     draw_rectangle_1(x,y,width,height);
  234.  
  235.     x=51;
  236.     y=10;
  237.     draw_rectangle_2(x,y,width,height);
  238.  
  239.     gotoxy(0,20);
  240.  
  241.     return 0;
  242.  
  243. } // main()
  244.  
Add Comment
Please, Sign In to add comment