83
SHRI ATMANAND JAIN INSTITUTE OF MANAGEMENT AND TECHNOLOGY PRACTICAL FILE OF PROGRAMMING IN C-GRAPHICS SUBMITTED BY Shubham Kanojia MCA 4th SEMESTER Roll No. 1511

C graphics programs file

Embed Size (px)

Citation preview

Page 1: C graphics programs file

SHRI ATMANAND JAIN INSTITUTE OF MANAGEMENT AND TECHNOLOGY

PRACTICAL FILE OF

PROGRAMMING IN C-GRAPHICS

SUBMITTED BYShubham Kanojia

MCA 4th SEMESTER

Roll No. 1511

HEAD OF DEPTT. SUBMITTED TO EXAMINER

Mrs. Rinkey Chaudhary Mrs. Rinkey Chaudhary

(HOD of, MCA) (HOD of, MCA)

Page 2: C graphics programs file

INDEX

Sr.No

PARTICULARS SIGNATURE

1. Program to draw a line using polynomial method

2. Program to draw a line using DDA algorithm

3. Program to draw a line using Bresenham’s Algorithm

4. Program to draw circle using Polynomial method

5. Program to draw circle using Bresenham’s Algorithm

6. Program to draw a circle using trigonometric method

7. Program to draw a circle using mid-point method

8. Program to draw a ellipse using polynomial method

9. Program to draw a ellipse using Bresenham’s Algorithm

10. Program to draw a ellipse using trigonometric method

11. Program to draw a ellipse using mid-point method

12. Program to implement 2-D Translation

13. Program to implement 2-D Rotation

14. Program to implement 2-D Scaling

15. Program to draw sine curve

16. Program to draw X-Y axis

17. Program to print HELLO using line function

18. Program to print random lines on screen

19. Program to find whether a point is inside or outside the polygon

20. Program to implement mapping from window to viewport

21. Program to plot an ARC using trigonometric method

22. Program to implement 2-D reflection

23. Program to implement 2-D shearing

24. Program to implement line clipping using Cohan Sutherland algorithm

25. Program to implement line clipping using Liang Barsky method

26. Program of Boundary fill algorithm

27. Program of Flood fill algorithm

Page 3: C graphics programs file

1. Program to draw a line using polynomial method :

#include<stdio.h>#include<conio.h>#include<graphics.h>#include<math.h>int main(){float m;int i,gd=DETECT,gm,x1,y1,x2,y2,x,y,dy,dx,b;printf("enter the x coordinates:");scanf("%d%d",&x1,&x2);printf("enter the y coordinates");scanf("%d%d",&y1,&y2);initgraph(&gd,&gm,"c:\\tc\\bgi");dx=x2-x1;dy=y2-y1;m=dy/dx;b=y1-(m*x1);x=x1;y=y1;if(m<1)while(x<=x2){putpixel(x, y+0.5,WHITE);x++;y=(m*x)+b;}else{while(y<=y2){putpixel(x+0.5,y,WHITE);y++;x=(y-b)/m;}

Page 4: C graphics programs file

}getch();closegraph();}

Output :

Page 5: C graphics programs file

2. Program to draw a line using DDA algorithm :

#include<stdio.h>#include<conio.h>#include<graphics.h>#include<math.h>int main(){float x,y,x1,y1,x2,y2,dx,dy,pixel;int i,gd=DETECT,gm;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);initgraph(&gd,&gm,"c:\\tc\\bgi");dx=abs(x2-x1);dy=abs(y2-y1);if(dx>=dy)pixel=dx;elsepixel=dy;dx=dx/pixel;dy=dy/pixel;x=x1;y=y1;i=1;while(i<=pixel){putpixel(x,y,WHITE);x=x+dx;y=y+dx;i=i+1;delay(100);}

Page 6: C graphics programs file

getch();closegraph();}

Output:

Page 7: C graphics programs file
Page 8: C graphics programs file

3. Program to draw a line using Bresenham’s algorithm:

#include<stdio.h>#include<conio.h>#include<graphics.h>#include<math.h>int main(){float x,y,x1,y1,x2,y2,dx,dy;int i,gd=DETECT,gm,p,end;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);

initgraph(&gd,&gm,"c:\\tc\\bgi");dx=abs(x1-x2);dy=abs(y1-y2);p=2*dy-dx;if(x1>x2){x=x2;y=y2;end=x1;}else{x=x1;y=y1;end=x2;}putpixel(x,y,WHITE);while(x<end){x=x+1;if(p<0)

Page 9: C graphics programs file

{p=p+2*dy;}else{y=y+1;p=p+2*(dy-dx);}putpixel(x,y,WHITE);}

getch();closegraph();}

Output :

Page 10: C graphics programs file
Page 11: C graphics programs file

4. Program to draw a circle using Polynomial method :

