Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
46 views22 pages

Computer Graphics Lab Page No

Download as doc, pdf, or txt
Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1/ 22

Computer Graphics Lab

Page No:

Program No: 1

Date:

PROGRAM
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void line_DDA(int,int,int,int);
int steps,k;
float x_increment,y_increment,x,y;
void main()
{
int gd=DETECT,gm;
int xa,xb,ya,yb,ch;
char c;
initgraph(&gd,&gm,"c:\\tc\\bgi");
cleardevice();
do
{
cleardevice();
printf("\n\nEnter the first Co-ordinates : ");
scanf("%d%d",&xa,&ya);
printf("\n\nEnter the second Co-ordinates : ");
scanf("%d%d",&xb,&yb);
line_DDA(xa,ya,xb,yb);
printf("\n\n\n\tMENU");
printf("\n\t----");
printf("\n\n1.Solid line\n2.Dashed line\n3.Dotted line");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
for(k=0;k<steps;k++)
{
x+=x_increment;
y+=y_increment;
putpixel(x,y,4);
}
break;
case 2:
for(k=0;k<steps;k++)
{
x+=x_increment;
y+=y_increment;
if(k%5!=0)
putpixel(x,y,4);
}
CHMM College For Advanced Studies

Department of MCA

Computer Graphics Lab

Page No:

break;
case 3:
for(k=0;k<steps;k++)
{
x+=x_increment;
y+=y_increment;
if(k%3==0)
putpixel(x,y,4);
}
break;
default :
printf("\n\n\n Invalid choice !");
break;
}
printf("\n\n\nDo you want to continue ? (y/n): ");
scanf(" %c",&c);
//c=getchar();
}while(c=='y');
getch();
closegraph();
}

void line_DDA(int xa,int ya,int xb,int yb)


{
int dx=abs(xb-xa),dy=abs(yb-ya);
if(dx > dy)
steps=dx;
else
steps=dy;
x_increment=(float)dx/steps;
y_increment=(float)dy/steps;
x=xa;
y=ya;
putpixel(x,y,4);
}

CHMM College For Advanced Studies

Department of MCA

Computer Graphics Lab

Page No:

OUTPUT
Enter the first Co-ordinates : 100 150
Enter the second Co-ordinates : 200 300
MENU
1.Solid line
2.Dashed line
3.Dotted line
Enter your choice: 1

Do you want to continue ? (y/n): Y


Enter the first Co-ordinates : 150 200
Enter the second Co-ordinates : 100 50
MENU
1.Solid line
2.Dashed line
3.Dotted line
Enter your choice: 3

CHMM College For Advanced Studies

Department of MCA

Computer Graphics Lab

CHMM College For Advanced Studies

Page No:

Department of MCA

Computer Graphics Lab

Page No:

Program No: 2

Date:

PROGRAM
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void Bresenhams_line(int,int,int,int);
void main()
{
int gd=DETECT,gm;
int xa,xb,ya,yb;
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("\n\nEnter the first Co-ordinates : ");
scanf("%d%d",&xa,&ya);
printf("\n\nEnter the second Co-ordinates : ");
scanf("%d%d",&xb,&yb);
Bresenhams_line(xa,ya,xb,yb);
getch();
closegraph();
}
void Bresenhams_line(int xa,int ya,int xb,int yb)
{
int dx=abs(xb-xa),dy=abs(yb-ya),p,xend,x,y;
p=2*dy-dx;
if(xa > xb)
{
x=xb;
y=yb;
xend=xa;
}
else
{
x=xa;
y=ya;
xend=xb;
}
putpixel(x,y,4);
while(x<xend)
{
x=x+1;
if(p<0)
p= p+2*dy;
else
CHMM College For Advanced Studies

Department of MCA

Computer Graphics Lab

Page No:

{
y=y+1;
p=p+2*dy-2*dx;
}
putpixel(x,y,4);
}
}
OUTPUT
Enter the first Co-ordinates : 200 250
Enter the second Co-ordinates : 150 100

CHMM College For Advanced Studies

Department of MCA

Computer Graphics Lab

Page No:

Program No: 17

Date:
CLOCK SIMULATION (ANALOG & DIGITAL)

