WAP To Draw A Line Using DDA Algorithm
WAP To Draw A Line Using DDA Algorithm
1.
2.
#include<stdio.h> #include<stdlib.h> #include<graphics.h> #include<conio.h> #include<iostream.h> #include<math.h> void main( ) { int gdriver=DETECT,gmode,errorcode; int x1,y1,x2,y2,dx,dy,x,y,p,ps,pl,xend; initgraph(&gdriver,&gmode,"c:\\tc\\bgi"); errorcode=graphresult( ); if(errorcode!=grOk) { printf("Graphics error: %s \n",grapherrormsg(errorcode)); printf("\nPress any key to halt:"); getch( ); exit(1); } cout<<"Enter starting coordinates"; cin>>x1>>y1; cout<<"Enter ending coordinates"; cin>>x2>>y2; clrscr( ); dx=x2-x1; dy=y2-y1; p=2*dy-dx; ps=2*dy; pl=2*(dy-dx); if(x1>x2) { x=x2; y=y2; xend=x1; } else { x=x1; y=y1; xend=x2; } putpixel(x,y,1); while(x<xend) { x++;
4
if(p<0) p+=ps; else { y++; p+=pl; } putpixel(x,y,1); } }
3.
#include<stdio.h> #include<stdlib.h> #include<graphics.h> #include<conio.h> #include<iostream.h> #include<math.h> void circlepoints(int x1,int y1,int x,int y) { putpixel(x1+x,y1+y,1); putpixel(x1-x,y1+y,1); putpixel(x1+x,y1-y,1); putpixel(x1-x,y1-y,1); putpixel(x1+y,y1+x,1); putpixel(x1-y,y1+x,1); putpixel(x1+y,y1-x,1); putpixel(x1-y,y1-x,1); } void main( ) { int gdriver=DETECT,gmode,errorcode; int x1,y1,x,y,r; double p,ps,pl; initgraph(&gdriver,&gmode,"c:\\tc\\bgi"); errorcode=graphresult( ); if(errorcode!=grOk) { printf("Graphics error: %s \n",grapherrormsg(errorcode)); printf("\nPress any key to halt:"); getch( ); exit(1); } cout<<"\n Enter x and y coordinates of center:"; cin>>x1>>y1; cout<<" Enter The Radius :"; cin>>r; clrscr( ); ps=4.0*x+6.0; pl=4.0*(x-y)+10.0; x=0; y=r; p=3-2*r; while(x<=y) { circlepoints(x1,y1,x,y); x++; if(p<0)
7
p+=ps; else { y--; p+=pl; } circlepoints(x1,y1,x,y); } getch( ); }
4.
#include<stdio.h> #include<stdlib.h> #include<graphics.h> #include<conio.h> #include<iostream.h> #include<math.h> void circlepoints(int x1,int y1,int x,int y) { putpixel(x1+x,y1+y,1); putpixel(x1-x,y1+y,1); putpixel(x1+x,y1-y,1); putpixel(x1-x,y1-y,1); putpixel(x1+y,y1+x,1); putpixel(x1-y,y1+x,1); putpixel(x1+y,y1-x,1); putpixel(x1-y,y1-x,1); } void main( ) { int gdriver=DETECT,gmode,errorcode; int x1,y1,x,y,r,; double p,ps,pl; initgraph(&gdriver,&gmode,"c:\\tc\\bgi"); errorcode=graphresult( ); if(errorcode!=grOk) { printf("Graphics error: %s \n",grapherrormsg(errorcode)); printf("\nPress any key to halt:"); getch( ); exit(1); } cout<<"\n Enter x and y coordinates of center:"; cin>>x1>>y1; cout<<" Enter The Radius :"; cin>>r; clrscr( ); ps=2.0*x+3.0; pl=2.0*(x-y)+5.0; x=0; y=r; p=1-r; while(x<=y) { circlepoints(x1,y1,x,y); x++; if(p<0)
10
p+=ps; else { y--; p+=pl; } circlepoints(x1,y1,x,y); } getch( ); }
11
12
5.
#include<stdio.h> #include<stdlib.h> #include<graphics.h> #include<conio.h> #include<iostream.h> #include<math.h> void ellipsepoints(int x1,int y1,int x,int y) { putpixel(x1+x,y1+y,1); putpixel(x1-x,y1+y,1); putpixel(x1+x,y1-y,1); putpixel(x1-x,y1-y,1); } void main( ) { int gdriver=DETECT,gmode,errorcode; int x1,y1,a,b,x,y; double d1,d2; initgraph(&gdriver,&gmode,"c:\\tc\\bgi"); errorcode=graphresult( ); if(errorcode!=grOk) { printf("Graphics error: %s \n",grapherrormsg(errorcode)); printf("\nPress any key to halt:"); getch( ); exit(1); } cout<<"\n Enter x and y coordinates of center:"; cin>>x1>>y1; cout<<"\n Enter a and b coordinates:"; cin>>a>>b; clrscr( ); int x=0; int y=b; d1=(b*b)-(a*a*b)+(0.25*a*a); ellipsepoints(x1,y1,x,y); while((a*a*(y-0.5))>(b*b*(x+1))) { x++; if(d1<0) d1+=b*b*(2*x+3); else { d1+=(b*b*(2*x+3))+((a*a)*(-2*y+2)); y--; }
13
ellipsepoints(x1,y1,x,y); } d2=(b*b*(x+0.5)*(x+0.5)) + (a*a*(y-1)*(y-1)) -(a*a*b*b); while(y>0) { y--; if(d2<0) { d2+=(b*b)*(2*x+2)+(a*a*(-2*y+3)); x++; } else d2+=a*a*(-2*y+3); ellipsepoints(x1,y1,x,y); } getch( ); }
14
15
6.
#include<stdio.h> #include<stdlib.h> #include<graphics.h> #include<conio.h> #include<iostream.h> #include<math.h> void main( ) { int gdriver=DETECT,gmode,errorcode; int x1,y1,x2,y2,x3,y3,x,y,ch; initgraph(&gdriver,&gmode,"c:\\tc\\bgi"); errorcode=graphresult( ); if(errorcode!=grOk) { printf("Graphics error: %s \n",grapherrormsg(errorcode)); printf("\nPress any key to halt:"); getch( ); exit(1); } do { clrscr( ); cout<<"****Main Menu****\n"; cout<<"Translation\n"; cout<<"1.Line\n"; cout<<"2.Rectangle\n"; cout<<"3.Triangle\n"; cout<<"Enter your choice: (0 for exit)\n"; cin>>ch; switch(ch) { case 1: cout<<"Enter the values of line coordinates:"; cin>>x1>>y1>>x2>>y2; cout<<"\nEnter the translation vectors:"; cin>>x>>y; line(x1,y1,x2,y2); cout<<"\nNow hit a key to see translation\n"; getch( ); clrscr( ); line(x1+x,y1+y,x2+x,y2+y); break; case 2: cout<<"Enter the top left coordinates:"; cin>>x1>>y1; cout<<"\nEnter the bottom right coordinates:"; cin>>x2>>y2; cout<<"\nEnter the translation vectors:"; cin>>x>>y;
16
rectangle(x1,y1,x2,y2); cout<<"\nNow hit a key to see translation\n"; getch( ); clrscr( ); rectangle(x1+x,y1+y,x2+x,y2+y); break; case 3: cout<<"Enter the coordinates of line1:"; cin>>x1>>y1>>x2>>y2; cout<<"\nEnter coordinates for relative line:"; cin>>x3>>y3; cout<<"\nEnter the translation vectors:"; cin>>x>>y; line(x1,y1,x2,y2); moveto(x2,y2); lineto(x3,y3); moveto(x3,y3); lineto(x1,y1); cout<<"\nNow hit a key to see translation\n"; getch( ); clrscr( ); moveto(x1+x,y1+y); lineto(x2+x,y2+y); moveto(x2+x,y2+y); lineto(x3+x,y3+y); moveto(x3+x,y3+y); lineto(x1+x,y1+y); break; case 0: break; default:cout<< "\nInvalid choice\n "; break; } }while(ch!=0); getch( ); }
17
18
7.
#include<stdio.h> #include<stdlib.h> #include<graphics.h> #include<conio.h> #include<iostream.h> #include<math.h> void main( ) { int gdriver=DETECT,gmode,errorcode; int x1,y1,x2,y2,x3,y3,ch,a; initgraph(&gdriver,&gmode,"c:\\tc\\bgi"); errorcode=graphresult( ); if(errorcode!=grOk) { printf("Graphics error: %s \n",grapherrormsg(errorcode)); printf("\nPress any key to halt:"); getch( ); exit(1); } do { clrscr( ); cout<<"****Main Menu****\n"; cout<<"Rotation\n"; cout<<"1.Line\n"; cout<<"2.Rectangle\n"; cout<<"3.Triangle\n"; cout<<"Enter your choice: (0 for exit)\n"; cin>>ch; switch(ch) { case 1: cout<<"Enter the values of line coordinates:"; cin>>x1>>y1>>x2>>y2; cout<<"\nEnter the angle of rotation:"; cin>>a; a=a*(3.14/180); line(x1,y1,x2,y2); x1=x1*cos(a)-y1*sin(a); y1=x1*sin(a)+y1*cos(a); x2=x2*cos(a)-y2*sin(a); y2=x2*sin(a)+y2*cos(a); cout<<"\nNow hit a key to see rotation\n"; getch( ); clrscr( ); line(x1,y1,x2,y2); break; case 2: cout<<"Enter the top left coordinates:";
19
cin>>x1>>y1; cout<<"\nEnter the bottom right coordinates:"; cin>>x2>>y2; cout<<"\nEnter the angle of rotation:"; cin>>a; a=a*(3.14/180); rectangle(x1,y1,x2,y2); x1=x1*cos(a)-y1*sin(a); y1=x1*sin(a)+y1*cos(a); x2=x2*cos(a)-y2*sin(a); y2=x2*sin(a)+y2*cos(a); cout<<"\nNow hit a key to see rotation\n"; getch( ); clrscr( ); rectangle(x1,y1,x2,y2); break; case 3: cout<<"Enter the coordinates of line1:"; cin>>x1>>y1>>x2>>y2; cout<<"\nEnter coordinates for relative line:"; cin>>x3>>y3; cout<<"\nEnter the angle of rotation:"; cin>>a; a=a*(3.14/180); line(x1,y1,x2,y2); moveto(x2,y2); lineto(x3,y3); moveto(x3,y3); lineto(x1,y1); x1=x1*cos(a)-y1*sin(a); y1=x1*sin(a)+y1*cos(a); x2=x2*cos(a)-y2*sin(a); y2=x2*sin(a)+y2*cos(a); x3=x3*cos(a)-y3*sin(a); y3=x3*sin(a)+y3*cos(a); cout<<"\nNow hit a key to see rotation\n"; getch( ); clrscr( ); line(x1,y1,x2,y2); moveto(x2,y2); lineto(x3,y3); moveto(x3,y3); lineto(x1,y1); break; case 0: break; default:cout<< "\nInvalid choice\n "; break; } }while(ch!=0); getch( ); }
20
21
8.
#include<stdio.h> #include<stdlib.h> #include<graphics.h> #include<conio.h> #include<iostream.h> #include<math.h> void main( ) { int gdriver=DETECT,gmode,errorcode; int x1,y1,x2,y2,x3,y3,x,y,ch,; initgraph(&gdriver,&gmode,"c:\\tc\\bgi"); errorcode=graphresult( ); if(errorcode!=grOk) { printf("Graphics error: %s \n",grapherrormsg(errorcode)); printf("\nPress any key to halt:"); getch( ); exit(1); } do { clrscr( ); cout<<"****Main Menu****\n"; cout<<"Scaling\n"; cout<<"1.Line\n"; cout<<"2.Rectangle\n"; cout<<"3.Triangle\n"; cout<<"Enter your choice: (0 for exit)\n"; cin>>ch; switch(ch) { case 1: cout<<"Enter the values of line coordinates:"; cin>>x1>>y1>>x2>>y2; cout<<"\nEnter the scaling factors:"; cin>>x>>y; line(x1,y1,x2,y2); cout<<"\nNow hit a key to see scaling\n"; getch( ); clrscr( ); line(x1*x,y1*y,x2*x,y2*y); break; case 2: cout<<"Enter the top left coordinates:"; cin>>x1>>y1; cout<<"\nEnter the bottom right coordinates:"; cin>>x2>>y2; cout<<"\nEnter the scaling factors:"; cin>>sx>>sy;
22
rectangle(x1,y1,x2,y2); cout<<"\nNow hit a key to see scaling\n"; getch( ); clrscr( ); rectangle(x1*x,y1*y,x2*x,y2*y); break; case 3: cout<<"Enter the coordinates of line1:"; cin>>x1>>y1>>x2>>y2; cout<<"\nEnter coordinates for relative line:"; cin>>x3>>y3; cout<<"\nEnter the scaling factors:"; cin>>sx>>sy; line(x1,y1,x2,y2); moveto(x2,y2); lineto(x3,y3); moveto(x3,y3); lineto(x1,y1); cout<<"\nNow hit a key to see scaling\n"; getch( ); clrscr( ); moveto(x1*x,y1*y); lineto(x2*x,y2*y); moveto(x2*x,y2*y); lineto(x3*x,y3*y); moveto(x3*x,y3*y); lineto(x1*x,y1*y); break; case 0: break; default:cout<< "\nInvalid choice\n "; break; } }while(ch!=0); getch( ); }
23
24
9.
#include<stdio.h> #include<stdlib.h> #include<graphics.h> #include<conio.h> #include<iostream.h> #include<math.h> void main( ) { int gdriver=DETECT,gmode,errorcode; int x1,y1,x2,y2,x3,y3,ch,; initgraph(&gdriver,&gmode,"c:\\tc\\bgi"); errorcode=graphresult( ); if(errorcode!=grOk) { printf("Graphics error: %s \n",grapherrormsg(errorcode)); printf("\nPress any key to halt:"); getch( ); exit(1); } do { clrscr( ); cout<<"****Main Menu****\n"; cout<<"Reflection\n"; cout<<"1.Line\n"; cout<<"2.Rectangle\n"; cout<<"3.Triangle\n"; cout<<"Enter your choice: (0 for exit)\n"; cin>>ch; switch(ch) { case 1: cout<<"Enter the values of line coordinates:"; cin>>x1>>y1>>x2>>y2; line(x1,y1,x2,y2); cout<<"\nNow hit a key to see reflection along x-axis\n"; getch( ); line(x1,-y1,x2,-y2); cout<<"\nNow hit a key to see reflection along y-axis\n"; getch( ); clrscr( ); line(-x1,y1,-x2,y2); break; case 2: cout<<"Enter the top left coordinates:"; cin>>x1>>y1; cout<<"\nEnter the bottom right coordinates:"; cin>>x2>>y2; rectangle(x1,y1,x2,y2);
25
cout<<"\nNow hit a key to see reflection along x-axis\n"; getch( ); rectangle(x1,-y1,x2,-y2); cout<<"\nNow hit a key to see reflection along y-axis\n"; getch( ); clrscr( ); rectangle(-x1,y1,-x2,y2); break; case 3: cout<<"Enter the coordinates of line1:"; cin>>x1>>y1>>x2>>y2; cout<<"\nEnter coordinates for relative line:"; cin>>x3>>y3; line(x1,y1,x2,y2); moveto(x2,y2); lineto(x3,y3); moveto(x3,y3); lineto(x1,y1); cout<<"\nNow hit a key to see reflection along x-axis\n"; getch( ); moveto(x1,-y1); lineto(x2,-y2); moveto(x2,-y2); lineto(x3,-y3); moveto(x3,-y3); lineto(x1,-y1); cout<<"\nNow hit a key to see reflection along x-axis\n"; getch( ); clrscr( ); moveto(-x1,y1); lineto(-x2,y2); moveto(-x2,y2); lineto(-x3,y3); moveto(-x3,y3); lineto(-x1,y1); break; case 0: break; default:cout<< "\nInvalid choice\n "; break; } }while(ch!=0); getch( ); }
26
27
10.
#include<stdio.h> #include<stdlib.h> #include<graphics.h> #include<conio.h> #include<iostream.h> #include<math.h> void main( ) { int gdriver=DETECT,gmode,errorcode; int x1,y1,x2,y2,x3,y3,x,y,ch,; initgraph(&gdriver,&gmode,"c:\\tc\\bgi"); errorcode=graphresult( ); if(errorcode!=grOk) { printf("Graphics error: %s \n",grapherrormsg(errorcode)); printf("\nPress any key to halt:"); getch( ); exit(1); } do { clrscr( ); cout<<"****Main Menu****\n"; cout<<"Shearing\n"; cout<<"1.Line\n"; cout<<"2.Rectangle\n"; cout<<"3.Triangle\n"; cout<<"Enter your choice: (0 for exit)\n"; cin>>ch; switch(ch) { case 1: cout<<"Enter the values of line coordinates:"; cin>>x1>>y1>>x2>>y2; cout<<"Enter the value of shearing for x-axis"; cin>>x; cout<<"Enter the value of shearing for y-axis"; cin>>y; line(x1,y1,x2,y2); cout<<"\nNow hit a key to see reflection along x-axis\n"; getch( ); clrscr( ); line(x1,y1,x2+x*y2,y2); cout<<"\nNow hit a key to see reflection along y-axis\n"; getch( ); clrscr( ); line(x1,y1,x2,y2+y*x2); break; case 2: cout<<"Enter the top left coordinates:"; cin>>x1>>y1;
28
cout<<"\nEnter the bottom right coordinates:"; cin>>x2>>y2; cout<<"Enter the value of shearing for x-axis"; cin>>x; cout<<"Enter the value of shearing for y-axis"; cin>>y; rectangle(x1,y1,x2,y2); cout<<"\nNow hit a key to see reflection along x-axis\n"; getch( ); clrscr( ); rectangle(x1,y1,x2+x*y2,y2); cout<<"\nNow hit a key to see reflection along y-axis\n"; getch( ); clrscr( ); rectangle(x1,y1,x2,y2+y*x2); break; case 3: cout<<"Enter the coordinates of line1:"; cin>>x1>>y1>>x2>>y2; cout<<"\nEnter coordinates for relative line:"; cin>>x3>>y3; cout<<"Enter the value of shearing for x-axis"; cin>>x; cout<<"Enter the value of shearing for y-axis"; cin>>y; line(x1,y1,x2,y2); moveto(x2,y2); lineto(x3,y3); moveto(x3,y3); lineto(x1,y1); cout<<"\nNow hit a key to see reflection along x-axis\n"; getch( ); clrscr( ); moveto(x1,y1); lineto(x2+x*y2,y2); moveto(x2+x*y2,y2); lineto(x3+x*y3,y3); moveto(x3+x*y3,y3); lineto(x1,y1); cout<<"\nNow hit a key to see reflection along x-axis\n"; getch( ); clrscr( ); moveto(x1,y1); lineto(x2, y2+y*x2); moveto(x2, y2+y*x2); lineto(x3, y3+y*x3); moveto(x3, y3+y*x3); lineto(x1,y1); break; case 0: break; default:cout<< "\nInvalid choice\n "; break; }
29
}while(ch!=0); getch( ); }
30
11.
#include<stdio.h> #include<stdlib.h> #include<graphics.h> #include<conio.h> #include<iostream.h> #include<math.h> #define left 0*1 #define right 0*2 #define bottom 0*4 #define top 0*8 int compute_code(double x,double y,double xmin,double xmax,double ymin,double ymax) { int code=0; if(y>ymax) code |=top; else if(y<ymin) code |=bottom; if(x>xmax) code |=right; else if(x<xmin) code |=left; return code; } void cohen_lineclip(double x0,double y0,double x1,double y1, double xmin, double ymin, double xmax, double ymax); { int outcode0,outcode1,outcodeout; int accept=0,done=0; outcode0= compute_code(x0,y0,xmin,xmax,ymin,ymax); outcode1= compute_code(x1,y1,xmin,xmax,ymin,ymax); do { if(!(outcode0 | outcode1)) { accept=1; done=1; } else if(outcode0 & outcode1) done=1; else { double x,y; outcodeout=outcode0?outcode0:outcode1; if(outcodeout & top) { x=x0+(x1-x0)*(ymax-y0)/(y1-y0); y=ymax;
31
} else if(outcodeout & bottom) { x=x0+(x1-x0)*(ymin-y0)/(y1-y0); y=ymin; } else if(outcodeout & right) { y=y0+(y1-y0)*(xmax-x0)/(x1-x0); x=xmax; } else { y=y0+(y1-y0)*(xmin-x0)/(x1-x0); x=xmin; } if(outcodeout==outcode0) { x0=x; y0=y; outcode0= compute_code(x0,y0,xmin,xmax,ymin,ymax); } else { x1=x; y1=y; outcode1= compute_code(x1,y1,xmin,xmax,ymin,ymax); } } }while(done==0); if(accept) line(x0,y0,x1,y1); } void main( ) { int gdriver=DETECT,gmode,errorcode; int x1,y1,x2,y2,xmin,ymin,xmax,ymax; initgraph(&gdriver,&gmode,"c:\\tc\\bgi"); errorcode=graphresult( ); if(errorcode!=grOk) { printf("Graphics error: %s \n",grapherrormsg(errorcode)); printf("\nPress any key to halt:"); getch( ); exit(1); } cout<<"Enter line coordinates(x1,y1,x2,y2): "; cin>> xmin>>ymin>>xmax>>ymax; rectangle(xmin,ymin,xmax,ymax); getch( ); cohen_lineclip(x1,y1,x2,y2, xmin,ymin,xmax,ymax);
32
rectangle(xmin,ymin,xmax,ymax); getch( ); }
33
12.
#include<stdio.h> #include<stdlib.h> #include<graphics.h> #include<conio.h> #include<iostream.h> #include<math.h> int x,y,z; C(int n,int j) { int k,a,c; a=1; for(k=j+1;k<=n;k++) { a*=k; } for(k=1;k<=(n-j);k++) { a=a/k; } c=a; return(c); } float blend(int j,int n,float u) { int k; float v,blend; v=C(n,j); for(k=0;k<j;k++) v*=u; for(k=1;k<=(n-j);k++) { v *= (1-u); } blend=v; return(blend); } bezier(float u;int n; int p[4][3]); { int j; float v,b; float blend(int,int,float); x=0;y=0;z=0; for(j=0;j<=n;j++) { b=blend(j,n,u); x=x+(p[j][0]*b); y=y+(p[j][1]*b); z=z+(p[j][2]*b); } } void main( ) { float u; int gd,gm,ymax,i,n,c[4][3];
34
for(i=0;i<4;i++) c[i][0]=0; c[i][1]=0; printf("\n\n Enter four points : \n\n"); for(i=0; i<4; i++) { printf("\t X%d Y%d : ",i,i); scanf("%d %d",&c[i][0],&c[i][1]); } c[4][0]=c[0][0]; c[4][1]=c[0][1]; detectgraph(&gd,&gm); initgraph(&gd,&gm,"e:\\tc\\bgi"); ymax = 480; setcolor(13); for(i=0;i<3;i++) line(c[i][0],ymax-c[i][1],c[i+1][0],ymax-c[i+1][1]); setcolor(3); n=3; for(i=0;i<=40;i++) { u=(float)i/40.0; bezier(u,n,c); if(i==0) moveto(x,ymax-y); else lineto(x,ymax-y); getch( ); } getch( ); }
35