#include<stdio.h>#include<conio.h>#include<graphics.h>#include<math.h>int main(){int gd=DETECT,gm,xc,yc,xend,r,x,y;initgraph(&gd,&gm,"c:\\tc\\bgi");printf("***draw circle using polunomial method ***\n");printf("enter x centre and y centre\t");scanf("%d%d",&xc,&yc);printf("enter the radius \t");scanf("%d",&r);x=0;xend=r/sqrt(2);while(x<xend){y=sqrt(r*r-x*x);putpixel(xc+x,yc+y,WHITE);putpixel(xc+y,yc+x,WHITE);putpixel(xc-x,yc-y,WHITE);putpixel(xc-y,yc-x,WHITE);putpixel(xc+x,yc-y,WHITE);putpixel(xc+y,yc-x,WHITE);putpixel(xc-x,yc+y,WHITE);putpixel(xc-y,yc+x,WHITE);x++;}getch();closegraph();}

Page 12: C graphics programs file

Output :

Page 13: C graphics programs file

5. Program to draw a circle using Bresenham’s algorithm :

#include<stdio.h>#include<conio.h>#include<graphics.h>#include<math.h>int main(){int gd=DETECT,gm,xc,yc,r,x,y,pk;initgraph(&gd,&gm,"c:\\tc\\bgi");printf("*** Bresenham's circle drawing algorithm***");printf("enter the vlaue of xc \t");scanf("%d",&xc);printf("enter the vlaue of yc \t");scanf("%d",&yc);printf("enter the radius \t");scanf("%d",&r);x=0;y=r;putpixel(xc+x,yc-y,WHITE);pk=3-(2*r);for(x=0;x<=y;x++){if(pk<0){y=y;pk=(pk+(4*x)+6);}else{y=y-1;pk=pk+((4*(x-y)+10));}putpixel(xc+x,yc+y,WHITE);putpixel(xc+y,yc+x,WHITE);putpixel(xc-x,yc-y,WHITE);putpixel(xc-y,yc-x,WHITE);putpixel(xc+x,yc-y,WHITE);putpixel(xc+y,yc-x,WHITE);putpixel(xc-x,yc+y,WHITE);

Page 14: C graphics programs file

putpixel(xc-y,yc+x,WHITE);delay(100);}getch();closegraph();}

Output :

Page 15: C graphics programs file

6. Program to draw a circle using trigonometric method : #include<stdio.h>#include<graphics.h>#include<conio.h>#include<math.h>void put8pixel(int,int,int,int);void main(){int gd=DETECT,gm;int x,y,x1,y1,r,h,k,theta;float n=3.14159/180;printf("*Trignometric Method to draw a circle*\n");printf("\nenter the x and y coordinates:-");scanf("%d%d",&h,&k);printf("\nenter the radius:-\n");scanf("%d",&r);initgraph(&gd,&gm,"c:\\tc\\bgi");for(theta=0;theta<=45;theta++){x1=r*cos(theta*n);y1=r*sin(theta*n);x= x1+0.5;y=y1+0.5;put8pixel(x,y,h,k);}outtextxy(115,70,"circle using Trigonometric method");getch();closegraph();}void put8pixel(int x,int y,int h,int k){putpixel(x+h,y+k,WHITE);putpixel(x+h,-y+k,WHITE);putpixel(-x+h,y+k,WHITE);putpixel(-x+h,-y+k,WHITE);putpixel(y+h,x+k,WHITE);putpixel(y+h,-x+k,WHITE);putpixel(-y+h,x+k,WHITE);putpixel(-y+h,-x+k,WHITE);

Page 16: C graphics programs file

}

Output :

Page 17: C graphics programs file

7. Program to draw a circle using mid-point method :

#include<stdio.h>#include<conio.h>#include<graphics.h>void main(){int gd=DETECT, gm;int xc,yc,x,y,r,f;initgraph(&gd,&gm,"c:\\tc\\bgi");printf("Enter x-centre, y-centre and radius");scanf("%d%d%d",&xc,&yc,&r);x=0;y=r;f=1-r;while(x<=y){if(f<0){f=f+(3+2*x);x++;}else{f=f+(2*x-2*y+5);x++;y--;}putpixel(x+xc, y+yc, WHITE);putpixel(y+xc, x+yc,WHITE);putpixel(-x+xc, y+yc, WHITE);putpixel(-y+xc, x+yc, WHITE);putpixel(-x+xc, -y+yc, WHITE);putpixel(-y+xc, -x+yc, WHITE);putpixel(x+xc,-y+yc,WHITE);putpixel(y+xc,-x+yc,WHITE);}getch();closegraph();}

Page 18: C graphics programs file

Output :

Page 19: C graphics programs file

8. Program to draw a ellipse using polynomial method :

#include<stdio.h>#include<graphics.h>#include<conio.h>#include<math.h>void plot4pixels(int,int,int,int);void main(){int gd=DETECT,gm;int x,y,r,i,h,k,a,b;printf("\n*Polynomial method to draw an ellipse*\n");printf("\nenter the x and y coordinates:-\n");scanf("%d%d",&h,&k);printf("\nenter the first radius:-\n");scanf("%d",&a);printf("\nenter the second radius:-\n");scanf("%d",&b);x=0;y=b;

initgraph(&gd,&gm,"c:\\tc\\bgi");while(x<a){plot4pixels(x,y,h,k);x++;y=b*sqrt(((a*a)-(x*x*1.0))/(a*a));}plot4pixels(x,y,h,k);setcolor(WHITE);outtextxy(100,60,"ellipse using polynomial method");getch();}void plot4pixels(int x,int y,int h,int k){putpixel(x+h,y+k,WHITE);putpixel(x+h,-y+k,WHITE);putpixel(-x+h,y+k,WHITE);putpixel(-x+h,-y+k,WHITE);}

