C++ Graphics

Using C, C++ Graphics, we can create basic shapes like line, circle, rectangle, ellipse, arc, bar, bar3d, and display text. To use graphics functions, we must include graphics.h header file.

Line Function

Line function is used to draw a straight line, there are 4 parameters used in line function (x1,y1,x2,y2).

Example 1: Line function in C - Graphics. 450

#include<stdio.h>
#include<conio.h>
#include<graphics.h>

void main()
{
    int gd=DETECT, gm;
    int x1=100, y1=100, x2=200, y2=200;
    initgraph(&gd, &gm, "c:\\turboc3\\bgi");
    cleardevice();
    line(x1,y1,x2,y2);
    getch();
    closegraph();
}
Code Explanation:
  • graphics.h: header file is must in all the programs, if you are using graphics functions.
  • initgraph: is used to initialize the system graphics by loading a graphics driver from disk and thereby putting the system into graphics mode. initgraph takes 3 parameters: graphic detect, graphic mode, and bgi file path.
  • bgi: stands for Borland Graphics Interface, it is a graphic library. This library loads graphic drivers and vector fonts (*.CHR).
  • closegraph: deallocates the memory allocated by the system graphics and then restores the screen to the mode it was before calling initgraph.

Circle Function

Circle function is used to draw a circle which required 3 parameters i.e x, y and radius.

Example 2: WAP to draw a circle with radius of 80 pixels. 388

#include<stdio.h>
#include<conio.h>
#include<graphics.h>

void main()
{
    int gd=DETECT, gm;
    int x=100, y=100, r=80
    initgraph(&gd, &gm, "c:\\turboc3\\bgi");
    cleardevice();
    circle(x, y, r);
    getch();
    closegraph();
}

Example 3: WAP to draw a circle with radius of 80 pixels on the center of screen.

#include<stdio.h>
#include<conio.h>
#include<graphics.h>

void main()
{
    int gd=DETECT, gm;
    int x, y, r=80
    initgraph(&gd, &gm, "c:\\turboc3\\bgi");
    cleardevice();
    x = getmaxx()/2;
    y = getmaxy()/2;
    circle(x, y, r);
    outtextxy(x-100, y-50, "Circle on center of screen");
    getch();
    closegraph();
}
  • getmaxx: It returns the maximum X coordinate
  • getmaxy: It returns the maximum Y coordinate

Rectangle Function

Rectangle function is used to draw a rectangle which required 4 parameters i.e x1(left), y1(top), x2(right) and y2(bottom).

Example 4: Rectangle function in C - Graphics. 357

#include<stdio.h>
#include<conio.h>
#include<graphics.h>

void main()
{
    int gd=DETECT, gm;
    initgraph(&gd, &gm, "c:\\turboc3\\bgi");
    cleardevice();
    rectangle(100,100,200,200);
    getch();
    closegraph();
}

Ellipse Function

Ellipse function is used to draw a ellipse. Syntax: ellipse(int x, int y, int stangle, int endangle, int xradius, int yradius);

Example 5: Ellipse function in C - Graphics. 263

#include<stdio.h>
#include<conio.h>
#include<graphics.h>

void main()
{
    int gd=DETECT, gm;
    initgraph(&gd, &gm, "c:\\turboc3\\bgi");
    cleardevice();
    ellipse(100, 100, 0, 360, 50, 25);
    getch();
    closegraph();
}

Arc Function

Arc function is used to draw a arc. Syntax: arc(int x, int y, int stangle, int endangle, int radius);

Example 6: Arc function in C - Graphics. 320

#include<stdio.h>
#include<conio.h>
#include<graphics.h>

void main()
{
    int gd=DETECT, gm;
    initgraph(&gd, &gm, "c:\\turboc3\\bgi");
    cleardevice();
    arc(100, 100, 0, 135, 50);
    getch();
    closegraph();
}

Bar Function

Bar function draws a rectangle and fill it with current fill pattern and color. Syntax: bar(int left, int top, int right, int bottom);

Example 7: Bar function in C - Graphics. 345

#include<stdio.h>
#include<conio.h>
#include<graphics.h>

void main()
{
    int gd=DETECT, gm;
    initgraph(&gd, &gm, "c:\\turboc3\\bgi");
    cleardevice();
    bar(100, 100, 200, 200);
    getch();
    closegraph();
}

Bar3d Function

Bar3d function is used to draw a 2-dimensional rectangular filled in bar. Syntax:

bar3d(int left, int top, int right, int bottom, int depth, int topflag);
left, top, right, bottom are the positions
depth specifies the depth of bar in pixels
topflag determines whether a 3 dimensional top is put on the bar or not (1 for yes, 0 for not )

Example 8: Bar3d function in C - Graphics. 291

#include<stdio.h>
#include<conio.h>
#include<graphics.h>

void main()
{
    int gd=DETECT, gm;
    initgraph(&gd, &gm, "c:\\turboc3\\bgi");
    cleardevice();
    bar3d(100, 100, 200, 200, 20, 1);
    getch();
    closegraph();
}

Outtextxy Function

Example 9: outtextxy function in C - Graphics. 289

#include<stdio.h>
#include<conio.h>
#include<graphics.h>

void main()
{
    int gd=DETECT, gm;
    initgraph(&gd, &gm, "c:\\turboc3\\bgi");
    cleardevice();
    outtextxy(100, 200, "My first C graphics program");
    getch();
    closegraph();
}

C Graphics Color functions

In C Graphics, there are 16 colors declared. We use colors to set background color, change color of shapes, fill color of shapes, change text color.

  1. textcolor is use to set the text color.
  2. textcolor(GREEN);
    cprintf("Text in Green");
  3. setbkcolor is use to set the background color by specifying the color name or the number
  4. setbkcolor(GREEN);
    rectangle(100,100,200,200);
  5. setcolor is use to set the text color or set the outline color of the various shapes.
  6. setcolor(GREEN); or setcolor(3);
    rectangle(100,100,200,200);
    outtextxy(110,110,"Text in Green")

List of available colors and their values

S.No.Color NameColor Value
BLACK0
BLUE1
GREEN2
CYAN3
RED4
MAGENTA5
BROWN6
LIGHTGRAY7
DARKGRAY8
LIGHTBLUE9
LIGHTGREEN10
LIGHTCYAN11
LIGHTRED12
LIGHTMAGENTA13
YELLOW14
WHITE15

C Language Feedback, Questions, Suggestions, Discussion.