Sei sulla pagina 1di 34

LAB MANUAL

Computer Graphics
(ETCS 258)
B.Tech. Programme (CSE+IT)

Maharaja Surajmal Institute of Technology Affiliated: GGSIPUniversity C-4, JanakPuri, New Delhi - 110058

Contents

1. 2. 3. 4. 5. 6. 7.

Introduction Hardware and Software requirements List of Experiments Marking Scheme for the Practical Lab Examination Details of Experiments Expected viva voce Questions References

2. H/W & S/W Requirements

Software requirements:

Turbo C Operating System: Windows - XP

Hardware requirements:

P-IV 2.8 GHz Intel 845 MB/40 GB HDD/ 512 MB RAM

List of Experiments

1. Study of Fundamental Graphics Functions. 2. Write a program to implement DDA Line Drawing Algorithm 3. Write a program to implement Bresenham's Line Drawing Algorithm 4. Write a program to implement Bresenham's Circle Drawing Algorithm 5. Write a program to implement Text Animation 6. Write a program to implement 2D Scaling 7. Write a program to implement 2D Rotation 8. Write a program to implement 2D Translation Triangle Program 9. Write a program to implement Midpoint Circle Algorithm 10. Write a program to implement reflection 11. Write a program to implement shearing 12. Write a program to draw Bezier curve 13. Write a program to implement cohen Sutherland line clipping algorithm

Marking Scheme for the Practical Lab Exam


There will be two practical exams in each semester. Internal Practical Exam External Practical Exam Internal Practical Exam: It is taken by the concerned Faculty member of the batch. Marking Scheme: Total Marks: 40 Division of 40 marks is as follows: 1. Regularity: 30 Weekly performance in the lab Attendance File 2. Viva Voce: 10 NOTE: For the regularity, marks are awarded to the student out of 10 for each experiment performed in the lab and at the end the average marks are giving out of 30. External Practical Exam: It is taken by the concerned faculty member of the batch and by an external examiner. In this exam student needs to perform the experiment allotted at the time of the examination, a sheet will be given to the student in which some details asked by the examiner needs to be written and at last viva will be taken by the external examiner. Marking Scheme: Total Marks: 60 Division of 60 marks is as follows: a. Evaluation of the answer sheet 20 b. Viva Voce 15 c. Experiment performance 15 d. File submitted 10 NOTE: Internal marks + External marks = Total marks given to the students (40 marks) + (60 marks) (100 marks) Experiments given to perform can be from any section of the lab.

Experiment No 1 Aim: Study of Fundamental Graphics Functions Coding: #include<stdio.h> #include<conio.h> #include<process.h> #include<graphics.h> void main() { int gd=DETECT,gm,ch; initgraph(&gd,&gm," "); do { clrscr(); printf("\nmenu\n1.circle\n2.line\n3.rectangle\n4.exit\nEnter your choice: "); scanf("%d",&ch); cleardevice(); outtextxy(10,10,"FUNDAMENTALS"); switch(ch) { case 1: circle(200,200,80); break; case 2: line(200,200,300,300); break; case 3: rectangle(100,100,400,400); break; } getch(); }while(ch<=3); getch(); }

Experiment No 2 Aim: Write a program to implement DDA Line Drawing Algorithm Coding: #include<stdio.h> #include<math.h> #include<conio.h> #include<graphics.h> #define round(val) (int)(val+0.5) void main() { int gd=DETECT,gm; void line_dda(int,int,int,int); int xa,xb,ya,yb; printf("Enter the two values"); scanf("%d%d%d%d",&xa,&ya,&xb,&yb); initgraph(&gd,&gm,""); cleardevice(); line_dda(xa,ya,xb,yb); getch(); closegraph(); } void line_dda(int xa,int ya,int xb,int yb) { int Dx=xb-xa,Dy=yb-ya,steps,k; float xin,yin,X=xa,Y=ya; if(abs(Dx)>abs(Dy)) steps=abs(Dx); else steps=abs(Dy); xin=Dx/(float)steps; yin=Dy/(float)steps; putpixel(round(X),round(Y),6); for(k=0;k<steps;k++) { X=X+xin; Y=Y+yin; putpixel(round(X),round(Y),6); } }