Page 20: C graphics programs file

Output :

Page 21: C graphics programs file

9. Program to draw a ellipse using bresenham’s algorithm :

#include<stdio.h>#include<conio.h>#include<graphics.h>#include<math.h>void bresenhamellipse(int,int,int,int);void ellipseplotpoints(int,int,int,int,int);

int main(){int gd=DETECT,gm;initgraph(&gd,&gm,"c:\\tc\\bgi");bresenhamellipse(300,300,30,40);getch();closegraph();}void bresenhamellipse(int xcenter,int ycenter,int rx,int ry){// xcenter and ycenter are the centre points.//rx and ry ,in these two any one can be the following,semi major axis and semi minor axis.long rx2=rx*rx,ry2=ry,x=0,y=ry,twory2=2*ry2,tworx2=2*rx2,p=0; ellipseplotpoints(xcenter,ycenter,x,y,WHITE); p=((int)(ry2*ry)+(.25*rx2)); while(twory2*x<tworx2*y) { x++; if(p<0) p+=(int)twory2*x+ry2; else { y--; p+=(int)twory2*ry2-ry2-tworx2*y; }

Page 22: C graphics programs file

ellipseplotpoints(xcenter,ycenter,x,y,WHITE); } //for region 2 p=(int)(ry2*(x+.5)*(x+.5)*(x+.5)+rx2*(y-1)*(y-1)-rx2*ry2); while(y>0) { y--; if(p>0) p+=rx2-tworx2*y; else { x++; p+=rx2+twory2*x-tworx2*y; } ellipseplotpoints(xcenter,ycenter,x,y,WHITE); } } void ellipseplotpoints(int xcenter,int ycenter,int x,int y,int color) { putpixel(xcenter+x,ycenter+y,WHITE); putpixel(xcenter-x,ycenter+y,WHITE); putpixel(xcenter+x,ycenter-y,WHITE); putpixel(xcenter-x,ycenter-y,WHITE); }

Page 23: C graphics programs file

Output :

Page 24: C graphics programs file

10. Program to draw a ellipse using trigonometric method :

#include<stdio.h>#include<graphics.h>#include<conio.h>#include<math.h>void put4pixel(int,int,int,int);void main(){int x,y,x1,y1,a,b,h,k,theta;int gd=DETECT,gm;float p=3.14159/180;printf("*Trignometric method to draw an ellippse*\n");printf("\nenter the x and y coordinates:-\n");scanf("%d%d",&h,&k);printf("\nenter the first radius:-\n");scanf("%d",&a);printf("\nenter the second radius:-\n");scanf("%d",&b);

initgraph(&gd,&gm,"c:\\tc\\bgi");for(theta=0;theta<=90;theta++){x1=a*cos(theta*p);y1=b*sin(theta*p);x=x1+0.5;y=y1+0.5;put4pixel(x,y,h,k);}outtextxy(100,60,"ellipse using Trigonometric method");getch();closegraph();}void put4pixel(int x,int y,int h,int k){putpixel(x+h,y+k,WHITE);putpixel(x+h,-y+k,WHITE);putpixel(-x+h,y+k,WHITE);putpixel(-x+h,-y+k,WHITE);}

Page 25: C graphics programs file

Output :

Page 26: C graphics programs file

11. Program to draw a ellipse using mid-point method :

#include<stdio.h>#include<conio.h>#include<graphics.h>#include<math.h>void disp();float x,y;int xc,yc;void main(){int gd=DETECT,gm;int a,b;float p1,p2;clrscr();initgraph(&gd,&gm,"c:\\tc\\bgi");printf("enter the xc and yc,a and b radius");scanf("%d%d",&xc,&yc);scanf("%d%d",&a,&b);x=0;y=b;disp();p1=(b*b)-(a*a*b)+(a*a)/4;while((2.0*b*b*x)<=(2.0*a*a*y)){x++;if(p1<=0)p1=p1+(2.0*b*b*x)+(b*b);else{y--;p1=p1+(2.0*b*b*x)+(b*b)-(2.0*a*a*y);}disp();x=-x;disp();x=-x;}x=a;y=0;disp();

Page 27: C graphics programs file

p2=(a*a)+2.0*(b*b*a)+(b*b)/4;while((2.0*b*b*x)>(2.0*a*a*y)){y++;if(p2>0)p2=p2+(a*a)-(2.0*a*a*y);else{x--;p2=p2+(2.0*b*b*x)-(2.0*a*a*y)+(a*a);}disp();y=-y;disp();y=-y;}getch();closegraph();}void disp(){putpixel(xc+x,yc+y,WHITE);putpixel(xc-x,yc+y,WHITE);putpixel(xc+x,yc-y,WHITE);putpixel(xc+x,yc-y,WHITE);}

Page 28: C graphics programs file

Output :

Page 29: C graphics programs file

12. Program to implement 2-D translation :

