0% found this document useful (0 votes)
11 views44 pages

Assignment

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 44

LIST OF PRACTICALS

CGMA LAB (BCA-405)

S.NO. AIM
1 Write a program to draw a line using DDA algorithm
2 Write a program to draw a line through Bresenham’s Algorithm
3 Write a program to draw a circle using mid-point algorithm
4 Write a program to draw an Ellipse using Mid-Point algorithm
5 Write a program to handle mouse
6 Write a program to create simple car shape
7 Write a program to show line clipping
8 Write a program to translate a line
9 Write a program to scale a line
10 Write a program to rotate a line
11 Write a program to handle screen
12 Write a program to draw circle of circles
13 Write a program to draw pie chart
14 Write a program to draw a bar chart
15 Write a program for 3D translation
16 Write a program for 3D scaling
17 Write a program for 3D rotation
18 WAP for 4-connected flood fill
19 WAP for 8-connected flood fill
20 WAP for 4-connected Boundary fill
21 WAP for 8-connected Boundary fill
22 Write a program for polygon filling
23 Write a program for man walking
24 Write a program for moving circle in different directions
25 Write a program for windmill rotation
26 Write a program for countdown
27 Write a program for rotation of wheel
28 Write a program for event handling through mouse
29 Write a program for moving car
30 Write a program to simulate a man walking in rain
1. WAP TO DRAW A LINE USING DDA ALGORITHM.

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int x,y,x1,x2,y1,y2,k,dx,dy,s,xi,yi;
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"C:\\tc\\bgi:");
printf("enter first point");
scanf("%d%d",&x1,&y1);
printf("enter second point");
scanf("%d%d",&x2,&y2);
x=x1;
y=y1;
putpixel(x,y,7);
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy)) s=abs(dx);
else
s=abs(dy);
xi=dx/s;
yi=dy/s;
x=x1;
y=y1;
putpixel(x,y,7);
for(k=0;k<s;k++)
x=x+xi;
y=y+yi;
putpixel(x,y,7);
}
getch();
closegraph();
}

