Graphics in C

Graphic Function In C

In C Language by Baqir AliLeave a Comment

Turbo C has a lot of built-in graphics functions that come in use while displaying pixels on the screen. A few of them will be discussed in this blog.

Putpixel

To draw a dot on screen with a given location and color.

putpixel(x,y,c)

x= column
y= row
c= color

Example:

putpixel(320,240, RED);
Graphics in C
Putpixel Example

Code Example

#include<graphics.h>
main()
{
  int driver, mod;
  driver = VGA/DETECT;
  mod    = VGAHI/Optional if DETECT is used;
  initgraph(&driver, &mod,"C:/tc/bgi");
  putpixel(320,240,RED);
  getch();
  closegraph();
}

circle

To draw Circle on-screen with given location and radius

circle(x,y,r)

x= column
y= row
r= radius

Example:

circle(320,240,100);

Code Example

#include<graphics.h>
main()
{
  int driver, mod;
  driver = VGA;
  mod    = VGAHI;
  initgraph(&driver, &mod,"C:\\tc\\bgi");
  setcolor(RED);
  circle(320,240,50);
  getch();
  closegraph();
}
Circle in C
circle function in c

line

Draw a line with starting and ending point.

Syntax:

line(x1,y1,  x2,y2)

Code Example:

#include<graphics.h>
main()
{
  int driver, mod;
  driver = VGA;
  mod    = VGAHI;
  initgraph(&driver, &mod,"C:\\tc\\bgi");
  setcolor(RED);
  line(0,0,320,240);
  getch();
  closegraph();
}
line function in c
line function in c

Rectangle

To draw a rectangle on-screen with the help of left top corner and right bottom corner.

Syntax :

retangle(x1,y1,x2y2)
code will be
rectangle(0,0,330,340);

Code Example:

#include<graphics.h>
main()
{
  int driver, mod;
  driver = VGA;
  mod    = VGAHI;
  initgraph(&driver, &mod,"C:\\tc\\bgi");
  setcolor(RED);
  rectangle(0,0,320,240);
  getch();
  closegraph();
}

Bar

To draw a box on screen with help of left top corner and right bottom corner.

Syntax :

bar(x1,y1,x2,y2)
Example: 
bar(0,0,330,340);

Code Example:

#include<graphics.h>
main()
{
  int driver, mod;
  driver = VGA;
  mod    = VGAHI;
  initgraph(&driver, &mod,"C:\\tc\\bgi");
  setcolor(RED);
  setfillstyle(1,2);
bar(100,100,320,240);
  getch();
  closegraph();
}

Set Fill Style

To set color and pattern of filled object.

Syntax:

setfillstyle( Pattern, Color )
Example:
setfillstyle( SOLIDFIL, RED );

Filling Patterns

Identifier Code Identifier Code
EMPTY_FILL 0 LTBKSLASH_FILL 6
LINE_FILL 1 HATCH_FILL 7
SOLID_FILL 2 XHATCH_FILL 8
LTSLASH_FILL 3 INTERLEAVE_FILL 9
SLASH_FILL 4 WIDE_DOT_FILL 10
BKSLASH_FILL 5 CLOSE_DOT_FILL 11

Code Example for setfillstyle()

#include<graphics.h>
main()
{
  int driver, mod,k;
  driver = VGA;
  mod    = VGAHI;
  initgraph(&driver, &mod,"C:\\tc\\bgi");
  for(k=0;k<13;k++)
  {
   setcolor(RED);
   setfillstyle( k, RED);
   bar(100,100,400,450);
   gotoxy(1,1);printf("Pattern = %d, color = %d",k,4);
   getch();
  }
  getch();
  closegraph();
}

Bar 3D

To draw box with Three Dimensions view  on screen with help of left top corner, right bottom corner, depth, and top.

Syntax :

bar3d(x1, y1, x2, y2, depth, TopFlag)
Example: 
 bar3d(0,0,330,340,15,15);

Code Example:

#include<graphics.h>
main()
{
  int driver, mod;
  driver = VGA;
  mod    = VGAHI;
  initgraph(&driver, &mod,"C:\\tc\\bgi");
  setcolor(RED);
  setfillstyle(1,2);
  bar3d(100,100,320,240,15,10);
  getch();
  closegraph();
}

Arc

To draw part of circle

Syntax:

arc ( x,y,sa,ea,r)
x = colum
y = row
sa = Start Angle
ea= End Angle
r = radius

Code Example:

#include<graphics.h>
main()
{
  int driver, mod;
  driver = VGA;
  mod    = VGAHI;
  initgraph(&driver, &mod,"C:\\tc\\bgi");
  setcolor(RED);
  arc(320,240,0,180,100);
  getch();
  closegraph();
}

Pie slice

To draw slice of circle

Syntax:

pieslice ( x,y,sa,ea,r)
x = colum
y = row
sa = Start Angle
ea= End Angle
r = radius
Pie Slice Function in C
Pie Slice Function in C

Code Example

#include<graphics.h>
main()
{
  int driver, mod;
  driver = VGA;
  mod    = VGAHI;
  initgraph(&driver, &mod,"C:\\tc\\bgi");
  setcolor(RED);
  setfillstyle(SOLID_FILL,GREEN);
  pieslice(320,240,0,180,100);
  getch();
  closegraph();
}

There are more functions in c such as

  • Fillellipse();
  • Ellipse();
  • Sector();
  • Floodfill();

Leave a Comment