Guru Nanak Institute of Management & Technology: Computer Graphics (Software Lab-VII)
Guru Nanak Institute of Management & Technology: Computer Graphics (Software Lab-VII)
Guru Nanak Institute of Management & Technology: Computer Graphics (Software Lab-VII)
& Technology
Practical File
Of
Computer Graphics
(Software Lab-VII)
2|Page
Program 1: Print resolution in computer graphics.
#include<stdio.h>
#include<graphics.h>
int main()
{
intgdriver=DETECT, gmode;
initgraph(&gdriver, &gmode, "c:\\turboc\\bgi");
int x=getmaxx();
int y=getmaxy();
printf(“Resolution is : %d x %d”, x,y);
getch();
}
3|Page
Program 2: Draw Parallel Lines.
/*C graphics program to draw a line.*/
#include <graphics.h>
#include <conio.h>
main()
{
intgd = DETECT, gm;
//init graphics
initgraph(&gd, &gm, "C:/TURBOC3/BGI");
line(100,100,200,100); //will draw a horizontal line
line(10,10,200,10); //will draw another horizontal line
getch();
closegraph();
return 0;
}
Output:
4|Page
Program 3: Draw concentric circlesin Computer Graphics.
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
int main(){
intgd = DETECT,gm;
int x ,y;
x = getmaxx()/2;
y = getmaxy()/2;
setcolor(RED);
circle(x, y, 30);setcolor(GREEN);
circle(x, y, 50);setcolor(YELLOW);
circle(x, y, 70);setcolor(BLUE);
circle(x, y, 90);getch();closegraph();
return 0;
Output:
5|Page
Program 4: Using DrawPolyfunctionin Computer Graphics.
#include<graphics.h>
int main()
{ intgd=DETECT, gm;
int octagon[18]={450,150, 430,120, 430,100, 450,70, 500,70, 520,100, 520,120, 500,150, 450,150};
initgraph(&gd, &gm,NULL);
getch();closegraph();
return 0;}
Output:
6|Page
Program 5: Displaying text in different fonts in Computer Graphics.
#include <graphics.h>
int main()
int x = 150;
int y = 150;
int font = 8;
int direction = 0;
intfont_size = 5;
getch();
closegraph();
return 0;
Output:
7|Page
Program 6: Draw Face in Computer Graphics.
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
void main()
{intgd = DETECT,gm;
//for head
circle(200,200,30);
circle(190,190,5);
arc(190,190,50,130,10);
circle(210,190,5);
arc(210,190,50,130,10);
//for nose
line(198,195,195,200); line(202,195,205,200);line(195,200,200,205);
line(205,200,200,205);
getch();closegraph();
Output:
8|Page
Program 7: Animation (Bouncing Ball) Computer Graphics.
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <dos.h>
int main() {
x = getmaxx()/2;
y = 30;
while (!kbhit()) {
delay(50);
/* clears screen */
cleardevice();
if(flag){
y = y + 5; } else {y = y - 5;
Output:
9|Page
Program 8: DDA Line Drawing Algorithm.
#include <graphics.h>
#include <stdio.h>
#include <math.h>
#include <dos.h>
void main( )
{
float x,y,x1,y1,x2,y2,dx,dy,step;
inti,gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
printf("Enter the value of x1 and y1 : ");
scanf("%f%f",&x1,&y1);
printf("Enter the value of x2 and y2: ");
scanf("%f%f",&x2,&y2);
dx=abs(x2-x1);
dy=abs(y2-y1);
if(dx>=dy)
step=dx;
else
step=dy;
dx=dx/step;
dy=dy/step;
x=x1;
y=y1;
i=1;
while(i<=step)
{
putpixel(x,y,5);
x=x+dx;
y=y+dy;
i=i+1;
delay(100);
}
closesgraph();
}
Output:
10 | P a g e
Program 9: Bresenham’s Line Drawing Algorithm.
#include<stdio.h>
#include<graphics.h>
voiddrawline (int x0, int y0, int x1, int y1)
{
int dx, dy, p, x, y;
dx=x1-x0;
dy=y1-y0;
x=x0;
y=y0;
p=2*dy-dx;
while(x<x1)
{ if(p>=0)
{
putpixel(x,y,7);
y=y+1;
p=p+2*dy-2*dx; }
else
{ putpixel(x,y,7);
p=p+2*dy; }
x=x+1 } }
int main()
{
intgdriver=DETECT, gmode, error, x0, y0, x1, y1;
initgraph(&gdriver, &gmode, "c:\\turboc\\bgi");
printf("Enter co-ordinates of first point: ");
scanf("%d%d", &x0, &y0);
printf("Enter co-ordinates of second point: ");
scanf("%d%d", &x1, &y1);
drawline(x0, y0, x1, y1);
return 0;
}
Output:
11 | P a g e
Program 10: Draw Dotted Line using Bresenham’s Algorithm.
#include<stdio.h>
#include<graphics.h>
#include<math.h>
void main()
intgd=DETECT,gm,x,y,x1,y1,x2,y2,dx,dy,i,e;
floatxinc,yinc;
initgraph(&gd,&gm,"");
cleardevice();
printf("Enter x1,y1,x2,y2:\n");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
dx=x2-x1;
dy=y2-y1;
if(x1<x2)
xinc=1;
else
xinc=-1;
if(y1<y2)
yinc=1;
else
yinc=-1;
x=x1; y=y1;
if(dx>=dy)
{e=(2*dy)-dx;
while(x!=x2)
{if(e<0)
e=e+(2*dy); else
{e=e+(2*(dy-dx));
y=y+yinc;
y=y+yinc; }
12 | P a g e
x=x+xinc;
x=x+xinc;
putpixel(x,y,WHITE);
} }
else
{e=(2*dx)-dy;
while(y!=y2)
{ if(e<0)
e=e+(2*dx);
else
{ e=e+(2*(dx-dy));
x=x+xinc;
x=x+xinc; }
y=y+yinc;
y=y+yinc;
putpixel(x,y,WHITE);
}}getch();closegraph();restorecrtmode(); }
Output:
13 | P a g e
Program 11: Bresenham’s Circle Drawing Algorithm.
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
void main()
intgd=DETECT,gm;
intd,r,x,y,xc,yc;
clrscr();
initgraph(&gd,&gm,"c:\\tc\\");
printf("Enter Radius\n");
scanf("%d",&r);
scanf("%d",&xc);
scanf("%d",&yc);
d=3-2*r;
x=0;
y=r;
while(x<=y)
putpixel(xc+x,yc+y,5);
putpixel(xc-y,yc-x,5);
putpixel(xc+y,yc-x,5);
putpixel(xc-y,yc+x,5);
putpixel(xc+y,yc+x,5);
putpixel(xc-x,yc-y,5);
putpixel(xc+x,yc-y,5);
putpixel(xc-x,yc+y,5);
if(d<=0)
d=d+4*x+6;
14 | P a g e
}
else
d=d+4*x-4*y+10;
y=y-1;
x=x+1;
getch();
Output:
15 | P a g e
Program 12: Midpoint Circle Algorithm.
#include<stdio.h>
#include<graphics.h>
voiddrawcircle(int x0, int y0, int radius)
{
int x = radius;
int y = 0;
int err = 0;
while (x >= y)
{
putpixel(x0 + x, y0 + y, 7);putpixel(x0 + y, y0 + x, 7);
putpixel(x0 - y, y0 + x, 7); putpixel(x0 - x, y0 + y, 7);
putpixel(x0 - x, y0 - y, 7);putpixel(x0 - y, y0 - x, 7);
putpixel(x0 + y, y0 - x, 7);putpixel(x0 + x, y0 - y, 7);
if (err <= 0)
{ y += 1;
err += 2*y + 1;
}
if (err > 0)
{ x -= 1;
err -= 2*x + 1;} } }
int main()
{ intgdriver=DETECT, gmode, error, x, y, r;
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");
printf("Enter radius of circle: ");
scanf("%d", &r);
printf("Enter co-ordinates of center(x and y): ");
scanf("%d%d", &x, &y);
drawcircle(x, y, r);
return 0;
}
Output:
16 | P a g e
Program 13: Boundary Fill Algorithm to fill different Shapes.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void BoundaryFill4(int,int,int,int);
void BoundaryFill8(int,int,int,int);
void main()
intgd=DETECT,gm;
initgraph(&gd,&gm,"");
setbkcolor(15);
setcolor(BLUE);
rectangle(50,50,100,100);
circle(50,50,30);
BoundaryFill8(56,56,5,BLUE);
BoundaryFill4(40,40,14,BLUE);
getch();
{int current;
current=getpixel(x,y);
{putpixel(x,y,fill);
BoundaryFill4(x+1,y,fill,boundary);
BoundaryFill4(x-1,y,fill,boundary);
BoundaryFill4(x,y-1,fill,boundary);
BoundaryFill4(x,y+1,fill,boundary);
} }
{ int current;
current=getpixel(x,y);
17 | P a g e
if((current!=boundary) && (current!=fill))
{ putpixel(x,y,fill);
BoundaryFill8(x+1,y,fill,boundary);
BoundaryFill8(x-1,y,fill,boundary);
BoundaryFill8(x,y-1,fill,boundary);
BoundaryFill8(x,y+1,fill,boundary);
BoundaryFill8(x+1,y+1,fill,boundary);
BoundaryFill8(x-1,y+1,fill,boundary);
BoundaryFill8(x-1,y-1,fill,boundary);
BoundaryFill8(x+1,y-1,fill,boundary);
} }
Output:
#include<graphics.h>
#include<dos.h>
#include<conio.h>
voidfloodfill(intx,inty,intold,intnewcol)
{ int current;
current=getpixel(x,y);
if(current==old)
{ delay(5);
putpixel(x,y,newcol);
floodfill(x+1,y,old,newcol); floodfill(x-1,y,old,newcol);
floodfill(x,y+1,old,newcol); floodfill(x,y-1,old,newcol);
floodfill(x+1,y+1,old,newcol); floodfill(x-1,y+1,old,newcol);
floodfill(x+1,y-1,old,newcol); floodfill(x-1,y-1,old,newcol); } }
void main()
{ intgd=DETECT,gm; initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
closegraph();
Output:
19 | P a g e
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include <math.h>
void translate(int,int);
void scale(int,int);
void rotate(float);
void main()
intch;
intgd=DETECT,gm;
inttx,ty,sx,sy;
float theta;
initgraph(&gd,&gm,"c:\\tc\\bgi");
setbkcolor(WHITE);
setcolor(6);
outtextxy (100,88,"Object.");
rectangle(100,250,150,200);
printf("---MENU---");
scanf("%d",&ch);
cleardevice();
switch(ch)
case 1:
scanf("%d %d",&tx,&ty);
translate(tx,ty);
break;
case 2:
20 | P a g e
outtextxy(10,45,"Enter the value of sx and sy:");
scanf("%d%d",&sx,&sy);
scale(sx,sy);
break;
case 3:
scanf("%f",&theta);
rotate(theta);
break;
break;
getch();
closegraph();
void translate(inttx,intty)
setcolor(2);
outtextxy(240,10,"TRANSLATION");
outtextxy(238,20,"------------");
rectangle(100,250,150,200);
rectangle(100+tx,250+ty,150+tx,200+ty);
void scale(intsx,intsy)
setcolor(2);
outtextxy(240,10,"SCALING");
outtextxy(238,20,"--------");
rectangle(100,250,150,200);
rectangle(100*sx,250*sy,150*sx,200*sy);
21 | P a g e
void rotate(float theta)
int x1,x2,x3,x4;
int y1,y2,y3,y4;
int ax1,ax2,ax3,ax4,ay1,ay2,ay3,ay4;
intrefx,refy;
theta=theta*(3.14/180);
setcolor(2);
outtextxy(240,10,"ROTATE");
outtextxy(238,20,"-------");
refx=100;
refy=100;
x1=100;
y1=100;
x2=150;
y2=100;
x3=150;
y3=150;
x4=100;
y4=150;
ax1=refy+(x1-refx)*cos(theta)-(y1-refy)*sin(theta);
ay1=refy+(x1-refx)*sin(theta)+(y1-refy)*cos(theta);
ax2=refy+(x2-refx)*cos(theta)-(y2-refy)*sin(theta);
ay2=refy+(x2-refx)*sin(theta)+(y2-refy)*cos(theta);
ax3=refy+(x3-refx)*cos(theta)-(y3-refy)*sin(theta);
ay3=refy+(x3-refx)*sin(theta)+(y3-refy)*cos(theta);
ax4=refy+(x4-refx)*cos(theta)-(y4-refy)*sin(theta);
ay4=refy+(x4-refx)*sin(theta)+(y4-refy)*cos(theta);
rectangle(100,150,150,100);
line(ax1,ay1,ax2,ay2);
line(ax2,ay2,ax3,ay3);
22 | P a g e
line(ax3,ay3,ax4,ay4);
line(ax4,ay4,ax1,ay1);
Output:
23 | P a g e
24 | P a g e