31
COMPUTER GRAPHICS Lab Manual By- Raj Ankur Singh

Computer Graphics Practical Manual

Embed Size (px)

Citation preview

Page 1: Computer Graphics Practical Manual

COMPUTER GRAPHICS

Lab Manual

By-

Raj Ankur Singh

Page 2: Computer Graphics Practical Manual

S.No. Practical Date

Page 3: Computer Graphics Practical Manual

PRACTICAL 1Write a program in C to implement DDA Algorithm.

#include<stdio.h>#include<conio.h>#include<graphics.h>#include<dos.h>#include<math.h>main(){

float x,y,x1,y1,x2,y2,dx,dy,length;int gm,gd=DETECT,i;printf("enter the value of x1:\t");scanf("%f",&x1);printf("enter the value of y1:\t");scanf("%f",&y1);printf("enter the value of x2:\t");scanf("%f",&x2);printf("enter the value of y2:\t");scanf("%f",&y2);detectgraph(&gd,&gm);initgraph(&gd,&gm,"c:\\tc\\bgi");dx=abs(x2-x1);dy=abs(y2-y1);if(dx>=dy){

length=dx;}else{

length=dy;}

Page 4: Computer Graphics Practical Manual

dx=(x2-x1)/length;dy=(y2-y1)/length;x=x1+0.5;y=y1+0.5;i=1;while(i<=length){

putpixel(x,y,15);x=x+dx;y=y+dy;i=i+1;delay(100);

}getch();closegraph();

}

Page 5: Computer Graphics Practical Manual

Output

Page 6: Computer Graphics Practical Manual

PRACTICAL 2Write a program in C to implement Bresenham’s Line Drawing Algorithm.

#include<conio.h>#include<stdio.h>#include<graphics.h>#include<dos.h>#include<math.h>main(){

clrscr();float x1,y1,x2,y2,dx,dy,p;int gm,gd=DETECT,i=1;printf("Enter The Value of x1:");scanf("%f",&x1);printf("Enter The Value of y1: ");scanf("%f",&y1);printf("Enter The Value of x2: ");scanf("%f",&x2);printf("Enter The Value of y2: ");scanf("%f",&y2);detectgraph(&gd,&gm);initgraph(&gd,&gm,"c:\\tc\\bgi");dx=abs(x2-x1);dy=abs(y2-y1);p=((2*dy)-dx);while(i<=dx){

putpixel(x1,x2,15);if(p<0){

++x1;

Page 7: Computer Graphics Practical Manual

p=p+(2*dy);}else{

++x1;++y1;p=p+(2*dy)-(2*dx);

}

i=i+1;delay(100);

}getch();closegraph();

}

Page 8: Computer Graphics Practical Manual

Output

Page 9: Computer Graphics Practical Manual

PRACTICAL 3Write a program in C to implement Mid-Point Circle Algorithm.#include<conio.h>#include<stdio.h>#include<graphics.h>#include<dos.h>#include<math.h>main(){ float x,y,r,p;

int gm,gd=DETECT;printf("Enter the radius: ");scanf("%f",&r);x=0;y=r;detectgraph(&gd,&gm);initgraph(&gd,&gm,"c:\\tc\\bgi");p=1-r;do{

putpixel(200+x,200+y,15);putpixel(200+y,200+x,15);putpixel(200+x,200-y,15);putpixel(200+y,200-x,15);putpixel(200-x,200-y,15);putpixel(200-y,200-x,15);putpixel(200-x,200+y,15);putpixel(200-y,200+x,15);

if(p<0){

++x;p=p+(2*x)+1;

Page 10: Computer Graphics Practical Manual

}else{

++x;--y;p=p+(2*x)-(2*y)+1;

}delay(100);

}while(x<y);getch();closegraph();}

Page 11: Computer Graphics Practical Manual

Output

Page 12: Computer Graphics Practical Manual

PRACTICAL 4

Write a program in C to implement Bresenham’s Line Drawing Algorithm to rasterize the line.

