1. WAP to draw a line using DDA Algorithm covering all cases of slope.
Solution:
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
void main()
{
int i,gd=0,gm;
char msg;
int a,x1,x2,y1,y2,mx,my;
float m,x,y;
clrscr();
initgraph(&gd,&gm,"C:\\TC\\BGI");
printf("Enter x1 Coordinate: ");
scanf("%d",&x1);
printf("Enter y1 Coordinate: ");
scanf("%d",&y1);
printf("Enter x2 Coordinate: ");
scanf("%d",&x2);
printf("Enter y2 Coordinate: ");
scanf("%d",&y2);
mx=x2-x1;
my=y2-y1;
m=(float)my/mx;
printf("\nSlope: %f \n",m);
x=x1;
y=y1;
printf("\n X : %f \t Y: %f",x,y);
for(i=x1;i<=x2;i++)
{
if(m<1)
{
x=x+1;
y=m+y;
printf("\n X : %f \t Y: %f",x,y);
}
else if(m>1)
{
x=1/m+x;
y=y+1;
printf("\n X : %f \t Y: %f",x,y);
}
else
{
x=x+1;
y=m+y;
printf("\n X : %f \t Y: %f",x,y);
}
Ajit kumar Roy
Roll No.: 03 & Semester-3
}
getch();
}
Output:
Ajit kumar Roy
Roll No.: 03 & Semester-3
2. WAP to draw a line using Bresenhem Algorithm covering all cases of
slope and decision parameter.
Solution:
# include <stdio.h>
# include <conio.h>
# include <graphics.h>
void main()
{
int dx,dy,x,y,p,x1,y1,x2,y2;
int gd=0,gm;
clrscr();
initgraph(&gd,&gm,"C:\\TC\\BGI");
printf("\nEnter the x1 Coordinate: ");
scanf("%d",&x1);
printf("\nEnter the y1 Coordinate: ");
scanf("%d",&y1);
printf("\nEnter the x2 Coordinate: ");
scanf("%d",&x2);
printf("\nEnter the y2 Coordinate: ");
scanf("%d",&y2);
dx = (x2 - x1);
dy = (y2 - y1);
p = 2 * (dy) - (dx);
x = x1;
y = y1;
putpixel(x,y,WHITE);
while(x <= x2)
{
if(p < 0)
{
x=x+1;
y=y;
p = p + 2 * (dy);
}
else
{
x=x+1;
y=y+1;
p = p + 2 * (dy - dx);
}
putpixel(x,y,WHITE);
}
getch();
}
Ajit kumar Roy
Roll No.: 03 & Semester-3
Output:
Ajit kumar Roy
Roll No.: 03 & Semester-3
3. WAP to implement all the graphics functions.
1. Initgraph()
2. Closegraph()
3. Putpixel()
4. Getpixel()
5. Getmaxx()
6. Getmaxy()
7. Line()
8. Setlinestyle()
9. Moveto()
10. Setcolor()
11. Setfillstyle()
12. Floodfill()
13. Circle()
14. Getmaxcolor()
15. Rectangle()
16. Setbkcolor()
17. Outtext()
18. Outtextxy()
19. Settextstyle()
20. Settextjustify()
21. Getx()
22. Gety()
23. Getgraphmode()
24. Getmodename()
25. Bar()
26. Bar3d()
27. Arc()
28. Ellipse()
29. Pieslice()
30. Sector()
31. Cleardevice()
32. Fillellipse()
33. Lineto()
Ajit kumar Roy
Roll No.: 03 & Semester-3
34. Gotoxy()
35. delay()
Solution:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm,col,x,y,i,mode;
char *mode1;
initgraph(&gd,&gm,"C:\\TC\\BGI");
cleardevice();
putpixel(100,100,WHITE);
col=getpixel(100,100);
printf("\nColor No is: %d\n",col);
setcolor(CYAN);
setfillstyle(1,CYAN);
circle(getmaxx()/4,getmaxy()/4,40);
floodfill(getmaxx()/4,getmaxy()/4,CYAN);
x=getx();
y=gety();
printf("GET X= %d GET Y= %d\n",x,y);
// initgraph
//cleardevice
// putpixel
//getpixel
//setcolor
//setfillstyle
//circle,getmaxx,getmaxy
//floodfill
//getx,gety
setcolor(WHITE);
setlinestyle(SOLID_LINE,1,10);
//setlinestyle
line(250,100,250,200);
//line
delay(200);
//delay
outtextxy(220,220,"Hello through outtextxy");
//outtextxy
outtext("HELLO");
//outtext
setbkcolor(RED);
rectangle(140,200,190,300);
//rectangle
x= getmaxcolor();
printf("Maximum colour is: %d\n",x);
//getmaxcolor
settextstyle(SMALL_FONT,HORIZ_DIR,2);
//settextstyle
outtextxy(250,50,"HELLO...!") ;
bar(100,350,200,400);
//bar
bar3d(250,300,350,250,20,10);
//bar3d
//lineto(350,175);
//lineto
pieslice(400,400,420,380,50);
//pieslice
sector(480,400,460,380,50,50);
//sector
mode=getgraphmode();
printf("\n\n\n\n\n\nMode= %d",mode);
//graphmode
printf("\n");
mode1=getmodename(mode);
printf("Mode name is %s",getmodename(mode));
getch();
closegraph();
}
Ajit kumar Roy
//closegraph
6
Roll No.: 03 & Semester-3
Output:
Ajit kumar Roy
Roll No.: 03 & Semester-3
4. WAP to draw a hut and show sun on right side at top and rainbow on left
top side.
Solution:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm,i,x,y;
initgraph(&gd,&gm,"C:\\TC\\BGI");
for(i=20;i<60;i++)
{
setcolor(i/5);
arc(120,200,45,135,180-i);
delay(100);
}
setcolor(14);
setfillstyle(SOLID_FILL,14);
circle(getmaxx()-50,100,40);
floodfill(getmaxx()-50,100,14);
setcolor(WHITE);
line(200,220,250,100);
line(300,220,250,100);
line(320,220,270,100);
line(340,220,290,100);
line(360,220,310,100);
line(380,220,330,100);
line(400,220,350,100);
line(420,220,370,100);
line(440,220,390,100);
line(460,220,410,100);
line(480,220,430,100);
line(500,220,450,100);
line(200,220,350,220);
line(250,100,500,100);
line(260,120,500,120);
line(265,140,500,140);
line(275,160,500,160);
line(285,180,500,180);
line(295,200,500,200);
line(250,220,500,220);
line(500,220,500,100);
line(300,400,300,220);
Ajit kumar Roy
Roll No.: 03 & Semester-3
line(280,400,280,250);
line(250,400,250,250);
line(220,250,280,250);
line(300,500,400,500);
line(200,400,200,220);
line(220,400,220,250);
line(200,400,300,400);
line(500,220,500,400);
line(270,400,500,400);
circle(getmaxx()/2-70,getmaxy()/2-60,20);
circle(getmaxx()/2-70,getmaxy()/2-60,15);
circle(getmaxx()/2-70,getmaxy()/2-60,10);
circle(getmaxx()/2-70,getmaxy()/2-60,5);
setcolor(CYAN);
rectangle(350,250,450,350);
line(350,300,450,300);
line(400,250,400,350);
getch();
}
Output:
Ajit kumar Roy
Roll No.: 03 & Semester-3
5. WAP to draw all lines horizontally first and then vertical lines.
Solution:
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
void main()
{
int i,x=0,y=0;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
while(y<=getmaxy())
{
line(x,y,getmaxx(),y);
y=y+40;
delay(300);
}
while(x<getmaxx())
{
line(x,0,x,getmaxy());
x=x+40;
delay(300);
}
getch();
}
Output:
Ajit kumar Roy
10
Roll No.: 03 & Semester-3
6. WAP to display al lines with different styles and display text with specific
style as name of line style below it.
Solution:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm,i,y=75;
initgraph(&gd,&gm,"C:\\TC\\BGI");
for(i=0;i<5;i++)
{
setlinestyle(i,1,1);
line(0,y,150,y);
y=y+75;
moveto (100,100);
}
outtextxy(10,95,"SOLID LINE");
outtextxy(10,170,"DOTTED LINE");
outtextxy(10,245,"CENTER LINE");
outtextxy(10,320,"DASHED LINE");
outtextxy(10,395,"USER-DEFINED LINE");
getch();
}
Output:
Ajit kumar Roy
11
Roll No.: 03 & Semester-3
7. WAP to display current positions at different places on screen.
Solution:
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <dos.h>
void main()
{
int i,gd=0,gm;
char msg;
initgraph(&gd,&gm,"C://TC//BGI");
putpixel(30,60,RED);
outtextxy(25,65,"RED");
delay(500);
putpixel(100,40,BLUE);
outtextxy(25,65,"RED");
delay(500);
putpixel(300,200,GREEN);
outtextxy(25,65,"RED");
delay(500);
putpixel(80,160,CYAN);
outtextxy(25,65,"RED");
delay(500);
putpixel(100,100,BROWN);
outtextxy(25,65,"RED");
delay(500);
putpixel(300,20,MAGENTA);
outtextxy(25,65,"RED");
delay(500);
putpixel(500,230,WHITE);
outtextxy(25,65,"RED");
getch();
}
Output:
Ajit kumar Roy
12
Roll No.: 03 & Semester-3
8. WAP to draw nested circles with different colors and patterns.
Solution:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm,r=100,i;
initgraph(&gd,&gm,"C:\\TC\\BGI");
for(i=0;i<10;i++)
{
setcolor(i);
setfillstyle(6,i);
circle(getmaxx()/2,getmaxy()/2,r);
floodfill(getmaxx()/2,getmaxy()/2,i);
r=r-10;
}
getch();
}
Output:
Ajit kumar Roy
13
Roll No.: 03 & Semester-3
9. WAP to change background color of screen and and display text as name
of style in different styles in different colors.
Solution:
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <string.h>
void main()
{
int i,j,gd=0,gm,k=0;
char msg;
char c[10]
[15]={"DEFAULT_FONT","TRIPLEX_FONT","SMALL_FONT","SANS_SERIF_F
ONT","GOTHIC_FONT"};
clrscr();
initgraph(&gd,&gm,"C://TC//BGI");
for(i=0;i<5;i++)
{
setbkcolor(i+1);
settextstyle(i,k,5);
outtextxy(100,100,c[i]);
delay(1000);
cleardevice();
}
getch();
}
Output:
Ajit kumar Roy
14
Roll No.: 03 & Semester-3
Ajit kumar Roy
15
Roll No.: 03 & Semester-3
10. WAP to show different arcs starting from lower corner of screen to upper
corner.
Solution:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd = DETECT,gm,mx,my,i,r=50;
initgraph(&gd,&gm,"C:\\TC\\BGI");
mx=getmaxx();
my=getmaxy();
for(i=0;i<=50;i++)
{
arc(mx,my,90,180,r);
r=r+20;
}
getch();
}
Output:
Ajit kumar Roy
16
Roll No.: 03 & Semester-3
11. WAP to draw a bar3d and its pattern should keep on changing.
Solution:
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <dos.h>
void main()
{
int i,gd=0,gm;
char msg;
initgraph(&gd,&gm,"C://TC//BGI");
for(i=1;i<10;i++)
{
setfillstyle(i,BLUE);
bar3d(150,350,getmaxy()-150,getmaxx()-350,30,1);
floodfill(150,350,WHITE);
delay(1000);
cleardevice();
}
getch();
}
Output:
Ajit kumar Roy
17
Roll No.: 03 & Semester-3
Ajit kumar Roy
18
Roll No.: 03 & Semester-3
12. WAP to draw arcs in four corners of the screen, its pattern should keep
changing and background color of screen should also keep changing.
Solution:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd = DETECT,gm,mx,my,i;
initgraph(&gd,&gm,"C:\\TC\\BGI");
mx=getmaxx();
my=getmaxy();
for(i=1;i<=5;i++)
{
setbkcolor(i);
setfillstyle(4,10);
arc(0,0,270,360,100);
floodfill(0,0,WHITE);
delay(600);
setfillstyle(4,10);
arc(mx,0,180,270,100);
floodfill(mx,0,WHITE);
delay(600);
setfillstyle(4,10);
arc(0,my,0,90,100);
floodfill(0,my,WHITE);
delay(600);
setfillstyle(4,10);
arc(mx,my,90,180,100);
floodfill(mx,my,WHITE);
delay(400);
cleardevice();
}
getch();
}
Ajit kumar Roy
19
Roll No.: 03 & Semester-3
Output:
Ajit kumar Roy
20
Roll No.: 03 & Semester-3
Ajit kumar Roy
21
Roll No.: 03 & Semester-3
13. WAP to display text in the same color according to color code in center of
screen.
Solution:
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <string.h>
void main()
{
int i,j,gd=0,gm,n;
char msg;
char c[10]
[12]={ "BLUE","GREEN","CYAN","RED","MAGENTA","BROWN","LIGHT
GREY","DARK GREY","LIGHT BLUE","LIGHT GREEN"};
clrscr();
initgraph(&gd,&gm,"C://TC//BGI");
for(i=0;i<10;i++)
{
setcolor(i+1);
outtextxy(getmaxx()/4,getmaxy()/4,c[i]);
delay(1000);
cleardevice();
}
getch();
}
Output:
Ajit kumar Roy
22
Roll No.: 03 & Semester-3
Ajit kumar Roy
23
Roll No.: 03 & Semester-3
14. WAP to draw a fan.
Solution:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd = DETECT,gm,mx,my,i;
initgraph(&gd,&gm,"C:\\TC\\BGI");
mx=getmaxx()/2;
my=getmaxy()/2;
circle(mx,my,5);
pieslice(mx,my,0,20,80);
pieslice(mx,my,230,250,80);
pieslice(mx,my,110,130,80);
getch();
}
Output:
Ajit kumar Roy
24
Roll No.: 03 & Semester-3
15. WAP to draw a car.
Solution:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd = DETECT,gm,x,a;
initgraph(&gd,&gm,"C:\\TC\\BGI");
x=getmaxx();
cleardevice();
//Road
line(0,148,x,148);
line(0,152,x,152);
line(0,148,0,152);
line(x,148,x,152);
//delay(2);
//Car 1
line(40,100,100,100);
line(40,100,10,120);
line(100,100,150,120);
line(10,120,10,130);
line(150,120,150,130);
line(10,130,150,130);
line(10,120,150,120);
//Window 1
line(40,105,25,115);
line(40,105,65,105);
line(25,115,65,115);
line(65,105,65,115);
//Window 2
line(70,105,70,115);
line(70,105,100,105);
line(70,115,125,115);
line(100,105,125,115);
//Tyres
circle(40,138,8);
circle(40,138,4);
circle(110,138,8);
circle(110,138,4);
getch();
Ajit kumar Roy
25
Roll No.: 03 & Semester-3
}
Output:
Ajit kumar Roy
26
Roll No.: 03 & Semester-3
16. WAP to divide rectangle into 4 parts. Each part should have different
colors initially and then color of 1 part should be visible at time.
Solution:
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
void main()
{
int i,gd=0,gm;
char msg;
clrscr();
initgraph(&gd,&gm,"C:\\TC\\BGI");
setfillstyle(1,5);
rectangle(100,100,250,150);
floodfill(110,110,WHITE);
setfillstyle(1,4);
rectangle(250,100,400,150);
floodfill(260,110,WHITE);
setfillstyle(1,7);
rectangle(100,150,250,200);
floodfill(110,160,WHITE);
setfillstyle(1,10);
rectangle(250,150,400,200);
floodfill(260,160,WHITE);
delay(1000);
cleardevice();
rectangle(100,100,250,150);
setfillstyle(1,4);
rectangle(250,100,400,150);
floodfill(260,110,WHITE);
rectangle(100,150,250,200);
rectangle(250,150,400,200);
delay(1000);
cleardevice();
setfillstyle(1,5);
rectangle(100,100,250,150);
floodfill(110,110,WHITE);
rectangle(250,100,400,150);
rectangle(100,150,250,200);
rectangle(250,150,400,200);
delay(1000);
cleardevice();
cleardevice();
Ajit kumar Roy
27
Roll No.: 03 & Semester-3
rectangle(100,100,250,150);
rectangle(250,100,400,150);
setfillstyle(1,7);
rectangle(100,150,250,200);
floodfill(110,160,WHITE);
rectangle(250,150,400,200);
delay(1000);
cleardevice();
rectangle(100,100,250,150);
rectangle(250,100,400,150);
rectangle(100,150,250,200);
setfillstyle(1,10);
rectangle(250,150,400,200);
floodfill(260,160,WHITE);
delay(1000);
cleardevice();
getch();
}
Output:
Ajit kumar Roy
28
Roll No.: 03 & Semester-3
Ajit kumar Roy
29
Roll No.: 03 & Semester-3
17. Create a menu-driven program to fill the shape of circle/rectangle/ellipse
by different patterns and colors according to user choice.
Solution:
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
void main()
{
int i,gd=0,gm,c,p,n;
char msg;
clrscr();
initgraph(&gd,&gm,"c:\\TC\\BGI");
printf("1. CIRCLE \n");
printf("2. RECTANGLE \n");
printf("3. ELLIPSE \n");
printf("Enter your choice ");
scanf("%d",&n);
printf("\nEnter Color ");
scanf("%d",&c);
printf("\nEnter Pattern ");
scanf("%d",&p);
switch(n)
{
case 1:
setfillstyle(p,c);
circle(250,200,60);
floodfill(250,200,WHITE);
break;
case 2:
setfillstyle(p,c);
rectangle(150,100,350,200);
floodfill(155,110,WHITE);
break;
case 3:
setfillstyle(p,c);
ellipse(250,200,0,360,100,60);
floodfill(255,210,WHITE);
}
getch();
}
Ajit kumar Roy
30
Roll No.: 03 & Semester-3
Output:
Ajit kumar Roy
31
Roll No.: 03 & Semester-3
18. WAP to move a ball horizontally and then vertically back and forth.
Solution:
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
void main()
{
int gd = DETECT,gm,i;
initgraph(&gd,&gm,"C:\\TC\\BGI");
for(i=30;i<getmaxx()-10;i++)
{
circle(i,120,30);
delay(10);
cleardevice();
if(kbhit())
break;
}
for(i=getmaxx()-10;i>30;i--)
{
circle(i,120,30);
delay(10);
cleardevice();
}
//vertical
for(i=30;i<getmaxy()-10;i++)
{
circle(50,i,30);
delay(10);
cleardevice();
if(kbhit())
break;
}
for(i=getmaxy()-10;i>30;i--)
{
circle(50,i,30);
delay(10);
cleardevice();
if(kbhit())
break;
}
getch();
}
Ajit kumar Roy
32
Roll No.: 03 & Semester-3
Output:
Ajit kumar Roy
33
Roll No.: 03 & Semester-3
19. WAP to move ball along 4 sides of the screen till user wants.
Solution:
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
void main()
{
int gd = DETECT,gm,i,n;
initgraph(&gd,&gm,"C:\\TC\\BGI");
for(i=30;i<getmaxx()-30;i++)
{
circle(i,30,30);
delay(10);
cleardevice();
}
for(i=30;i<getmaxy()-30;i++)
{
circle(getmaxx()-30,i,30);
delay(10);
cleardevice();
}
for(i=getmaxx()-30;i>30;i--)
{
circle(i,getmaxy()-30,30);
delay(10);
cleardevice();
}
for(i=getmaxy()-30;i>30;i--)
{
circle(30,i,30);
delay(10);
cleardevice();
}
getch();
}
Ajit kumar Roy
34
Roll No.: 03 & Semester-3
Output:
Ajit kumar Roy
35
Roll No.: 03 & Semester-3
Ajit kumar Roy
36
Roll No.: 03 & Semester-3
20. WAP to show a moving car.
Solution:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd = DETECT,gm,i,x,a;
initgraph(&gd,&gm,"C:\\TC\\BGI");
x=getmaxx();
for(i=0;i<x/2;i++)
{
cleardevice();
//Road
line(0,148,x,148);
line(0,152,x,152);
line(0,148,0,152);
line(x,148,x,152);
//delay(2);
//Car 1
line(40+i,100,100+i,100);
line(40+i,100,10+i,120);
line(100+i,100,150+i,120);
line(10+i,120,10+i,130);
line(150+i,120,150+i,130);
line(10+i,130,150+i,130);
line(10+i,120,150+i,120);
//Window 1
line(40+i,105,25+i,115);
line(40+i,105,65+i,105);
line(25+i,115,65+i,115);
line(65+i,105,65+i,115);
//Window 2
line(70+i,105,70+i,115);
line(70+i,105,100+i,105);
line(70+i,115,125+i,115);
line(100+i,105,125+i,115);
//Tyres
circle(40+i,138,8);
circle(40+i,138,4);
circle(110+i,138,8);
circle(110+i,138,4);
}
Ajit kumar Roy
37
Roll No.: 03 & Semester-3
getch();
}
Output:
Ajit kumar Roy
38
Roll No.: 03 & Semester-3
21. WAP to shown an accident scene.
Solution:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd = DETECT,gm,i,x,j,a;
initgraph(&gd,&gm,"C:\\TC\\BGI");
x=getmaxx();
for(i=10;i<170;i++)
{
cleardevice();
//Road
line(0,148,x,148);
line(0,152,x,152);
line(0,148,0,152);
line(x,148,x,152);
//delay(2);
//Car 1
line(40+i,100,100+i,100);
line(40+i,100,10+i,120);
line(100+i,100,150+i,120);
line(10+i,120,10+i,130);
line(150+i,120,150+i,130);
line(10+i,130,150+i,130);
line(10+i,120,150+i,120);
//Window 1
line(40+i,105,25+i,115);
line(40+i,105,65+i,105);
line(25+i,115,65+i,115);
line(65+i,105,65+i,115);
//Window 2
line(70+i,105,70+i,115);
line(70+i,105,100+i,105);
line(70+i,115,125+i,115);
line(100+i,105,125+i,115);
//Tyres
circle(40+i,138,8);
circle(40+i,138,4);
circle(110+i,138,8);
circle(110+i,138,4);
Ajit kumar Roy
39
Roll No.: 03 & Semester-3
//Car 2
line(x-40-i,100,x-100-i,100);
line(x-40-i,100,x-10-i,120);
line(x-100-i,100,x-150-i,120);
line(x-10-i,120,x-10-i,130);
line(x-150-i,120,x-150-i,130);
line(x-10-i,130,x-150-i,130);
line(x-10-i,120,x-150-i,120);
//Window 1
line(x-40-i,105,x-25-i,115);
line(x-40-i,105,x-65-i,105);
line(x-25-i,115,x-65-i,115);
line(x-65-i,105,x-65-i,115);
//Window 2
line(x-70-i,105,x-70-i,115);
line(x-70-i,105,x-100-i,105);
line(x-70-i,115,x-125-i,115);
line(x-100-i,105,x-125-i,115);
//Tyres
circle(x-40-i,138,8);
circle(x-40-i,138,4);
circle(x-110-i,138,8);
circle(x-110-i,138,4);
if(kbhit())
break;
}
//Fire
delay(20);
for(j=0;j<4;j++)
{
line(320,120,300,95);
delay(100);
line(320,120,340,95);
delay(30);
line(320,120,315,95);
delay(30);
line(320,120,330,95);
}
getch();
}
Ajit kumar Roy
40
Roll No.: 03 & Semester-3
Output:
Ajit kumar Roy
41
Roll No.: 03 & Semester-3
22. WAP to illustrate traffic simulation program along with countdown timer
and car movement according to signal red, green, yellow.
Solution:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
char output[256];
int gd = DETECT,gm,i,x;
initgraph(&gd,&gm,"C:\\TC\\BGI");
x=getmaxx();
line(x-40,320,x-100,320);
line(x-40,320,x-10,340);
line(x-100,320,x-150,340);
line(x-10,340,x-10,350);
line(x-150,340,x-150,350);
line(x-10,350,x-150,350);
line(x-10,340,x-150,340);
//Window 1
line(x-40,325,x-25,335);
line(x-40,325,x-65,325);
line(x-25,335,x-65,335);
line(x-65,325,x-65,335);
//Window 2
line(x-70,325,x-70,335);
line(x-70,325,x-100,325);
line(x-70,335,x-125,335);
line(x-125,335,x-100,325);
//Tyres
circle(x-40,358,8);
circle(x-40,358,4);
circle(x-110,358,8);
circle(x-110,358,4);
line(24,140,24,370);
line(25,140,25,370);
line(0,370,getmaxx(),370);
line(0,377,getmaxx(),377);
rectangle(0,50,50,140);
for(i=9;i>=0;i--)
{
Ajit kumar Roy
42
Roll No.: 03 & Semester-3
delay(400);
setfillstyle(1,RED);
circle(25,70,10);
floodfill(25,70,WHITE);
//sprintf(output,"%d",i);
//outtextxy(40,65,output);
gotoxy(6,5);
printf("%d",i);
delay(400);
gotoxy(8,5);
printf("STOP");
setfillstyle(1,YELLOW);
circle(25,98,10);
floodfill(25,98,BLACK);
setfillstyle(1,GREEN);
circle(25,125,10);
floodfill(25,125,BLACK);
}
delay(300);
cleardevice();
line(x-40,320,x-100,320);
line(x-40,320,x-10,340);
line(x-100,320,x-150,340);
line(x-10,340,x-10,350);
line(x-150,340,x-150,350);
line(x-10,350,x-150,350);
line(x-10,340,x-150,340);
//Window 1
line(x-40,325,x-25,335);
line(x-40,325,x-65,325);
line(x-25,335,x-65,335);
line(x-65,325,x-65,335);
//Window 2
line(x-70,325,x-70,335);
line(x-70,325,x-100,325);
line(x-70,335,x-125,335);
line(x-125,335,x-100,325);
//Tyres
circle(x-40,358,8);
circle(x-40,358,4);
circle(x-110,358,8);
circle(x-110,358,4);
Ajit kumar Roy
43
Roll No.: 03 & Semester-3
line(24,140,24,370);
line(25,140,25,370);
line(0,370,getmaxx(),370);
line(0,377,getmaxx(),377);
rectangle(0,50,50,140);
for(i=5;i>=0;i--)
{
setfillstyle(1,RED);
circle(25,70,10);
floodfill(25,70,BLACK);
setfillstyle(1,GREEN);
circle(25,125,10);
floodfill(25,125,BLACK);
setfillstyle(1,YELLOW);
circle(25,98,10);
floodfill(25,98,WHITE);
gotoxy(8,7);
printf("READY");
delay(400);
gotoxy(6,7);
delay(100);
printf("%d",i);
//sprintf(output,"%d",i);
//outtextxy(40,95,output);
}
delay(300);
cleardevice();
line(x-40,320,x-100,320);
line(x-40,320,x-10,340);
line(x-100,320,x-150,340);
line(x-10,340,x-10,350);
line(x-150,340,x-150,350);
line(x-10,350,x-150,350);
line(x-10,340,x-150,340);
//Window 1
line(x-40,325,x-25,335);
line(x-40,325,x-65,325);
line(x-25,335,x-65,335);
line(x-65,325,x-65,335);
//Window 2
line(x-70,325,x-70,335);
line(x-70,325,x-100,325);
line(x-70,335,x-125,335);
Ajit kumar Roy
44
Roll No.: 03 & Semester-3
line(x-125,335,x-100,325);
//Tyres
circle(x-40,358,8);
circle(x-40,358,4);
circle(x-110,358,8);
circle(x-110,358,4);
line(24,140,24,370);
line(25,140,25,370);
line(0,370,getmaxx(),370);
line(0,377,getmaxx(),377);
rectangle(0,50,50,140);
setfillstyle(1,RED);
circle(25,70,10);
floodfill(25,70,BLACK);
setfillstyle(1,YELLOW);
circle(25,98,10);
floodfill(25,98,BLACK);
setfillstyle(1,GREEN);
circle(25,125,10);
floodfill(25,125,WHITE);
gotoxy(8,9);
printf("START");
line(24,140,24,370);
line(25,140,25,370);
line(0,370,getmaxx(),370);
line(0,377,getmaxx(),377);
getch();
}
Ajit kumar Roy
45
Roll No.: 03 & Semester-3
Output:
Ajit kumar Roy
46
Roll No.: 03 & Semester-3
Ajit kumar Roy
47
Roll No.: 03 & Semester-3
23. WAP to show a bucket filling with water.
Solution:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd = DETECT,gm,i,j;
initgraph(&gd,&gm,"C:\\TC\\BGI");
line(0,50,150,50);
line(0,60,130,60);
line(150,50,150,80);
line(130,60,130,80);
ellipse(140,80,0,360,10,5);
ellipse(150,150,0,360,60,10);
line(90,150,120,250);
line(210,150,180,250);
ellipse(150,250,0,360,30,10);
for(i=1;i<5;i++)
{
setcolor(BLUE);
setfillstyle(1,BLUE);
delay(200);
setlinestyle(2,2,1);
line(132+(3*i),80,132+(3*i),150);
}
setcolor(BLUE);
setfillstyle(1,BLUE);
ellipse(150,250,0,360,30,10);
floodfill(151,251,BLUE);
for(j=1;j<90;j++)
{
setcolor(BLUE);
setfillstyle(1,BLUE);
setlinestyle(0,1,1);
line(120-(0.25*j),250-j,180+(0.25*j),250-j);
delay(200);
}
getch();
}
Ajit kumar Roy
48
Roll No.: 03 & Semester-3
Output:
Ajit kumar Roy
49
Roll No.: 03 & Semester-3
24. WAP to show a rotating fan controlled with speed like if pressed 1 then
speed is slow, on 2 it increases, on 3 it increases further and on 0 it gets
off.
Solution:
#include<graphics.h>
#include<stdlib.h>
#include<conio.h>
#include<stdio.h>
void fan();
void fan1();
void fan2();
void fan3();
void main()
{
int gd=DETECT,gm,mx,my,x,y,i,j,ch;
char ch1;
initgraph(&gd,&gm,"C:\\TC\\BGI");
printf("0:OFF 1:LOW SPEED 2:SPEED 3:HIGH SPEED 4:EXIT");
printf("\n");
printf("Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 0:
fan();
break;
case 1:
fan1();
break;
case 2:
fan2();
break;
case 3:
fan3();
break;
case 4:
exit(1);
}
getch();
}
Ajit kumar Roy
50
Roll No.: 03 & Semester-3
void fan()
{
int mx,my;
mx=getmaxx()/2;
my=getmaxy()/2;
circle(mx,my,5);
pieslice(mx,my,0,20,80);
pieslice(mx,my,230,250,80);
pieslice(mx,my,110,130,80);
if(kbhit())
{
main();
}
}
void fan1()
{
int mx,my,i,j;
mx=getmaxx()/2;
my=getmaxy()/2;
circle(mx,my,5);
cleardevice();
for(i=0;i<10;i++)
{
cleardevice();
for(j=0;j<=10;j++)
{
pieslice(mx,my,0,20,80);
pieslice(mx,my,230,250,80);
pieslice(mx,my,110,130,80);
}
delay(1000);
cleardevice();
for(j=0;j<=10;j++)
{
pieslice(mx,my,20,40,80);
pieslice(mx,my,250,270,80);
pieslice(mx,my,130,150,80);
}
delay(1000);
cleardevice();
for(j=0;j<=10;j++)
{
pieslice(mx,my,40,60,80);
pieslice(mx,my,270,290,80);
pieslice(mx,my,150,170,80);
}
delay(1000);
cleardevice();
for(j=0;j<=10;j++)
Ajit kumar Roy
51
Roll No.: 03 & Semester-3
{
pieslice(mx,my,60,80,80);
pieslice(mx,my,290,310,80);
pieslice(mx,my,170,190,80);
}
delay(1000);
cleardevice();
for(j=0;j<=10;j++)
{
pieslice(mx,my,80,100,80);
pieslice(mx,my,310,330,80);
pieslice(mx,my,190,210,80);
}
if(kbhit())
{
main();
}
delay(1000);
}
}
void fan2()
{
int mx,my,i,j;
mx=getmaxx()/2;
my=getmaxy()/2;
circle(mx,my,5);
cleardevice();
for(i=0;i<10;i++)
{
cleardevice();
for(j=0;j<=10;j++)
{
pieslice(mx,my,0,20,80);
pieslice(mx,my,230,250,80);
pieslice(mx,my,110,130,80);
}
delay(500);
cleardevice();
for(j=0;j<=10;j++)
{
pieslice(mx,my,20,40,80);
pieslice(mx,my,250,270,80);
pieslice(mx,my,130,150,80);
}
delay(500);
cleardevice();
for(j=0;j<=10;j++)
{
pieslice(mx,my,40,60,80);
Ajit kumar Roy
52
Roll No.: 03 & Semester-3
pieslice(mx,my,270,290,80);
pieslice(mx,my,150,170,80);
}
delay(500);
cleardevice();
for(j=0;j<=10;j++)
{
pieslice(mx,my,60,80,80);
pieslice(mx,my,290,310,80);
pieslice(mx,my,170,190,80);
}
delay(500);
cleardevice();
for(j=0;j<=10;j++)
{
pieslice(mx,my,80,100,80);
pieslice(mx,my,310,330,80);
pieslice(mx,my,190,210,80);
}
if(kbhit())
{
main();
}
delay(500);
cleardevice();
}
}
void fan3()
{
int mx,my,i,j;
mx=getmaxx()/2;
my=getmaxy()/2;
circle(mx,my,5);
cleardevice();
for(i=0;i<10;i++)
{
cleardevice();
for(j=0;j<=10;j++)
{
pieslice(mx,my,0,20,80);
pieslice(mx,my,230,250,80);
pieslice(mx,my,110,130,80);
}
delay(50);
cleardevice();
for(j=0;j<=10;j++)
{
pieslice(mx,my,20,40,80);
pieslice(mx,my,250,270,80);
Ajit kumar Roy
53
Roll No.: 03 & Semester-3
pieslice(mx,my,130,150,80);
}
delay(50);
cleardevice();
for(j=0;j<=10;j++)
{
pieslice(mx,my,40,60,80);
pieslice(mx,my,270,290,80);
pieslice(mx,my,150,170,80);
}
delay(50);
cleardevice();
for(j=0;j<=10;j++)
{
pieslice(mx,my,60,80,80);
pieslice(mx,my,290,310,80);
pieslice(mx,my,170,190,80);
}
delay(50);
cleardevice();
for(j=0;j<=10;j++)
{
pieslice(mx,my,80,100,80);
pieslice(mx,my,310,330,80);
pieslice(mx,my,190,210,80);
}
if(kbhit())
{
main();
}
delay(50);
cleardevice();
}
}
Output:
Ajit kumar Roy
54
Roll No.: 03 & Semester-3
Ajit kumar Roy
55
Roll No.: 03 & Semester-3
25. Create a menu-driven program to implement all the transformations:
a) Translation
b) Scaling
c) Rotation.
Solution:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd=DETECT,gm;
int x1,y1,x2,y2,tx,sx,ty,sy,m,n,o,p,ch;
float th;
initgraph(&gd,&gm,"C:\\TC\\BGI");
printf("Enter the coordinates: \n");
scanf("%d%d%d%d",&x1,&x2,&y1,&y2);
printf("The Object given below is: \n");
rectangle(x1,y1,x2,y2);
printf("2D-TRANSFORMATION MENU:-\n");
printf("1: TRANSLATION \n2: SCALING \n3: ROTATION \n4: EXIT \n");
printf("Enter your choice: \n");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
printf("Enter the translation coordinates: \n ");
scanf("%d%d",&tx,&ty);
m=x1+tx;
n=x2+ty;
o=x2+tx;
p=y2+ty;
rectangle(m,n,o,p);
break;
}
case 2:
{
printf("Enter the scaling coordinates:\n ");
scanf("%d%d",&sx,&sy);
m=x1+sx;
n=x2+sy;
o=x2+sx;
p=y2+sy;
rectangle(m,n,o,p);
break;
}
Ajit kumar Roy
56
Roll No.: 03 & Semester-3
case 3:
{
printf("Enter the angle from rotation transformation:\n ");
scanf("%f",&th);
printf("The Rotation angle is: \n");
m=(-1)*(x1*cos(th)-y1*sin(th));
n=(x1*sin(th)+y1*cos(th));
o=(-1)*(x2*cos(th)-y2*cos(th));
p=x2*sin(th)+y2*cos(th);
rectangle(m,n,o,p);
break;
}
case 4:
{
exit(1);
break;
}
default:
printf("INVALID") ;
}
getch();
}
Output:
Ajit kumar Roy
57
Roll No.: 03 & Semester-3
Ajit kumar Roy
58
Roll No.: 03 & Semester-3
26. WAP to illustrate a ball hitting the wickets.
Solution:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void pitch();
void pitch2();
void main()
{
int i,gd=DETECT,gm,x=500,y=150,a;
initgraph(&gd,&gm,"C:\\TC\\BGI");
for(i=10;i<150;i++)
{
pitch();
setcolor(RED);
setfillstyle(1,RED);
circle(x-i,y+i,10);
floodfill(x-i,y+i,RED);
delay(20);
cleardevice();
}
for(i=10;i<60;i++)
{
pitch();
setcolor(RED);
setfillstyle(1,RED);
circle(350-i,260-i,10);
floodfill(350-i,260-i,RED);
setcolor(RED);
delay(20);
cleardevice();
}
for(i=10;i<250;i++)
{
pitch();
setcolor(RED);
setfillstyle(1,RED);
circle(300-i,200,10);
floodfill(300-i,200,RED);
delay(20);
cleardevice();
}
delay(20);
pitch2();
setcolor(RED);
setfillstyle(1,RED);
Ajit kumar Roy
59
Roll No.: 03 & Semester-3
circle(30,260,10);
floodfill(25,260,RED);
while(!kbhit);
getch();
}
void pitch()
{
setcolor(WHITE);
rectangle(20,150,30,300);
rectangle(35,150,45,300);
rectangle(50,150,60,300);
rectangle(15,300,65,310);
}
void pitch2()
{
setcolor(WHITE);
rectangle(20,290,170,300);
rectangle(35,150,45,300);
rectangle(50,150,60,300);
rectangle(15,300,65,310);
}
Output:
Ajit kumar Roy
60
Roll No.: 03 & Semester-3
Ajit kumar Roy
61
Roll No.: 03 & Semester-3
27. WAP to implement Bresenhem Circle Drawing Algorithm.
Solution:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int r,x,y,d1,xc,yc;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
cleardevice();
printf("Enter the x-coordinates of a circle: ");
scanf("%d",&xc);
printf("Enter the y-coordinates of a circle: ");
scanf("%d",&yc);
printf("Enter radius: ");
scanf("%d",&r);
x=0;
y=r;
d1=3-(2*r);
for(x=0;x<=y;x++)
{
if(d1<0)
{
y=y;
d1=(d1+(4*x)+6);
}
else
{
y=y-1;
d1=d1+((4*(x-y)+10));
}
putpixel(xc+x,yc-y,1);
putpixel(xc-x,yc-y,2);
putpixel(xc+x,yc+y,3);
putpixel(xc-x,yc+y,4);
putpixel(xc+y,yc-x,5);
putpixel(xc-y,yc-x,6);
putpixel(xc+y,yc+x,7);
putpixel(xc-y,yc+x,8);
}
getch();
}
Ajit kumar Roy
62
Roll No.: 03 & Semester-3
Output:
Ajit kumar Roy
63
Roll No.: 03 & Semester-3
28. WAP to implement polynomial Circle Drawing Algorithm.
Solution:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void main()
{
int x,y,r,xc,yc,xe;
int gd=0,gm;
initgraph(&gd,&gm,"C:\\TC\\BGI") ;
printf("Enter the x-coordinate: ");
scanf("%d",&xc);
printf("Enter the y-coordinate: ");
scanf("%d",&yc);
printf ("Enter radius: ");
scanf("%d",&r);
x=0;
xe=(int)r/sqrt(2)+0.5;
y=r;
while(x<=xe)
{
putpixel(x+xc,y+yc,11);
putpixel(x+xc,-y+yc,11);
putpixel(y+xc,-x+yc,11);
putpixel(-y+xc,-x+yc,11);
putpixel(-x+xc,-y+yc,11);
putpixel(-x+xc,y+yc,11);
putpixel(-y+xc,x+yc,11);
putpixel(y+xc,x+yc,11);
x++;
y=(int)sqrt(r*r-x*x)+0.5;
}
getch();
}
Output:
Ajit kumar Roy
64
Roll No.: 03 & Semester-3
29. WAP to implement mid point Circle Drawing Algorithm.
Solution:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int r,x,y,d1,xc,yc;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
cleardevice();
printf("Enter the x-coordinate of a circle: ");
scanf("%d",&xc);
printf("Enter the y-coordinate of a circle: ");
scanf("%d",&yc);
printf("Enter radius: ");
scanf("%d",&r);
x=0;
y=r;
d1=1-r;
for(x=0;x<=y;x++)
{
if(d1<0)
{
d1=d1+2*x+1;
}
else
{
d1=d1+2*(x-y)+1;
y--;
}
putpixel(xc+x,yc+y,2);
putpixel(xc-x,yc+y,2);
putpixel(xc+x,yc-y,2);
putpixel(xc-x,yc-y,2);
putpixel(xc+y,yc+x,2);
putpixel(xc-y,yc+x,2);
putpixel(xc+y,yc-x,2);
putpixel(xc-y,yc-x,2);
}
getch();
}
Ajit kumar Roy
65
Roll No.: 03 & Semester-3
Output:
Ajit kumar Roy
66
Roll No.: 03 & Semester-3
30. WAP to display the wall clock.
Solution:
#include<time.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
#include<dos.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
circle(300,200,80);
circle(300,200,110);
circle(300,200,100);
circle(300,200,1);
line(300,200,300,250);
circle(300,250,2);
line(300,200,275,127);
circle(275,127,2);
outtextxy(350,120,"1");
outtextxy(370,150,"2");
outtextxy(390,200,"3");
outtextxy(370,250,"4");
outtextxy(340,280,"5");
outtextxy(300,290,"6");
outtextxy(250,275,"7");
outtextxy(215,240,"8");
outtextxy(205,200,"9");
outtextxy(215,155,"10");
outtextxy(245,125,"11");
outtextxy(290,110,"12");
getch();
}
Ajit kumar Roy
67
Roll No.: 03 & Semester-3
Output:
Ajit kumar Roy
68
Roll No.: 03 & Semester-3
31. WAP to display the flying Kite.
Solution:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
#include<dos.h>
void main()
{
int gd=DETECT,gm;
int x=20,y=400,a=20,b=50;
initgraph(&gd,&gm,"C:\\TC\\BGI");
while(!kbhit())
{
x=x+1;
y=y-1;
line(x-a,y,x,y-b);
line(x,y-b,x+a,y);
line(x+a,y,x,y+b);
line(x,y+b,x-a,y);
line(x,y-b,x,y+b);
line(x-a,y,x+a,y);
line(x,y+b,x+5,y+100);
line(x,y+b,x-5,y+100);
line(x,y,x+100,y+300);
delay(50);
cleardevice();
}
getch();
}
Output:
Ajit kumar Roy
69
Roll No.: 03 & Semester-3
32. WAP to implement Sutherland line clipping algorithm.
Solution:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
float m,i,xmax,ymax,xmin,ymin,x1,y1,x2,y2;
float start[4],end[4],code[4];
clrscr();
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("Enter the bottom left coordinate of viewport: ");
scanf("%f%f",&xmin,&ymin);
printf("Enter the top right coordinate of viewport: ");
scanf("%f%f",&xmax,&ymax);
printf("Enter the coordinates for starting point of line: ");
scanf("%f%f",&x1,&y1);
printf("Enter the coordinates 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);
//starting coordinates
if(x1<xmin)start[0]=1;
if(x1>xmax)start[1]=1;
if(y1>ymax)start[2]=1;
if(y1<ymin)start[3]=1;
//ending coordinates
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("The line is tottaly visible\n and not a clipping candidate: ");
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
getch();
}
Ajit kumar Roy
70
Roll No.: 03 & Semester-3
else
{
cleardevice();
printf("Line is partially: ");
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();
Ajit kumar Roy
71
Roll No.: 03 & Semester-3
cleardevice();
printf("After clipping: \n");
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
getch();
}
}
else
{
clrscr();
cleardevice();
printf("After clipping: \n");
rectangle(xmin,ymin,xmax,ymax);
}
getch();
closegraph();
}
Output:
Ajit kumar Roy
72
Roll No.: 03 & Semester-3
Ajit kumar Roy
73
Roll No.: 03 & Semester-3