graphics programs developed by sendash pangambam

24
1 ASSIGNMENT SUBMITTED BY: PANGAMBAM SENDASH SINGH-13486SC019 PROGRAM NO. 1: DDA LINE DRAWING ALGORITHM // C IMPLEMENTATION OF DDA LINE DRAWING ALGORITHM #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> int sign(float k) { if(k<0) return -1; //sign(x)={-1,0,1} for {x<0,x=0,x>0} else if(k>0) return 1; else return 0; } void main() { int gd,gm; int x1,y1,x2,y2,i; float dx,dy,len,x,y; printf("\nEnter the value of x1,y1 "); scanf("%d%d",&x1,&y1); printf("\nEnter the value of x2,y2 "); scanf("%d%d",&x2,&y2); detectgraph(&gd,&gm); initgraph(&gd,&gm,""); if(abs(x2-x1)>=abs(y2-y1)) len=abs(x2-x1); else len=abs(y2-y1); dx=(x2-x1)/len; dy=(y2-y1)/len; x=x1+0.5*sign(dx); y=y1+0.5*sign(dy); i=1; //MY NEW CO-ORDINATE SYSTEM CENTRED AT (320,240) line(0,240,650,240); line(320,0,320,490); while(i<=len) { delay(50); putpixel(ceil(x+320),ceil(240-y),RED); //PIXEL VALUE IS ALWAYS INTEGER SO CEIL() IS USED x=x+dx; y=y+dy; i=i+1; } getch(); closegraph(); }

Upload: sendash-pangambam

Post on 24-Dec-2014

274 views

Category:

Education


2 download

DESCRIPTION

These are some of the graphics program that I developed during my M.Sc. 2nd Semester. PROGRAM NO. 1: DDA LINE DRAWING ALGORITHM PROGRAM NO. 2: BRESENHAM’S LINE DRAWING ALGORITHM PROGRAM NO. 3: BRESENHAM’S CIRCLE DRAWING ALGORITHM PROGRAM NO. 4: TRANSLATION PROGRAM NO. 5: ROTATION PROGRAM NO. 6: REFLECTION PROGRAM NO. 7: SCALING PROGRAM NO. 8: SHEARING PROGRAM NO. 9: CLIPPING PROGRAM NO. 10: BEZIER CURVE

TRANSCRIPT

Page 1: Graphics Programs Developed By Sendash Pangambam

1

ASSIGNMENT SUBMITTED BY: PANGAMBAM SENDASH SINGH-13486SC019

PROGRAM NO. 1: DDA LINE DRAWING ALGORITHM // C IMPLEMENTATION OF DDA LINE DRAWING ALGORITHM #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> int sign(float k) { if(k<0) return -1; //sign(x)={-1,0,1} for {x<0,x=0,x>0} else if(k>0) return 1; else return 0; } void main() { int gd,gm; int x1,y1,x2,y2,i; float dx,dy,len,x,y; printf("\nEnter the value of x1,y1 "); scanf("%d%d",&x1,&y1); printf("\nEnter the value of x2,y2 "); scanf("%d%d",&x2,&y2); detectgraph(&gd,&gm); initgraph(&gd,&gm,""); if(abs(x2-x1)>=abs(y2-y1)) len=abs(x2-x1); else len=abs(y2-y1); dx=(x2-x1)/len; dy=(y2-y1)/len; x=x1+0.5*sign(dx); y=y1+0.5*sign(dy); i=1; //MY NEW CO-ORDINATE SYSTEM CENTRED AT (320,240) line(0,240,650,240); line(320,0,320,490); while(i<=len) { delay(50); putpixel(ceil(x+320),ceil(240-y),RED);

//PIXEL VALUE IS ALWAYS INTEGER SO CEIL() IS USED x=x+dx; y=y+dy; i=i+1; } getch(); closegraph(); }

Page 2: Graphics Programs Developed By Sendash Pangambam

2

ASSIGNMENT SUBMITTED BY: PANGAMBAM SENDASH SINGH-13486SC019

Output:

