How to write Computer graphics Programming in C language?
While writing computer graphics program in C-Language we have to do few changes in program as follows:
⦁ Add header file for graphics functions as
# include <graphics.h>
⦁ Declare two variables gd, gm( Variable name can be any ) and call initgraph function
int gd=DETECT,gm;
initgraph(&gd,&gm, “full path of BGI file”);
even after placing full path of BGI directory in initgraph function if graphics unable to initialize then do following ulternate procedure by keeping initgraph function .
a. go to file-> DOS Shell and click
b. here you will be in BIN directory. come out of BIN using cd.. command.
c. then type cd BGI and press ENTER.you will enter into BGI Directory.
d. type exit command so that you will reach to program window.
e. after doing above process compile and run your program.
⦁ use putpixel function for plotting pixel on computer screen.
putpixel(x,y,color)
where x and y are coordinates of pixel to be plotted and color means color for pixel. in C language there is color range from 0 to 15. you can choose any color from available.
Important Keyboard Shortcuts and commands used in Computer graphics Programming in C or C++ (in TC) .
Many time we are using shortcuts to do some function while writing and executing computer graphics program in C or C++ language in TC.Shortcuts are also useful when our mouse is not working properly.
Following is the list of few important shortcuts and commands:
Sr.No. | Description | Shortcut or command |
---|---|---|
1 | Program Compilation | Alt+F9 |
2 | Program Execution | Ctrl+F9 |
3 | Save program | F2 |
4 | Seeing Output if program returns into blue screen | Alt+F5 |
5 | Increasing or Decreasing size of TC window | Alt+Enter |
6 | Copy | Ctrl+Insert |
7 | Cut | Shift+Delete |
8 | Paste | Shift+Insert |
9 | To change directory in command Prompt | cd directrory_name |
10 | To come out of directory | cd.. |
11 | Select Menu (File,edit etc) | F10 |
12 | Go to Cursor | F5 |
Computer Graphics Programs in C-Language
1)DDA line drawing algorithm Implementation in C-Language:
Program:
#include<stdio.h>
#include<graphics.h>
#include<math.h>
#include<conio.h>
void main()
{
int gd=DETECT,gm;
int i,len;
float x1,y1,x2,y2,dx,dy,xinc,yinc;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
printf("Enter x1");
scanf("%f",&x1);
printf("Enter y1");
scanf("%f",&y1);
printf("Enter x2");
scanf("%f",&x2);
printf("Enter y2");
scanf("%f",&y2);
dx=(x2-x1);
dy=(y2-y1);
if(abs(dx)>=abs(dy))
{
len=dx;
}
else
{
len=dy;
}
xinc=dx/len;
yinc=dy/len;
putpixel(x1,y1,4);
for(i=1;i<=len;i++)
{
x1=x1+xinc;
y1=y1+yinc;
putpixel(x1,y1,4);
}
getch();
}
Output:
2)Bresenham’s line drawing algorithm in C-Language:
Program:
#include<stdio.h>
#include<graphics.h>
#include<math.h>
#include<conio.h>
void main()
{
int gd=DETECT,gm;
int x1,y1,x2,y2,dx,dy,p,i;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
printf("Enter x1");
scanf("%d",&x1);
printf("Enter y1");
scanf("%d",&y1);
printf("Enter x2");
scanf("%d",&x2);
printf("Enter y2");
scanf("%d",&y2);
dx=(x2-x1);
dy=(y2-y1);
p=2*dy-dx;
putpixel(x1,y1,3);
for(i=0;i<=dx;i++)
{
if(p<0)
{
x1=x1+1;
y1=y1;
p=p+2*dy;
}
else
{
x1=x1+1;
y1=y1+1;
p=p+2*dy-2*dx;
}
putpixel(x1,y1,3);
}
getch();
}
Output:
3)Midpoint circle drawing algorithm in C-Language:
Program:
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
void main()
{
int xc,yc,r,p,x1,y1;
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
printf("Enter center coorinates of Circle");
scanf("%d%d",&xc,&yc);
printf("Enter radius of circle");
scanf("%d",&r);
x1=0,y1=r; //initialize x1 and y1
p=1-r;
do{
if(p<0)
{
x1=x1+1;
y1=y1;
p=p+2*x1+1;
}
else
{
x1=x1+1;
y1=y1-1;
p=p+2*x1+1-2*y1;
}
putpixel(x1+xc,y1+yc,4); //putpixel for eight octants of circle
putpixel(-x1+xc,y1+yc,4);
putpixel(-x1+xc,-y1+yc,4);
putpixel(x1+xc,-y1+yc,4);
putpixel(y1+xc,x1+yc,4);
putpixel(-y1+xc,x1+yc,4);
putpixel(y1+xc,-x1+yc,4);
putpixel(-y1+xc,-x1+yc,4);
}while(x1<=y1); // stop condition when x>=y
getch();
}
Output:
4)Character Generation using Bitmap Method in C-Language:
Following program will draw character using bitmap method.
Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm,i,j;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
int a[8][5]={{0,0,1,0,0},
{0,1,1,1,0},
{0,1,0,1,0},
{1,1,0,1,1},
{1,0,0,0,1},
{1,1,1,1,1},
{1,0,0,0,1},
{1,0,0,0,1}};
int b[8][5]={{1,1,1,1,0},
{1,0,0,0,1},
{1,0,0,0,1},
{1,1,1,1,0},
{1,0,0,0,1},
{1,0,0,0,1},
{1,0,0,0,1},
{1,1,1,1,0}};
int c[8][5]={{0,1,1,1,0},
{1,0,0,0,0},
{1,0,0,0,0},
{1,0,0,0,0},
{1,0,0,0,0},
{1,0,0,0,0},
{1,0,0,0,0},
{0,1,1,1,0}};
int d[8][5]={{1,1,1,1,0},
{1,0,0,0,1},
{1,0,0,0,1},
{1,0,0,0,1},
{1,0,0,0,1},
{1,0,0,0,1},
{1,0,0,0,1},
{1,1,1,1,0}};
for(i=0;i<8;i++)
{
for(j=0;j<5;j++)
{
putpixel(100+j,100+i,5*a[i][j]);
putpixel(107+j,100+i,6*b[i][j]);
putpixel(114+j,100+i,7*c[i][j]);
putpixel(121+j,100+i,9*d[i][j]);
}
}
getch();
}
Output:
5)Concentric Circles drawing in C-Language:
Program:
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
void main()
{
int gd=DETECT,gm,i,xc,yc,r;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
printf("Enter center points");
scanf("%d %d",&xc,&yc);
printf("Enter radius of outer circle");
scanf("%d",&r);
circle(xc,yc,r);
for(i=1;i<=r;i++)
{
delay(100);
setcolor(i);
circle(xc,yc,i);
}
getch();
}
Output:
6) Program for Boundary Fill Algorithm
#include<stdio.h>
#include<graphics.h>
#include<dos.h>
#include<conio.h>
#include<conio.h>
void boundary_fill_algo(int,int,int,int); // function prototype
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
setbkcolor(12); //used to set background color
setcolor(6); //used to set circle boundary color
circle(200,200,40); // function used to draw circle directly
boundary_fill_algo(200,200,6,3);//boundary_fill function call
setcolor(8);
rectangle(300,300,350,350);
boundary_fill(301,301,8,10);
setcolor(6);
circle(450,200,40);
boundary_fill(450,200,6,3);
getch();
}
//boundary_fill function definition
void boundary_fill_algo(int x,int y,int bound_col,int fill_col)
{
int b=getpixel(x,y);
if(b!=bound_col & b!=fill_col)
{
delay(50); //delay() function adds delay into execution
putpixel(x,y,fill_col);
boundary_fill_algo( x+1, y, bound_col, fill_col);
boundary_fill_algo( x-1, y, bound_col, fill_col);
boundary_fill_algo( x, y+1, bound_col, fill_col);
boundary_fill_algo( x, y-1, bound_col, fill_col);
}
}
⦁ Output:
How to Write C Code for Object Drawing in Computer Graphics
Steps to be followed:
•Draw rough outline of Object on paper.
•Identify graphics primitives required for drawing the object.
•Find command from graphics library to draw the object.
•Find Coordinates of graphics primitives with respect to Object and Computer Screen.
Functions required from graphics library of C language:
•Circle: circle(int x, int y, int radius);
•Ellipse:ellipse(int x, int y, int stangle, int endangle, int xradius, int yradius);
•Line:line(int x1, int y1, int x2, int y2)
•Rectangle:rectangle(int left, int top, int right, int bottom)
•Arc: arc(int x, int y, int stangle, int endangle, int radius);
•Fill ellipse: fillellipse(int x, int y, int xradius, int yradius)
•To set color of primitives : setcolor(int color);