2. WAP TO DRAW A LINE USING BRESENHAM’S ALGORITHM.

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int x,y,x1,y1,x2,y2,p,dx,dy;
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"C:\\tc\\BGI:");
printf("\nEnter the x-coordinate of the first point ::");
scanf("%d",&x1);
printf("\nEnter the y-coordinate of the first point ::");
scanf("%d",&y1);
printf("\nEnter the x-coordinate of the second point ::");
scanf("%d",&x2);
printf("\nEnter the y-coordinate of the second
point ::"); scanf("%d",&y2);
x=x
1;
y=y
1;
dx=
x2-
x1;
dy=
y2-
y1;
putpixel(
x,y,2);
p=(2dy-
dx);
while(x<
=x2) {
i
f
(
p
<
0
)
{
x=x+1;
p=2*x-dx;
}
else
{
x=x+1;
y=y+1;
p=p+2*dy;
}
putpixel(x,y,7);
}
getch();
closegraph();

3. WAP to draw a circle using mid-point algorithm


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

void circleMidpoint(int, int, int);


void drawCircle(int, int, int, int);

void main()
{
int xc, yc, r;
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");

printf("Enter center coordinates of circle: ");


scanf("%d %d", &xc, &yc);

printf("Enter radius of circle: ");


scanf("%d", &r);

circleMidpoint(xc, yc, r);


getch();
}

void circleMidpoint(int xc, int yc, int r)


{
int x = 0, y = r;
int p = 1 - r;

while (x < y)
{
drawCircle(xc, yc, x, y);
x++;
if (p < 0)
p = p + 2 * x + 1;
else
{
y--;
p = p + 2 * (x - y) + 1;
}
drawCircle(xc, yc, x, y);
delay(50);
}
}
void drawCircle(int xc, int yc, int x, int y)
{
putpixel(xc+x, yc+y, RED);
putpixel(xc-x, yc+y, RED);
putpixel(xc+x, yc-y, RED);
putpixel(xc-x, yc-y, RED);
putpixel(xc+y, yc+x, RED);
putpixel(xc-y, yc+x, RED);
putpixel(xc+y, yc-x, RED);
putpixel(xc-y, yc-x, RED);
}

4. WAP to draw an ellipse using mid-point algorithm

#include <stdio.h>
#include <dos.h>
#include <graphics.h>
#include <conio.h>
void ellipseMidpoint(float, float, float, float);
void drawEllipse(float, float, float, float);
void main()
{
float xc, yc, rx, ry;
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
printf("\nEnter the center coordinates of ellipse: ");
scanf("%f %f", &xc, &yc);
printf("\nEnter x-radius coordinate: ");
scanf("%f", &rx);
printf("\nEnter y-radius coordiante: ");
scanf("%f", &ry);
ellipseMidpoint(xc, yc, rx, ry);
getch();
}
void ellipseMidpoint(float xc, float yc, float rx, float ry)
{
float rxSq = rx * rx;
float rySq = ry * ry;
float x = 0, y = ry, p;
float px = 0, py = 2 * rxSq * y;

drawEllipse(xc, yc, x, y);


//Region 1
p = rySq - (rxSq * ry) + (0.25 * rxSq);
while (px < py)
{
x++;
px = px + 2 * rySq;
if (p < 0)
p = p + rySq + px;
else
{
y--;
py = py - 2 * rxSq;
p = p + rySq + px - py;
}
drawEllipse(xc, yc, x, y);
delay(30);
}
//Region 2
p = rySq*(x+0.5)*(x+0.5) + rxSq*(y-1)*(y-1) - rxSq*rySq;
while (y > 0)
{
y--;
py = py - 2 * rxSq;
if (p > 0)
p = p + rxSq - py;
else
{
x++;
px = px + 2 * rySq;
p = p + rxSq - py + px;
}
drawEllipse(xc, yc, x, y);
delay(30);
}
}
void drawEllipse(float xc, float yc, float x, float y)
{
putpixel(xc+x, yc+y, RED);
putpixel(xc-x, yc+y, RED);
putpixel(xc+x, yc-y, RED);
putpixel(xc-x, yc-y, RED);
}

5. WAP to handle mouse

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

union REGS i,o;

void show_mouse()
{
i.x.ax=1;
int86(0x33,&i,&o);
}

void hide_mouse()
{
i.x.ax=2;
int86(0x33,&i,&o);
}
void get_mouse_pos(int *x,int *y,int *button)
{
i.x.ax=3;
int86(0x33,&i,&o);
*x=o.x.cx;
*y=o.x.dx;
*button=o.x.bx&1;
}
void detect_mouse()
{
int button;
while (!kbhit () )
{
i.x.ax = 3;
int86 (0X33,&i,&o);
button=o.x.bx&7;
switch(button)
{
case 1:
printf("left button pressed\n");
break;
case 2:
printf("right button pressed\n");
break;
case 4:
printf("middle button pressed\n");
break;
case 3:
printf("left and right button pressed\n");
break;
case 5:
printf("left and middle button pressed\n");
break;
case 6:
printf("right and middle button pressed\n");
break;
case 7:
printf("all the three buttons pressed\n");
break;
default:
printf("No button pressed\n");
}
delay (200);
}
}

void main()
{
int gdriver = DETECT, gmode, errorcode,button,x1,y1,x2,y2;
initgraph(&gdriver, &gmode, "C:\\TURBOC3\\BGI");
detect_mouse ();
outtextxy(230,400,"Press any key to exit....");
while(!kbhit())
{
show_mouse();
get_mouse_pos(&x1,&y1,&button);
x2=x1;
y2=y1;
while(button==1)
{
hide_mouse();
line(x1,y1,x2,y2) ;
x1=x2;
y1=y2;
get_mouse_pos(&x2,&y2,&button);
}
}
getch();

6. WAP to create simple car shape

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TurboC3\\BGI");
cleardevice();
line( 150, 100, 242, 100);
ellipse(242, 105, 0, 90, 10, 5);
line(150, 100, 120, 150);
line(252, 105, 280, 150);
line(100, 150, 320, 150);
line(100, 150, 100, 200);
line(320,150,320,200);
line(100, 200, 110, 200);
line( 320, 200, 310, 200);
arc(130, 200, 0, 180, 20);
arc( 290, 200, 0, 180, 20);
line( 270, 200, 150, 200);
circle(130, 200, 17);
circle(290, 200, 17);
getch();
}

7. WAP to show line clipping

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT, gm;
float i,xmax,ymax,xmin,ymin,x1,y1,x2,y2,m;
float start[4],end[4],code[4];
clrscr();
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
printf("\n\tPlease enter the bottom left co-ordinate of viewport: ");
scanf("%f %f",&xmin,&ymin);
printf("\n\tPlease enter the top right co-ordinate of viewport: ");
scanf("%f %f",&xmax,&ymax);
printf("\nPlease enter the co-ordinates for starting point of line: ");
scanf("%f %f",&x1,&y1);
printf("\nPlease enter the co-ordinates for ending point of line: ");
scanf("%f %f",&x2,&y2);
for(i=0;i <4;i++)

{
start[i]=0;
end[i]=0;
}
m=(y2-y1)/(x2-x1);
if(x1 <xmin) start[0]=1;
if(x1 >xmax) start[1]=1;
if(y1 >ymax) start[2]=1;
if(y1 <ymin) start[3]=1;
if(x2 <xmin) end[0]=1;
if(x2 >xmax) end[1]=1;
if(y2 >ymax) end[2]=1;
if(y2 <ymin) end[3]=1;
for(i=0;i <4;i++)

code[i]=start[i]&&end[i];

if((code[0]==0)&&(code[1]==0)&&(code[2]==0)&&(code[3]==0))
{
if((start[0]==0)&&(start[1]==0)&&(start[2]==0)&&(start[3]==0)&&(end[0]==0)&&(end[1]==0
)&&(end[2]==0)&&(end[3]==0))
{
cleardevice();
printf("\n\t\tThe line is totally visible\n\t\tand not a clipping candidate");
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
getch();
}
else
{
cleardevice();
printf("\n\t\tLine is partially visible");
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
getch();

if((start[2]==0)&&(start[3]==1))
{
x1=x1+(ymin-y1)/m;
y1=ymin;
}
if((end[2]==0)&&(end[3]==1))
{
x2=x2+(ymin-y2)/m;
y2=ymin;
}
if((start[2]==1)&&(start[3]==0))
{
x1=x1+(ymax-y1)/m;
y1=ymax;
}
if((end[2]==1)&&(end[3]==0))
{
x2=x2+(ymax-y2)/m;
y2=ymax;
}
if((start[1]==0)&&(start[0]==1))
{
y1=y1+m*(xmin-x1);
x1=xmin;
}
if((end[1]==0)&&(end[0]==1))
{
y2=y2+m*(xmin-x2);
x2=xmin;
}
if((start[1]==1)&&(start[0]==0))
{
y1=y1+m*(xmax-x1);
x1=xmax;
}
if((end[1]==1)&&(end[0]==0))
{
y2=y2+m*(xmax-x2);
x2=xmax;
}

clrscr();
cleardevice();
printf("\n\t\tAfter clippling:");
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
getch();
}
}
else
{
clrscr();
cleardevice();
printf("\nLine is invisible");
rectangle(xmin,ymin,xmax,ymax);
}
getch();
closegraph();
}