PROGRAM NO. 2: BRESENHAM’S LINE DRAWING ALGORITHM /*C IMPLEMENTATION OF BRESENHAM'S LINE DRAWING*/ #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> int sign(int k) { if(k<0) return -1; //sign(x)={-1,0,1} for {x<0,x=0,x>0} else if(k>0) return 1; else return 0; } void main() { int gd,gm; int x,y,x1,y1,x2,y2,s1,s2,i,temp,flag,error; int dx,dy,len; clrscr(); printf("\nEnter the value of x1,y1 "); scanf("%d%d",&x1,&y1); printf("\nEnter the value of x2,y2 "); scanf("%d%d",&x2,&y2); detectgraph(&gd,&gm); initgraph(&gd,&gm,"");

Page 3: Graphics Programs Developed By Sendash Pangambam

3

ASSIGNMENT SUBMITTED BY: PANGAMBAM SENDASH SINGH-13486SC019

//MY NEW CO-ORDINATE SYSTEM CENTRED AT (320,240) line(0,240,650,240); line(320,0,320,490); x=x1; y=y1; dx=abs(x2-x1); dy=abs(y2-y1); s1=sign(x2-x1); s2=sign(y2-y1); if(dy>dx) { temp=dx; dx=dy; dy=temp; flag=1; } else flag=0; error=2*dy-dx; for(i=1;i<=dx;i++) { putpixel(x+320,240-y,RED); while(error>=0) { if(flag==1) x=x+s1; else y=y+s2; error=error-2*dx; } if(flag==1) y=y+s2; else x=x+s1; error=error+2*dy; delay(30); } getch(); closegraph(); }

Output:

Page 4: Graphics Programs Developed By Sendash Pangambam

4

ASSIGNMENT SUBMITTED BY: PANGAMBAM SENDASH SINGH-13486SC019

PROGRAM NO. 3: BRESENHAM’S CIRCLE DRAWING ALGORITHM //C implementation of Bresenhem's Circle Drawing Algorithm #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> void brescircle(int xc,int yc,int x,int y) //FUNCTION TO DRAW ARCS THROUGH THE EIGHT OCTANTS { putpixel(320+x+xc,240+y-yc,1); putpixel(320+y+xc,240+x-yc,2); //320+ and 240- putpixel(320+y+xc,240-x-yc,3); //MY NEW CO-0ORDINATE CENTRED AT (320,240) putpixel(320+x+xc,240-y-yc,4); //so x->320=x and y->240-y putpixel(320-x+xc,240-y-yc,5); putpixel(320-y+xc,240-x-yc,6); putpixel(320-y+xc,240+x-yc,7); putpixel(320-x+xc,240+y-yc,8); delay(50); } void main() { int gd,gm; int x,y,xc,yc,d,r; printf("\nEnter the co-ordinates(Xc,Yc) of the centre: "); scanf("%d%d",&xc,&yc); printf("\nEnter the radius(r) of the circle: "); scanf("%d",&r); x=0; y=r; detectgraph(&gd,&gm); initgraph(&gd,&gm,""); printf("\nBRESENHEM'S CIRCLE DRAWING\n\nCentre=(%d,%d)\nRadius=%d",xc,yc,r); //MY NEW CO-ORDINATE SYSTEM CENTRED AT (320,240) line(0,240,650,240); line(320,0,320,490); d=3-2*r; while(x<y) {

Page 5: Graphics Programs Developed By Sendash Pangambam

5

ASSIGNMENT SUBMITTED BY: PANGAMBAM SENDASH SINGH-13486SC019

if(d<0) { d=d+4*x+6; x++; } else { d=d+4*x-4*y+10; x++; y--; } putpixel(320+xc,240-yc,WHITE); //Centre is plotted brescircle(xc,yc,x,y); } getch(); closegraph(); }

Output:

Page 6: Graphics Programs Developed By Sendash Pangambam

6

ASSIGNMENT SUBMITTED BY: PANGAMBAM SENDASH SINGH-13486SC019

