5Practical List Of Computer Graphics
1. Write a program to draw line using DDA line draw algorithm
2. Write a program to draw line using bresenham’s line draw algorithm
3. Write a program to draw circle using midpoint circle algorithm.
4. Write a program to fill area using flood fill algorithm
5. Write a program to fill area using boundary fill algorithm
6. Write a program to fill area using fillarea function.
7. Write a program to write a name using san serif font.
8. Write a program to change background color.
9. Write a program to simulate traffic light.
10. Write a program to draw a moving wheel.
11. Write a program to draw a Indian flag.
12. Write a program to implement human face.(eyebro, eye ,ears, nose and lips)
Pooja Dayani | BE Final Year | Computer Graphics
PROGRAM-1
AIM:
Write a program to draw line using DDA line draw algorithm
PROGRAM CODE:
#include<graphics.h>
#include<conio.h>
#define ROUND(a) ((int)(a+0.5))
void linedda(int xa,int ya,int xb,int yb)
{
int dx=xb-xa, dy=yb-ya, step, k;
float xincrement, yincrement, x=xa,y=ya;
if(abs(dx)>abs(dy))
step=abs(dx);
else
step=abs(dy);
xincrement=dx/(float)step;
yincrement=dy/(float)step;
putpixel(ROUND(x),ROUND(y),15);
for(k=0;k<step;k++)
{
x+=xincrement;
y+=yincrement;
putpixel(ROUND(x),ROUND(y),15);
}
}
void main()
{
int x1=50,x2=50,x3=100,x4=100;
int gd=DETECT,gm;
initgraph(&gd, &gm, "C ");
linedda(x1,x2,x3,x4);
getch();
}
RESULT:
Pooja Dayani | BE Final Year | Computer Graphics
Pooja Dayani | BE Final Year | Computer Graphics
PROGRAM-2
AIM:
Write a program to draw line using bresenham’s line draw algorithm
PROGRAM CODE:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd = DETECT, gm;
int dx, dy, p[100],i;
float x1=50, x2=100, y1=80, y2=170;
initgraph(&gd, &gm, "c ");
dx = abs(x1 - x2);
dy = abs(y1 - y2);
p[0] = 2 * dy - dx;
putpixel(x1, y1, 10);
for(i=0;i<x2;i++)
{
x1 = x1 + 1;
if(p[i] < 0)
{
p[i+1] = p[i] + 2 * dy;
}
else
{
y1 = y1 + 1;
p[i+1]= p[i] + 2 * (dy - dx);
}
putpixel(x1, y1, 10);
getch();
}
RESULT:
Pooja Dayani | BE Final Year | Computer Graphics
Pooja Dayani | BE Final Year | Computer Graphics
PROGRAM-3
AIM:
Write a program to draw circle using midpoint circle algorithm.
PROGRAM CODE:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void draw_circle(int,int,int);
void symmetry(int,int,int,int);
void main()
{
int xc,yc,R;
int gd=DETECT,gm;
clrscr();
printf("Enter the center of the circle:\n");
printf("Xc =");
scanf("%d",&xc);
printf("Yc =");
scanf("%d",&yc);
printf("Enter the radius of the circle :");
scanf("%d",&R);
clrscr();
initgraph(&gd,&gm,"c:\\ ");
draw_circle(xc,yc,R);
getch();
closegraph();
}
void draw_circle(int xc,int yc,int rad)
{
int x = 0;
int y = rad;
int p = 1-rad;
symmetry(x,y,xc,yc);
Pooja Dayani | BE Final Year | Computer Graphics
while(x<y)
{
x++;
if(p<0)
p += 2*x + 1;
else
{
p += 2*(x-y) + 1;
y--;
}
symmetry(x,y,xc,yc);
}
}
void symmetry(int x,int y,int xc,int yc)
{
putpixel(xc+x,yc-y,10);//For pixel (x,y)
putpixel(xc+y,yc-x,10);//For pixel (y,x)
putpixel(xc+y,yc+x,10);//For pixel (y,-x)
putpixel(xc+x,yc+y,10);//For pixel (x,-y)
putpixel(xc-x,yc+y,10);//For pixel (-x,-y)
putpixel(xc-y,yc+x,10);//For pixel (-y,-x)
putpixel(xc-y,yc-x,10);//For pixel (-y,x)
putpixel(xc-x,yc-y,10);//For pixel (-x,y)
}
RESULT:
Pooja Dayani | BE Final Year | Computer Graphics
Pooja Dayani | BE Final Year | Computer Graphics
PROGRAM-4
AIM:
Write a program to fill area using flood fill algorithm
PROGRAM CODE:
# include <iostream.h>
# include <graphics.h>
# include <conio.h>
# include <math.h>
void Flood_fill(const int,const int,const int,const int);
int main( )
{
int gd=DETECT,gm;
initgraph(&gd,&gm," tc\\Bgi");
setcolor(15);
circle(75,75,10);
line(65,65,65,85);
line(65,65,85,65);
line(85,85,65,85);
line(85,85,85,65);
Flood_fill(75,75,10,0);
Flood_fill(67,83,12,0);
Flood_fill(83,83,7,0);
Flood_fill(83,67,6,0);
Flood_fill(67,67,5,0);
getch( );
return 0;
}
void Flood_fill(const int x,const int y, const int fill_color,const int old_color)
{
Pooja Dayani | BE Final Year | Computer Graphics
if(getpixel(x,y)==old_color)
{
putpixel(x,y,fill_color);
Flood_fill((x+1),y,fill_color,old_color);
Flood_fill((x-1),y,fill_color,old_color);
Flood_fill(x,(y+1),fill_color,old_color);
Flood_fill(x,(y-1),fill_color,old_color);
}
}
RESULT:
Pooja Dayani | BE Final Year | Computer Graphics
PROGRAM-5
AIM:
Write a program to fill area using boundary fill algorithm
PROGRAM CODE:
# include <iostream.h>
# include <graphics.h>
# include <conio.h>
# include <math.h>
void bound_fill(const int,const int,const int,const int);
int main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm," \\Bgi");
setcolor(15);
circle(75,75,20);
bound_fill(75,75,12,15);
getch();
return 0;
}
void bound_fill(const int x,const int y,
const int fill_color,const int boundry_color)
{
int current;
current=getpixel(x,y);
if((current!=boundry_color)&&(current!=fill_color))
{
putpixel(x,y,fill_color);
bound_fill((x+1),y,fill_color,boundry_color);
bound_fill((x-1),y,fill_color,boundry_color);
bound_fill(x,(y+1),fill_color,boundry_color);
bound_fill(x,(y-1),fill_color,boundry_color);
}
Pooja Dayani | BE Final Year | Computer Graphics
}
RESULT:
Pooja Dayani | BE Final Year | Computer Graphics
PROGRAM-6
AIM:
Write a program to fill area using fillarea function.
PROGRAM CODE:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main()
{
int gdriver = DETECT, gmode;
int i, maxx, maxy;
int poly[8];
initgraph(&gdriver, &gmode, "");
maxx = getmaxx();
maxy = getmaxy();
poly[0] = 20;
poly[1] = maxy / 2;
poly[2] = maxx - 20;
poly[3] = 20;
poly[4] = maxx - 50;
poly[5] = maxy - 20;
poly[6] = maxx / 2;
poly[7] = maxy / 2;
fillpoly(4, poly);
getch();
closegraph();
return 0;
Pooja Dayani | BE Final Year | Computer Graphics
}
RESULT:
Pooja Dayani | BE Final Year | Computer Graphics
PROGRAM-7
AIM:
Write a program to write a name using san serif font.
PROGRAM CODE:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
line(150,200,150,150);
line(149,200,151,200);
line(150,150,180,200);
line(148,150,150,150);
line(180,200,180,150);
line(179,150,181,150);
line(200,200,215,150);
line(199,200,201,200);
line(215,150,230,200);
line(229,200,231,200);
line(207,180,222,180);
line(250,200,250,150);
line(249,200,251,200);
line(250,150,265,180);
line(248,150,250,150);
line(265,180,280,150);
line(280,150,282,150);
line(280,150,280,200);
line(279,200,281,200);
line(300,200,300,150);
line(298,200,300,200);
line(300,150,330,150);
line(298,150,300,150);
Pooja Dayani | BE Final Year | Computer Graphics
line(300,175,330,175);
line(330,150,330,152);
line(330,174,330,176);
line(300,200,330,200);
line(330,198,330,200);
line(140,210,340,210);
getch();
closegraph();
}
RESULT:
Pooja Dayani | BE Final Year | Computer Graphics
PROGRAM-8
AIM:
Write a program to change background color.
PROGRAM CODE:
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include<iostream.h>
#include<dos.h>
void main()
{
clrscr();
int gd=DETECT,gm;
char ch;
int n;
initgraph(&gd,&gm,"");
setbkcolor(1);
setcolor(2);
outtextxy(320,240,"GRAPHICS");
cout<<"\n enter time enterval to change color(sec): ";
cin >>n;
n=n*1000;
cout<<"\n please wait!!";
delay(n);
cout<<"\n done";
setbkcolor(2);
setcolor(1);
outtextxy(320,240,"GRAPHICS");
getch();
closegraph();
}
Pooja Dayani | BE Final Year | Computer Graphics
OUTPUT:
Pooja Dayani | BE Final Year | Computer Graphics
PROGRAM:9
AIM:
Write a program to simulate traffic light.
Program Code:-
# include<stdio.h>
# include<conio.h>
# include<graphics.h>
# include<dos.h>
void main()
{
int gd=DETECT, gm;
int i=0,j=0;
//char a[][10]={"RED","YELLOW","GREEN","YELLOW"};
initgraph(&gd,&gm,"c:\\TC\\BGI");
while(j<6)
{
for(i=0;i<4;i++)
{
//setbkcolor(1);
setcolor(3);
rectangle(250,60,370,340);
circle(310,110,40);
circle(310,200,40);
circle(310,290,40);
if(i==0)
{
outtextxy(400,70,"STOP");
setfillstyle(1,RED);
fillellipse(310,110,40,40);
}
if((i==1)||(i==3))
{
outtextxy(400,160," GET READY");
Pooja Dayani | BE Final Year | Computer Graphics
setfillstyle(1,YELLOW);
fillellipse(310,200,40,40);
}
if(i==2)
{
outtextxy(400,250,"START");
setfillstyle(1,GREEN);
fillellipse(310,290,40,40);
}
delay(2000);
cleardevice();
}
j++;
}
}
OUTPUT:
Pooja Dayani | BE Final Year | Computer Graphics
PROGRAM:10
AIM:
Write a program to draw a moving wheel.
Program Code:
# include<stdio.h>
# include<conio.h>
# include<graphics.h>
# include<dos.h>
void main()
{
int gd=DETECT, gm;
int j=50,i;
initgraph(&gd,&gm,"c:\\TC\\BGI");
i=getmaxy()/2;
while(j<(getmaxx()-50))
{
setcolor(WHITE);
setfillstyle(1,WHITE);
fillellipse(j,i,52,52);
setfillstyle(1,BLACK);
fillellipse(j,i,50,50);
circle(j,i,50);
line(j,(i-50),j,(i+50));
line((j-25),(i-44),(j+25),(i+44));
delay(20);
line((j-44),(i-25),(j+44),(i+25));
delay(20);
line((j+25),(i-44),(j-25),(i+44));
delay(20);
line((j+44),(i-25),(j-44),(i+25));
delay(20);
line((j-50),i,(j+50),i);
setfillstyle(1,WHITE);
fillellipse(j,i,5,5);
delay(20);
cleardevice();
j=j+5;
Pooja Dayani | BE Final Year | Computer Graphics
}
OUTPUT:
Pooja Dayani | BE Final Year | Computer Graphics
PROGRAM:11
AIM:
Write a program to draw a Indian flag.
Program Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
int gd=DETECT,gmode,left,top,right,bottom,i,j,x,y;
initgraph(&gd,&gmode,"C:\\TC\\BGI");
setbkcolor(15);
setcolor(1);
rectangle(200,50,400,100);
rectangle(200,100,400,150);
rectangle(200,150,400,200);
rectangle(190,45,200,400);
rectangle(170,400,220,410);
setfillstyle(0,0);
for(i=0,j=15;i<=360;i+=15,j+=15)
{
pieslice(300,125,i,j,24);
}
for(x=200;x<=400;x++)
{ for(y=50;y<=100;y++)
putpixel(x,y,RED);
}
for(x=200;x<=400;x++)
{ for(y=150;y<=200;y++)
putpixel(x,y,GREEN);
}
for(x=190;x<=200;x++)
{ for(y=45;y<=400;y++)
putpixel(x,y,7);
}
for(x=170;x<=220;x++)
{ for(y=400;y<=410;y++)
putpixel(x,y,8);
}
getch();
closegraph();
}
Pooja Dayani | BE Final Year | Computer Graphics
Pooja Dayani | BE Final Year | Computer Graphics
PROGRAM:12
AIM:
Write a program to implement human face. (eyebro, eye ,ears, nose and lips)
Program Code:
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gmode;
initgraph(&gd,&gmode ,"C:\\TC\\BGI");
//face
ellipse(300,200,0,180,65,65);
ellipse(300,200,180,360,65,95);
//eyebrow
ellipse(265,190,0,180,18,8);
ellipse(335,190,0,180,18,8);
//eyes
ellipse(265,200,0,360,16,10);
ellipse(335,200,0,360,16,10);
fillellipse(265,200,7,7);
fillellipse(335,200,7,7);
//ears
ellipse(235,200,90,240,15,15);
ellipse(365,200,310,90,15,15);
ellipse(231,213,180,0,4,5);
ellipse(369,213,180,0,4,5);
//nose
line(295,210,290,240);
line(305,210,310,240);
ellipse(300,240,180,0,11,5);
//mouth
ellipse(294,270,0,180,8,3);
ellipse(310,270,0,180,8,3);
ellipse(302,270,180,0,16,4);
//hairs
sector(300,160,0,180,54,25);
sector(300,150,180,360,44,15);
getch();
closegraph();
}
OUTPUT:
Pooja Dayani | BE Final Year | Computer Graphics
Pooja Dayani | BE Final Year | Computer Graphics