Experiment No 3 Aim: Write a program to implement Bresenham's Line Drawing Algorithm Coding: #include<stdio.h> #include<math.h> #include<conio.h> #include<graphics.h> void main() { int x1,x2,y1,y2; int gd=DETECT,gm; void linebres(int,int,int,int); printf("Enter the two end points:"); scanf("%d%d%d%d",&x1,&x2,&y1,&y2); initgraph(&gd,&gm,""); cleardevice(); linebres(x1,y1,x2,y2); getch(); line(x1,y1,x2,y2); getch(); closegraph(); } void linebres(int x1,int y1,int x2,int y2) { int dx=abs(x1-x2),dy=abs(y1-y2); int p,x,y,i,xend,yend; if(dx!=0) { p=2*dy-dx; if(x1>x2) { x=x2; y=y2; xend=x1; } else { x=x1; y=y1; xend=x2; } putpixel(x,y,2);

for(i=x;i<xend;i++) { x+=1; if(p<0) p+=2*dy; else p+=2*(dy-dx); } putpixel(x,y,2); } else { p=2*dx-dy; if(y1>y2) { x=x2; y=y2; yend=y2; } putpixel(x,y,2); for(i=y;i<yend;i++) { y+=1; if(p<0) p+=2*dx; else { x+=1; p+=2*(dx-dy); } putpixel(x,y,2); } } }

Experiment No 4 Aim: Write a program to implement Bresenham's Circle Drawing Algorithm Coding: #include<stdio.h> #include<conio.h> #include<graphics.h> void main() { int gd=DETECT,gm; int x,y,r; void cir(int,int,int); printf("Enter the Mid points and Radious:"); scanf("%d%d%d",&x,&y,&r); initgraph(&gd,&gm,""); cir(x,y,r); getch(); closegraph(); } void cir(int x1,int y1,int r) { int x=0,y=r,p=1-r; void cliplot(int,int,int,int); cliplot(x1,y1,x,y); while(x<y) { x++; if(p<0) p+=2*x+1; else { y--; p+=2*(x-y)+1; } cliplot(x1,y1,x,y); } } void cliplot(int xctr,int yctr,int x,int y) { putpixel(xctr +x,yctr +y,1); putpixel(xctr -x,yctr +y,1); putpixel(xctr +x,yctr -y,1); putpixel(xctr -x,yctr -y,1); putpixel(xctr +y,yctr +x,1);

putpixel(xctr -y,yctr +x,1); putpixel(xctr +y,yctr -x,1); putpixel(xctr -y,yctr -x,1); getch(); }

Experiment No 5 Aim: Write a program to implement Text Animation Coding: include<stdio.h> #include<math.h> #include<conio.h> #include<graphics.h> #define round(val) (int)(val+0.5) void main() { int gd=DETECT,gm,sx,sy,tx,ty; char text[50]; void move(int,int,int,int,char[]); printf("Enter the text:"); scanf("%s",text); printf("Enter the initial points:"); scanf("%d%d",&sx,&sy); printf("Enter the TARGET points:"); scanf("%d%d",&tx,&ty); initgraph(&gd,&gm,""); outtextxy(sx,sy,text); move(sx,sy,tx,ty,text); getch(); closegraph(); } void move(int sx,int sy,int tx,int ty,char text[50]) { int dx=tx-sx,dy=ty-sy,steps,k; float xin,yin,x=sx,y=sy; getch(); if(abs(dx)>abs(dy)) steps=abs(dy); else steps=abs(dy); xin=dx/(float)steps; yin=dy/(float)steps; for(k=0;k<steps;k++) { cleardevice(); x+=xin; y+=yin; setcolor(15); outtextxy(round(x),round(y),text); delay(50); } }