AIM
Develop a program to animate Clock Simulation (Analog & Digital). Set start time.
PROGRAM
#include<stdio.h>
#include<graphics.h>
#include<dos.h>
#include<stdlib.h>
void drawclock();
void test();
char S[12][3]={"1","2","3","4","5","6","7","8","9","10","11","12"};
struct time t;
void main()
{
int gd=DETECT, gm, x, y;
int hr, mi, sec, choice;
char ch;
gettime(&t);
printf("Hour: ");
scanf("%d",&hr);
printf("Min: ");
scanf("%d",&mi);
printf("Sec: ");
scanf("%d",&sec);
clrscr();
printf("1. am\n2. pm");
scanf("%d", &choice);
if(choice == 1)
t.ti_hour = hr;
else
t.ti_hour = hr+12;
t.ti_min = mi;
t.ti_sec = sec;
settime(&t);
initgraph(&gd, &gm,"..\\bgi");
drawclock();
test();
getch();
}

CHMM College For Advanced Studies

Department of MCA

Computer Graphics Lab

Page No:

void drawclock()
{
int x = getmaxx()/2, y = getmaxy()/2, i, j;
struct arccoordstype arcinfo;
setcolor(WHITE);
arc(x, y, 0, 360, 110);
outtextxy(x-5, y-100, "12");
for(i=0 , j=5; i<11; i++, j+=5)
{
setcolor(BLACK);
arc(x, y, 90-(j*6), 90, 100);
getarccoords(&arcinfo);
setcolor(WHITE);
outtextxy(arcinfo.xstart, arcinfo.ystart, S[i]);
}
}
void test()
{
int x = getmaxx()/2, y = getmaxy()/2, hr, min, sec;
int x1, y1, x2, y2, x3, y3;
char buff[10];
struct arccoordstype arcinfo;
sec = 90;
min = 90;
hr = 90;
while(!kbhit())
{
gettime(&t);
sec = (t.ti_sec+60)*6;
min = t.ti_min*6;
hr = t.ti_hour*30;
setcolor(BLACK);
arc(x, y, 90-sec, 90, 70);
getarccoords(&arcinfo);
x1 = arcinfo.xstart;
y1 = arcinfo.ystart;
arc(x, y, 90-min, 90, 60);
getarccoords(&arcinfo);
x2 = arcinfo.xstart;
y2 = arcinfo.ystart;

CHMM College For Advanced Studies

Department of MCA

Computer Graphics Lab

Page No:

arc(x, y, 90-hr, 90, 50);


getarccoords(&arcinfo);
x3 = arcinfo.xstart;
y3 = arcinfo.ystart;
setcolor(WHITE);
line(x, y, x1, y1);
line(x, y, x2, y2);
line(x, y, x3, y3);
if(t.ti_hour>12)
sprintf(buff, "%2d:%02d:%02d pm", t.ti_hour-12, t.ti_min, t.ti_sec);
else
sprintf(buff, "%2d:%02d:%02d am", t.ti_hour, t.ti_min, t.ti_sec);
outtextxy(x-20, y+120, buff);
delay(100);
setcolor(BLACK);
outtextxy(x-20, y+120, buff);
line(x, y, x1, y1);
line(x, y, x2, y2);
line(x, y, x3, y3);
}
}

CHMM College For Advanced Studies

Department of MCA

Computer Graphics Lab

Page No:

OUTPUT
Hour : 11
Min : 00
Sec : 20
1.am
2.pm
1

CHMM College For Advanced Studies

Department of MCA

Computer Graphics Lab

Page No:

Program No: 18

Date:
FILLING OF WATER IN A BUCKET (ANIMATION)