#include<conio.h>#include<stdio.h>#include<graphics.h>#include<dos.h>#include<math.h>main(){

clrscr();float x1,y1,x2,y2,x,y,dx,dy,e;int gm,gd=DETECT,i=1;printf("Enter The Value of x1:");scanf("%f",&x1);printf("Enter The Value of y1: ");scanf("%f",&y1);printf("Enter The Value of x2: ");scanf("%f",&x2);printf("Enter The Value of y2: ");scanf("%f",&y2);detectgraph(&gd,&gm);initgraph(&gd,&gm,"c:\\tc\\bgi");dx=abs(x2-x1);dy=abs(y2-y1);e=((2*dy)-dx);x=x1;y=y1;while(i<=dx)

Page 13: Computer Graphics Practical Manual

{putpixel(x,y,15);while(e>=0){

++y;e=e-(2*dx);

}++x;e=e+(2*dy);i=i+1;}getch();closegraph();

}

Page 14: Computer Graphics Practical Manual

Output

Page 15: Computer Graphics Practical Manual

PRACTICAL 5

Write a program in C to implement Mid-Point Ellipse Algorithm.#include<conio.h>#include<stdio.h>#include<dos.h>#include<graphics.h>void main(){ int gm,gd=DETECT;

int Rx,Ry,P10,x,y,a,b,P20;printf("Enter Value of Rx and Ry: ");scanf("%d %d",&Rx,&Ry);x=0;y=Ry;detectgraph(&gd,&gm);initgraph(&gd,&gm,"c:\\tc\\bgi");P10=(Ry*Ry)-(Rx*Rx)-(Rx*Rx*Ry)+(0.25*Rx*Rx);do{putpixel(200+x,200+y,15);putpixel(200+x,200-y,15);putpixel(200-x,200+y,15);putpixel(200-x,200-y,15);a=(2*Ry*Ry*x)+(2*Ry*Ry);b=(2*Rx*Rx*y)+(2*Rx*Rx);if(P10<0){

x++;P10=P10+a+(Ry*Ry);

}else{

Page 16: Computer Graphics Practical Manual

x++;y--;P10=P10+a+(Ry*Ry)-b;

}delay(100);}while(a>b);P20=((Ry*Ry)*((x+0.5)*(x+0.5)))+((Rx*Rx)*((y-1)*(y-1)))-((Rx*Rx)*(Ry*Ry));a=(2*Ry*Ry*x)+(2*Ry*Ry);b=(2*Rx*Rx*y)+(2*Rx*Rx);do{putpixel(200+x,200+y,15);putpixel(200+x,200-y,15);putpixel(200-x,200+y,15);putpixel(200-x,200-y,15);a=(2*Ry*Ry*x)+(2*Ry*Ry);b=(2*Rx*Rx*y)+(2*Rx*Rx);if(P20>0){

y--;P20=P20-b+(Rx*Rx);

}else{

x++;y--;P20=P20-b+a+(Rx*Rx);

}delay(100);}while(y>0);getch();closegraph();

}

Page 17: Computer Graphics Practical Manual

Output

Page 18: Computer Graphics Practical Manual

PRACTICAL 6Write a program in C to perform translation on a triangle.#include<stdio.h>#include<conio.h>#include<graphics.h>#include<math.h>#include<process.h>void main(){

int gd=DETECT, gm,ch;int x1,x2,x3,y1,y2,y3,tx,ty;printf("Enter coordinates of the triangle: ");scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3);initgraph(&gd,&gm,"c:\\tc\\bgi");line(x1,y1,x2,y2);line(x2,y2,x3,y3);line(x3,y3,x1,y1);printf("Enter the translation factors: ");scanf("%d%d",&tx,&ty);x1=x1+tx;y1=y1+ty;x2=x2+tx;y2=y2+ty;x3=x3+tx;y3=y3+ty;line(x1,y1,x2,y2);line(x2,y2,x3,y3);line(x3,y3,x1,y1);

getche(); closegraph();}

Page 19: Computer Graphics Practical Manual