8. WAP to translate a line

#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
#include<math.h>

void main()
{
int graphdriver=DETECT,graphmode,errorcode;
int i;
int x2,y2,x1,y1,x,y;
initgraph(&graphdriver,&graphmode,"C:\\TURBOC3\\BGI");
printf("Enter the 2 line end points:");
printf("x1,y1,x2,y2");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
setcolor(2);
line(x1,y1,x2,y2);

printf("Enter translation co-ordinates ");


printf("x,y");
scanf("%d%d",&x,&y);
x1=x1+x;
y1=y1+y;
x2=x2+x;
y2=y2+y;
printf("Line after translation");
setcolor(3);
setbkcolor(4);
line(x1,y1,x2,y2);

getch();
closegraph();
}

9. WAP to scale a line

#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
#include<math.h>

void main()
{
int graphdriver=DETECT,graphmode,errorcode;
int i;
int x2,y2,x1,y1,x,y;
initgraph(&graphdriver,&graphmode,"C:\\TURBOC3\\BGI");
printf("Enter the 2 line end points:");
printf("x1,y1,x2,y2");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);

setcolor(2);
line(x1,y1,x2,y2);

printf("Enter scaling co-ordinates ");


printf("x,y");
scanf("%d%d",&x,&y);

x1=(x1*x);
y1=(y1*y);
x2=(x2*x);
y2=(y2*y);

printf("Line after scaling");

setbkcolor(4);
setcolor(3);
line(x1,y1,x2,y2);

getch();
closegraph();
}

10. WAP to rotate a line

#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
void main()
{
int graphdriver=DETECT,graphmode,errorcode;
int i;
int x2,y2,x1,y1,x,y,xn,yn;
double r11,r12,r21,r22,th;
clrscr();
initgraph(&graphdriver,&graphmode,"C:\\TURBOC3\\BGI");
printf("Enter the 2 line end points:");
printf("x1,y1,x2,y2");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
line(x1,y1,x2,y2);
printf("\n\n\n[ Enter the angle");
scanf("%lf",&th);
r11=cos((th*3.1428)/180);
r12=sin((th*3.1428)/180);
r21=(-sin((th*3.1428)/180));
r22=cos((th*3.1428)/180);
//printf("%lf %lf %lf %lf",r11,r12,r21,r22);
xn=((x2*r11)-(y2*r12));
yn=((x2*r12)+(y2*r11));
line(x1,y1,xn,yn);
getch();
closegraph();
}

11. WAP to handle screen

#include<iostream.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>

void main()
{
textmode(C80);
textbackground(GREEN);
textcolor(BLUE);
int choice;

clrscr();

cout<<"\n\n\n \t\t\t -------------------------------------"<<endl;


cout<<"\t\t\t Hello Welcome to Agrohi Hospital "<<endl;
cout<<" \t\t\t -------------------------------------"<<endl;
sleep(5);

while(choice !=6)
{
cout<<" \t 1. Dr Arun Agrohi \n \t 2. Dr (Ms) Anju Agrohi \n\t 3. Dr Vineet Sirohi \n\t 4. Dr
Naveen Sahagal \n\t 5. Dr JV Chikara \n\t 6. Exit "<<endl;
cout<<"\n\n\t\t\tEnter Your Choice !!"<<endl;
cin>>choice;
switch(choice)
{
case 1: clrscr();
cout<<"\n \t\t Case -1 "<<endl;
break;

case 2: clrscr();
cout<<"\n \t\t Case -2 "<<endl;
break;
case 3: clrscr();
cout<<"\n \t\t Case -3 "<<endl;
break;
case 4: clrscr();
cout<<"\n \t\t Case -4 "<<endl;
break;
case 5: clrscr();
cout<<"\n \t\t Case -5 "<<endl;
break;
case 6: clrscr();
exit(0);
default: clrscr();
cout<<"Entered Wrong Choice !"<<endl;
break;

}
}

getch();
}

12. WAP to draw circle of circles

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

main()
{
int gd = DETECT, gm, x, y, color, angle = 0;
struct arccoordstype a, b;
initgraph(&gd, &gm, "C:\\TDM-GCC-64\\BGI");

//while(angle<=360)
// {
setcolor(BLACK);
arc(getmaxx()/2,getmaxy()/2,angle,angle+2,100);
setcolor(RED);
getarccoords(&a);
circle(a.xstart,a.ystart,25);

// setcolor(BLACK);
// arc(getmaxx()/2,getmaxy()/2,angle,angle+2,150);
// getarccoords(&a);
// setcolor(GREEN);
// circle(a.xstart,a.ystart,50);
// angle = angle+5;
// delay(100);
// }

getch();
closegraph();
return 0;
}

13. WAP to draw a pie chart

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

int main() {
int gd = DETECT, gm, x, y;
initgraph(&gd, &gm, "C:\\TC\\BGI");

settextstyle(BOLD_FONT,HORIZ_DIR,2);
outtextxy(220,10,"PIE CHART");
/* Setting cordinate of center of circle */
x = getmaxx()/2;
y = getmaxy()/2;

settextstyle(SANS_SERIF_FONT,HORIZ_DIR,1);
setfillstyle(SOLID_FILL, RED);
pieslice(x, y, 0, 60, 120);
outtextxy(x + 140, y - 70, "FOOD");

setfillstyle(SOLID_FILL, YELLOW);
pieslice(x, y, 60, 160, 120);
outtextxy(x - 30, y - 170, "RENT");

setfillstyle(SOLID_FILL, GREEN);
pieslice(x, y, 160, 220, 120);
outtextxy(x - 250, y, "ELECTRICITY");

setfillstyle(SOLID_FILL, BROWN);
pieslice(x, y, 220, 360, 120);
outtextxy(x, y + 150, "SAVINGS");

getch();
closegraph();
return 0;
}

Program Output
14. WAP to draw a bar chart

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

int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "X:\\TC\\BGI");

settextstyle(BOLD_FONT,HORIZ_DIR,2);
outtextxy(275,0,"BAR GRAPH");

setlinestyle(SOLID_LINE,0,2);
/* Draw X and Y Axis */
line(90,410,90,50);
line(90,410,590,410);
line(85,60,90,50);
line(95,60,90,50);
line(585,405,590,410);
line(585,415,590,410);

outtextxy(65,60,"Y");
outtextxy(570,420,"X");
outtextxy(70,415,"O");
/* Draw bars on screen */
setfillstyle(XHATCH_FILL, RED);
bar(150,80,200,410);
bar(225,100,275,410);
bar(300,120,350,410);
bar(375,170,425,410);
bar(450,135,500,410);

getch();
closegraph();
return 0;
}

15. WAP for 3D translation

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<process.h>
#include<graphics.h>
int x1,x2,y1,y2,mx,my,depth;
void draw();
void trans();
void main()
{

int gd=DETECT,gm,c;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
printf("\n\t\t3D Translation\n\n");
printf("\nEnter 1st top value(x1,y1):");
scanf("%d%d",&x1,&y1);
printf("Enter right bottom value(x2,y2):");
scanf("%d%d",&x2,&y2);
depth=(x2-x1)/4;
mx=(x1+x2)/2;
my=(y1+y2)/2;
draw();
getch();
cleardevice();
trans();
getch();

}
void draw()
{
bar3d(x1,y1,x2,y2,depth,1);
}
void trans()
{

int a1,a2,b1,b2,dep,x,y;
printf("\n Enter the Translation Distances:");
scanf("%d%d",&x,&y);
a1=x1+x;
a2=x2+x;
b1=y1+y;
b2=y2+y;
dep=(a2-a1)/4;
bar3d(a1,b1,a2,b2,dep,1);
setcolor(5);
draw();
16. WAP for 3D scaling

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<process.h>
#include<graphics.h>
int x1,x2,y1,y2,mx,my,depth;
void draw();
void scale();
void main()
{

int gd=DETECT,gm,c;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
printf("\n\t\t3D Scaling\n\n"); printf("\
nEnter 1st top value(x1,y1):"); scanf("%d
%d",&x1,&y1);
printf("Enter right bottom value(x2,y2):");
scanf("%d%d",&x2,&y2);
depth=(x2-x1)/4;
mx=(x1+x2)/2;
my=(y1+y2)/2;
draw();
getch();
cleardevice();
scale();
getch();
}
void draw()
{
bar3d(x1,y1,x2,y2,depth,1);
}
void scale()
{
int x,y,a1,a2,b1,b2,dep;
printf("\n\n Enter scaling Factors:");
scanf("%d%d",&x,&y);
a1=mx+(x1-mx)*x;
a2=mx+(x2-mx)*x;
b1=my+(y1-my)*y;
b2=my+(y2-my)*y;
dep=(a2-a1)/4;
bar3d(a1,b1,a2,b2,dep,1);
setcolor(5);
draw();
}
17. WAP for 3D rotation

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
int x1,x2,y1,y2,mx,my,depth;
void draw();
void rotate();
void main()
{
int gd=DETECT,gm,c;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
printf("\n3D Transformation Rotating\n\n");
printf("\nEnter 1st top value(x1,y1):");
scanf("%d%d",&x1,&y1);
printf("Enter right bottom value(x2,y2):");
scanf("%d%d",&x2,&y2);
depth=(x2-x1)/4;
mx=(x1+x2)/2;
my=(y1+y2)/2;
draw(); getch();
cleardevice();
rotate();
getch();
}
void draw()
{
bar3d(x1,y1,x2,y2,depth,1);
}
void rotate()
{
float t;
int a1,b1,a2,b2,dep;
printf("Enter the angle to rotate=");
scanf("%f",&t);
t=t*(3.14/180);
a1=mx+(x1-mx)*cos(t)-(y1-my)*sin(t);
a2=mx+(x2-mx)*cos(t)-(y2-my)*sin(t);
b1=my+(x1-mx)*sin(t)-(y1-my)*cos(t);
b2=my+(x2-mx)*sin(t)-(y2-my)*cos(t);
if(a2>a1)
dep=(a2-a1)/4;
else
dep=(a1-a2)/4;
bar3d(a1,b1,a2,b2,dep,1); setcolor(5);
}

18. WAP for 4-connected flood fill

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

void flood(int,int,int,int);

void
main() {
int gd,gm=DETECT;
clrscr();
detectgraph(&gd,&g
m);
initgraph(&gd,&gm,"C:\\TurboC3\\
BGI"); rectangle(50,50,100,100);
flood(55,55,9,0);
getch()
;}

void flood(int x,int y, int fill_col, int


old_col) {
if(getpixel(x,y)==old_c
ol) {
delay(10);
putpixel(x,y,fill_col);
flood(x+1,y,fill_col,old_co
l); flood(x-
1,y,fill_col,old_col);
flood(x,y+1,fill_col,old_co
l); flood(x,y-
1,fill_col,old_col);
}
}
19. WAP for 8-connected flood fill

#include<stdio.h>
#include<conio.h>
#include<graphics.
h> #include<dos.h>
void
flood(int,int,int,int);
void main()
{
int gd,gm=DETECT;
clrscr();
detectgraph(&gd,&g
m);
initgraph(&gd,&gm,"C:\\TurboC3\\
BGI"); rectangle(50,50,100,100);
flood(55,55,12,0);
getch()
;}
void flood(int x,int y, int fill_col, int
old_col) {
if(getpixel(x,y)==old_c
ol) {
delay(10);
putpixel(x,y,fill_col);
flood(x+1,y,fill_col,old_co
l); flood(x-
1,y,fill_col,old_col);
flood(x,y+1,fill_col,old_co
l); flood(x,y-
1,fill_col,old_col);
flood(x + 1, y - 1, fill_col,
old_col); flood(x + 1, y + 1,
fill_col, old_col); flood(x - 1, y -
1, fill_col, old_col); flood(x - 1,
y + 1, fill_col, old_col);
}
}
20. WAP for 4-connected Boundary fill

#include<stdio.h>
#include<conio.h>
#include<graphics.
h> #include<dos.h>
void boundary_fill(int x, int y, int fcolor, int
bcolor) {
if ((getpixel(x, y) != bcolor) && (getpixel(x, y) != fcolor))
{ delay(10);
putpixel(x, y,
fcolor);
boundary_fill(x + 1, y, fcolor,
bcolor); boundary_fill(x - 1, y,
fcolor, bcolor); boundary_fill(x, y
}
+ 1, fcolor, bcolor);
}
boundary_fill(x, y - 1, fcolor, bcolor);
void
main()
{
int x, y, fcolor,
bcolor; int
gd=DETECT,gm;
initgraph(&gd, &gm, "C:\\TurboC3\\
BGI"); printf("Enter the seed point (x,y)
: "); scanf("%d%d", &x, &y);
printf("Enter boundary color :
"); scanf("%d", &bcolor);
printf("Enter new color : ");
scanf("%d", &fcolor);
circle(100,200,45);
boundary_fill(x,y,fcolor,bcolor
); getch();
}
21. WAP for 8-connected Boundary fill

#include<stdio.h>
#include<conio.h>
#include<graphics.
h> #include<dos.h>
void boundary_fill(int x, int y, int fcolor, int
bcolor) {

if ((getpixel(x, y) != bcolor) && (getpixel(x, y) !=


fcolor)) { delay(10);
putpixel(x, y, fcolor);
boundary_fill(x + 1, y, fcolor,
bcolor); boundary_fill(x , y+1, fcolor,
bcolor); boundary_fill(x+1, y + 1,
fcolor, bcolor); boundary_fill(x-1, y -
1, fcolor, bcolor); boundary_fill(x-1,
y, fcolor, bcolor); boundary_fill(x , y-
1, fcolor, bcolor); boundary_fill(x-1,
y + 1, fcolor, bcolor);
boundary_fill(x+1, y - 1, fcolor,
bcolor);

}
}
void
main() {
int x, y, fcolor,
bcolor; int
gd=DETECT,gm;
initgraph(&gd, &gm, "C:\\TurboC3\\
BGI"); printf("Enter the seed point (x,y)
: "); scanf("%d%d", &x, &y);
printf("Enter boundary color :
"); scanf("%d", &bcolor);
printf("Enter new color : ");
scanf("%d", &fcolor);
rectangle(50,50,100,100);
boundary_fill(x,y,fcolor,bcolor
); getch();
}

22. WAP for polygon filling

#include <stdio.h>
#include <conio.h>
#include <graphics.h>
main()
{

int n,i,j,k,gd,gm,dy,dx;
int x,y,temp;
int a[20][2],xi[20];
float slope[20];

clrscr();
printf("\n\n\tEnter the no. of edges of polygon : ");
scanf("%d",&n);
printf("\n\n\tEnter the cordinates of polygon :\n\n\n ");

for(i=0;i<n;i++)
{
printf("\tX%d Y%d : ",i,i);
scanf("%d %d",&a[i][0],&a[i][1]);
}

a[n][0]=a[0][0];
a[n][1]=a[0][1];

detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");

/*- draw polygon -*/

for(i=0;i<n;i++)
{
line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
}

getch();

for(i=0;i<n;i++)
{
dy=a[i+1][1]-a[i][1];
dx=a[i+1][0]-a[i][0];

if(dy==0) slope[i]=1.0;
if(dx==0) slope[i]=0.0;

if((dy!=0)&&(dx!=0)) /*- calculate inverse slope -*/


{
slope[i]=(float) dx/dy;
}
}

for(y=0;y< 480;y++)
{
k=0;
for(i=0;i<n;i++)
{

if( ((a[i][1]<=y)&&(a[i+1][1]>y))||
((a[i][1]>y)&&(a[i+1][1]<=y)))
{
xi[k]=(int)(a[i][0]+slope[i]*(y-a[i][1]));
k++;
}
}

for(j=0;j<k-1;j++) /*- Arrange x-intersections in order -*/


for(i=0;i<k-1;i++)
{
if(xi[i]>xi[i+1])
{
temp=xi[i];
xi[i]=xi[i+1];
xi[i+1]=temp;
}
}

setcolor(35);
for(i=0;i<k;i+=2)
{
line(xi[i],y,xi[i+1]+1,y);
getch();
}

23. WAP for man walking

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h
> void main()
{
int gd=DETECT,gm,i;
initgraph(&gd,&gm,"C:\\TurboC3\\
BGI"); //creation of man object by using
circle,line. setcolor(7);
setfillstyle(SOLID_FILL,10);
circle(50,50,30); // drawing
head floodfill(52,52,7);
setcolor(13);
line(50,80,50,200); //drawing
body line(50,110,20,140); //left
hand line(50,110,80,140);
//right hand
line(50,200,20,230); //left leg
line(50,200,80,230); //right leg
// for loop for moving
man
for(i=50;i<=getmaxx();i+
+) {
setcolor(7);
setfillstyle(SOLID_FILL,10);
circle(i,50,30); // drawing
head floodfill(i+2,52,7);
setcolor(13);
line(i,80,i,200); //drawing
body line(i,110,i-30,140);
//left hand
line(i,110,i+30,140); //right
hand line(i,200,i-30,230);
//left leg line(i,200,i+30,230);
//right leg cleardevice();
delay(10)
;}
//doing simple animation using
translation for(i=50;i<=getmaxx()/2;i+
+)
{
setcolor(7);
setfillstyle(SOLID_FILL,10);
circle(i,50,30); // drawing
head floodfill(i+2,52,7);
setcolor(13);
line(i,80,i,200); //drawing
body line(i,110,i-30,140);
//left hand
line(i,110,i+30,140); //right
hand line(i,200,i-30,230);
//left leg line(i,200,i+30,230);
//right leg cleardevice();
delay(10);
}
getch()
;}

24. Write a program for moving circle in different directions


#include<stdio.h>
#include<graphics.h
> #include<conio.h>
#include<dos.h>
void main()
{
int gd=DETECT,gm,i;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
//for moving circle from left to right,the following loop works
for(i=50;i<=getmaxx();i++)
{
setcolor(3);
setfillstyle(SOLID_FILL,9);
circle(50+i,50,50);
floodfill(52+i,52,3); delay(20);
cleardevice(); }
//for moving circle from right to left, the following loop works
for(i=getmaxy();i>=0;i--)
{
setcolor(3);
setfillstyle(SOLID
_FILL,9);
circle(i,50,50);
floodfill(i+2,52,3);
delay(20);
cleard
evice();
}
//for moving circle from top to bottom,the
following loop works for(i=50;i<=getmaxy();i++)
{
setcolor(3);
setfillstyle(SOLID
_FILL,9);
circle(50,i,50);
floodfill(52,i+2,3);
delay(20);
cleard
evice();
}
//for moving circle from bottom to top,the
following loop works for(i=getmaxy();i>=0;i--)
{
setcolor(3);
setfillstyle(SOLID
_FILL,9);
circle(50,i,50);
floodfill(52,i+2,3);
delay(20);
cleard
evice();
}
//for moving circle in diagonal direction,the
following loop works for(i=50;i<=getmaxx();i++)
{
setcolor(3);
setfillstyle(SOLID
_FILL,9);
circle(i,i,50);
floodfill(i+2,i+2,3)
; delay(20);
cleard
evice();
}
//for moving circle in reverse diagonal direction,the
following loop works for(i=getmaxx();i>=0;i--)
{
setcolor(3);
setfillstyle(SOLID
_FILL,9);
circle(i,i,50);
floodfill(i+2,i+2,3)
; delay(20);
cleard
evice();
}

g
e
t
c
h
(
)
;

25. Write a program for windmill rotation

#include<stdio.h>
#include<graphics.
h>
#include<math.h>
void wind(float
x[7],float y[7]);
void main()
{
int gd=DETECT,gm;
float
x[7],y[7],maxx,maxy,xw1,yw
1,xw2,yw2; float theta=30;
initgraph(&gd,&gm,"C:\\
TurboC3\\BGI");
maxx=getmaxx();
maxy
=get
maxy
();
x[0]=
maxx
/2;
y[0]=
maxy
/2;
x[1]=
y[4]=
x[2]=
-90;
y[6]=
y[5]=
y[1]=
60;
y[2]=
35;
y
[
3
]
=
-
1
0
0
;

x
[
4
]
=
2
0
;

x
[
3
]
=
0
;

x
[
5
]
=
9
0
;

x
[
6
]
=
6
5
;
theta=th
eta*22/7
/180;
while(kb
hit()==0)
{
wind(x,y);
xw1=cos(theta)*x[1]+s
in(theta)*y[1]; yw1=-
sin(theta)*x[1]+cos(th
eta)*y[1];
xw2=cos(theta)*x[2]+s
in(theta)*y[2]; yw2=-
sin(theta)*x[2]+cos(th
eta)*y[2]; x[1]=xw1;
y[1]=yw1;
x[2]=xw2;
y[2]=yw2;
xw1=cos(theta)*x[3]+s
in(theta)*y[3];
yw1=-
sin(theta)*x[3]+cos(th
eta)*y[3];
xw2=cos(theta)*x[4]+s
in(theta)*y[4]; yw2=-
sin(theta)*x[4]+cos(th
eta)*y[4]; x[3]=xw1;
y
[
3
]
=
y
w
1
;

x
[
4
]
=
x
w
2
;

y
[
4
]
=
y
w
2
;
xw1=cos(theta)*x[5]+s
in(theta)*y[5]; yw1=-
sin(theta)*x[5]+cos(th
eta)*y[5];
xw2=cos(theta)*x[6]+s
in(theta)*y[6]; yw2=-
sin(theta)*x[6]+cos(th
eta)*y[6]; x[5]=xw1;
y
[
5
]
=
y
w
1
;

x
[
6
]
=
x
w
2
;

y
[
6
]
=
y
w
2
;
d
e
l
a
y
(
5
0
)
;
c
l
e
a
r
d
e
v
i
c
e
(
)
;
}

closegraph();
}

void wind(float
x[7],float y[7])
{
cleardevice();
line(x[0],y[0],x[0
]-50,y[0]+200);
line(x[0],y[0],x[0]+50,y[
0]+200); line(x[0]-
60,y[0]+200,x[0]+60,y[0]
+200);
line(x[0],y[0],x[0]+x[1],y
[0]-y[1]);
line(x[0],y[0],x[0]+x[2],y
[0]-y[2]);
line(x[0]+x[1],y[0]-
y[1],x[0]+x[2],y[0]-y[2]);
line(x[0],y[0],x[0]+x[3],y[0]
-y[3]);
line(x[0],y[0],x[0]+x[4],y[0]
-y[4]); line(x[0]+x[3],y[0]-
y[3],x[0]+x[4],y[0]-y[4]);
line(x[0],y[0],x[0]+x[5],y[0]
-y[5]);
line(x[0],y[0],x[0]+x[6],y[0]
-y[6]); line(x[0]+x[5],y[0]-
y[5],x[0]+x[6],y[0]-y[6]);
}
26. WAP for countdown

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

int main()
{
int gd = DETECT, gm, i;
char a[5];

initgraph( &gd, &gm, "C:\\TC\\BGI");

settextjustify( CENTER_TEXT, CENTER_TEXT );


settextstyle(DEFAULT_FONT,HORIZ_DIR,3);
setcolor(RED);

for (i = 30; i >=0; i--)


{
sprintf(a,"%d",i);
outtextxy(getmaxx()/2, getmaxy()/2, a);
delay(1000);

if ( i == 0 )
break;
cleardevice();
}

getch();
closegraph();
return 0;
}

27. WAP for rotation of wheels

#include<math.h>
#include<graphics.h>
#define N getmaxy()
#define Radius 40
void main()
{
int gd=DETECT,gm;

//xc,yc coordinate of center


//i for loop iteration
int i,xc,yc;

//rx1,ry1,sep used for wheel's cross


int rx1,ry1,sep;

//change bgi directory according to your's


initgraph(&gd,&gm,"c:\\turboc3\\bgi");

//initialise yc it is constant
yc=getmaxy()/2-Radius;
i=0;

//loop stops when keyboard key down or i>=N


//kbhit checks whether keyboard pressed or not

while(i<N&&!kbhit())
{

//increment xc for moving the wheel


xc=i;
delay(50);

//clear previous screen


clearviewport();

//represent road or some ground as stright line


line(0,getmaxy()/2,getmaxx(),getmaxy()/2);

//draw wheel
circle(xc,yc,Radius);

//following if else block draws


//wheel cross in two arrangments shows alternatively
if(i%2==0)
{
line(xc,yc+Radius,xc,yc-Radius);
line(xc-Radius,yc,xc+Radius,yc);
}
else
{
rx1=xc-(Radius/sqrt(2));
ry1=yc-(Radius/sqrt(2));
sep=sqrt(2)*Radius;
line(rx1,ry1,rx1+sep,ry1+sep);
line(rx1,ry1+sep,rx1+sep,ry1);
}

//increment i
i+=3;
}
}

28. Write a program for event handling through mouse

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

union REGS i,o;


void function1()
{
gotoxy(50,50);printf(" Function 1 Called.");
}

void function2()
{
gotoxy(100,100); printf(" Function 2 Called.");
}

void show_mouse()
{
i.x.ax=1;
int86(0x33,&i,&o);
}

void get_mouse_pos(int *x,int *y,int *b)


{
i.x.ax=3;
int86(0x33,&i,&o);
*x=o.x.cx;
*y=o.x.dx;
*b=o.x.bx&1;
}

void main()
{

int gdriver = DETECT,gmode,x1,y1,x,y,b;


initgraph(&gdriver, &gmode, "C:\\TURBOC3\\BGI");

rectangle(100,200,140,230);
rectangle(150,200,190,230);
show_mouse();
while(!kbhit())
{
get_mouse_pos(&x,&y,&b);

if(b==1)
{
for(x1=100;x1<=140;x1++)
for(y1=200;y1<=230;y1++)
if( x==x1 && y==y1)
function1();

for(x1=150;x1<=190;x1++)
for(y1=200;y1<=230;y1++)
if( x==x1 && y==y1)
function2();
}
delay(100);
}
getch();
closegraph();
}

29. WAP for moving car

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>

void main()
{
clrscr();
int gd=DETECT,gm,i;
initgraph(&gd,&gm,"C:\\turboC3\\BGI");
//setbkcolor(RED);
line(0,390,639,390); //road
i=0;
while(!kbhit())
//for(int i=0;i<500;i++)
{
/*CAR BODY*/
setcolor(YELLOW);
setfillstyle(SOLID_FILL,RED);

line(50+i,370,90+i,370);
arc(110+i,370,0,180,20);
line(130+i,370,220+i,370);
arc(240+i,370,0,180,20);
line(260+i,370,300+i,370);
line(300+i,370,300+i,350);
line(300+i,350,240+i,330);
line(240+i,330,200+i,300);
line(200+i,300,110+i,300);
line(110+i,300,80+i,330);
line(80+i,330,50+i,340);
line(50+i,340,50+i,370);

/*CAR WINDIOW FRONT*/


line(165+i,305,165+i,330);
line(165+i,330,230+i,330);
line(230+i,330,195+i,305);
line(195+i,305,165+i,305);

/*CAR WINDOW BACK*/


line(160+i,305,160+i,330);
line(160+i,330,100+i,330);
line(95+i,330,120+i,305);
line(120+i,305,160+i,305);

/*CAR WHEEL*/
setcolor(YELLOW);
setfillstyle(SOLID_FILL,RED);
circle(110+i,370,17);
floodfill(110+i,370,YELLOW);
circle(240+i,370,17);
floodfill(240+i,370,YELLOW);

delay(10);
cleardevice();
line(0,390,639,390);
i++;
}
getch();
//return0;

30. WAP to simulate a man walking in rain

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

#define ScreenWidth getmaxx()


#define ScreenHeight getmaxy()
#define GroundY ScreenHeight*0.75

int ldisp=0;

void DrawManAndUmbrella(int x,int ldisp)


{
//head
circle(x,GroundY-90,10);
line(x,GroundY-80,x,GroundY-30);
//hand
line(x,GroundY-70,x+10,GroundY-60);
line(x,GroundY-65,x+10,GroundY-55);
line(x+10,GroundY-60,x+20,GroundY-70);
line(x+10,GroundY-55,x+20,GroundY-70);
//legs
line(x,GroundY-30,x+ldisp,GroundY);
line(x,GroundY-30,x-ldisp,GroundY);
//umbrella
pieslice(x+20,GroundY-120,0,180,40);
line(x+20,GroundY-120,x+20,GroundY-70);
}

void Rain(int x)
{
int i,rx,ry;
for(i=0;i<400;i++)
{
rx=rand() % ScreenWidth;
ry=rand() % ScreenHeight;
if(ry<GroundY-4)
{
if(ry<GroundY-120 || (ry>GroundY-120 && (rx<x-20 ||
rx>x+60)))
line(rx,ry,rx+0.5,ry+4);
}
}
}
void main()
{
int gd=DETECT,gm,x=0;
//Change BGI directory according to yours
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
while(!kbhit())
{
//Draw Ground
line(0,GroundY,ScreenWidth,GroundY);
Rain(x);
ldisp=(ldisp+2)%20;
DrawManAndUmbrella(x,ldisp);
delay(75);
cleardevice();
x=(x+2)%ScreenWidth;
}
getch();
}

Output

You might also like