PROGRAM NO. 4: TRANSLATION //TRANSLATION OF AN OBJECT USING HOMOGENEOUS CO-ORDINATE TRANSFORMATION #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> void draw(int X[10][10],int n,int col) //FUNCTION TO DRAW OBJECT { int i; setcolor(col); for(i=0;i<n-1;i++) line(X[i][0]+320,240-X[i][1],X[i+1][0]+320,240-X[i+1][1]); //+320 and -240: to transform to my new co-ordinate axes line(X[n-1][0]+320,240-X[n-1][1],X[0][0]+320,240-X[0][1]); } void main() { int gd,gm; int i,j,k,n; float sum; int X[10][10]; int Tx,Ty; float trans[3][2]; int newX[10][10]; clrscr(); printf("\nEnter the no. of vertices of your object:"); scanf("%d",&n); printf("\nEnter the co-ordinate of %d vertices of your object:\n",n); for(i=0;i<n;i++) { scanf("%d%d",&X[i][0],&X[i][1]); X[i][2]=1; //3rd element of row vector is always 1 } printf("\nEnter the translation values Tx and Ty: "); scanf("%d%d",&Tx,&Ty); //TRANSFORMATION MATRIX(THIRD COLUMN ELEMENTS HAVE NOTHING TO DO DURING COMPUTATION, SO THEY ARE NEGLECTED) trans[0][0]=1; trans[0][1]=0; trans[1][0]=0; trans[1][1]=1; trans[2][0]=Tx; trans[2][1]=Ty; detectgraph(&gd,&gm); initgraph(&gd,&gm,""); //DEFINE A NEW CO-ORDINATE SYSTEM WITH ORIGIN AT (320,240) line(0,240,640,240); line(320,0,320,480); //CREATING OBJECT printf("THIS IS THE CREATED OBJECT\n"); //OBJECT IS CREATED HERE draw(X,n,10); //TRANSFORMATION OF THE VERTICES OF THE OBJECT USING HOMOGENEOUS TRANSFORMATION for(i=0;i<n;i++) { for(j=0;j<2;j++) {

Page 7: Graphics Programs Developed By Sendash Pangambam

7

ASSIGNMENT SUBMITTED BY: PANGAMBAM SENDASH SINGH-13486SC019

sum=0.0; for(k=0;k<=2;k++) sum=sum+X[i][k]*trans[k][j]; newX[i][j]=ceil(sum); //why ceil() ?? } //it has to be converted to integer because pixel value takes only integer values } //OBJECT AFTER TRANSLATION delay(2000); printf("YOUR OBJECT AFTER TRANSLATION:"); draw(newX,n,5); getch(); closegraph(); }

Output:

Page 8: Graphics Programs Developed By Sendash Pangambam

8

ASSIGNMENT SUBMITTED BY: PANGAMBAM SENDASH SINGH-13486SC019

PROGRAM NO. 5: ROTATION //+VE(COUNTER-CLOCKWISE) ROTATION OF AN OBJECT ABOUT AN ARBITRARY POINT USING HOMOGENEOUS CO-ORDINATE TRANSFORMATION #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> void draw(int X[10][10],int n,int col) //FUNCTION TO DRAW OBJECT { int i; setcolor(col); for(i=0;i<n-1;i++) line(X[i][0]+320,240-X[i][1],X[i+1][0]+320,240-X[i+1][1]); //+320 and -240: to transform to my new co-ordinate axes line(X[n-1][0]+320,240-X[n-1][1],X[0][0]+320,240-X[0][1]); } void main() { int gd,gm; int i,j,k,n;

Page 9: Graphics Programs Developed By Sendash Pangambam

9

ASSIGNMENT SUBMITTED BY: PANGAMBAM SENDASH SINGH-13486SC019