Experiment No 6 Aim: Write a program to implement 2D Scaling Coding: #include<stdio.h> #include<conio.h> #include<graphics.h> #include<process.h> #include<math.h> int x1,y1,x2,y2,x3,y3,mx,my; void draw(); void scale(); void main() { int gd=DETECT,gm; int c; initgraph(&gd,&gm," "); printf("Enter the 1st point for the triangle:"); scanf("%d%d",&x1,&y1); printf("Enter the 2nd point for the triangle:"); scanf("%d%d",&x2,&y2); printf("Enter the 3rd point for the triangle:"); scanf("%d%d",&x3,&y3); draw(); scale(); } void draw() { line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x3,y3,x1,y1); } void scale() { int x,y,a1,a2,a3,b1,b2,b3; int mx,my; printf("Enter the scalling coordinates"); scanf("%d%d",&x,&y); mx=(x1+x2+x3)/3; my=(y1+y2+y3)/3; cleardevice(); a1=mx+(x1-mx)*x; b1=my+(y1-my)*y; a2=mx+(x2-mx)*x;

b2=my+(y2-my)*y; a3=mx+(x3-mx)*x; b3=my+(y3-my)*y; line(a1,b1,a2,b2); line(a2,b2,a3,b3); line(a3,b3,a1,b1); draw(); getch(); }

Experiment No 7 Aim: Write a program to implement 2D Rotation Coding: include<stdio.h> #include<conio.h> #include<graphics.h> #include<process.h> #include<math.h>

void TriAngle(int x1,int y1,int x2,int y2,int x3,int y3); void Rotate(int x1,int y1,int x2,int y2,int x3,int y3);

void main() { int gd=DETECT,gm; int x1,y1,x2,y2,x3,y3; initgraph(&gd,&gm," ");

printf("Enter the 1st point for the triangle:"); scanf("%d%d",&x1,&y1); printf("Enter the 2nd point for the triangle:"); scanf("%d%d",&x2,&y2); printf("Enter the 3rd point for the triangle:"); scanf("%d%d",&x3,&y3); TriAngle(x1,y1,x2,y2,x3,y3); getch(); cleardevice(); Rotate(x1,y1,x2,y2,x3,y3); setcolor(1); TriAngle(x1,y1,x2,y2,x3,y3); getch(); } void TriAngle(int x1,int y1,int x2,int y2,int x3,int y3) { line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x3,y3,x1,y1); } void Rotate(int x1,int y1,int x2,int y2,int x3,int y3) {

int x,y,a1,b1,a2,b2,a3,b3,p=x2,q=y2; float Angle; printf("Enter the angle for rotation:"); scanf("%f",&Angle); cleardevice(); Angle=(Angle*3.14)/180; a1=p+(x1-p)*cos(Angle)-(y1-q)*sin(Angle); b1=q+(x1-p)*sin(Angle)+(y1-q)*cos(Angle); a2=p+(x2-p)*cos(Angle)-(y2-q)*sin(Angle); b2=q+(x2-p)*sin(Angle)+(y2-q)*cos(Angle); a3=p+(x3-p)*cos(Angle)-(y3-q)*sin(Angle); b3=q+(x3-p)*sin(Angle)+(y3-q)*cos(Angle); printf("Rotate"); TriAngle(a1,b1,a2,b2,a3,b3); }

Experiment No 8 Aim: Write a program to implement 2D Translation Triangle Program Coding: #include<stdio.h> #include<conio.h> #include<graphics.h> #include<process.h> #include<math.h> int x1,y1,x2,y2,x3,y3,mx,my; void draw(); void tri(); void main() { int gd=DETECT,gm; int c; initgraph(&gd,&gm,"d:\\tc\\bgi "); printf("Enter the 1st point for the triangle:"); scanf("%d%d",&x1,&y1); printf("Enter the 2nd point for the triangle:"); scanf("%d%d",&x2,&y2); printf("Enter the 3rd point for the triangle:"); scanf("%d%d",&x3,&y3); cleardevice(); draw(); getch(); tri(); getch(); } void draw() { line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x3,y3,x1,y1); } void tri() { int x,y,a1,a2,a3,b1,b2,b3; printf("Enter the Transaction coordinates"); scanf("%d%d",&x,&y); cleardevice(); a1=x1+x;

b1=y1+y; a2=x2+x; b2=y2+y; a3=x3+x; b3=y3+y; line(a1,b1,a2,b2); line(a2,b2,a3,b3); line(a3,b3,a1,b1); }

Experiment No 9 Aim: Write a program to implement Midpoint Circle Algorithm Coding: #include<stdio.h> #include<conio.h> #include<graphics.h> void main() { int gd=DETECT,gm; int x,y,r; void Drawcircle(int,int,int); printf("Enter the Mid points and Radious:"); scanf("%d%d%d",&x,&y,&r); initgraph(&gd,&gm,""); Drawcircle(x,y,r); getch(); closegraph(); } void Drawcircle(int x1,int y1,int r) { int x=0,y=r,p=1-r; void cliplot(int,int,int,int); cliplot(x1,y1,x,y); while(x<y) { x++; if(p<0) p+=2*x+1; else { y--; p+=2*(x-y)+1; } cliplot(x1,y1,x,y); } } void cliplot(int xctr,int yctr,int x,int y) { putpixel(xctr +x,yctr +y,1); putpixel(xctr -x,yctr +y,1); putpixel(xctr +x,yctr -y,1);

putpixel(xctr -x,yctr -y,1); putpixel(xctr +y,yctr +x,1); putpixel(xctr -y,yctr +x,1); putpixel(xctr +y,yctr -x,1); putpixel(xctr -y,yctr -x,1); getch(); }

Experiment No 10 Aim: Write a program to implement reflection Coding: #include<iostream.h> #include<conio.h> #include<graphics.h> #include<stdlib.h> #include<math.h> #include<process.h> void main() {int ch1; int gd=DETECT,gm,n,a[3][3],b[3][10],i,x[10],y[10],c[3][10],j,k; initgraph(&gd,&gm,"c:\\tc\\bgi"); clrscr(); line(320,0,320,480); line(0,240,640,240); cout<<"\nenter the no. of vertices"; cin>>n; cout<<"\n1.about x-axis"; cout<<"\n2.about y-axis"; cin>>ch1; a[0][0]=1; a[0][1]=0; a[0][2]=0; a[1][0]=0; a[1][1]=-1; a[1][2]=0;

a[2][0]=0; a[2][1]=0; a[2][2]=1; cout<<"\nenter the co-ordinates"; for(i=0;i<n;i++) {cin>>x[i]; cin>>y[i];} for(i=0;i<n;i++) {b[0][i]=x[i]; b[1][i]=y[i]; b[2][i]=1;} for(i=0;i<n-1;i++) {line(b[0][i]+320,b[1][i]+240,b[0][i+1]+320,b[1][i+1]+240);} line(b[0][n-1]+320,b[1][n-1]+240,b[0][0]+320,b[1][0]+240); if(ch1==1) { for( i=0;i<3;i++) { for(j=0;j<n;j++) { c[i][j]=0; for(k=0;k<3;k++) { c[i][j]+=a[i][k]*b[k][j]; } } }

} if (ch1==2) {

a[0][0]=-1; a[1][1]=1; for( i=0;i<3;i++) { for(j=0;j<n;j++) { c[i][j]=0; for(k=0;k<3;k++) { c[i][j]+=a[i][k]*b[k][j]; } } } } for(i=0;i<n-1;i++) {line(c[0][i]+320,c[1][i]+240,c[0][i+1]+320,c[1][i+1]+240);} line(c[0][n-1]+320,c[1][n-1]+240,c[0][0]+320,c[1][0]+240); getch(); }

Experiment No 11 Aim: Write a program to implement shearing Coding: #include<iostream.h> #include<conio.h> #include<graphics.h> #include<stdlib.h> #include<math.h> #include<process.h> void main() {int ch1; int gd=DETECT,gm,n,a[3][3],b[3][10],i,x[10],y[10],c[3][10],j,k,a1,b1; initgraph(&gd,&gm,"c:\\tc\\bgi"); clrscr(); line(320,0,320,480); line(0,240,640,240); cout<<"\nenter the no. of vertices"; cin>>n; cout<<"\nenter the co-ordinates"; for(i=0;i<n;i++) {cin>>x[i]; cin>>y[i];} for(i=0;i<n;i++) {b[0][i]=x[i]; b[1][i]=y[i]; b[2][i]=1;} for(i=0;i<n-1;i++)

{line(b[0][i]+320,b[1][i]+240,b[0][i+1]+320,b[1][i+1]+240);} line(b[0][n-1]+320,b[1][n-1]+240,b[0][0]+320,b[1][0]+240); cout<<"\n1.about x-axis"; cout<<"\n2.about y-axis"; cout<<"\n3.about both axis"; cin>>ch1; a[0][0]=1; a[0][1]=a1; a[0][2]=0; a[1][0]=b1; a[1][1]=1; a[1][2]=0; a[2][0]=0; a[2][1]=0; a[2][2]=1; if (ch1==1) { cout<<"\nenter value of shear in x-direction"; cin>>a1; a[0][1]=a1; for( i=0;i<3;i++) { { for(j=0;j<n;j++) c[i][j]=0; for(k=0;k<3;k++) { c[i][j]+=a[i][k]*b[k][j]; } } } }

if (ch1==2) { cout<<"\nenter shear in y-direction"; cin>>b1; a[1][0]=b1; for( i=0;i<3;i++) { { for(j=0;j<n;j++) c[i][j]=0; for(k=0;k<3;k++) { } } } if (ch1==3) { cout<<"\nenter shear in both-direction"; cin>>a1>>b1; a[0][1]=a1; a[1][0]=b1; for( i=0;i<3;i++) { for(j=0;j<n;j++) { c[i][j]=0; for(k=0;k<3;k++) { c[i][j]+=a[i][k]*b[k][j]; } } c[i][j]+=a[i][k]*b[k][j];

} } } for(j=0;j<n-1;j++) {

for(i=0;i<1;i++) {

line(c[i][j]+320,c[i+1][j]+240,c[i][j+1]+320,c[i+1][j+1]+240); } } line(c[0][n-1]+320,c[1][n-1]+240,c[0][0]+320,c[1][0]+240); getch();

Experiment No 12 Aim: Write a program to draw Bezier curve Coding: #include <stdio.h> #include <stdlib.h> #include <graphics.h> #include <math.h> void bezier (int x[4], int y[4]) { int gd = DETECT, gm; int i; double t; initgraph (&gd, &gm, "..\\bgi"); for (t = 0.0; t < 1.0; t += 0.0005) { double xt = pow (1-t, 3) * x[0] + 3 * t * pow (1-t, 2) * x[1] + 3 * pow (t, 2) * (1-t) * x[2] + pow (t, 3) * x[3]; double yt = pow (1-t, 3) * y[0] + 3 * t * pow (1-t, 2) * y[1] + 3 * pow (t, 2) * (1-t) * y[2] + pow (t, 3) * y[3]; putpixel (xt, yt, WHITE); } for (i=0; i<4; i++) putpixel (x[i], y[i], YELLOW); getch(); closegraph(); return; } void main() { int x[4], y[4]; int i; printf ("Enter the x- and y-coordinates of the four control points.\n"); for (i=0; i<4; i++) scanf ("%d%d", &x[i], &y[i]); bezier (x, y); }

Experiment No 13 Aim: Write a program to implement cohen Sutherland line clipping algorithm Coding: #include<stdio.h> #include<graphics.h> #include<conio.h> typedef unsigned int outcode; enum { TOP=0x1, BOTTOM=0x2, RIGHT=0x4, LEFT=0x8 }; void lineclip(x0,y0,x1,y1,xwmin,ywmin,xwmax,ywmax ) float x0,y0,x1,y1,xwmin,ywmin,xwmax,ywmax; { int gd,gm; outcode code0,code1,codeout; int accept = 0, done=0; code0 = calcode(x0,y0,xwmin,ywmin,xwmax,ywmax); code1 = calcode(x1,y1,xwmin,ywmin,xwmax,ywmax); do{ if(!(code0 | code1)) { accept =1 ; done =1; } else if(code0 & code1) done = 1; else { float x,y; codeout = code0 ? code0 : code1; if(codeout & TOP) { x = x0 + (x1-x0)*(ywmax-y0)/(y1-y0); y = ywmax; } else if( codeout & BOTTOM) { x = x0 + (x1-x0)*(ywmin-y0)/(y1-y0);