AIM
Animate the filling of water in a bucket, from a water tap and its overflow. Show how
to vary the rate of filling.
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
int main()
{
int gd=DETECT,gm,x=100,y=400,i,k,j;
float h;
char c;
struct arccoordstype a;
struct arccoordstype b;
initgraph(&gd,&gm,"..\bgi");
line(x-50,y-50,0,getmaxy());//boundary
line(x-50,y-50,x,y-50);
line(x+15,y-50,x+107,y-50);//"
line(x+193,y-50,getmaxx(),y-50);
ellipse(x+8,y,0,360,9,5);//hole
moveto(x,y);
lineto(x,y-210);
ellipse(x+16,y-204,100,140,20,10); // main pipe
line(x+13,y-214,x+100,y-214);
line(x+15,y,x+15,y-195);
ellipse(x+31,y-190,110,140,20,10);
line(x+25,y-200,x+100,y-200);
for(i=0;i<3;i++)
{
ellipse(x+102+i,y-205,0,360,5,12);
}
line(x+100,y-210,x+114,y-210);//1
ellipse(x+135,y-210,0,180,20,8);
line(x+100,y-202,x+115,y-202);//Tap pipe-2
ellipse(x+135,y-202,70,180,20,2);
line(x+135,y-218,x+135,y-225);//Tap cap line
line(x+130,y-218,x+130,y-225);//"
setfillstyle(4,15);
ellipse(x+135,y-230,0,360,20,5);// Tap Hand
floodfill(x+135,y-229,15);
setfillstyle(9,15);
line(x+155,y-210,x+155,y-195); //Tap water hole-Right
CHMM College For Advanced Studies

Department of MCA

Computer Graphics Lab

Page No:

line(x+144,y-203,x+144,y-195);//left
fillellipse(x+150,y-195,7,3);
for(i=0;i<3;i++)
{
setcolor(15);
ellipse(x+150,y-90,0,360,50-i,10-i); // Bucket
line(x+103+i,y-90,x+113+i,y);
line(x+197-i,y-90,x+187-i,y);
}
setfillstyle(1,15);
fillellipse(x+150,y,38,4);
for(i=0;i<3;i++)
{
ellipse(x+151-i,y-85,180,360,46,35);//Bucket hand
}
for(i=0;i<3;i++)
{
ellipse(x+151,y-50-i,0,360,11,2); // bucket hand ( middile)
}
delay(3000);
setcolor(9);
setfillstyle(1,1);
for(i=0;i<190;i++) //water from pipe point to bucket
{
if(kbhit())
{
getch();
getch();
}
setcolor(9);
setfillstyle(1,1);
circle(x+150,y-192+i,6);
delay(10);
for(j=0;j<3;j++)
{
setcolor(15);
ellipse(x+150,y-90,0,360,50-j,10-j);
}
for(k=0;k<3;k++)
{
ellipse(x+151-k,y-85,180,360,46,35);//Bucket hand
}
for(k=0;k<3;k++)
{
ellipse(x+151,y-50-k,0,360,11,2); // bucket hand ( middile)
}
}
setcolor(9);
setfillstyle(1,1);

CHMM College For Advanced Studies

Department of MCA

Computer Graphics Lab

Page No:

for(i=0;i<10;i++) //Bucket filling-1


{
fillellipse(x+151,y-3-i,36,4);
delay(25);
if(kbhit())
{
getch(); getch();
}
}
for(i=0;i<10;i++) //Bucket filling-2
{
fillellipse(x+151,y-10-i,37,4);
delay(25);
if(kbhit())
{
getch();
getch();
}
}
for(i=0;i<10;i++) //Bucket filling-3
{
fillellipse(x+151,y-20-i,38,4);
delay(25);
if(kbhit())
{
getch();
getch();
}
}
for(i=0;i<10;i++) //Bucket filling-4
{
fillellipse(x+151,y-30-i,39,4);
delay(25);
if(kbhit())
{
getch();
getch();
}
}
for(i=0;i<10;i++) //Bucket filling-5
{
fillellipse(x+151,y-40-i,40,4);
delay(25);
if(kbhit())
{
getch();
getch();
}
}
CHMM College For Advanced Studies

Department of MCA

Computer Graphics Lab

Page No:

for(i=0;i<10;i++) //Bucket filling-6


{
fillellipse(x+151,y-50-i,41,4);
delay(25);
setcolor(15);
for(k=0;k<3;k++)
{
ellipse(x+151-k,y-85,180,360,46,35);//Bucket hand
}
for(k=0;k<3;k++)
{
ellipse(x+151,y-50-k,0,360,11,2); // bucket hand ( middile )
}
setcolor(9);
if(kbhit())
{
getch();
getch();
}
}
for(i=0;i<10;i++) //Bucket filling
{
fillellipse(x+151,y-60-i,42,4);
delay(25);
setcolor(15);
for(k=0;k<3;k++)
{
ellipse(x+151-k,y-85,180,360,46,35);//Bucket hand
}
for(k=0;k<3;k++)
{
ellipse(x+151,y-50-k,0,360,11,2); // bucket hand ( middile)
}
setcolor(9);
if(kbhit())
{
getch();
getch();
}
}
for(i=0;i<6;i++) //Bucket filling
{
fillellipse(x+151,y-70-i,44,4);
delay(25);
setcolor(15);
for(k=0;k<3;k++)
{
ellipse(x+151-k,y-85,180,360,46,35);//Bucket hand
}
CHMM College For Advanced Studies

Department of MCA

Computer Graphics Lab

Page No:

for(k=0;k<3;k++)
{
ellipse(x+151,y-50-k,0,360,11,2); // bucket hand ( middile )
}
setcolor(9);
if(kbhit())
{
getch();
getch();
}
}
for(i=0;i<4;i++) //Bucket filling
{
if(kbhit())
{
getch();
getch();
}
fillellipse(x+151,y-76-i,45,4);
delay(25);
for(j=0;j<3;j++)
{
setcolor(15);
ellipse(x+150,y-90,0,360,50-j,10-j); // Bucket
}
setcolor(9);
setcolor(15);
for(k=0;k<3;k++)
{
ellipse(x+151-k,y-85,180,360,46,35);//Bucket hand
}
for(k=0;k<3;k++)
{
ellipse(x+151,y-50-k,0,360,11,2); // bucket hand ( middile)
}
setcolor(9);
}
//------Last-----///
setfillstyle(1,1);
for(i=0;i<10;i++) //Bucket filling
{
if(kbhit())
{
getch();
getch();
}

CHMM College For Advanced Studies

Department of MCA

Computer Graphics Lab

Page No:

setfillstyle(1,1);
fillellipse(x+151,y-80-i,45,8);
delay(25);
for(j=0;j<3;j++)
{
setcolor(15);
ellipse(x+150,y-90,0,360,50-j,10-j); // Bucket
}
setcolor(9);
setcolor(15);
for(k=0;k<3;k++)
{
ellipse(x+151-k,y-85,180,360,46,35);//Bucket hand
}
for(k=0;k<3;k++)
{
ellipse(x+151,y-50-k,0,360,11,2); // bucket hand ( middile)
}
setcolor(9);
}
//--------Over flow-------//
setcolor(1);
for(i=0,h=0;i<30;i++)
{
line(218+h,315+i,280-h,315+i);
h=h+.5;
}
for(i=0;i<40;i++)
{
line(228,335+i,265,335+i);
}
setfillstyle(1,1);
for(i=0;i<40;i++)
{
fillellipse(248,365+i,20,3);
}
for(i=0;i<40;i++)
{
fillellipse(245+i,405+i,17+i,3+i);
delay(75);
}
getch();
closegraph();
}