float sum; float theta; int X[10][10]; int xc,yc; float trans[3][2]; int newX[10][10]; clrscr(); printf("\nEnter the no. of vertices of your object:"); scanf("%d",&n); printf("\nEnter the co-ordinate of %d vertices of your object:\n",n); for(i=0;i<n;i++) { scanf("%d%d",&X[i][0],&X[i][1]); X[i][2]=1; //3rd element of row vector is always 1 } printf("\nEnter the point (Xc,Yc) about which the object is to be rotated: "); scanf("%d%d",&xc,&yc); printf("\nEnter the rotation angle in degree: "); scanf("%f",&theta); theta=0.017453*theta; //conversion of degree to radian //TRANSFORMATION MATRIX(THIRD COLUMN ELEMENTS HAVE NOTHING TO DO DURING COMPUTATION, SO THEY ARE NEGLECTED) trans[0][0]=cos(theta); trans[0][1]=sin(theta); trans[1][0]=-(sin(theta)); trans[1][1]=cos(theta); trans[2][0]=xc+(yc*sin(theta))-(xc*cos(theta));

trans[2][1]=yc-(yc*cos(theta))-(xc*sin(theta)); detectgraph(&gd,&gm); initgraph(&gd,&gm,""); //DEFINE A NEW CO-ORDINATE SYSTEM WITH ORIGIN AT (320,240) line(0,240,640,240); line(320,0,320,480); //CREATING OBJECT printf("THIS IS THE CREATED OBJECT\n"); //OBJECT IS CREATED HERE draw(X,n,10); //TRANSFORMATION OF THE VERTICES OF THE OBJECT USING HOMOGENEOUS TRANSFORMATION for(i=0;i<n;i++) { for(j=0;j<2;j++) { sum=0.0; for(k=0;k<=2;k++) sum=sum+X[i][k]*trans[k][j]; newX[i][j]=ceil(sum); //why ceil() ?? } //it has to be converted to integer because pixel value takes only integer values } //OBJECT AFTER ROTATION delay(2000); printf("YOUR OBJECT AFTER ROTATION:"); putpixel(xc+320,240-yc,RED); //Point about which rotation takes place draw(newX,n,5); getch(); closegraph(); }

Page 10: Graphics Programs Developed By Sendash Pangambam

10

ASSIGNMENT SUBMITTED BY: PANGAMBAM SENDASH SINGH-13486SC019

Page 11: Graphics Programs Developed By Sendash Pangambam

11

ASSIGNMENT SUBMITTED BY: PANGAMBAM SENDASH SINGH-13486SC019

PROGRAM NO. 6: REFLECTION //REFLECTION OF AN OBJECT ABOUT VARIOUS LINES //ABOUT X-AXIS,Y-AXIS AND THE LINE Y=mX+C //USING HOMOGENEOUS CO-ORDINATE TRANSFORMATION #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> void draw(int X[10][10],int n,int col) //FUNCTION TO DRAW THE OBJECT { int i; setcolor(col); for(i=0;i<n-1;i++) line(X[i][0]+320,240-X[i][1],X[i+1][0]+320,240-X[i+1][1]); //+320 and -240: to transform to my new co-ordinate axes line(X[n-1][0]+320,240-X[n-1][1],X[0][0]+320,240-X[0][1]); } void main() { int gd,gm; int i,j,k,n,choice; float sum; float m,c,m2; int X[10][10]; float trans[3][2]; int newX[10][10]; clrscr(); printf("\nEnter the no. of vertices of your object:"); scanf("%d",&n); printf("\nEnter the co-ordinate of %d vertices of your object: ",n); for(i=0;i<n;i++)

Page 12: Graphics Programs Developed By Sendash Pangambam

12

ASSIGNMENT SUBMITTED BY: PANGAMBAM SENDASH SINGH-13486SC019

{ scanf("%d%d",&X[i][0],&X[i][1]); X[i][2]=1; //3rd element of row vector is always 1 } while(1) { printf("\nEnter your reflection axis (1,2,3)\n\t1:Reflection along X-axis\n\t2:Reflection along Y-axis\n\t3:Reflection along the line Y=mX+C\n\tAny other key to exit\n\t"); scanf("%d",&choice); switch(choice) /*TRANSFORMATION MATRICES*/ { case 1: /*ALONG X-AXIS*/ { trans[0][0]=1.0; trans[0][1]=0.0; trans[1][0]=0.0; trans[1][1]=-1.0; trans[2][0]=0.0; trans[2][1]=0.0; break; } case 2: /*ALONG Y-AXIS*/ { trans[0][0]=-1.0; trans[0][1]=0.0; trans[1][0]=0.0; trans[1][1]=1.0; trans[2][0]=0.0; trans[2][1]=0.0; break; } case 3: /*ALONG THE LINE Y=mX+C*/ { printf("\nEnter the values of m and C respectively: "); scanf("%f%f",&m,&c); m2=m*m; trans[0][0]=(1-m2)/(1+m2); trans[0][1]=(2*m)/(1+m2); trans[1][0]=(2*m)/(1+m2);; trans[1][1]=(m2-1)/(1+m2); trans[2][0]=-(2*m*c)/(1+m2); trans[2][1]=(2*c)/(1+m2); break; } default: { exit(); } } detectgraph(&gd,&gm); initgraph(&gd,&gm,""); //DEFINE A NEW CO-ORDINATE SYSTEM WITH ORIGIN AT (320,240) line(0,240,640,240); line(320,0,320,480); if(choice==3) { setcolor(BLUE); line(120,200*m+240-c,520,240-(c+200*m)); } //CREATING OBJECT printf("THIS IS THE CREATED OBJECT\n"); //OBJECT IS CREATED HERE draw(X,n,10);

Page 13: Graphics Programs Developed By Sendash Pangambam

13

ASSIGNMENT SUBMITTED BY: PANGAMBAM SENDASH SINGH-13486SC019

//TRANSFORMATION OF THE VERTICES OF THE OBJECT USING HOMOGENEOUS TRANSFORMATION for(i=0;i<n;i++) { for(j=0;j<2;j++) { sum=0.0; for(k=0;k<=2;k++) sum=sum+X[i][k]*trans[k][j]; newX[i][j]=ceil(sum); //why ceil() ?? } //it has to be converted to integer because pixel value takes only integer values } //OBJECT AFTER REFLECTION delay(2000); printf("YOUR OBJECT AFTER REFLECTION:"); draw(newX,n,5); getch(); closegraph(); } }

Page 14: Graphics Programs Developed By Sendash Pangambam

14

ASSIGNMENT SUBMITTED BY: PANGAMBAM SENDASH SINGH-13486SC019

Page 15: Graphics Programs Developed By Sendash Pangambam

15

ASSIGNMENT SUBMITTED BY: PANGAMBAM SENDASH SINGH-13486SC019

PROGRAM NO. 7: SCALING //SCALING AN OBJECT USING HOMOGENEOUS CO-ORDINATE TRANSFORMATION #include<stdio.h> #include<conio.h> #include<graphics.h> void draw(int X[10][10],int n,int col) //FUNCTION TO DRAW OBJECT { int i; setcolor(col); for(i=0;i<n-1;i++) line(X[i][0]+320,240-X[i][1],X[i+1][0]+320,240-X[i+1][1]); //+320 and -240: to transform to my new co-ordinate axes line(X[n-1][0]+320,240-X[n-1][1],X[0][0]+320,240-X[0][1]); } void main() { int gd,gm; int i,j,k,n; int sum; int X[10][10],trans[3][2],newX[10][10]; int Sx,Sy; clrscr(); printf("\nEnter the no. of vertices of your object:"); scanf("%d",&n); printf("\nEnter the co-ordinate of %d vertices of your object:\n",n); for(i=0;i<n;i++) { scanf("%d%d",&X[i][0],&X[i][1]); X[i][2]=1; //3rd element of row vector is always 1 } printf("\nEnter the scale factors Sx and Sy: "); scanf("%d%d",&Sx,&Sy); //TRANSFORMATION MATRIX(THIRD COLUMN ELEMENTS HAVE NOTHING TO DO DURING COMPUTATION, SO THEY ARE NEGLECTED) trans[0][0]=Sx; trans[0][1]=0; trans[1][0]=0; trans[1][1]=Sy; trans[2][0]=0; trans[2][1]=0; detectgraph(&gd,&gm); initgraph(&gd,&gm,""); //DEFINE A NEW CO-ORDINATE SYSTEM WITH ORIGIN AT (320,240) line(0,240,640,240); line(320,0,320,480); //CREATING OBJECT printf("THIS IS THE CREATED OBJECT\n"); //OBJECT IS CREATED HERE draw(X,n,10); //TRANSFORMATION OF THE VERTICES OF THE OBJECT USING HOMOGENEOUS TRANSFORMATION for(i=0;i<n;i++) { for(j=0;j<2;j++) { sum=0; for(k=0;k<=2;k++) sum=sum+X[i][k]*trans[k][j];

Page 16: Graphics Programs Developed By Sendash Pangambam

16

ASSIGNMENT SUBMITTED BY: PANGAMBAM SENDASH SINGH-13486SC019

newX[i][j]=sum; //why ceil() ?? } //it has to be converted to integer because pixel value takes only integer values } //OBJECT AFTER ROTATION delay(2000); printf("YOUR OBJECT AFTER SCALING:"); draw(newX,n,5); getch(); closegraph(); }

PROGRAM NO. 8: SHEARING //SHEARING AN OBJECT USING HOMOGENEOUS CO-ORDINATE TRANSFORMATION #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> void draw(int X[10][10],int n,int col) //FUNCTION TO DRAW OBJECT {

Page 17: Graphics Programs Developed By Sendash Pangambam

17

ASSIGNMENT SUBMITTED BY: PANGAMBAM SENDASH SINGH-13486SC019

int i; setcolor(col); for(i=0;i<n-1;i++) line(X[i][0]+320,240-X[i][1],X[i+1][0]+320,240-X[i+1][1]); //+320 and -240: to transform to my new co-ordinate axes line(X[n-1][0]+320,240-X[n-1][1],X[0][0]+320,240-X[0][1]); } void main() { int gd,gm; int i,j,k,n; int sum; int choice; int X[10][10],trans[3][2],newX[10][10]; clrscr(); printf("\nEnter the no. of vertices of your object:"); scanf("%d",&n); printf("\nEnter the co-ordinate of %d vertices of your object:\n",n); for(i=0;i<n;i++) { scanf("%d%d",&X[i][0],&X[i][1]); X[i][2]=1; //3rd element of row vector is always 1 } printf("\nEnter your choice:\n\t1: Shearing along X-Direction\n\t2: Shearing along Y-Direction\n\t3: Shearing along both X and Y Directions\t"); scanf("%d",&choice); if((choice!=1)&&(choice!=2)&&(choice!=3)) { printf("\nCHOICE VALUE ERROR!! TRY AGAIN LATER!!"); getch(); exit(); } //TRANSFORMATION MATRIX trans[0][0]=1; if(choice==1) //Shearing along X { printf("\nEnter the shearing factor (S): "); scanf("%d",&trans[1][0]); trans[0][1]=0; } if(choice==2) //Shearing along Y { printf("\nEnter the shearing factor (S): "); scanf("%d",&trans[0][1]); trans[1][0]=0; } if(choice==3) //Shearing along both X and Y directions { printf("\nEnter the shearing factors (Sx and Sy)"); scanf("%d%d",&trans[1][0],&trans[0][1]); } trans[1][1]=1; trans[2][0]=0; trans[2][1]=0; detectgraph(&gd,&gm); initgraph(&gd,&gm,""); //DEFINE A NEW CO-ORDINATE SYSTEM WITH ORIGIN AT (320,240)

Page 18: Graphics Programs Developed By Sendash Pangambam

18

ASSIGNMENT SUBMITTED BY: PANGAMBAM SENDASH SINGH-13486SC019

line(0,240,640,240); line(320,0,320,480); //CREATING OBJECT printf("THIS IS THE CREATED OBJECT\n"); //OBJECT IS CREATED HERE draw(X,n,10); //TRANSFORMATION OF THE VERTICES OF THE OBJECT USING HOMOGENEOUS TRANSFORMATION for(i=0;i<n;i++) { for(j=0;j<=2;j++) { sum=0; for(k=0;k<=2;k++) sum=sum+X[i][k]*trans[k][j]; newX[i][j]=sum; //why ceil() ?? } //it has to be converted to integer because pixel value takes only integer values } //OBJECT AFTER ROTATION delay(2000); printf("YOUR OBJECT AFTER SHEARING:"); draw(newX,n,5); getch(); closegraph(); }

Output:

Page 19: Graphics Programs Developed By Sendash Pangambam

19

ASSIGNMENT SUBMITTED BY: PANGAMBAM SENDASH SINGH-13486SC019

PROGRAM NO. 9: CLIPPING //C-IMPLEMENTATION FOR CLIPPING ALGORITHM #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 {

Page 20: Graphics Programs Developed By Sendash Pangambam

20

ASSIGNMENT SUBMITTED BY: PANGAMBAM SENDASH SINGH-13486SC019

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(320+x0,240-y0,320+x1,240-y1); rectangle(320+xwmin,240-ywmin,320+xwmax,240-ywmax); getch(); } int calcode (x,y,xwmin,ywmin,xwmax,ywmax) //REGION CODES 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;

Page 21: Graphics Programs Developed By Sendash Pangambam

21

ASSIGNMENT SUBMITTED BY: PANGAMBAM SENDASH SINGH-13486SC019

return(code); } void main() { float x2,y2,x1,y1,xwmin,ywmin,xwmax,ywmax; int gd,gm; clrscr(); printf("\nEnter 1st co-ordinate of the line X1 Y1 : "); scanf("%f%f",&x1,&y1); printf("\nEnter 2nd co-ordinate of the line X2 Y2 : "); scanf("%f%f",&x2,&y2); printf("\nEnter the co_ordinates(xwmin , ywmin) of window :"); scanf("%f%f",&xwmin,&ywmin); printf("\nEnter the co_ordinates(xwmax , ywmax) of window :"); scanf("%f%f",&xwmax,&ywmax); detectgraph(&gd,&gm); initgraph(&gd,&gm,""); printf("\nBEFORE CLIPPING"); //NEW CO-ORDINATE line(0,240,640,240); line(320,0,320,480); line(320+x1,240-y1,320+x2,240-y2); rectangle(320+xwmin,240-ywmin,320+xwmax,240-ywmax); getch(); cleardevice(); printf("\nAFTER CLIPPING"); //NEW CO-ORDINATE line(0,240,640,240); line(320,0,320,480); lineclip(x1,y1,x2,y2,xwmin,ywmin,xwmax,ywmax ); getch(); closegraph(); }

Output:

Page 22: Graphics Programs Developed By Sendash Pangambam

22

ASSIGNMENT SUBMITTED BY: PANGAMBAM SENDASH SINGH-13486SC019

Page 23: Graphics Programs Developed By Sendash Pangambam

23

ASSIGNMENT SUBMITTED BY: PANGAMBAM SENDASH SINGH-13486SC019

PROGRAM NO. 10: BEZIER CURVE //C-IMPLEMENTATION FOR DRAWING BEZIER CURVE #include<stdio.h> #include<conio.h> #include<math.h> #include<graphics.h> int fact(int x) //FUNCTION TO CALCULATE FACTORIAL { int i; int pro=1; if((x==0)||(x==1)) { return pro; } else { for(i=2;i<=x;i++) pro=pro*i; return pro; } } double Jnit(int n,double i,double t) //FUNCTION FOR BASIS FUNCTION J(n,i,t) { double ni; ni=fact(n)/(fact(i)*fact(n-i)); return ni*pow(t,i)*pow(1.00-t,n-i); } void main() { int n,i; int gd,gm; int x[10],y[10]; double xt,yt,sx,sy,t; clrscr(); printf("\nEnter the order of the curve: "); scanf("%d",&n); printf("\nEnter the X and Y co-ordinates of the %d control points: ",n+1); for(i=0;i<=n;i++) scanf("%d%d",&x[i],&y[i]); detectgraph(&gd,&gm); initgraph(&gd,&gm,""); //MY NEW CO-ORDINATE AXES line(0,240,650,240); line(320,0,320,490); for (i=0;i<n;i++) //CONTROL POLYGON IS DRAWN HERE { setcolor(RED); line(320+x[i],240-y[i],320+x[i+1],240-y[i+1]); } for(t=0.0;t<1.0;t+=0.0005) //t IS THE PARAMETER {

Page 24: Graphics Programs Developed By Sendash Pangambam

24

ASSIGNMENT SUBMITTED BY: PANGAMBAM SENDASH SINGH-13486SC019

xt=yt=0; for(i=0;i<=n;i++) //SUMMATION OF B[i]*J(n,i,t) IS DONE HERE { sx=x[i]*Jnit(n,i,t); sy=y[i]*Jnit(n,i,t); xt+=sx; yt+=sy; } putpixel(320+xt,240-yt,BLUE); //320+ and 240- Transforms x and y to my new co-ordinate delay(2); } getch(); closegraph(); }

Output: