1            /* Bresenhams Line drawing program */#include<stdio.h>#include<math.h>#include<dos.h>#include<conio.h>#includ...
2{y=ys;yend=ye;}while(y<=yend){    delay(10);putpixel(x,y,GREEN);if(d>=0){d=d+inc2;}elsed=d+inc1;y=y+1;}}if(dx!=0){m=(floa...
3if(dx<0){x=xe;y=ye;xend=xs;}else{x=xs;y=ys;xend=xe;}while(x<=xend){ delay(10);putpixel(x,y,GREEN);if(d>=0){y=y+1;d=d+inc2...
4else if(m>1){inc1=2*dx;inc2=2*(dx-dy);d=inc1-dy;if(dy<0){x=xe;y=ye;xend=xs;}else{x=xs;y=ys;xend=xe;}while(x<=xend){putpix...
5elsed=d+inc1;y=y+1;}}}getch();}              /* Simple DDA Line drawing program */#include<stdio.h>#include<process.h>#in...
6scanf("%d%d%d%d",&xs,&ys,&xe,&ye);dx=xe-xs;dy=ye-ys;if(dx<0){x=xe;xend=xs;}else{x=xs;xend=xe;}if(dy<0){y=ye;yend=ys;}else...
7printf("x=%f y=%f",x,y);if(dy>dx)length=dy;elselength=dx;if(dx==0 && dy==0){putpixel(x,y,YELLOW);getch();exit(0);}xinc=(f...
8{putpixel(floor(round(x)),floor(round(y)),BLUE);x=x+xinc;y=y+yinc;}}getch();}      /* Symmetrical DDA Line drawing progra...
9scanf("%d%d%d%d",&xs,&ys,&xe,&ye);dx=xe-xs;dy=ye-ys;if(dx<0){x=xe;xend=xs;}else{x=xs;xend=xe;}if(dy<0){y=ye;yend=ys;}else...
10printf("x=%f y=%f",x,y);if(dy>dx)length=dy;elselength=dx;if(dx==0 && dy==0){putpixel(x,y,YELLOW);getch();exit(0);}n=log(...
11while(x<=xend){putpixel(floor(round(x)),floor(round(y)),BLUE);x=x+xinc;y=y+yinc;}}getch();}     /* Circle drawing progra...
12while(theta<=thetaend){angle=temp*theta;x=r*cos(angle);y=r*sin(angle);putpixel(x+h,y+k,RED);putpixel(y+h,x+k,RED);putpix...
13float xend,x,y;clrscr();initgraph(&gdriver,&gmode,"");printf("enter centre coordinates and radius of circle");scanf("%d%...
14/* Circle drawing program using Bresenhams method*/#include<stdio.h>#include<math.h>#include<conio.h>#include<graphics.h...
15if(d<0)d=d+4*x+6;else{d=d+4*(x-y)+10;y--;}x++;}getch();}/* Circle drawing program using Mid-Point method*/#include<stdio...
16d=1-r;while(x<=y){putpixel(x+h,y+k,RED);putpixel(y+h,x+k,RED);putpixel(-y+h,x+k,RED);putpixel(-x+h,y+k,RED);putpixel(-x+...
17    /* Ellipse drawing program using Trigonometric method*/#include<stdio.h>#include<math.h>#include<conio.h>#include<gr...
18putpixel(-x+h,y+k,RED);putpixel(-x+h,-y+k,RED);putpixel(x+h,-y+k,RED);theta=theta+thetainc;}getch();}    /* Ellipse draw...
19{y=b*sqrt(1-(x*x)/(a*a));putpixel(x+h,y+k,RED);putpixel(-x+h,y+k,RED);putpixel(-x+h,-y+k,RED);putpixel(x+h,-y+k,RED);x++...
20int x11 , y11 , x22 , y22 ;clrscr();printf("Enter the windows left xmin , top boundry yminn");scanf("%d%d",&wxmin,&wymin...
21printf("Line is rejectedn");getch();detectgraph(&gd,&gm);initgraph(&gd,&gm,"c:tcbgi");setcolor(RED);rectangle(wxmin,wyma...
22r2 = (float ) q2 /p2 ;if( p2 < 0 )u1 = max(r2 , u1 );elseu2 = min(r2 , u2 );}if( p3 != 0.0 ){r3 = (float )q3 /p3 ;if( p3...
23printf("line rejectedn");else{x11 = x1 + u1 * ( x2 - x1 ) ;y11 = y1 + u1 * ( y2 - y1 ) ;x22 = x1 + u2 * ( x2 - x1 );y22 ...
24setcolor(3);line(x11,y11,x22,y22);getch();}}}                                    2d transformation#include<stdio.h>#incl...
25getch();}void scaling(){int xn1,yn1,xn2,yn2;float sx,sy;printf("Enter the Sacling Factor:");scanf("%f%f",&sx,&sy);cleard...
26outtextxy(500,200,"ROTATION");rx=(r*3.14)/180;xn1=x1*cos(rx)-y1*sin(rx);yn1=y1*cos(rx)+x1*sin(rx);xn2=x2*cos(rx)-y2*sin(...
27}void reflection(){int xn1,yn1,xn2,yn2;cleardevice();outtextxy(300,100,"REFLECTION");if((x1<y1)||(x2<y2)){xn1=x1+50;xn2=...
28{printf("Enter the Co-ordinates x1,y1,x2,y2:");scanf("%d%d%d%d",&x1,&y1,&x2,&y2);cleardevice();outtextxy(200,100,"ORIGIN...
29{case 1:{translation();break;}case 2:{scaling();break;}case 3:{rotation();break;}case 4:{shearing();break;}case 5:{refle...
30}case 6:{exit(0);break;}}}while(ch<6);}                        2d Viewing Transformation#include<stdio.h>#include<conio....
31scanf("%d%d",&xwmax,&ywmax);printf("Enter the viewport min coordinate(xvmin,yvmin):n");scanf("%d%d",&xvmin,&yvmin);print...
Upcoming SlideShare
Loading in …5
×

Cg my own programs

3,686 views

Published on

Cg my own programs

  1. 1. 1 /* Bresenhams Line drawing program */#include<stdio.h>#include<math.h>#include<dos.h>#include<conio.h>#include<graphics.h>void main(){int gdriver=DETECT,gmode,xs,ys,xe,ye,dx,dy,x,y,d,inc1,inc2,xend,yend;float m;clrscr();initgraph(&gdriver,&gmode,"");printf("enter line starting & ending end points");scanf("%d%d%d%d",&xs,&ys,&xe,&ye);dx=(xe-xs);dy=(ye-ys);if(dx==0){x=xs;if(dy<0){y=ye;yend=ys;}else
  2. 2. 2{y=ys;yend=ye;}while(y<=yend){ delay(10);putpixel(x,y,GREEN);if(d>=0){d=d+inc2;}elsed=d+inc1;y=y+1;}}if(dx!=0){m=(float) dy/dx;if(m<=1){inc1=2*abs(dy);inc2=2*abs(dy-dx);d=inc1-abs(dx);
  3. 3. 3if(dx<0){x=xe;y=ye;xend=xs;}else{x=xs;y=ys;xend=xe;}while(x<=xend){ delay(10);putpixel(x,y,GREEN);if(d>=0){y=y+1;d=d+inc2;}elsed=d+inc1;x=x+1;}}
  4. 4. 4else if(m>1){inc1=2*dx;inc2=2*(dx-dy);d=inc1-dy;if(dy<0){x=xe;y=ye;xend=xs;}else{x=xs;y=ys;xend=xe;}while(x<=xend){putpixel(x,y,RED);if(d>=0){x=x+1;d=d+inc2;}
  5. 5. 5elsed=d+inc1;y=y+1;}}}getch();} /* Simple DDA Line drawing program */#include<stdio.h>#include<process.h>#include<math.h>#include<dos.h>#include<conio.h>#include<graphics.h>float round(float r){return(r+0.5);}void main(){int gdriver=DETECT,gmode,xs,ys,xe,ye,dx,dy,length,xend,yend;float x,y,xinc,yinc;clrscr();initgraph(&gdriver,&gmode,"");printf("enter line starting & ending end points");
  6. 6. 6scanf("%d%d%d%d",&xs,&ys,&xe,&ye);dx=xe-xs;dy=ye-ys;if(dx<0){x=xe;xend=xs;}else{x=xs;xend=xe;}if(dy<0){y=ye;yend=ys;}else{y=ys;yend=ye;}dx=abs(xe-xs);dy=abs(ye-ys);
  7. 7. 7printf("x=%f y=%f",x,y);if(dy>dx)length=dy;elselength=dx;if(dx==0 && dy==0){putpixel(x,y,YELLOW);getch();exit(0);}xinc=(float)dx/length;yinc=(float)dy/length;if(dx==0){while(y<=yend){putpixel(floor(round(x)),floor(round(y)),RED);x=x+xinc;y=y+yinc;}}else if(dx!=0){while(x<=xend)
  8. 8. 8{putpixel(floor(round(x)),floor(round(y)),BLUE);x=x+xinc;y=y+yinc;}}getch();} /* Symmetrical DDA Line drawing program */#include<stdio.h>#include<process.h>#include<math.h>#include<dos.h>#include<conio.h>#include<graphics.h>float round(float r){return(r+0.5);}void main(){int gdriver=DETECT,gmode,xs,ys,xe,ye,dx,dy,length,xend,yend;float x,y,xinc,yinc,n;clrscr();initgraph(&gdriver,&gmode,"");printf("enter line starting & ending end points");
  9. 9. 9scanf("%d%d%d%d",&xs,&ys,&xe,&ye);dx=xe-xs;dy=ye-ys;if(dx<0){x=xe;xend=xs;}else{x=xs;xend=xe;}if(dy<0){y=ye;yend=ys;}else{y=ys;yend=ye;}dx=abs(xe-xs);dy=abs(ye-ys);
  10. 10. 10printf("x=%f y=%f",x,y);if(dy>dx)length=dy;elselength=dx;if(dx==0 && dy==0){putpixel(x,y,YELLOW);getch();exit(0);}n=log(length)/log(2);xinc=(float)dx/pow(2,ceil(n));yinc=(float)dy/pow(2,ceil(n));if(dx==0){while(y<=yend){putpixel(floor(round(x)),floor(round(y)),RED);x=x+xinc;y=y+yinc;}}else if(dx!=0){
  11. 11. 11while(x<=xend){putpixel(floor(round(x)),floor(round(y)),BLUE);x=x+xinc;y=y+yinc;}}getch();} /* Circle drawing program using Trigonometric method*/#include<stdio.h>#include<math.h>#include<conio.h>#include<graphics.h>void main(){int gdriver=DETECT,gmode,r,h,k;float xend,x,y,temp,thetainc,theta,thetaend,angle;clrscr();initgraph(&gdriver,&gmode,"");printf("enter centre coordinates and radius of circle");scanf("%d%d%d",&h,&k,&r);temp=3.141/180;thetainc=1.0/r;theta=0;thetaend=45;
  12. 12. 12while(theta<=thetaend){angle=temp*theta;x=r*cos(angle);y=r*sin(angle);putpixel(x+h,y+k,RED);putpixel(y+h,x+k,RED);putpixel(-y+h,x+k,RED);putpixel(-x+h,y+k,RED);putpixel(-x+h,-y+k,RED);putpixel(-y+h,-x+k,RED);putpixel(y+h,-x+k,RED);putpixel(x+h,-y+k,RED);theta=theta+thetainc;}getch();} /* Circle drawing program using Polynomial method*/#include<stdio.h>#include<math.h>#include<conio.h>#include<graphics.h>void main(){int gdriver=DETECT,gmode,r,h,k;
  13. 13. 13float xend,x,y;clrscr();initgraph(&gdriver,&gmode,"");printf("enter centre coordinates and radius of circle");scanf("%d%d%d",&h,&k,&r);xend=r/1.414;x=0;y=r;while(x<=xend){y=sqrt(r*r-x*x);putpixel(x+h,y+k,RED);putpixel(y+h,x+k,RED);putpixel(-y+h,x+k,RED);putpixel(-x+h,y+k,RED);putpixel(-x+h,-y+k,RED);putpixel(-y+h,-x+k,RED);putpixel(y+h,-x+k,RED);putpixel(x+h,-y+k,RED);x++;}getch();}
  14. 14. 14/* Circle drawing program using Bresenhams method*/#include<stdio.h>#include<math.h>#include<conio.h>#include<graphics.h>void main(){int gdriver=DETECT,gmode,r,h,k,x,y,d;clrscr();initgraph(&gdriver,&gmode,"");printf("enter centre coordinates and radius of circle");scanf("%d%d%d",&h,&k,&r);x=0;y=r;d=3-2*r;while(x<=y){putpixel(x+h,y+k,RED);putpixel(y+h,x+k,RED);putpixel(-y+h,x+k,RED);putpixel(-x+h,y+k,RED);putpixel(-x+h,-y+k,RED);putpixel(-y+h,-x+k,RED);putpixel(y+h,-x+k,RED);putpixel(x+h,-y+k,RED);
  15. 15. 15if(d<0)d=d+4*x+6;else{d=d+4*(x-y)+10;y--;}x++;}getch();}/* Circle drawing program using Mid-Point method*/#include<stdio.h>#include<math.h>#include<conio.h>#include<graphics.h>void main(){int gdriver=DETECT,gmode,r,h,k,x,y,d;clrscr();initgraph(&gdriver,&gmode,"");printf("enter centre coordinates and radius of circle");scanf("%d%d%d",&h,&k,&r);x=0;y=r;
  16. 16. 16d=1-r;while(x<=y){putpixel(x+h,y+k,RED);putpixel(y+h,x+k,RED);putpixel(-y+h,x+k,RED);putpixel(-x+h,y+k,RED);putpixel(-x+h,-y+k,RED);putpixel(-y+h,-x+k,RED);putpixel(y+h,-x+k,RED);putpixel(x+h,-y+k,RED);if(d<0)d=d+2*x+3;else{d=d+2*(x-y)+5;y--;}x++;}getch();}
  17. 17. 17 /* Ellipse drawing program using Trigonometric method*/#include<stdio.h>#include<math.h>#include<conio.h>#include<graphics.h>void main(){int gdriver=DETECT,gmode,r,h,k,a,b;float xend,x,y,temp,thetainc,theta,thetaend,angle;clrscr();initgraph(&gdriver,&gmode,"");printf("enter centre coordinates of ellipse");scanf("%d%d",&h,&k);printf("enter major and minor axis of ellipse");scanf("%d%d",&a,&b);temp=3.141/180;thetainc=3.141/(2*a);theta=0;thetaend=90;while(theta<=thetaend){angle=temp*theta;x=a*cos(angle);y=b*sin(angle);putpixel(x+h,y+k,RED);
  18. 18. 18putpixel(-x+h,y+k,RED);putpixel(-x+h,-y+k,RED);putpixel(x+h,-y+k,RED);theta=theta+thetainc;}getch();} /* Ellipse drawing program using Polynomial method*/#include<stdio.h>#include<math.h>#include<conio.h>#include<graphics.h>void main(){int gdriver=DETECT,gmode,r,h,k,a,b;float xend,x,y;clrscr();initgraph(&gdriver,&gmode,"");printf("enter centre coordinates of ellipse");scanf("%d%d",&h,&k);printf("enter major and minor axis of ellipse");scanf("%d%d",&a,&b);x=0;xend=a;while(x<=xend)
  19. 19. 19{y=b*sqrt(1-(x*x)/(a*a));putpixel(x+h,y+k,RED);putpixel(-x+h,y+k,RED);putpixel(-x+h,-y+k,RED);putpixel(x+h,-y+k,RED);x++;}getch();} Laing Barsky line clipping algorithm#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 ;
  20. 20. 20int x11 , y11 , x22 , y22 ;clrscr();printf("Enter the windows left xmin , top boundry yminn");scanf("%d%d",&wxmin,&wymin);printf("Enter the windows right xmax ,bottom boundry ymaxn");scanf("%d%d",&wxmax,&wymax);printf("Enter line x1 , y1 co-ordinaten");scanf("%d%d",&x1,&y1);printf("Enter line x2 , y2 co-ordinaten");scanf("%d%d",&x2,&y2);printf("liang barsky express these 4 inequalities using lpk<=qpkn");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 clippingn");printf("p2=0 line is parallel to right clippingn");printf("p3=0 line is parallel to bottom clippingn");printf("p4=0 line is parallel to top clippingn");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 ) ) ){
  21. 21. 21printf("Line is rejectedn");getch();detectgraph(&gd,&gm);initgraph(&gd,&gm,"c:tcbgi");setcolor(RED);rectangle(wxmin,wymax,wxmax,wymin);setcolor(BLUE);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 ){
  22. 22. 22r2 = (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 ;if( p4 < 0 )u1 = max(r4 , u1 );elseu2 = min(r4 , u2 );}if( u1 > u2 )
  23. 23. 23printf("line rejectedn");else{x11 = x1 + u1 * ( x2 - x1 ) ;y11 = y1 + u1 * ( y2 - y1 ) ;x22 = x1 + u2 * ( x2 - x1 );y22 = y1 + u2 * ( y2 - y1 );printf("Original line cordinatesn");printf("x1 = %d , y1 = %d, x2 = %d, y2 = %dn",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 = %dn",x11,y11,x22,y22);detectgraph(&gd,&gm);initgraph(&gd,&gm,"C:TCBGI");setcolor(2);rectangle(wxmin,wymax,wxmax,wymin);setcolor(1);line(x1,y1,x2,y2);getch();setcolor(0);line(x1,y1,x2,y2);
  24. 24. 24setcolor(3);line(x11,y11,x22,y22);getch();}}} 2d transformation#include<stdio.h>#include<conio.h>#include<math.h>#include<graphics.h>int x1,y1,x2,y2;void translation(){int tx,ty,xn1,xn2,yn1,yn2;printf("Enter the Translation Vector:");scanf("%dn%d",&tx,&ty);cleardevice();outtextxy(400,100,"TRANSLATION");xn1=x1+tx;yn1=y1+ty;xn2=x2+tx;yn2=y2+ty;line(x1,y1,x2,y2);line(xn1,yn1,xn2,yn2);
  25. 25. 25getch();}void scaling(){int xn1,yn1,xn2,yn2;float sx,sy;printf("Enter the Sacling Factor:");scanf("%f%f",&sx,&sy);cleardevice();outtextxy(300,200,"SCALING");xn1=x1*sx;yn1=y1*sy;xn2=x2*sx;yn2=y2*sy;line(x1,y1,x2,y2);line(xn1,yn1,xn2,yn2);getch();}void rotation(){int r;float rx,xn1,yn1,xn2,yn2;printf("Enter the Angle for Rotation:");scanf("%d",&r);cleardevice();
  26. 26. 26outtextxy(500,200,"ROTATION");rx=(r*3.14)/180;xn1=x1*cos(rx)-y1*sin(rx);yn1=y1*cos(rx)+x1*sin(rx);xn2=x2*cos(rx)-y2*sin(rx);yn2=y2*cos(rx)+x2*sin(rx);line(x1,y1,x2,y2);line(xn1,yn1,xn2,yn2);getch();}void shearing(){int sh;float xn1,yn1,xn2,yn2;printf("Enter the value of Shearing");scanf("%d",&sh);cleardevice();outtextxy(500,100,"SHEARING");xn1=x1+sh*y1;yn1=y1;xn2=x2+sh*y2;yn2=y2;line(x1,y1,x2,y2);line(xn1,yn1,xn2,yn2);getch();
  27. 27. 27}void reflection(){int xn1,yn1,xn2,yn2;cleardevice();outtextxy(300,100,"REFLECTION");if((x1<y1)||(x2<y2)){xn1=x1+50;xn2=x2+50;yn1=y1;yn2=y2;}else{xn1=x1;xn2=x2;yn1=y1+50;yn2=y2+50;}line(x1,y1,x2,y2);line(xn1,yn1,xn2,yn2);getch();}void get()
  28. 28. 28{printf("Enter the Co-ordinates x1,y1,x2,y2:");scanf("%d%d%d%d",&x1,&y1,&x2,&y2);cleardevice();outtextxy(200,100,"ORIGINAL OBJECT");line(x1,y1,x2,y2);getch();}void main(){int ch,gd=DETECT,gm;initgraph(&gd,&gm,"C:/tc/bgi");get();do{cleardevice();outtextxy(10,10,"1.Translation");outtextxy(10,20,"2.Scaling");outtextxy(10,30,"3.Rotation");outtextxy(10,40,"4.Shearing");outtextxy(10,50,"5.Reflection");outtextxy(10,60,"6.Exit");outtextxy(10,70,"Enter Your Choice:");scanf("%d",&ch);switch(ch)
  29. 29. 29{case 1:{translation();break;}case 2:{scaling();break;}case 3:{rotation();break;}case 4:{shearing();break;}case 5:{reflection();break;
  30. 30. 30}case 6:{exit(0);break;}}}while(ch<6);} 2d Viewing Transformation#include<stdio.h>#include<conio.h>#include<graphics.h>void main(){int gm,gr,xwmin,ywmin,xwmax,ywmax,xvmin,yvmin,xvmax,yvmax,xw,yw,xv,yv,sx,sy;clrscr();detectgraph(&gm,&gr);initgraph(&gm,&gr,"d:tcBGI");printf("Enter the window min coordinate(xwmin,ywmin):n");scanf("%d%d",&xwmin,&ywmin);printf("Enter the window max coordinate(xwmax,ywmax):n");
  31. 31. 31scanf("%d%d",&xwmax,&ywmax);printf("Enter the viewport min coordinate(xvmin,yvmin):n");scanf("%d%d",&xvmin,&yvmin);printf("Enter the viewport max coordinate(xvmax,yvmax):n");scanf("%d%d",&xvmax,&yvmax);sx=(xvmax-xvmin)/(xwmax-xwmin);sy=(yvmax-yvmin)/(ywmax-ywmin);printf("Enter the point coordinate(xw,yw) in the window:n");scanf("%d%d",&xw,&yw);xv=(sx*(xw-xwmin))+xvmin;yv=(sy*(yw-ywmin))+yvmin;cleardevice();getch();rectangle(xwmin,ywmax,xwmax,ywmin);putpixel(xw,yw,15); getch();cleardevice();getch();rectangle(xvmin,yvmax,xvmax,yvmin);putpixel(xv,yv,15);getch();}

×