#include<graphics.h>#include<stdlib.h>#include<stdio.h>#include<math.h>void main(){int graphdriver=DETECT,graphmode,errorcode;int i;int x2,y2,x1,y1,x,y;printf("Enter the 2 line end points:");printf("x1,y1,x2,y2");scanf("%d%d%d%d",&x1,&y1,&x2,&y2);initgraph(&graphdriver,&graphmode,"c:\\tc\\bgi");line(x1,y1,x2,y2);printf("Enter translation co-ordinates ");printf("x,y");scanf("%d%d",&x,&y);x1=x1+x;y1=y1+y;x2=x2+x;y2=y2+y;printf("Line after translation");line(x1,y1,x2,y2);getch();closegraph();}

Page 30: C graphics programs file

Output :

Page 31: C graphics programs file

13. Program to implement 2-D rotation :

#include<graphics.h>#include<stdlib.h>#include<stdio.h>#include<math.h>void main(){int graphdriver=DETECT,graphmode,errorcode;int i;int x2,y2,x1,y1,x,y,xn,yn;double r11,r12,r21,r22,th;clrscr();printf("Enter the 2 line end points:");printf("x1,y1,x2,y2");scanf("%d%d%d%d",&x1,&y1,&x2,&y2);initgraph(&graphdriver,&graphmode,"c:\\tc\\bgi");line(x1,y1,x2,y2);printf("\n\n\n[ Enter the angle");scanf("%lf",&th);r11=cos((th*3.1428)/180);r12=sin((th*3.1428)/180);r21=(-sin((th*3.1428)/180));r22=cos((th*3.1428)/180);//printf("%lf %lf %lf %lf",r11,r12,r21,r22);xn=((x2*r11)-(y2*r12));yn=((x2*r12)+(y2*r11));line(x1,y1,xn,yn);getch();closegraph();}

Page 32: C graphics programs file

Output :

Page 33: C graphics programs file

14. Program to implement a 2-D scaling :

#include<graphics.h>#include<stdlib.h>#include<stdio.h>#include<math.h>void main(){int graphdriver=DETECT,graphmode,errorcode;int i;int x2,y2,x1,y1,x,y;printf("Enter the 2 line end points:");printf("x1,y1,x2,y2");scanf("%d%d%d%d",&x1,&y1,&x2,&y2);initgraph(&graphdriver,&graphmode,"c:\\tc\\bgi");line(x1,y1,x2,y2);printf("Enter scaling co-ordinates ");printf("x,y");scanf("%d%d",&x,&y);x1=(x1*x);y1=(y1*y);x2=(x2*x);y2=(y2*y);printf("Line after scaling");line(x1,y1,x2,y2);getch();closegraph();}

Page 34: C graphics programs file

Output :

Page 35: C graphics programs file

15. Program to draw sine curve :

#include <conio.h>#include <math.h>#include <graphics.h>#include <dos.h>

int main() {int gd = DETECT, gm;int angle = 0;double x, y;initgraph(&gd, &gm, "C:\\TC\\BGI");line(0, getmaxy() / 2, getmaxx(), getmaxy() / 2);/* generate a sine wave */for(x = 0; x < getmaxx(); x+=3) {/* calculate y value given x */y = 50*sin(angle*3.141/180);y = getmaxy()/2 - y;/* color a pixel at the given position */putpixel(x, y, 15);delay(100);/* increment angle */angle+=5;}getch();/* deallocate memory allocated for graphics screen */closegraph();return 0;}

Page 36: C graphics programs file

Output :

Page 37: C graphics programs file

16. Program to draw X-Y axis :

#include<stdio.h>#include<conio.h>#include<graphics.h>int main(){int gd=DETECT,gm;float xm,ym;initgraph(&gd,&gm,"c:\\tc\\bgi");//calculating the mid point of screenxm=getmaxx()/2;ym=getmaxy()/2;line(xm,0,xm,2*ym); //creating y axisline(0,ym,2*xm,ym); //creating x axisouttextxy(2*xm-60,ym+5,"X-axis");outtextxy(xm+4,10,"Y-axis");getch();closegraph();}

Page 38: C graphics programs file

Output :

Page 39: C graphics programs file

17. Program to print HELLO using line function :

#include<stdio.h>#include<conio.h>#include<graphics.h>void main(){int gd=DETECT,gm;int x,y,x1,y1;initgraph(&gd,&gm,"c:\\tc\\bgi");//hline(100,100,100,200);line(100,150,150,150);line(150,100,150,200);//eline(200,100,250,100);line(200,100,200,200);line(200,200,250,200);line(200,150,250,150);//lline(300,100,300,200);line(300,200,350,200);//lline(400,100,400,200);line(400,200,450,200);//oline(500,100,500,200);line(500,100,550,100);line(500,200,550,200);line(550,100,550,200);getch();closegraph();}

Page 40: C graphics programs file

Output :

Page 41: C graphics programs file

18. Program to print random lines on screen :

#include<stdio.h>#include<graphics.h>#include<conio.h>#include<stdlib.h>void main(){int gd=DETECT,gm;int i,j,xxmax,yymax,xmax,ymax,x,y,xx,yy;initgraph(&gd,&gm,"c:\\tc\\bgi");xmax=getmaxx();ymax=getmaxy();xxmax=getmaxx();yymax=getmaxy();while(!kbhit()){for(i=0;i<=100;i++){x=random(xmax);y=random(ymax);xx=random(xxmax);yy=random(yymax);for(j=1;j<=5;j++){setcolor(WHITE);line(x,y,xx,yy);}}}getch();closegraph();}resorecrtmode():}