Output

Enter the translation factors: 45 78

Page 20: Computer Graphics Practical Manual

PRACTICAL 7Write a program in C to perform rotation on a triangle.#include<stdio.h>#include<conio.h>#include<graphics.h>#include<math.h>#include<process.h>void main(){ int gd=DETECT, gm,ch;

int x1,x2,x3,y1,y2,y3,tx,ty;float rangle;printf("Enter coordinates of the triangle: ");scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3);initgraph(&gd,&gm,"c:\\tc\\bgi");line(x1,y1,x2,y2);line(x2,y2,x3,y3);line(x3,y3,x1,y1);printf("Enter the rotation angle:");scanf("%f",&rangle);rangle=(rangle*3.14)/180;x1=x1*cos(rangle)-y1*sin(rangle);y1=x1*sin(rangle)+y1*cos(rangle);x2=x2*cos(rangle)-y2*sin(rangle);y2=x2*sin(rangle)+y2*cos(rangle);x3=x3*cos(rangle)-y3*sin(rangle);y3=x3*sin(rangle)+y3*cos(rangle);line(x1,y1,x2,y2);line(x2,y2,x3,y3);line(x3,y3,x1,y1);getche();closegraph();

}

Page 21: Computer Graphics Practical Manual

Output

Enter the rotation angle: 10

Page 22: Computer Graphics Practical Manual

PRACTICAL 8Write a program in C to perform scaling on a triangle.

#include<stdio.h>#include<conio.h>#include<graphics.h>#include<math.h>#include<process.h>void main(){

int gd=DETECT, gm;int x1,x2,x3,y1,y2,y3,sx,sy;printf("Enter coordinates of the triangle: ");scanf("%d %d %d %d %d %d",&x1,&y1,&x2,&y2,&x3,&y3);initgraph(&gd,&gm,"c:\\tc\\bgi");line(x1,y1,x2,y2);line(x2,y2,x3,y3);line(x3,y3,x1,y1);printf("Enter the scaling factors: ");scanf("%d %d",&sx,&sy);x1=x1*sx;y1=y1*sy;x2=x2*sx;y2=y2*sy;x3=x3*sx;y3=y3*sy;line(x1,y1,x2,y2);line(x2,y2,x3,y3);line(x3,y3,x1,y1);getch();closegraph();

}

Page 23: Computer Graphics Practical Manual

Output

Enter the scaling factors: 2 2

Page 24: Computer Graphics Practical Manual

PRACTICAL 9Write a program in C to perform Reflection on a triangle.#include<stdio.h>#include<conio.h>#include<graphics.h>#include<math.h>#include<process.h>void main(){

int gd=DETECT, gm,ch;int x1,x2,x3,y1,y2,y3,x11,x22,x33,y11,y22,y33;float angle;printf("Enter coordinates of the triangle: ");scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3);initgraph(&gd,&gm,"c:\\tc\\bgi");line(x1,y1,x2,y2);line(x2,y2,x3,y3);line(x3,y3,x1,y1);printf("Enter the axis of reflection: ");printf("\n1:about x-axis");printf("\n2:about y-axis\n");scanf("%d",&ch);cleardevice();line(x1,y1,x2,y2);line(x2,y2,x3,y3);line(x3,y3,x1,y1);if(ch==1){

x11=x1;y11=180-y1;x22=x2;y22=180-y2;

Page 25: Computer Graphics Practical Manual

x33=x3;y33=180-y3;

}if(ch==2){

x11=180-x1;y11=y1;x22=180-x2;y22=y2;x33=180-x3;y33=y3;

}gotoxy(1,24);line(x11,y11,x22,y22);line(x22,y22,x33,y33);line(x33,y33,x11,y11);getche();closegraph();

}

Page 26: Computer Graphics Practical Manual

Output

Enter the axis of reflection:1: about x-axis2: about y-axis1

Page 27: Computer Graphics Practical Manual

Enter the axis of reflection:1: about x-axis2: about y-axis2