How to draw shapes in c programming with example and explanation?

KARTHIK NEDUNCHEZHIYAN | 1232 Views | c programming | 16 Aug 2016

 

  • In this article we will discussing about fundamentals of Graphics programming in c , without any prior knowledge of graphics.
  • Graphics programming contains lots of fundamental graphics program like drawing of various geometrical shapes(rectangle, circle eclipse etc), use of mathematical function in drawing curves, coloring an object with different colors and patterns and simple animation programs like jumping ball and moving cars.
  • So The first step in any graphics program is to initialize the graphics drivers on the computer using initgraph method of graphics.h library.
void initgraph(int *graphicsDriver, int *graphicsMode, char *driverDirectoryPath);
  • The graphics driver are mainly used for changing the system into graphics mode. It also resets or initializes all graphics settings like color, palette, current position etc, to their default values.

GraphicsDriver :

  • It is a pointer to an integer specifying the graphics driver to be used.
  • It tells the compiler that what graphics driver to use or to automatically detect the drive. 
  • In all our programs we will use DETECT macro of graphics.h library that instruct compiler for auto detection of graphics driver.

GraphicsMode :

  • It is a pointer to an integer that specifies the graphics mode to be used. 
  • If *graphdriver is set to DETECT, then initgraph sets *graphmode to the highest resolution available for the detected driver.

DriverDirectoryPath : 

  • It specifies the directory path where graphics driver files (BGI files) are located.
  • If directory path is not provided, then it will seach for driver files in current working directory directory. 
  • In all our sample graphics programs, you have to change path of BGI directory accordingly where you turbo C compiler is installed.

Colors in C Graphics Programming :

  • There are 16 colors declared in C Graphics.
  • We use colors to set the current drawing color, change the color of background, change the color of text, to color a closed shape etc.
  • To specify a color, we can either use color constants like setcolor(RED), or their corresponding integer codes like setcolor(4). Below is the color code in increasing order.

COLOR MACRO

INTEGER VALUE

BLACK

0

BLUE

1

GREEN

2

CYAN

3

RED

4

MAGENTA

5

BROWN

6

LIGHTGRAY

7

DARKGRAY

8

LIGHTBLUE

9

LIGHTGREEN

10

LIGHTCYAN

11

LIGHTRED

12

LIGHTMAGENTA

13

YELLOW

14

WHITE

15

  • At the end of our graphics program, we have to unloads the graphics drivers and sets the screen back to text mode by calling closegraph function. Here is our first C Graphics program to draw a straight line on screen.

Straight Line : 

  • we can draw lines using the library function line() in c.
  • line function has four parameters. they are the co-ordinates of the starting point of the line and the co-ordinates of the ending point of the line. the point can be represented by using two co-ordinates "x" and "y". 

Syntax :

line(x1,y1,x2,y2);
where 

x1,y1 are the co-ordinates of the initial/starting point of the line.

x2,y2 are the co-ordinates of the final/ending point of the line. 

Example :

/* C graphics program to draw a line */
#include<graphics.h>
#include<conio.h>
int main() 
{
    int gd = DETECT, gm;
    /* initialization of graphic mode */
    initgraph(&gd, &gm, "C:\\TC\\BGI"); 
    line(50,50,100, 100);
    getch();
    closegraph();
    return 0;
}

Code Explanation :

  • In this program first we have to start the graphics driver using initgraph function So it auto detects an appropriate graphics driver and sets graphics mode maximum possible screen resolution. 
  • Then line function draws a straight line from coordinate (50, 50) to (100, 100).
  • The the getch function to avoid instant termination of program as it waits for user to press any key.
  • Then finally unloads the graphics drivers and sets the screen back to text mode by calling closegraph function.

Sample Output :

Circle :

  • circles can be drawn using the library function circle().
  • The library function circle() takes three integer arguments. they are the co-ordinates of the center and radius of the circle. 

Syntax :

circle(x,y,radius);

where

 x and y are the co-ordinates of the center of the circle 

radius is the radius of the circle. 

Example :

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
int main()
{
  int gd = DETECT,gm;
  int x ,y ,radius=80;
  initgraph(&gd, &gm, "C:\\TC\\BGI");
  /* Initialize center of circle with center of screen */
  x = getmaxx()/2;
  y = getmaxy()/2;
  outtextxy(x-50, 25, "CIRCLE Using Graphics in C");
  /* Draw circle on screen */
  circle(x, y, radius);
  getch();
  closegraph();
 return 0;
}

Code Explanation :

  • initgraph - It initializes the graphics system by loading the passed graphics driver then changing the system into graphics mode.
  • getmaxx - It returns the maximum "X" coordinate in current graphics mode and driver.
  • getmaxy - It returns the maximum "Y" coordinate in current graphics mode and driver.
  • outtextxy - It displays a string at a particular point (x,y) on screen.
  • circle - It draws a circle with radius r and centre at (x, y).
  • closegraph - It unloads the graphics drivers and sets the screen back to text mode.

Sample Output :

Rectangle :

  • Rectangles can be drawn using the library function rectangle().
  • The library function rectangle() takes four integer arguments. they are the co-ordinates of the starting point and breadth and length of the rectangle. 

Syntax :

rectangle(x,y,breadth,length);
where

x and y are the co-ordinates of the point and breadth and length are the breadth and length of the rectangle respectively. 

Example :

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
int main()
{
  int gd = DETECT,gm;
  initgraph(&gd, &gm, "C:\\TC\\BGI");
  /* Draw rectangle on screen */
  rectangle(150, 50, 400, 150);
  getch();
  closegraph();
  return 0;
}

Code Explanation :

  • In this program, we will draw a rectangle  on a screen.
  • We will use rectangle and bar functions of graphics.h header file to draw rectangle and bar on screen.
  • For example the basic syntax for draw a rectangle 
void rectangle(int xTopLeft, int yTopLeft, int xBottomRight, int yBottomRight);
  • xTopLeft - X coordinate of top left corner.
  • yTopLeft - Y coordinate of top left corner.
  • xBottomRight - X coordinate of bottom right corner.
  • yBottomRight - Y coordinate of bottom right corner.

Sample Output :