Page 42: C graphics programs file

Output :

Page 43: C graphics programs file

19. Program to find whether a point is inside or outside the polygon :

#include<stdio.h>#include<stdlib.h>#include<conio.h>#include<graphics.h>#include<math.h>void main(){int gd=DETECT,gm,errorcode;float x[10],y[10],x1,y1,c[10],xm,ym;int flag,n,i,j;initgraph(&gd,&gm,"c:\\tc\\bgi");printf("enter the number of vertices of polygon");scanf("%d",&n);printf("\n enter the vertices\n");printf("x[i] and y[i]\n");for(i=0;i<n;i++){scanf("%f%f",&x[i],&y[i]);}printf("\n enter the point to be checked\n");scanf("%f%f",&x1,&y1);xm=getmaxx()/2;ym=getmaxx()/2;line(xm,0,xm,2*ym);line(0,ym,2*xm,ym);for(i=0;i<n-1;i++){circle(x[i]+xm,(-y[i]+ym),2);line(x[i]+xm,(-y[i]+ym),x[i+1]+xm,(-y[i+1]+ym));}circle(x[n-1]+xm,(-y[n-1]+ym),2);line(x[n-1]+xm,(-y[n-1]+ym),x[0]+xm,(-y[0]+ym));circle(x1+xm,-y1+ym,2);for(i=0;i<n;i++)c[i]=((x[i+1]-x[i])*(y1-y[i]))-((y[i+1]-y[i])*(x1-x[i]));c[n-1]=((x[0]-x[n-1])*(y1-y[n-1]))-((y[0]-y[n-1])*(x1-x[n-1]));flag=0;for(i=0;i<n;i++)

Page 44: C graphics programs file

{if(c[i]>0)flag=1;if(c[i]<0){flag=0;break;}}if(flag==1)printf("\n point is inside the polygon");if(flag==0)printf("\n point outside the polygon");getch();}

Output :

Page 45: C graphics programs file

20. Program to implement mapping from window to viewport :

#include<stdio.h>#include<conio.h>#include<graphics.h>#include<stdlib.h>void main(){float xwmin,xwmax,ywmax,ywmin;float xvmin,xvmax,yvmax,yvmin;float x[10],y[10],yv,xv,sx,sy;int gd=DETECT,gm,i;clrscr();printf("\n enter window port coordinates:\n(xwmin,ywmin,xwmax,ywmax): ");scanf("%f%f%f%f",&xwmin,&ywmin,&xwmax,&ywmax);printf("\n enter view port coordinates:\n(xvmin,yvmin,xvmax,yvmax): ");scanf("%f%f%f%f",&xvmin,&yvmin,&xvmax,&yvmax);printf("\n enter vertices for triangle: ");for(i=0;i < 3;i++){printf("\n enter(x%d,y%d):",i,i);scanf("%f%f",&x[i],&y[i]);}sx=((xvmax-xvmin)/(xwmax-xwmin));sy=((yvmax-yvmin)/(ywmax-xwmin));initgraph(&gd,&gm,"c:\\tc\\bgi");outtextxy(80,30,"window port");rectangle(xwmin,ywmin,xwmax,ywmax);for(i=0;i <2;i++){line(x[i],y[i],x[i+1],y[i+1]);}line(x[2],y[2],x[0],y[0]);getch();cleardevice();for(i=0;i <3;i++){x[i]=xvmin+((x[i]-xwmin)*sx);y[i]=yvmin+((y[i]-ywmin)*sy);}

Page 46: C graphics programs file

outtextxy(150,10,"view port");rectangle(xvmin,yvmin,xvmax,yvmax);for(i=0;i <2;i++){line(x[i],y[i],x[i+1],y[i+1]);}line(x[2],y[2],x[0],y[0]);getch();}

Output :

Page 47: C graphics programs file
Page 48: C graphics programs file

21. Program to plot an ARC using trigonometric method :

#include<stdio.h>#include<conio.h>#include<math.h>#include<stdlib.h>#include<graphics.h>#define pi 3.14void main(){int gd=DETECT,gm,i,ch,errorcode;float a,b,h,k,x,y,theta,theta1;initgraph(&gd,&gm,"c:\\tc\\bgi");printf("\n enter the coordinates of arc center h and k");scanf("%f%f",&h,&k);printf("\n step size");scanf("%d",&i);printf("\n enter the length of major and minor axis");scanf("%f%f",&a,&b);printf("\n enter the starting and ending angle");scanf("%f%f",&theta,&theta1);errorcode=graphresult();//if(errorcode!=grOk)//{//printf("graphics error\n");//getch();//exit(1);//}while(theta<theta1){x=a*cos((pi*theta)/180)+h;y=b*sin((pi*theta)/180)+k;putpixel(x,y,WHITE);theta=theta+i;}getch();}

Page 49: C graphics programs file

Output :

Page 50: C graphics programs file

22. Program to implement 2-D Reflection :

#include<graphics.h>#include<math.h>#include<conio.h>#include<stdio.h>void main(){int gd=DETECT,gm;int i,xmid,ymid,x1,y1,x2,y2,x3,y3,x,y,dy,dx,p,gap=50,temp,option;int x1dash,x2dash,x3dash,y1dash,y2dash,y3dash;double theta;float m;char str[5];clrscr();initgraph(&gd,&gm,"..\\bgi");printf("Enter first co-ords of the triangle\n");scanf("%d %d",&x1,&y1);printf("Enter second co-ords of the triangle\n");scanf("%d %d",&x2,&y2);printf("Enter third co-ords of the triangle\n");scanf("%d %d",&x3,&y3);xmid= getmaxx()/2;ymid= getmaxy()/2;line(5,ymid,getmaxx()-5,ymid);line(xmid+3,5,xmid+3,getmaxy()-5);for( i= xmid+gap;i<getmaxx()-5;i=i+gap){outtextxy(i,ymid-3,"|");itoa(i-xmid,str,10);outtextxy(i,ymid+3,str);}for( i= ymid-gap;i>5;i=i-gap){outtextxy(xmid,i,"-");itoa(ymid-i,str,10);outtextxy(xmid+5,i,str);}for( i= xmid-gap;i>5;i=i-gap){

Page 51: C graphics programs file

outtextxy(i,ymid-3,"|");itoa(-(xmid-i),str,10);outtextxy(i-6,ymid+3,str);}for( i= ymid+gap;i<getmaxy()-5;i=i+gap){outtextxy(xmid,i,"-");itoa(-(i-ymid),str,10);outtextxy(xmid+8,i,str);}line(x1+xmid,ymid-y1,x2+xmid,ymid-y2);line(x2+xmid,ymid-y2,x3+xmid,ymid-y3);line(x3+xmid,ymid-y3,x1+xmid,ymid-y1);setcolor(255);printf("Reflection about \n");printf("X axis : 1\n");printf("Y axis : 2\n");printf("X=Y axis : 3\n");printf(" Enter the option (1-3):");scanf("%d",&option);switch( option){case 1: y1=-y1; y2=-y2;y3=-y3; break;case 2: x1=-x1;x2=-x2;x3=-x3;break;case 3: y1=-y1; y2=-y2;y3=-y3; theta= ((double) 90 *3.14f )/(double)180; x1dash=x1*cos(theta)-y1*sin(theta); x2dash=x2*cos(theta)-y2*sin(theta); x3dash=x3*cos(theta)-y3*sin(theta); y1dash=x1*sin(theta)+y1*cos(theta); y2dash=x2*sin(theta)+y2*cos(theta); y3dash=x3*sin(theta)+y3*cos(theta); x1=x1dash;x2=x2dash;x3=x3dash; y1=y1dash;y2=y2dash;y3=y3dash;}line(x1+xmid,ymid-y1,x2+xmid,ymid-y2);line(x2+xmid,ymid-y2,x3+xmid,ymid-y3);line(x3+xmid,ymid-y3,x1+xmid,ymid-y1);getch();closegraph();}

Page 52: C graphics programs file

Output :

23. Program to implement 2-D Shearing :

Page 53: C graphics programs file

#include<graphics.h>#include<conio.h>void main(){int gd=DETECT,gm,option,xref,yref;int i,maxx,maxy,x1,y1,x2,y2,x3,y3,x4,y4,gap=50;float shx=0.0,shy=0.0;char str[5];clrscr();initgraph(&gd,&gm,"..\\bgi");printf("enter the endpoints of the top of the rectangle (x1,y1) & (x2,y2):"); scanf("%d%d%d%d",&x1,&y1,&x2,&y2); printf("enter the endpoints of bottom of the rectangle (x3,y3) & (x4,y4)"); scanf("%d%d%d%d",&x3,&y3,&x4,&y4); printf(" Enter the axis to shear\n"); printf(" 1 - X axis Shear\n"); printf(" 2 - Y axis shear\n"); scanf("%d",&option ); if(option==1) { printf("enter the value for x-axis shear( can be fraction too):"); scanf("%f",&shx); } else { printf("enter the value for y-axis shear( can be fraction too):"); scanf("%f",&shy); }clearviewport();maxx= getmaxx();maxy=getmaxy();line(3,maxy-1,maxx-5,maxy-1);line(5,5,5,maxy-3);for( i= 0;i<maxx-5;i=i+gap) // code to display co-ordinates{outtextxy(i+3,maxy-7,"|");itoa(i,str,10);outtextxy(i,maxy-10,str);}

Page 54: C graphics programs file

for( i= maxy;i>0;i=i-gap){outtextxy(3,i,"-");itoa(maxy-i,str,10);outtextxy(9,i,str);} setcolor(WHITE); // drawing rectangle using endpoints line(x1,maxy-y1,x2,maxy-y2); line(x3,maxy-y3,x4,maxy-y4); line(x1,maxy-y1,x3,maxy-y3); line(x2,maxy-y2,x4,maxy-y4); outtextxy(10,10,"hit any key to see the shearing effect" ); getch(); setcolor(WHITE); // to hide the rectangle drawn line(x1,maxy-y1,x2,maxy-y2); line(x3,maxy-y3,x4,maxy-y4); line(x1,maxy-y1,x3,maxy-y3); line(x2,maxy-y2,x4,maxy-y4); setcolor(WHITE); // to redraw the rectangle if(option==1) { // shearing about x axis so only points x1 and x2 need to be recomputed line(x1+shx*y1,maxy-y1,x2+shx*y2,maxy-y2); line(x3,maxy-y3,x4,maxy-y4); line(x1+shx*y1,maxy-y1,x3,maxy-y3); line(x2+shx*y2,maxy-y2,x4,maxy-y4); } else { // shearing about y axis so only points y2 and y4 need to be recomputed line(x1,maxy-y1,x2,maxy-(y2+shy*x2)); line(x3,maxy-y3,x4,maxy-(y4+shy*x4)); line(x1,maxy-y1,x3,maxy-y3); line(x2,maxy-(y2+shy*x2),x4,maxy-(y4+shy*x4));

} getch(); closegraph();}

Output :

Page 55: C graphics programs file
Page 56: C graphics programs file
Page 57: C graphics programs file

24. Program to implement line clipping using Cohan Sutherland algorithm :

#include<stdio.h>#include<conio.h>#include<graphics.h>void main(){int gd=DETECT, gm;float i,xmax,ymax,xmin,ymin,x1,y1,x2,y2,m;float start[4],end[4],code[4];clrscr();initgraph(&gd,&gm,"c:\\tc\\bgi");printf("\n\tEnter the bottom-left coordinate of viewport: ");scanf("%f %f",&xmin,&ymin);printf("\n\tEnter the top-right coordinate of viewport: ");scanf("%f %f",&xmax,&ymax);printf("\nEnter the coordinates for starting point of line: ");scanf("%f %f",&x1,&y1);printf("\nEnter the coordinates for ending point of line: ");scanf("%f %f",&x2,&y2);for(i=0;i <4;i++){start[i]=0;end[i]=0;}m=(y2-y1)/(x2-x1);if(x1 <xmin) start[0]=1;if(x1 >xmax) start[1]=1;if(y1 >ymax) start[2]=1;if(y1 <ymin) start[3]=1;if(x2 <xmin) end[0]=1;if(x2 >xmax) end[1]=1;if(y2 >ymax) end[2]=1;if(y2 <ymin) end[3]=1;for(i=0;i <4;i++)code[i]=start[i]&&end[i];if((code[0]==0)&&(code[1]==0)&&(code[2]==0)&&(code[3]==0)){

Page 58: C graphics programs file

if((start[0]==0)&&(start[1]==0)&&(start[2]==0)&&(start[3]==0)&&(end[0]==0)&&(end[1]==0)&&(end[2]==0)&&(end[3]==0)){cleardevice();printf("\n\t\tThe line is totally visible\n\t\tand not a clipping candidate");rectangle(xmin,ymin,xmax,ymax);line(x1,y1,x2,y2);getch();}else{cleardevice();printf("\n\t\tLine is partially visible");rectangle(xmin,ymin,xmax,ymax);line(x1,y1,x2,y2);getch();if((start[2]==0)&&(start[3]==1)){ x1=x1+(ymin-y1)/m; y1=ymin;}if((end[2]==0)&&(end[3]==1)){ x2=x2+(ymin-y2)/m; y2=ymin;}if((start[2]==1)&&(start[3]==0)){ x1=x1+(ymax-y1)/m; y1=ymax;}if((end[2]==1)&&(end[3]==0)){ x2=x2+(ymax-y2)/m; y2=ymax;}if((start[1]==0)&&(start[0]==1)){ y1=y1+m*(xmin-x1); x1=xmin;}

Page 59: C graphics programs file

if((end[1]==0)&&(end[0]==1)){ y2=y2+m*(xmin-x2); x2=xmin;}if((start[1]==1)&&(start[0]==0)){ y1=y1+m*(xmax-x1); x1=xmax;}if((end[1]==1)&&(end[0]==0)){ y2=y2+m*(xmax-x2); x2=xmax;}clrscr();cleardevice();printf("\n\t\tAfter clippling:");rectangle(xmin,ymin,xmax,ymax);line(x1,y1,x2,y2);getch();}}else{clrscr();cleardevice();printf("\nLine is invisible");rectangle(xmin,ymin,xmax,ymax);}getch();closegraph();}

Output :

Page 60: C graphics programs file
Page 61: C graphics programs file
Page 62: C graphics programs file

25. Program to implement line clipping using Liang Barsky method :

#include<graphics.h>#include<dos.h>#include<conio.h>#include<stdlib.h>void main(){int gd, gm ;int x1 , y1 , x2 , y2 ;int wxmin,wymin,wxmax, wymax ;float u1 = 0.0,u2 = 1.0 ;int p1 , q1 , p2 , q2 , p3 , q3 , p4 ,q4 ;float r1 , r2 , r3 , r4 ;int x11 , y11 , x22 , y22 ;clrscr();printf("Enter the windows left xmin , top boundry ymin\n");scanf("%d%d",&wxmin,&wymin);printf("Enter the windows right xmax ,bottom boundry ymax\n");scanf("%d%d",&wxmax,&wymax);printf("Enter line x1 , y1 co-ordinate\n");scanf("%d%d",&x1,&y1);printf("Enter line x2 , y2 co-ordinate\n");scanf("%d%d",&x2,&y2);printf("liang barsky express these 4 inequalities using lpk<=qpk\n");p1 = -(x2 - x1 ); q1 = x1 - wxmin ;p2 = ( x2 - x1 ) ; q2 = wxmax - x1 ;p3 = - ( y2 - y1 ) ; q3 = y1 - wymin ;p4 = ( y2 - y1 ) ; q4 = wymax - y1 ;printf("p1=0 line is parallel to left clipping\n");printf("p2=0 line is parallel to right clipping\n");printf("p3=0 line is parallel to bottom clipping\n");printf("p4=0 line is parallel to top clipping\n");if( ( ( p1 == 0.0 ) && ( q1 < 0.0 ) ) ||( ( p2 == 0.0 ) && ( q2 < 0.0 ) ) ||( ( p3 == 0.0 ) && ( q3 < 0.0 ) ) ||( ( p4 == 0.0 ) && ( q4 < 0.0 ) ) ){printf("Line is rejected\n");getch();

Page 63: C graphics programs file

detectgraph(&gd,&gm);initgraph(&gd,&gm,"c:\\tc\\bgi");setcolor(WHITE);rectangle(wxmin,wymax,wxmax,wymin);setcolor(WHITE);line(x1,y1,x2,y2);getch();setcolor(WHITE);line(x1,y1,x2,y2);getch();}else{if( p1 != 0.0 ){r1 =(float) q1 /p1 ;if( p1 < 0 )u1 = max(r1 , u1 );elseu2 = min(r1 , u2 );}if( p2 != 0.0 ){r2 = (float ) q2 /p2 ;if( p2 < 0 )u1 = max(r2 , u1 );elseu2 = min(r2 , u2 );

}if( p3 != 0.0 ){r3 = (float )q3 /p3 ;if( p3 < 0 )u1 = max(r3 , u1 );elseu2 = min(r3 , u2 );}if( p4 != 0.0 ){r4 = (float )q4 /p4 ;

Page 64: C graphics programs file

if( p4 < 0 )u1 = max(r4 , u1 );elseu2 = min(r4 , u2 );}if( u1 > u2 )printf("line rejected\n");else{x11 = x1 + u1 * ( x2 - x1 ) ;y11 = y1 + u1 * ( y2 - y1 ) ;x22 = x1 + u2 * ( x2 - x1 );y22 = y1 + u2 * ( y2 - y1 );printf("Original line cordinates\n");printf("x1 = %d , y1 = %d, x2 = %d, y2 = %d\n",x1,y1,x2,y2);printf("Windows coordinate are \n");printf("wxmin = %d, wymin = %d,wxmax = %d , wymax = %d ",wxmin,wymin,wxmax,wymax);printf("New coordinates are \n");printf("x1 = %d, y1 = %d,x2 = %d , y2 = %d\n",x11,y11,x22,y22);detectgraph(&gd,&gm);initgraph(&gd,&gm,"C:\\TC\\BGI");setcolor(WHITE);rectangle(wxmin,wymax,wxmax,wymin);setcolor(WHITE);line(x1,y1,x2,y2);getch();setcolor(BLACK);line(x1,y1,x2,y2);setcolor(WHITE);line(x11,y11,x22,y22);getch();}}}

Output :

Page 65: C graphics programs file
Page 66: C graphics programs file
Page 67: C graphics programs file

26. Program of Boundary fill algorithm :

#include<stdio.h>#include<conio.h>#include<graphics.h>#include<dos.h>void fill_right(int x,int y);void fill_left(int x,int y);void main(){int gd=DETECT,gm,x,y,n,i;clrscr();initgraph(&gd,&gm,"c:\\tc\\bgi");printf("*** Boundary Fill algorithm ***");/*- draw object -*/line (50,50,200,50);line (200,50,200,300);line (200,300,50,300);line (50,300,50,50);/*- set seed point -*/x=100; y=100;fill_right(x,y);fill_left(x-1,y);getch();}void fill_right(int x,int y){if((getpixel(x,y) != WHITE)&&(getpixel(x,y) != RED)){putpixel(x,y,RED);fill_right(++x,y); x=x-1;fill_right(x,y-1);fill_right(x,y+1);}delay(1);}void fill_left(int x,int y){if((getpixel(x,y) != WHITE)&&(getpixel(x,y) != RED))

Page 68: C graphics programs file

{putpixel(x,y,RED);fill_left(--x,y); x=x+1;fill_left(x,y-1);fill_left(x,y+1);}delay(1);}

Output :

Page 69: C graphics programs file

27. Program of Flood fill algorithm :

#include<stdio.h>#include<conio.h>#include<graphics.h>#include<dos.h>void flood(int,int,int,int);void main(){int gd,gm=DETECT;clrscr();detectgraph(&gd,&gm);initgraph(&gd,&gm,"c:\\tc\\bgi");rectangle(50,50,100,100);flood(55,55,12,0);getch();}void flood(int x,int y, int fill_col, int old_col){if(getpixel(x,y)==old_col){delay(10);putpixel(x,y,fill_col);flood(x+1,y,fill_col,old_col);flood(x-1,y,fill_col,old_col);flood(x,y+1,fill_col,old_col);flood(x,y-1,fill_col,old_col);}}

Page 70: C graphics programs file

Output :