y = ywmin; } else if ( codeout & RIGHT) { y = y0+(y1-y0)*(xwmax-x0)/(x1-x0); x = xwmax; } else { y = y0 + (y1-y0)*(xwmin-x0)/(x1-x0); x = xwmin; } if( codeout == code0) { x0 = x; y0 = y; code0=calcode(x0,y0,xwmin,ywmin,xwmax,ywmax); } else { x1 = x; y1 = y; code1 = calcode(x1,y1,xwmin,ywmin,xwmax,ywmax); } } } while( done == 0); if(accept) line(x0,y0,x1,y1); rectangle(xwmin,ywmin,xwmax,ywmax); getch(); } int calcode (x,y,xwmin,ywmin,xwmax,ywmax) float x,y,xwmin,ywmin,xwmax,ywmax; { int code =0; if(y> ywmax) code |=TOP;

else if( y<ywmin) code |= BOTTOM; else if(x > xwmax) code |= RIGHT; else if ( x< xwmin) code |= LEFT; return(code); } main() { float x2,y2,x1,y1,xwmin,ywmin,xwmax,ywmax; int gd=DETECT,gm; clrscr(); initgraph(&gd,&gm,"e:\\tc\\bgi"); printf("\n\n\tEnter the co-ordinates of Line :"); printf("\n\n\tX1 Y1 : "); scanf("%f %f",&x1,&y1); printf("\n\n\tX2 Y2 : "); scanf("%f %f",&x2,&y2); printf("\n\tEnter the co_ordinates of window :\n "); printf("\n\txwmin , ywmin : "); scanf("%f %f",&xwmin,&ywmin); printf("\n\txwmax , ywmax : "); scanf("%f %f",&xwmax,&ywmax); clrscr(); line(x1,y1,x2,y2); rectangle(xwmin,ywmin,xwmax,ywmax); getch(); clrscr(); lineclip(x1,y1,x2,y2,xwmin,ywmin,xwmax,ywmax ); getch(); closegraph(); }

VIVA QUESTIONS
1. Define computer graphics and list out the types of computer graphics. 2. Write the important applications of computer graphics. 3. Differentiate between Raster and Vector graphics. 4. Differentiate between bitmap and pixmap. 5. Give any three components for generating the basic transformation matrix. 6. What is clipping and clip window. 7. Define window and viewport. 8. What is rotation? 9. Differentiate perspective and parallel projection. 10. Name any two three dimensional graphics package procedures related with suitable visible surface detection. 11. List out the any four input devices. 12. Define (i) aspect ratio (ii) Persistence (iii) Resolution 13. What is called inkjet printers? 14. Define plotters. 15. What is meant by refresh buffer and frame buffer? 16. Define window port and view port. 17. What is meant by clipping? What are all the types of clipping? 18. Define scaling. 19. What is meant by line attributes? 20. Define rotation. 21. What do you mean by emissive and non-emissive displays? 22. What is the difference between impact and non-impact printers? 23. What do you mean by jaggies?

24. What is scan line algorithm? 25. What is a Line cap? 26. What is antialiasing? 27. What is Transformation? 28. What is translation? 29. What is scaling? 30. What is shearing? 31. What is reflection? 32. List out the various Text clipping? 33. What are the steps involved in 3D transformation? 34. What do you mean by view plane? 35. What is view distance? 36. What is Projection reference point? 37. What are the different types of parallel projections? 38. What is orthographic parallel projection? 39. What is orthographic oblique projection? 40. What is view reference point?

REFERENCES
1. Foley et. al., Computer Graphics Principles & practice, Addison Wesley, 1999. 2. David F. Rogers, Procedural Elements for Computer Graphics, McGraw Hill Book Company, 1985. 3. D. Rogers and J. Adams, Mathematical Elements for Computer Graphics, MacGraw-Hill International Edition, 1989. 4. D. Hearn and P. Baker, Computer Graphics, Prentice Hall, 1986. 5. R. Plastock and G. Kalley, Theory and Problems of Computer Graphics, Schaums Series, McGraw Hill, 1986.

Potrebbero piacerti anche