CHMM College For Advanced Studies

Department of MCA

Computer Graphics Lab

Page No:

OUTPUT

CHMM College For Advanced Studies

Department of MCA

Computer Graphics Lab

Page No:

Program No: 19

Date:

MOVEMENT OF A CAR IN THE TRAFFIC CONTROL


AIM
Develop a C graphics program to animate the movement of a car (using polygon) in the
traffic control.
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int m, n;
unsigned int size;
void *red,*yellow,*green;
void main()
{
void drawlight();
int gd=DETECT,gm,x=480,y=400;
struct arccoordstype a;
int body[30],window[30],window_r[30];
unsigned int carsize ;
void *car;
initgraph(&gd,&gm,"c:\\tc\\bgi");
// road //
line(0,y+30,getmaxx(),y+30);//thazhe
line(0,y+1,getmaxx(),y+1);// mukalil
drawlight();//light
// main body//
setfillstyle(1,4);
moveto(x,y);
body[0]=x,body[1]=y,body[2]=x-25,body[3]=y,body[4]=x-25,
body[5]=y-20,body[6]=x-21,body[7]=y-24,body[8]=x+15,body[9]=y-24,
body[10]=x+40,body[11]=y-45,body[12]=x+100,body[13]=y-45,body[14]=x+130,
body[15]=y-30,body[16]=x+150,body[17]=y-30,body[18]=x+150,body[19]=y,
body[20]=x+135,body[21]=y;
drawpoly(11,body);
putpixel(x+10,y-20,2);
arc(x+10,y,0,180,10); // for left tyre
line(x+20,y,x+115,y);
arc(x+125,y,0,180,10);
circle(x+125,y,7);
circle(x+125,y,3);
floodfill(x+125,y,15);
circle(x+10,y,7);
circle(x+10,y,3);
CHMM College For Advanced Studies

Department of MCA

Computer Graphics Lab

Page No:

floodfill(x+10,y,15);
// window -right//
moveto(x+120,y-27);
lineto(x+70,y-27);
window_r[0]=x+70,window_r[1]=y-27,window_r[2]=x+70,window_r[3]=y40,window_r[4]=x+100,
window_r[5]=y-40,window_r[6]=x+120,window_r[7]=y-27;
drawpoly(4,window_r);
arc(x+125,y-2,97,180,25);
line(580,398,x+70,y-2);
line(x+70,y-2,x+70,y-27);
putpixel(x+90,y-15,2);
floodfill(x+90,y-15,15);
// window-left //
setfillstyle(1,4);//
moveto(x+28,y-2);
lineto(x+65,y-2);
window[0]=x+65,window[1]=y-2,window[2]=x+65,window[3]=y40,window[4]=x+41,
window[5]=y-40,window[6]=x+25,window[7]=y-27,window[8]=x+65,window[9]=y27;
drawpoly(5,window);
arc(x,y,4,44,29);
line(x+25,y-27,x+20,y-20);
putpixel(x+50,y-15,2);
floodfill(x+50,y-15,15);
moveto(x,y);
carsize = imagesize(x-27,y-53,x+152,y+10);
car = malloc(carsize);
getimage(x-27,y-53,x+152,y+10,car);
cleardevice();
// animation //
for(;x!=370;x--)
{
line(0,y+30,getmaxx(),y+30);//thazhe
line(0,y+1,getmaxx(),y+1);// mukalil
setfillstyle(1,8);
bar3d(m,n+30,m+10,n+180,1,1);
putimage(m-13,n-35,green,0);
putimage(x,y-55,car,0);
delay(15);
if(kbhit())
{
exit(0);
}
}
putimage(m-13,n-35,yellow,0);

CHMM College For Advanced Studies

Department of MCA

Computer Graphics Lab

Page No:

for(x=370;x!=320;x--)
{
line(0,y+30,getmaxx(),y+30);//thazhe
line(0,y+1,getmaxx(),y+1);// mukalil
putimage(m-13,n-35,yellow,0);
putimage(x,y-55,car,0);
setfillstyle(1,8);
bar3d(m,n+30,m+10,n+180,1,1);
delay(70);
if(kbhit())
{
exit(0);
}
}
putimage(m-13,n-35,red,0);
delay(2000);
putimage(m-13,n-35,yellow,0);
delay(2000);
for(x=320;x!=0;x--)
{
line(0,y+30,getmaxx(),y+30);//thazhe
line(0,y+1,getmaxx(),y+1);// mukalil
putimage(m-13,n-35,green,0);
putimage(x,y-55,car,0);
setfillstyle(1,8);
bar3d(m,n+30,m+10,n+180,1,1);
delay(25);
if(kbhit())
{
exit(0);
}
}
getch();
closegraph();
}
void drawlight()
{
m = getmaxx()/2-120;
n = getmaxy()/2;
setcolor(WHITE);// for Red light
bar3d(m-10, n-30, m+10, n+30, 5, 1);
setcolor(BLACK);
circle(m, n-20, 8);
setfillstyle(1, RED);
floodfill(m, n-20, BLACK);
circle(m, n, 8);
setfillstyle(1, BLACK);
CHMM College For Advanced Studies

Department of MCA

Computer Graphics Lab

Page No:

floodfill(m, n, BLACK);
circle(m, n+20, 8);
setfillstyle(1, BLACK);
floodfill(m, n+20, BLACK);
setfillstyle(1, WHITE);
setcolor(WHITE);
size = imagesize(m-13,n-35, m+18, n+33);
red = malloc(size);
getimage(m-13,n-35, m+18, n+33,red);
clearviewport();
setcolor(WHITE); // for Yellow light
bar3d(m-10, n-30, m+10, n+30, 5, 1);
setcolor(BLACK);
circle(m, n-20, 8);
setfillstyle(1, BLACK);
floodfill(m, n-20, BLACK);
circle(m, n, 8);
setfillstyle(1, YELLOW);
floodfill(m, n, BLACK);
circle(m, n+20, 8);
setfillstyle(1, BLACK);
floodfill(m, n+20, BLACK);
setfillstyle(1, WHITE);
size = imagesize(m-13,n-35, m+18, n+33);
yellow = malloc(size);
getimage(m-13,n-35, m+18, n+33,yellow);
clearviewport();
setcolor(WHITE); // For Green light
bar3d(m-10, n-30, m+10, n+30, 5, 1);
setfillstyle(1,8);
bar3d(m,n+30,m+10,n+180,1,1);
setcolor(BLACK);
circle(m, n-20, 8);
setfillstyle(1,BLACK);
floodfill(m, n-20, BLACK);
circle(m, n, 8);
setfillstyle(1, BLACK);
floodfill(m, n, BLACK);
circle(m, n+20, 8);
setfillstyle(1, GREEN);
floodfill(m, n+20, BLACK);
setfillstyle(1, WHITE);
size = imagesize(m-13,n-35, m+18, n+33);
green = malloc(size);
getimage(m-13,n-35, m+18, n+33,green);
setcolor(WHITE);
clearviewport();
setcolor(15);
}

CHMM College For Advanced Studies

Department of MCA

Computer Graphics Lab

Page No:

OUTPUT

CHMM College For Advanced Studies

Department of MCA

You might also like