13.4 – Drawing basic shapes – Turbo C

by subbu on January 10, 2014

Here in this session we are going to draw some basic shapes like horizontal line, vertical line, rectangle and splitting rectangle etc. Some of the graphical characters used to draw these basic shapes are

graphical printable characters

Drawing a horizontal line:

Logic:
To draw a horizontal line, we need to print a graphical character 196 for each column by keeping the row constant.
If we want to draw a horizontal line between x1 and x2 columns in the same row y then the loop may be

Drawing horizontal line

for(i=x1;i<=x2;i++)
{
  gotoxy(i,y);            /* changing column by keeping row (y) constant */
  cprintf("%c",196);   /* printing graphical character 196 */
}

Common function to draw a horizontal line can be written as

void hline(int x1,int x2,int y)
{
  int i;
  for(i=x1;i<=x2;i++)
  {
    gotoxy(i,y);
    cprintf("%c",196);
  }
}

Program:

#include<stdio.h>
#include<conio.h>
void hline(int,int,int);
int main()
{
  int c1,c2,r;
  clrscr();       /* clears the console (screen) */
  printf("Enter the starting and ending columns:\n");
  scanf("%d%d",&c1,&c2);
  printf("Enter the row:");
  scanf("%d",&r);
  hline(c1,c2,r);
  return 0;
}
void hline(int x1,int x2,int y)
{
  int i;
  for(i=x1;i<=x2;i++)
  {
    gotoxy(i,y);
    cprintf("%c",196);
  }
}

Execution:
horizontal line in C language

Drawing a vertical line:

Logic:
To draw a vertical line, we need to print a graphical character 179 for each row by keeping the column constant.
If we want to draw a vertical line between y1 and y2 rows in the same column x then the loop may be

Drawing vertical line in C language

for(i=y1;i<=y2;i++)
{
  gotoxy(x,i);            /* changing row by keeping column (x) constant */
  cprintf("%c",179);   /* printing graphical character 179 */
}

Common function to draw a vertical line can be written as

void vline(int y1,int y2,int x)
{
 int i;
 for(i=y1;i<=y2;i++)
 {
  gotoxy(x,i);
  cprintf("%c",179);
 }
}

Program:

#include<stdio.h>
#include<conio.h>
void vline(int,int,int);
int main()
{
 int r1,r2,c;
 clrscr();     /* clears the console (screen) */
 printf("Enter the starting and ending rows:\n");
 scanf("%d%d",&r1,&r2);
 printf("Enter the column:");
 scanf("%d",&c);
 vline(r1,r2,c);
 return 0;
}
void vline(int y1,int y2,int x)
{
 int i;
 for(i=y1;i<=y2;i++)
 {
   gotoxy(x,i);
   cprintf("%c",179);
 }
}

Execution:
How to draw a vertical line in C language

Drawing the rectangle

Rectangle can be draw in two steps, that are printing the corners using graphical characters top left corner (218), top right corner (191), bottom left corner (192) and bottom right corner (217) and connecting the corners using horizontal and vertical lines.

Printing corners in C language

/*printing corners*/
gotoxy(x2,y1);
cprintf("%c",191);  /* printing top right corner */
gotoxy(x1,y2)
cprintf("%c",192);  /* printing bottom left corner */
gotoxy(x2,y2);
cprintf("%c",217);  /* printing bottom right corner */
gotoxy(x1,y1);
cprintf("%c",218);  /* printing top left corner */

How to draw a rectangle in C lanuage

/* printing top horizontal line */
hline(x1+1,x2-1,y1);
/* printing bottom horizontal line */
hline(x1+1,x2-1,y2);
/* printing left vertical line */
vline(y1+1,y2-1,x1);
/* printing right vertical line */
vline(y1+1,y2-1,x2);

Common function to draw a rectangle can be written as

void rect(int x1,int y1,int x2, int y2)
{
 /*printing corners*/
 gotoxy(x2,y1);
 cprintf("%c",191);
 gotoxy(x1,y2)
 cprintf("%c",192);
 gotoxy(x2,y2);
 cprintf("%c",217);
 gotoxy(x1,y1);
 cprintf("%c",218);
 /* printing lines */
 hline(x1+1,x2-1,y1);
 hline(x1+1,x2-1,y2);
 vline(y1+1,y2-1,x1);
 vline(y1+1,y2-1,x2);
}

Program:

#include<stdio.h>
#include<conio.h>
void rect(int,int,int,int);
void hline(int,int,int);
void vline(int,int,int);
int main()
{
  int x1,y1,x2,y2;
  clrscr();     /* clears the console (screen) */
  printf("Enter the first point:\n");
  scanf("%d%d",&x1,&y1);
  printf("Enter the second point:\n");
  scanf("%d%d",&x2,&y2);
  rect(x1,y1,x2,y2);                     /* giving in points */
  return 0;
}
void rect(int x1,int y1,int x2, int y2)
{
  /*printing corners*/
  gotoxy(x2,y1);
  cprintf("%c",191);
  gotoxy(x1,y2);
  cprintf("%c",192);
  gotoxy(x2,y2);
  cprintf("%c",217);
  gotoxy(x1,y1);
  cprintf("%c",218);
  /* printing lines */
  hline(x1+1,x2-1,y1);
  hline(x1+1,x2-1,y2);
  vline(y1+1,y2-1,x1);
  vline(y1+1,y2-1,x2);
}
void hline(int x1,int x2,int y)
{
  int i;
  for(i=x1;i<=x2;i++)
  {
    gotoxy(i,y);
    cprintf("%c",196);
  }
}
void vline(int y1,int y2,int x)
{
  int i;
  for(i=y1;i<=y2;i++)
  {
    gotoxy(x,i);
    cprintf("%c",179);
  }
}

Execution:
How to draw a rectangle

Previous post:

Next post: