Computer Graphics
Computer Graphics
Computer graphics
PRACTICAL FILE
BCA-373
Nurturing Excellence
2. Write a program to draw a line, circle, ellipse, arc, sector, and bar using functions.
3. Write a program to implement functions and draw nested circle and rectangle.
8. Write a program to make bar chart for students for five years.
12. Write a program to scan convert a circle using Mid Point algorithm.
Basic functions
cleardevice( ) :- Cleardevice function clears the screen in graphics mode and sets the current
position to (0,0). Clearing the screen consists of filling the screen with current background color.
Declaration:- void cleardevice();
closegraph( ) :- closegraph function closes the graphics mode, deallocates all memory allocated
by graphics system and restores the screen to the mode it was in before you called initgraph.
Declaration :- void closegraph();
arc( ):- arc function is used to draw an arc with center (x,y) and stangle specifies starting angle,
endangle specifies the end angle and last parameter specifies the radius of the arc. arc function
can also be used to draw a circle but for that starting angle and end angle should be 0 and 360
respectively.
Declaration :- void arc(int x, int y, int stangle, int endangle, int radius);
circle():- Circle function is used to draw a circle with center (x,y) and third parameter specifies
the radius of the circle. The code given below draws a circle.
line():- line function is used to draw a line from a point(x1,y1) to point(x2,y2) i.e. (x1,y1) and
(x2,y2) are end points of the line.The code given below draws a line.
Declaration :- void line(int x1, int y1, int x2, int y2);
outtextxy ():-outtextxy function display text or string at a specified point(x,y) on the screen.
rectangle ():-rectangle function is used to draw a rectangle. Coordinates of left top and right
bottom corner are required to draw the rectangle. left specifies the X-coordinate of top left
corner, top specifies the Y-coordinate of top left corner, right specifies the X-coordinate of right
bottom corner, bottom specifies the Y-coordinate of right bottom corner. The code given below
draws a rectangle.
Declaration :- void rectangle(int left, int top, int right, int bottom);
Syntax :- delay(100);
The main objectives that were put forward for GKS are:
– To provide the complete range of graphical facilities in 2D, including the interactive
capabilities,
– To control all types of graphic devices such as plotters and display devices in a consistent
manner,
– To be small enough for a variety of programs.
IGES (Initial Graphics Exchange Specification)
IGES is the most comprehensive standard and is designed to transmit the entire product
definition including that of manufacturing and any other associated information.
Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\turboc3\\bgi");
circle(90,100,30);
outtextxy(125,100,"circle");
line(350,100,170,140);
outtextxy(380,110,"line");
arc(100,200,200,20,40);
outtextxy(160,200,"arc");
ellipse(260,200,0,360,40,60);
outtextxy(320,200,"ellipse");
sector(100,380,80,50,60,100);
outtextxy(130,360,"sector");
bar(300,380,340,310);
outtextxy(380,360,"bar");
getch();
}
Output:
Program 3: Write a program to implement functions and draw nested circle
and rectangle.
Code:
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<graphics.h>
void main(){
int gd=DETECT,gm,x,y,r;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
rectangle(80,80,180,180);
rectangle(60,60,200,200);
rectangle(40,40,220,220);
circle(450,150,70);
circle(450,150,50);
circle(450,150,30);
getch();
closegraph();
}
Output
Program 4: Write a program to display a string “Computer Graphics” in
center of the screen.
Code:
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<graphics.h>
void main(){
int gd=DETECT,gm,x,y;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
x=getmaxx();
y=getmaxy();
outtextxy(x/3,y/3,"COMPUTER GRAPHICS");
getch();
closegraph();
}
Output
Program 5: Write a program to display an asterisk on screen.
Code:
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
void main()
{
Int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\turboc3\\bgi");
line(225,200,375,200);
line(300,125,300,275);
line(250,150,350,250);
line(350,150,250,250);
getch();
}
Output
Program 6: Write a program to display an asterisk inside circle on screen.
Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
Int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\turboc3\\bgi");
circle(300,200,75);
line(225,200,375,200);
line(300,125,300,275);
line(250,150,350,250);
line(350,150,250,250);
getch();
}
Output
Program 7: Write a program to display a coordinate axis.
Code:
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
void main(){
int a,b,i;
int gd=DETECT,gm;
initgraph(&gd,&gm,"//turboc 3\\bgi");
a=getmaxx();
b=getmaxy();
line(a/2,0,a/2,b);
line(0,b/2,a,b/2);
outtextxy(a-50,b/2,"X AXIS");
outtextxy(a/2,b-50,"Y AXIS");
for(i=0;i<a;i+=35){
line(a/2+i,b/2-10,a/2+i,b/2+10);
line(a/2-i,b/2+10,a/2-i,b/2-10);
line(a/2+10,b/2-i,a/2-10,b/2-i);
line(a/2-10,b/2+i,a/2+10,b/2+i);}
getch();
}
Output
Program 8: Write a program to make bar chart for students for five years.
Code:
#include<stdio.h>
#include "conio.h"
#include<graphics.h>
void main()
{
int gd=DETECT,gm,x,y,i;
initgraph(&gd,&gm,"C:\\TC\\BGI");
line(80,60,80,380);
line(80,380,550,380);
bar(120,230,170,380);
bar(200,120,250,380);
bar(280,320,330,380);
bar(360,280,410,380);
bar(440,180,490,380);
outtextxy(30,70,"y-axis");
outtextxy(530,390,"x-axis");
outtextxy(130,390,"2010");
outtextxy(210,390,"2011");
outtextxy(290,390,"2012");
outtextxy(370,390,"2013");
outtextxy(450,390,"2014");
getch();
closegraph();
}
Output:
Program 9: Write a program to make pie chart for students.
Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
intgd=DETECT,gm;
initgraph(&gd,&gm,"c:\\TURBOC3\\BGI");
setfillstyle(SOLID_FILL,RED);
pieslice(100,100,0,70,50);
setfillstyle(SOLID_FILL,BLACK);
pieslice(100,100,70,150,50);
setfillstyle(SOLID_FILL,YELLOW);
pieslice(100,100,150,250,50);
setfillstyle(SOLID_FILL,BLUE);
pieslice(100,100,250,300,50);
setfillstyle(SOLID_FILL,GREEN);
pieslice(100,100,300,360,50);
getch();
closegraph();
}
Output
Program 10: Write a program to scan convert a line using DDA algorithm.
Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd=DETECT,gm;
int x1,x2,y1,y2,length,dx,dy,x,y,i;
initgraph(&gd,&gm,"C:\\turboc3\\bgi");
printf("ENTER X1 & Y1:-");
scanf("%d%d",&x1,&y1);
printf("ENTER X2 & Y2 :-");
scanf("%d%d",&x2,&y2);
if( abs(x2-x1)>=abs(y2-y1) )
{
length=abs(x2-x1);
}
else
length=abs(y2-y1);
dx=(x2-x1)/length;
dy=(y2-y1)/length;
x=x1+0.5;
y=y1+0.5;
i=1;
while(i<=length)
{
putpixel(x,y,WHITE);
x=x+dx;
y=y+dy;
i=i+1;
}
getch();
closegraph();
}
Output
Program 11: Write a program to scan convert a line using Bresenham’s
algorithm.
Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int dx,dy,x,y,p,x1,y1,x2,y2;
int gd=DETECT,gm;
printf("enter starting coordinates:-\t");
scanf("%d%d",&x1,&y1);
printf("enter ending coordinates:-\t");
scanf("%d%d",&x2,&y2);
dx=x2-x1;
dy=y2-y1;
p=2*(dy)-(dx);
x=x1;
y=y1;
initgraph(&gd,&gm,"C:\\TurboC3\\bgi");
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();
closegraph();
}
Output
Program 12: Write a program to scan convert a circle using Mid Point
algorithm.
Code:
#include<stdio.H>
#include<conio.H>
#include<graphics.H>
void main()
{
int xc,yc,R,X,Y,I,P;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\Turboc3\\Bgi");
printf("\nEnter Radius Of The circle:-");
scanf("%D",&R);
printf("Enter Center Of The Circle:-");
scanf("%D%D",&xc, &yc);
P=1-R;X=0;Y=R;
do
{if(P<0)
{X=X+1;
P=P+2*X+1;}
else
{
X=X+1;
Y=Y-1;
P=P+2*X-2*Y+10;}
putpixel(xc+X,yc+Y,1);
putpixel(xc-Y,yc-X,2);
putpixel(xc+Y,yc-X,3);
putpixel(xc-Y,yc+X,4);
putpixel(xc+Y,yc+X,5);
putpixel(xc-X,yc-Y,6);
putpixel(xc+X,yc-Y,7);
putpixel(xc-X,yc+Y,8);}
while(X<Y);
getch();
closegraph();
}
Output
Program 13: Write a program to scan convert a circle using Bresenham’s
algorithm.
Code:
#include<stdio.h>
#include"conio.h"
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int x,y,xc,yc,i,p,r;
clrscr();
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("enter the radius of circle");
scanf("%d",&r);
printf("enter the circle of circle");
scanf("%d%d",&xc,&yc);
p=3-2*r;
x=0;
y=r;
while(x<=y)
{
putpixel(xc+x,yc+y,1);
putpixel(xc-y,yc+x,2);
putpixel(xc+y,yc-x,3);
putpixel(xc-y,yc+x,4);
putpixel(xc+y,yc+x,5);
putpixel(xc-x,yc-y,6);
putpixel(xc+x,yc-y,7);
putpixel(xc-x,yc+y,8);
if(p<=0)
{
x=x+1;
p=p+4*x+6;
}
else
{
x=x+1;
y=y-1;
p=p+4*x-4*y+10;
}
}
getch();
closegraph();
}
Output
Program 14: Write a program to implement Cohen Sutherland line clipping
algorithm.
Code :
#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\tEnter the bottom-left coordinate of viewport: ");
scanf("%f %f",&xmin,&ymin);
printf("\n\tEnter the top-right coordinate of viewport: ");
scanf("%f %f",&xmax,&ymax);
printf("\nEnter the coordinates for starting point of line: ");
scanf("%f %f",&x1,&y1);
printf("\nEnter 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);
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();
}
Output
Program 15: Write a program to get translation vector from the user and
translate triangle accordingly.
Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int x,y,x1,y1,x2,y2,tx,ty;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("\n BEFORE THE TRANSLATION:-\n");
printf("ENTER THE COORDINATES OF TRIANGLE:\n");
scanf("%d%d%d%d%d%d",&x,&y,&x1,&y1,&x2,&y2);
line(x,y,x1,y1);
line(x1,y1,x2,y2);
line(x2,y2,x,y);
printf("\n\n\n\n\n\n\n\n\n\nENTER TRANSLATION COORDINATES:\n\n");
scanf("%d%d",&tx,&ty);
x=x+tx;
y=y+ty;
x1=x1+tx;
y1=y1+ty;
x2=x2+tx;
y2=y2+ty;
printf("\n\n\nAFTER TRANSLATION:-\n\n\n");
line(x,y,x1,y1);
line(x1,y1,x2,y2);
line(x2,y2,x,y);
getch();
closegraph();
}
Output
Program 16: Write a Program to get scaling vector from the user and scale triangle
accordingly.
Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd=DETECT,gm;
int x,y,x1,y1,x2,y2,sx,sy;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("\nBEFORE THE SCALING\n");
printf("ENTER THE COORDINATES OF TRIANGLE:\n");
scanf("%d%d%d%d%d%d",&x,&y,&x1,&y1,&x2,&y2);
line(x,y,x1,y1);
line(x1,y1,x2,y2);
line(x2,y2,x,y);
printf("\n\n\n\n\n\n\n\n\n\nENTER THE COORDINATES OF SCALING:\n");
scanf("%d%d",&sx,&sy);
x=sx*x;
y=sy*y;
x1=sx*x1;
y1=sy*y1;
x2=sx*x2;
y2=sy*y2;
printf("\n\n\nAFTER SCALING:-\n");
line(x,y,x1,y1);
line(x1,y1,x2,y2);
line(x2,y2,x,y);
getch();
closegraph();
}
Output
Program 17: Write a program to get rotational angle from the user and rotate
triangle accordingly.
Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd=DETECT,gm;
int x,y,x1,y1,x2,y2;
float t,r;
initgraph(&gd,&gm,"c:\\TURBOC3\\BGI");
printf("\n\t\tTRIANGLE BEFORE ROTATION\n");
printf("ENTER TRIANGLE AXIS & COORDINATES:-\t");
scanf("%d%d%d%d%d%d",&x,&y,&x1,&y1,&x2,&y2);
printf("TRIANGLE IS\t\t\t") ;
line(x,y,x1,y1);
line(x1,y1,x2,y2);
line(x2,y2,x,y);
printf("ENTER ANGLE OF ROTATION:-\t");
scanf("%f",&r);
t=r*3.14/180;
x=abs(x*cos(t)-y*sin(t));
y=abs(x*sin(t)+y*cos(t));
x1=abs(x1*cos(t)-y1*sin(t));
y1=abs(x1*sin(t)+y1*cos(t));
x2=abs(x2*cos(t)-y2*sin(t));
y2=abs(x2*sin(t)+y2*cos(t));
printf("\t\tTRIANGLE AFTER ROTATION\n");
line(x,y,x1,y1);
line(x1,y1,x2,y2);
line(x2,y2,x,y);
getch();
closegraph();
}
Output
Program 18: Write a program to implement reflection of a line.
Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int x1,y1,x2,y2,a;
clrscr();
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("\nenter the coordinates of line");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
line(x1,y1,x2,y2);
line(320,0,320,420);
line(0,240,640,240);
printf("enter 1 for reflection of line to x-axis AND enter 2 for y-axis\n");
scanf("%d",&a);
if(a==1)
{
line(x1+220,y1,x2+220,y2);
}
else if(a==2)
{
line(x1,y1+220,x2,y2+220);
}
else
{
printf("WRONG INPUT");
}
getch();
closegraph();
}
Output
Program 19: Write a program to design flying colored balloon.
Code:
#include<stdio.h>
#include"conio.h"
#include<dos.h>
#include<graphics.h>
void main(){
int gd=DETECT,gm,x,y,w,z,i;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
for(i=100;i<=210;i++)
{
delay(5);
cleardevice();
setfillstyle(SOLID_FILL,BLUE);
circle(150,250-i,40);
line(150,290-i,150,500-i);
floodfill(150,250-i,WHITE);
setfillstyle(SOLID_FILL,YELLOW);
circle(250,250-i,40);
line(250,290-i,250,500-i);
floodfill(250,250-i,WHITE);
setfillstyle(SOLID_FILL,RED);
circle(350,250-i,40);
line(350,290-i,350,500-i);
floodfill(350,250-i,WHITE);
}
getch();
closegraph();
}
Output
Program 20: Write a program to display sunrise and sunset.
Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm,i;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
for(i=0;i<150;i++)
{
delay(30);
cleardevice();
line(0,250,50,200);
line(50,200,100,250);
line(100,250,150,200);
line(150,200,200,250);
line(200,250,250,200);
line(250,200,300,250);
line(300,250,350,200);
line(350,200,400,250);
line(400,250,450,200);
line(450,200,500,250);
line(500,250,550,200);
line(550,200,600,250);
line(600,250,650,200);
setfillstyle(1,YELLOW);
circle(400,200-i,20);
floodfill(400,200-i,WHITE);
}
for(i=0;i<150;i++)
{
delay(30);
cleardevice();
line(0,250,50,200);
line(50,200,100,250);
line(100,250,150,200);
line(150,200,200,250);
line(200,250,250,200);
line(250,200,300,250);
line(300,250,350,200);
line(350,200,400,250);
line(400,250,450,200);
line(450,200,500,250);
line(500,250,550,200);
line(550,200,600,250);
line(600,250,650,200);
setfillstyle(1,YELLOW);
circle(400,50+i,20);
floodfill(400,50+i,WHITE);
}
getch();
closegraph();
}
Output
Program 21: Write a program to display a moving vehicle.
Code:
#include<stdio.h>
#include"conio.h"
#include<dos.h>
#include<graphics.h>
void main(){
int gd=DETECT,gm,x,y,w,z,i;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
for(i=100;i<=400;i++)
{
delay(10);
cleardevice();
rectangle(400-i,200,600-i,250);
rectangle(480-i,210,510-i,230);//window of car
line(420-i,200,460-i,160);
line(460-i,160,540-i,160);
line(540-i,160,580-i,200);
circle(450-i,250,20);//wheel of car
circle(550-i,250,20); //wheel of car
circle(450-i,250,10);
circle(550-i,250,10);
}
getch();
closegraph();
}
Output
Program 22: Write a program to display a view of city.
Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int i;
initgraph(&gd,&gm,"C:\\turboc3\\bgi");
setfillstyle(1,YELLOW);
circle(330,30,25);
floodfill(330,30,WHITE);
line(40,320,100,320);
line(10,370,140,370);
line(40,320,30,340);
line(100,320,110,340);
line(10,340,140,340);
line(10,340,10,370);
line(140,340,140,370);
circle(35,370,10);
circle(35,370,15);
line(10,385,800,385);
circle(110,370,10);
circle(110,370,15);
line(10,350,800,350);
line(10,450,800,450);
line(600,350,600,100);
line(460,350,460,100);
line(600,100,460,100);
rectangle(550,150,580,120);
rectangle(480,150,510,120);
rectangle(550,250,580,220);
rectangle(480,250,510,220);
rectangle(550,350,580,320);
rectangle(480,350,510,320);
line(300,350,300,200);
line(300,200,460,200);
rectangle(300,350,150,10);
rectangle(190,150,250,30);
rectangle(190,350,250,220);
rectangle(350,350,420,250);
rectangle(480,150,510,120);
rectangle(550,250,580,220);
rectangle(480,250,510,220);
rectangle(550,350,580,320);
rectangle(480,350,510,320);
getch();
}
Output
Program 23: Write a program to show graphic signal with road.
Code:
#include<stdio.h>
#include"conio.h"
#include<dos.h>
#include<graphics.h>
void main(){
int gd=DETECT,gm,x,y,w,z;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
line(10,180,getmaxx()/2-100,180);
line(getmaxx()/2+100,180,getmaxx(),180);
line(10,330,getmaxx()/2-100,330);
line(getmaxx()/2+100,330,getmaxx(),330);
line(getmaxx()/2-100,180,getmaxx()/2-100,10);
line(getmaxx()/2+100,180,getmaxx()/2+100,10);
line(getmaxx()/2-100,330,getmaxx()/2-100,580);
line(getmaxx()/2+100,330,getmaxx()/2+100,580);
rectangle(10,240,100,270);
rectangle(140,240,240,270);
rectangle(380,240,480,270);
rectangle(520,240,620,270);
rectangle(280,10,320,100);
rectangle(280,120,320,210);
rectangle(280,290,320,390);
rectangle(280,410,320,490);
line(360,60,360,220);
line(360,60,400,60);
line(400,60,400,220);
line(360,100,400,100);
line(360,140,400,140);
line(360,180,400,180);
setfillstyle(1,RED);
circle(380,80,10);
floodfill(380,80,WHITE);
setfillstyle(1,YELLOW);
circle(380,120,10);
floodfill(380,120,WHITE);
setfillstyle(1,GREEN);
circle(380,160,10);
floodfill(380,160,WHITE);
line(getmaxx()/2-20,getmaxy()/2-20,getmaxx()/2+50,getmaxy()/2-20);
line(getmaxx()/2+50,getmaxy()/2-20,getmaxx()/2+50,getmaxy()/2+20);
line(getmaxx()/2+50,getmaxy()/2+20,getmaxx()/2-40,getmaxy()/2+20);
line(getmaxx()/2-20,getmaxy()/2-20,getmaxx()/2-40,getmaxy()/2);
line(getmaxx()/2-40,getmaxy()/2,getmaxx()/2-40,getmaxy()/2+20);
line(getmaxx()/2-20,getmaxy()/2-20,getmaxx()/2-20,getmaxy()/2+20);
line(getmaxx()/220,getmaxy()/2,getmaxx()/2+50,
getmaxy()/2);
circle(getmaxx()/2-20,getmaxy()/2+20,10);
circle(getmaxx()/2+30,getmaxy()/2+20,10);
getch();
closegraph();
}
Output
Program 24: Write a program to display an accident scene.
Code:
#include<stdio.h>
#include"conio.h"
#include<dos.h>
#include<graphics.h>
void main(){
int gd=DETECT,gm,x,y,w,z,i;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
for(i=100;i<=200;i++)
{
delay(10);
cleardevice();
line(10,300,getmaxx(),300); // road line
rectangle(10,80,50,180); //red light rectangle
line(30,180,30,300);
setfillstyle(2,RED); //red light stick
circle(30,100,10);
floodfill(30,100,WHITE);
setfillstyle(2,YELLOW);
circle(30,130,10);
floodfill(30,130,WHITE);
setfillstyle(2,GREEN);
circle(30,160,10);
floodfill(30,160,WHITE);
//FIRST CAR
rectangle(400,200,600,250);
rectangle(480,210,510,230);//window of car
line(420,200,460,160);
line(460,160,540,160);
line(540,160,580,200);
circle(450,250,20);//wheel of car
circle(450,250,10);
circle(550,250,10);
circle(550,250,20); //wheel of car
//SECOND CAR
rectangle(400-i,200,600-i,250);
rectangle(480-i,210,510-i,230);//window of car
line(420-i,200,460-i,160);
line(460-i,160,540-i,160);
line(540-i,160,580-i,200);
circle(450-i,250,20);//wheel of car
circle(450-i,250,10);
circle(550-i,250,10);
circle(550-i,250,20); //wheel of car
//ACCIDENT SCENE
circle(400,170,50);
outtextxy(370,160,"ACCIDENT");
}
getch();
closegraph();
}
Output
Program 25: Write a program to display a flight or flying bird.
Code:
#include<stdio.h>
#include"conio.h"
#include<dos.h>
#include<graphics.h>
void main(){
int gd=DETECT,gm,x,y,w,z,i;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
for(i=100;i<=500;i++)
{
delay(20);
cleardevice();
line(300+i,100,130+i,100);
line(300+i,100,350+i,150);//diagonal line ryt side
line(350+i,150,100+i,150);//lower line
line(100+i,150,80+i,80);//left side diagonal line
line(80+i,80,130+i,100);//last line of plane
setfillstyle(2,YELLOW);
circle(110+i,110,10);
floodfill(110+i,110,WHITE);
Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm,x,y,i;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
for(i=100;i<=400;i++)
{
delay(10);
cleardevice();
line(100+i,100,50+i,180);
line(100+i,100,150+i,180);
line(50+i,180,100+i,250);
line(150+i,180,100+i,250);
line(100+i,100,100+i,250);
line(50+i,180,150+i,180);
line(100+i,150,10+i,300);
line(100+i,250,70+i,300);
line(100+i,250,130+i,300);
line(70+i,300,130+i,300);
}
getch();
closegraph();
}
Output