Académique Documents
Professionnel Documents
Culture Documents
Appendix A
Practical Programs Solved
1
COMPUTER GRAPHICS
#include<graphics.h>
#include<conio.h>
#include<dos.h>
void main()
{
int gd=0,gm,i,n,j,t,f;
char
a[20][80]={"PROGRAMMING","SKILLS","THROUGH","C","ANMOL","PUBLICATIO
NS", "BY","B.M.HAVALDAR"};
initgraph(&gd,&gm,"");
settextjustify(1,1);
setusercharsize(10,5,1,1);
i=getmaxy();
n=i;
j=0,t=-1,f=26;
while(!kbhit()) /* Animation continues till pressing and key */
{
if(i<=-50)
{
i=n;
j++;
f=26;
}
else i-=1;
if(j==8) /* the array of strings pointer is reset to starting */
j=0;
settextstyle(2,0,f);
2
COMPUTER GRAPHICS
setcolor(f);
outtextxy(getmaxx()/2,i,a[j]);
delay(5);
cleardevice();
if(i%20==0)
f=f+t*1;
if(f<=1)
t=1;
if(f>=26)
t= -1;
}
getch();
closegraph();
}
3
COMPUTER GRAPHICS
arc(300,200, 20,100,70);
/* arc with (300,200) as its center and
70 pixels as radious. It starts at an angle 20 and
ends at an angle 100 degrees */
getch();
closegraph(); /* closes the graph mode */
}
#include<stdio.h>
#include<dos.h>
#include<math.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT, gm,i,x,y,r,j;
float x1,y1;
clrscr();
/* initialization of center co-ordinates of the circle for wheel*/
x1=50;
y1=50;
/*for movement of wheel starting from 500 pixels along x-axis*/
j=500;
initgraph(&gd, &gm,"");/* graphics is initialized*/
x=x1;
y=y1;
r=sqrt(x*x+y*y);/* radius of wheel*/
while(1)
{
/* for every circle of the wheel*/
for(i=1;i<=360;++i)
{
x=x1*cos(i*3.142/180)+y1*sin(i*3.142/180);/*calculating the points to draw
line */
y=y1*cos(i*3.142/180)-x1*sin(i*3.142/180);
setcolor(4);
/*wheels outer circle*/
4
COMPUTER GRAPHICS
circle(j,200,r);
circle(j,200,r+5);
/*center of the wheel*/
circle(j,200,4);
circle(j+x,y+200,2);
circle(j-x,200-y,2);
line(j,200,x+j,y+200);
line(j,200,j-x,200-y);
delay(30);
/*erasing the line and circle that makes movement effect*/
setcolor(0);
circle(j,200,r+5);
circle(j,200,r);
circle(j,200,4);
circle(j+x,y+200,2);
circle(j-x,200-y,2);
line(j,200,x+j,y+200);
line(j,200,j-x,200-y);
j--;
if(j<=50)
break;
}
/* Rewriting the wheel at target position */
setcolor(4);
circle(j,200,r);
circle(j,200,4);
circle(j,200,r+5);
circle(j+x,y+200,2);
circle(j-x,200-y,2);
line(j,200,x+j,y+200);
line(j,200,j-x,200-y);
getch();
closegraph();
}
}
5
COMPUTER GRAPHICS
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
void main()
{
int gm,gd=DETECT,i;
int n,a[20],t;
clrscr();
do
{
t=0;
printf("\n enter n > 0 and <=20 \n");
scanf("%d",&n);
if(n<=0 || n>20)
{
printf(" enter the limit correctly press any key to continue...\n");
getch();
t=1;
}
}while(t==1);
for(i=0;i<n;++i)
{
do
{
t=0;
printf("\n enter value( >0 and <=400) %d : ",i+1);
scanf("%d",&a[i]);
if(a[i]<=0 || a[i]>400)
{
printf(" enter the limit correctly press any key to continue...\n");
getch();
t=1;
}
}while(t==1);
}
initgraph(&gd,&gm,"");
line(10,400,n*25+20,400);
for(i=0;i<n;++i) /* draws n bars with different filling
colors and patterns */
{
setfillstyle(i+1,i+1); /* fill pattern and color for each
6
COMPUTER GRAPHICS
getch();
closegraph(); /* closes the graph mode */
}
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
void main()
{
int gm,gd=DETECT,i;
int n,a[20],t;
clrscr();
do
{
t=0;
printf("\n enter n > 0 and <=20 \n");
scanf("%d",&n);
if(n<=0 || n>20)
{
printf(" enter the limit correctly press any key to continue...\n");
getch();
t=1;
}
}while(t==1);
for(i=0;i<n;++i)
{
do
{
7
COMPUTER GRAPHICS
t=0;
printf("\n enter value( > 0 and <= 600) %d : ",i+1);
scanf("%d",&a[i]);
if(a[i]<=0 || a[i]>600)
{
printf(" enter the limit correctly press any key to continue...\n");
getch();
t=1;
}
}while(t==1);
}
initgraph(&gd,&gm,"");
line(40,10,40,n*25+35);
for(i=0;i<n;++i) /* draws n bars with different filling
colors and patterns */
{
setfillstyle(i+1,i+1); /* fill pattern and color for each
of the bars changes dyanamically*/
rectangle(40,i*25+25,a[i]+40,i*25+40);
/* the x-coordinate of each
bars changes dynamically and
between each bars 10 pixels gap
is maintained */
getch();
closegraph(); /* closes the graph mode */
}
#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
#include<dos.h>
void main()
8
COMPUTER GRAPHICS
{
int gm,gd=DETECT,i;
initgraph(&gd,&gm,"");
while(!kbhit()) /* until pressing any key this continues */
{
putpixel(rand()%getmaxx(),rand()%getmaxy(),rand()%16);
/* X and Y co-ordinates and the color are taken randomly*/
delay(2); /* just to draw the pixels slowly */
}
getch();
closegraph(); /* closes the graph mode */
}
#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
#include<dos.h>
void main()
{
int gm,gd=DETECT;
int x1,x2,y1,y2,c,i;
initgraph(&gd,&gm,"");
while(!kbhit()) /* until pressing any key this continues */
{
/* for rectangle co-cordinates taking randomly */
x1=rand()%getmaxx();
x2=rand()%getmaxx();
y1=rand()%getmaxy();
y2=rand()%getmaxy();
if(x1>x2)
{
c=x1; /* exchange of x1 and x2 when x1 is > x2 */
x1=x2;
x2=c;
}
9
COMPUTER GRAPHICS
if(y1>y2)
{
c=y1; /* exchange of y1 and y2 when y1 is > y2 */
y1=y2;
y2=c;
}
c=rand()%16;
/* for rectangle using putpixel */
for(i=x1;i<=x2;++i)
{
putpixel(i,y1,c);
delay(1);
}
for(i=y1;i<=y2;++i)
{
putpixel(x2,i,c);
delay(1);
}
for(i=x2;i>=x1;--i)
{
putpixel(i,y2,c);
delay(1);
}
for(i=y2;i>=y1;--i)
{
putpixel(x1,i,c);
delay(1);
}
delay(200); /* just to draw the pixels slowly */
}
getch();
closegraph(); /* closes the graph mode */
}
#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
10
COMPUTER GRAPHICS
#include<dos.h>
void main()
{
int gm,gd=DETECT;
int x1,x2,y1,y2,c,i;
initgraph(&gd,&gm,"");
while(!kbhit()) /* until pressing any key this continues */
{
/* for rectangle co-cordinates taking randomly */
x1=rand()%getmaxx();
x2=rand()%getmaxx();
y1=rand()%getmaxy();
y2=rand()%getmaxy();
if(x1>x2)
{
c=x1; /* exchange of x1 and x2 when x1 is > x2 */
x1=x2;
x2=c;
}
if(y1>y2)
{
c=y1; /* exchange of y1 and y2 when y1 is > y2 */
y1=y2;
y2=c;
}
c=rand()%16;
/* for rectangle using putpixel */
for(i=x1;i<=x2;++i)
{
putpixel(i,y1,c);
delay(1);
}
for(i=y1;i<=y2;++i)
{
putpixel(x2,i,c);
delay(1);
}
for(i=x2;i>=x1;--i)
{
11
COMPUTER GRAPHICS
putpixel(i,y2,c);
delay(1);
}
for(i=y2;i>=y1;--i)
{
putpixel(x1,i,c);
delay(1);
}
setfillstyle(rand()%12,rand()%8); /* setting the random fill styles and colors */
floodfill(x1+1,y1+1,c);
delay(200); /* just to draw the pixels slowly */
}
getch();
closegraph(); /* closes the graph mode */
}
void main()
{
int gm,gd=DETECT;
int x1,x2,y1,y2,c,i;
initgraph(&gd,&gm,"");
while(!kbhit()) /* until pressing any key this continues */
{
/* for rectangle co-cordinates taking randomly */
x1=rand()%getmaxx();
x2=rand()%getmaxx();
y1=rand()%getmaxy();
y2=rand()%getmaxy();
12
COMPUTER GRAPHICS
getch();
closegraph(); /* closes the graph mode */
}
initgraph(&gd,&gm,"");
13
COMPUTER GRAPHICS
getch();
closegraph(); /* closes the graph mode */
}
setcolor(5);
settextstyle(4,0,5); /* sets the textstyle with
font, direction and char size */
moveto(100,100); /* takes the CP to 100,100 */
outtext("Bangalore is");
setcolor(4);
settextstyle(3,0,6);
moveto(200,200);
outtext("Silicon ");
setcolor(1);
settextstyle(5,0,6);
moveto(300,300);
outtext("Vally");
setcolor(2);
settextstyle(1,1,5);
outtextxy(150,50,"Bangalore is");
getch();
14
COMPUTER GRAPHICS
#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<alloc.h>
void main()
{
int gm,gd=DETECT,i;
void *z; /* to get the image */
initgraph(&gd,&gm,"");
rectangle(10,10,50,50);
z=malloc(imagesize(10,10,50,50)); /* memory allocation to the
variable to store the image */
getimage(10,10,50,50,z); /* gets the image into variable z */
putimage(10,10,z,1);/* erases the image from the original place */
for(i=0;i<=getmaxy()/2-50;++i)
{
putimage(getmaxx()/2,getmaxy()/2-i,z,2);
putimage(getmaxx()/2,getmaxy()/2+i,z,2);
putimage(getmaxx()/2-i,getmaxy()/2,z,2);
putimage(getmaxx()/2+i,getmaxy()/2,z,2);
putimage(getmaxx()/2-i,getmaxy()/2-i,z,2);
putimage(getmaxx()/2-i,getmaxy()/2+i,z,2);
putimage(getmaxx()/2+i,getmaxy()/2-i,z,2);
putimage(getmaxx()/2+i,getmaxy()/2+i,z,2);
putimage(getmaxx()/2,getmaxy()/2-i,z,1);
putimage(getmaxx()/2,getmaxy()/2+i,z,1);
putimage(getmaxx()/2-i,getmaxy()/2,z,1);
putimage(getmaxx()/2+i,getmaxy()/2,z,1);
putimage(getmaxx()/2-i,getmaxy()/2-i,z,1);
putimage(getmaxx()/2-i,getmaxy()/2+i,z,1);
putimage(getmaxx()/2+i,getmaxy()/2-i,z,1);
putimage(getmaxx()/2+i,getmaxy()/2+i,z,1);
15
COMPUTER GRAPHICS
putimage(getmaxx()/2,getmaxy()/2-i,z,2);
putimage(getmaxx()/2,getmaxy()/2+i,z,2);
putimage(getmaxx()/2-i,getmaxy()/2,z,2);
putimage(getmaxx()/2+i,getmaxy()/2,z,2);
putimage(getmaxx()/2-i,getmaxy()/2-i,z,2);
putimage(getmaxx()/2-i,getmaxy()/2+i,z,2);
putimage(getmaxx()/2+i,getmaxy()/2-i,z,2);
putimage(getmaxx()/2+i,getmaxy()/2+i,z,2);
getch();
void main()
{
int gm,gd=DETECT,i;
int x,y,x1,y1,j;
char *z; /* to get the image */
initgraph(&gd,&gm,"");
circle(getmaxx()/2,getmaxy()/2,50);
x=40;
y=40;
for(j=1;j<=20;++j)
{
for(i=0;i<=360;i+=10)
{
setcolor(i+1);
x1=x*cos(i*3.142/180)+y*sin(i*3.142/180);
y1=x*sin(i*3.142/180)-y*cos(i*3.142/180);
circle(x1+getmaxx()/2,y1+getmaxy()/2,5);
delay(10);
16
COMPUTER GRAPHICS
}
x=x+5;
y=y+5;
}
getch();
#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<alloc.h>
#include<math.h>
void main()
{
int gm,gd=DETECT,i;
int x,y,x1,y1,j;
initgraph(&gd,&gm,"");
x=40;
y=40;
for(j=0;j<20;++j)
{
for(i=0;i<=360;i+=10)
{
setcolor(i+1);
x1=x*cos(i*3.142/180)+y*sin(i*3.142/180);
y1=x*sin(i*3.142/180)-y*cos(i*3.142/180);
circle(x1+getmaxx()/2,y1+getmaxy()/2,5);
delay(10);
}
setcolor(j);
circle(getmaxx()/2,getmaxy()/2,j*10);
x=x+5;
y=y+5;
}
17
COMPUTER GRAPHICS
getch();
#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>
#include<alloc.h>
#include<math.h>
void main()
{
int gm,gd=DETECT,i,j;
initgraph(&gd,&gm,"");
setviewport(100,100,300,300,0);
for(j=0;j<200;j=j+20)
{
for(i=0;i<=200;++i)
{
if(i%20==0)
setcolor(rand()%16+1);
line(i,j,i,j+20);
delay(20);
}
delay(100);
}
getch();
#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<alloc.h>
18
COMPUTER GRAPHICS
#include<math.h>
void main()
{
int gm,gd=DETECT;
float x,y,x1,y1,i;
initgraph(&gd,&gm,"");
x=100;
y=100;
for(i=0;i<=360;i+=0.005)
{
x=x*cos(i*3.142/180)+y*sin(i*3.142/180);
y=-x*sin(i*3.142/180)+y*cos(i*3.142/180);
putpixel((int)x+200,(int)y+200,15);
if(i<7)
delay(10);
}
getch();
}
#include<stdio.h>
#include<dos.h>
#include<alloc.h>
#include<stdlib.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd =DETECT,gm,I,x,y,r,j ;
void *z;
clrscr();
initgraph(&gd,&gm,"");
19
COMPUTER GRAPHICS
circle(110,130,2);
arc(30,130,0,180,11);
arc(30,130,60,180,14);
line(16,130,19,130);
arc(110,130,0,180,11);
arc(110,130,0,120,14);
line(124,130,121,130);
arc(70,130,0,180,30);
arc(70,130,20,160,33);
arc(70,130,40,140,24);
line(41,130,99,130);
/*Getting the image of the car into variable ’z’ after allocating memory*/
z=malloc(imagesize(15,90,145,140));
getimage (15,90,145,140,z);
/* erasing the car image from its original position*/
putimage(15,90,z,1);
while (!kbhit())/* this loop continues until pressing any key*/
{
/*movement of car from left to right horizontally*/
y=rand()%400+50;/* y co-ordinate is taken randomly within the screen co-
ordinates along y-axis*/
for(I=getmaxx()-100;I>=10;--I)
{
putimage(I,y,z,2);
delay(rand()%5);
putimage(I,y,z,1);
}
}
putimage(I,y,z,2);
getch();
closegraph ();
}
20
COMPUTER GRAPHICS
# include <stdio.h>
#include<dos.h>
#include<conio.h>
#include<graphics.h>
#include<alloc.h>
void main()
{
int gd=DETECT,gm,i;
void*z;
initgraph(&gd,&gm,"");
/* Construction of man and umbrella in his hand */
circle (50,28,4);
line(50,32,50,56);
line (50,40,42,48);
line(50,40,58,48);
line(50,56,42,64);
line(50,56,58,64);
line(58,48,64,40);
line(64,40,64,11);
arc(64,20,0,180,17);
arc(64,30,30,150,20);
for(i=10;i<=getmaxx()-100;++i)
{
putimage(i,100,z,2);
delay(10);
putimage(i,100,z,1);
21
COMPUTER GRAPHICS
}
putimage(i,100,z,2);
getch();
closegraph();
}
22
COMPUTER GRAPHICS
References
23