Program To Generate A Line Using DDA Algorithm
Program To Generate A Line Using DDA Algorithm
Program To Generate A Line Using DDA Algorithm
ProgNo:1.a Date:
#include<GL/glut.h> #include<math.h> inline GLint round(const GLfloat a) { return GLint(a+0.5); } void init(void) { glClearColor(1.0,1.0,1.0,0.0); glMatrixMode(GL_PROJECTION); gluOrtho2D(0.0,300.0,0.0,300.0); } void setPixel(GLint xcoordinate,GLint ycoordinate) { glBegin(GL_POINTS); glVertex2i(xcoordinate,ycoordinate); glEnd(); glFlush(); } void lineDDA(GLint x0,GLint y0,GLint xEnd,GLint yEnd) { GLint dx=xEnd-x0; GLint dy=yEnd-y0; GLint steps,k; GLfloat xincrement,yincrement,x=x0,y=y0; if((dx)>(dy)) steps=(dx); else steps=(dy); xincrement=GLfloat(dx)/GLfloat(steps); yincrement=GLfloat(dy)/GLfloat(steps); setPixel(round(x),round(y)); for(k=0;k<=steps;k++) { x+=xincrement; y+=yincrement; setPixel(round(x),round(y)); } }
MCC Bangalore, Master Of Computer Application Page 1
void drawMyLine(void) { glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0,0.0,0.0); glPointSize(4.0); GLint x0=100; GLint y0=100; GLint xEnd=200; GLint yEnd=200; lineDDA(x0,y0,xEnd,yEnd); } void main(int argc,char **argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(500,500); glutInitWindowPosition(0,0); glutCreateWindow("DDA Algorithm"); init(); glutDisplayFunc(drawMyLine); glutMainLoop(); }
OUTPUT:
MCC Bangalore, Master Of Computer Application Page 2
ProgNo:1.b Date:
#include<GL/glut.h> #include<math.h> void init(void) { glClearColor(1.0,1.0,1.0,0.0); glMatrixMode(GL_PROJECTION); gluOrtho2D(0.0,300.0,0.0,300.0); } void setPixel(GLint xcoordinate,GLint ycoordinate) { glBegin(GL_POINTS); glVertex2i(xcoordinate,ycoordinate); glEnd(); glFlush(); } void lineBRES(GLint xa,GLint ya,GLint xb,GLint yb) { GLint dx=xb-xa; GLint dy=yb-ya; GLfloat p,xend,x=xa,y=ya; p=2*dy-dx; if ((xa)>(xb)) { x=xb; y=yb; xend=xa; //end; } else { x=xa; y=ya; xend=xb; } setPixel(x,y); while ((x)<(xend)) { x=x+1; if((p)<0) p=p+2*dy; else { y=y+1;
MCC Bangalore, Master Of Computer Application Page 4
p=p+2*(dy-dx); } setPixel(x,y); } } void drawMyLine(void) { glClear(GL_COLOR_BUFFER_BIT); glColor3f(0.0,1.0,0.0); glPointSize(4.0); GLint xa=100; GLint ya=150; GLint xb=200; GLint yb=175; lineBRES(xa,ya,xb,yb); } void main(int argc,char **argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(500,500); glutInitWindowPosition(0,0); glutCreateWindow("BRESENHAM Algorithm"); init(); glutDisplayFunc(drawMyLine); glutMainLoop(); }
OUTPUT:
Page 5
ProgNo:2.a Date:
#include <GL/glut.h> #include <math.h> inline GLint round(const GLfloat a) { return GLint(a + 0.5); } void init(void) { glClearColor(1.0,1.0,1.0,0.0); glMatrixMode(GL_PROJECTION); gluOrtho2D(0.0,300.0,0.0,300); } void setPixel(GLint xCoordinates, GLint yCoordinates) { glBegin(GL_POINTS); glVertex2i(xCoordinates, yCoordinates); glEnd(); glFlush(); } void circleDDA(GLfloat x1,GLfloat y1,GLint r) { GLint val,i; GLfloat x2,y2,x,y,e,p; GLint startx=x1; GLint starty=y1; p=2; i=0; do { val=pow(p,i); i++; } while(val<r); e=1/pow(p,i-1); do { x2=x1+y1*e; y2=y1-e*x2; x=200+x2; y=200+y2; setPixel(round(x),round(y)); x1=x2; y1=y2; } while((y1-starty)<e||(startx-x1)>e);
MCC Bangalore, Master Of Computer Application Page 7
} void dda_circle(void) { glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0,0.0,0.0); glPointSize(4.0); GLint r=60; GLfloat a1 = 0; GLfloat a2 = 0; GLfloat x0= r * cos(a1); GLfloat y0= r * sin(a2); circleDDA(x0,y0,r); } void main(int argc,char** argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(500,500); glutInitWindowPosition(10,10); glutCreateWindow("Digital Duffrential Analyzer Circle Drawing"); init(); glutDisplayFunc(dda_circle); glutMainLoop(); }
OUTPUT:
Page 8
ProgNo:2.b Date:
#include<GL/glut.h> #include<math.h> void init(void) { glClearColor(1.0,1.0,1.0,0.0); glMatrixMode(GL_PROJECTION); gluOrtho2D(0.0,300.0,0.0,300.0); } void setPixel(GLint xcoordinate,GLint ycoordinate) { glBegin(GL_POINTS); glVertex2i(xcoordinate,ycoordinate); glEnd(); glFlush(); } void circlemidpoint(GLint xcenter,GLint ycenter,GLint radius) { int x=0; int y=radius; int p=1-radius; void circleplotpoints(GLint,GLint,GLint,GLint); /*plot first set of points*/ circleplotpoints(xcenter,ycenter,x,y); while((x)<(y)) { x++; if((p)<0) p=p+2*x+1; else { y--; p=p+2*(x-y)+1; } circleplotpoints(xcenter,ycenter,x,y); } } void drawMycircle(void) { glClear(GL_COLOR_BUFFER_BIT); glColor3f(0.0,1.0,1.0); glPointSize(4.0); GLint xcenter=100;
MCC Bangalore, Master Of Computer Application Page 10
GLint ycenter=200; GLint radius=60; circlemidpoint(xcenter,ycenter,radius); } void circleplotpoints(int xcenter,int ycenter,int x,int y) { setPixel(xcenter+x,ycenter+y); setPixel(xcenter-x,ycenter+y); setPixel(xcenter+x,ycenter-y); setPixel(xcenter-x,ycenter-y); setPixel(xcenter+y,ycenter+x); setPixel(xcenter-y,ycenter+x); setPixel(xcenter+y,ycenter-x); setPixel(xcenter-y,ycenter-x); } void main(int argc,char **argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(500,500); glutInitWindowPosition(0,0); glutCreateWindow("BRESENHAM Circle Algorithm"); init(); glutDisplayFunc(drawMycircle); glutMainLoop(); }
OUTPUT
Page 11
ProgNo:3 Date:
//Program to implement basic 2D transformations. #include<stdio.h> #include<process.h> #include<conio.h> #include<graphics.h> #include<math.h> void disp(int n,float c[][3]) { float maxx,maxy; int i; maxx=getmaxx(); maxy=getmaxy(); maxx=maxx/2; maxy=maxy/2; i=0; while(i<n-1) { line(maxx+c[i][0],maxy-c[i][1],maxx+c[i+1][0],maxy-c[i+1][1]); i++; } i=n-1; line(maxx+c[i][0],maxy-c[i][1],maxx+c[0][0],maxy-c[0][1]); setcolor(GREEN); line(0,maxy,maxx*2,maxy); line(maxx,0,maxx,maxy*2); setcolor(WHITE); } void mul(int n,float b[][3],float c[][3],float a[][3]) { int i,j,k; for(i=0;i<n;i++) for(j=0;j<3;j++) a[i][j]=0; for(i=0;i<n;i++) for(j=0;j<3;j++) for(k=0;k<3;k++) { a[i][j] = a[i][j] + (c[i][k] * b[k][j]); } } void translation(int n,float c[][3],float tx,float ty) { int i; for(i=0;i<n;i++) { c[i][0]=c[i][0]+tx; c[i][1]=c[i][1]+ty;
MCC Bangalore, Master Of Computer Application Page 13
} } void scaling(int n,float c[][3],float sx,float sy) { float b[10][3],a[10][3]; int i=0,j; for(i=0;i<3;i++) for(j=0;j<3;j++) b[i][j]=0; b[0][0]=sx; b[1][1]=sy; b[2][2]=1; mul(n,b,c,a); setcolor(RED); disp(n,a); } void rotation(int n,float c[][3],float ra) { int i=0,j; float b[10][3],xp,yp,a[10][3]; xp=c[0][0]; yp=c[0][1]; for(i=0;i<3;i++) for(j=0;j<3;j++) b[i][j]=0; b[0][0]=b[1][1]=cos(ra*3.14/180); b[0][1]=sin(ra*3.14/180); b[1][0]=-sin(ra*3.14/180); b[2][0]=(-xp*cos(ra*3.14/180))+(yp*sin(ra*3.14/180))+xp; b[2][1]=(-xp*sin(ra*3.14/180))-(yp*cos(ra*3.14/180))+yp; b[2][2]=1; mul(n,b,c,a); setcolor(RED); disp(n,a); } void main() { int i,j,k,cho,n,gd=DETECT,gm; float c[10][3],tx,ty,sx,sy,ra; initgraph(&gd,&gm,"D:\\tc\\bgi"); printf("\nEnter the number of vertices : "); scanf("%d",&n); for(i=0;i<n;i++) { printf("\nEnter the co-ordinates of the %d vertex :",i+1); scanf("%f%f",&c[i][0],&c[i][1]); c[i][2]=1; }
MCC Bangalore, Master Of Computer Application Page 14
do { clrscr(); cleardevice(); printf("\n\t\t\t * * * MENU * * *"); printf("\n\t 1) TRANSLATION"); printf("\n\t 2) SCALING "); printf("\n\t 3) ROTATION "); printf("\n\t 4) EXIT"); printf("\n\t ENTER YOUR CHOICE: "); scanf("%d",&cho); switch(cho) { case 1: printf("\n\tEnter translation factor for X & Y axis :\t"); scanf("%f%f",&tx,&ty); clrscr(); cleardevice(); setcolor(BLUE); disp(n,c); translation(n,c,tx,ty); setcolor(RED); disp(n,c); getch(); break; case 2: printf("\n\tEnter scaling factor for X & Y axis :\t"); scanf("%f%f",&sx,&sy); clrscr(); cleardevice(); setcolor(BLUE); disp(n,c); scaling(n,c,sx,sy); getch(); break; case 3: printf("\n\n\tEnter the angle of rotation:\t"); scanf("%f",&ra); clrscr(); cleardevice(); setcolor(BLUE); disp(n,c); rotation(n,c,ra); getch(); break; case 4: exit(0); break; default: printf("\n\tInvalid choice !!"); break; } }while(cho!=4); getch();
MCC Bangalore, Master Of Computer Application Page 15
closegraph(); }
OUTPUT
Page 16
Page 17
Page 18
Page 19
ProgNo:5
Date:
// Program to implement Reflection and Shearing transformations. #include<stdio.h> #include<process.h> #include<conio.h> #include<graphics.h> #include<math.h> void disp(int n,float c[][3]) { float maxx,maxy; int x1,y1,x2,y2,x3,y3,i; maxx=getmaxx(); maxy=getmaxy(); maxx=maxx/2; maxy=maxy/2; i=0; while(i<n-1) { line(maxx+c[i][0],maxy-c[i][1],maxx+c[i+1][0],maxy-c[i+1][1]); i++; } i=n-1; line(maxx+c[i][0],maxy-c[i][1],maxx+c[0][0],maxy-c[0][1]); setcolor(GREEN); line(0,maxy,maxx*2,maxy); line(maxx,0,maxx,maxy*2); setcolor(WHITE); } void mul(int n,float b[][3],float c[][3],float a[][3]) { int i,j,k; for(i=0;i<n;i++) for(j=0;j<3;j++) a[i][j]=0; for(i=0;i<n;i++) for(j=0;j<3;j++) for(k=0;k<3;k++) { a[i][j] = a[i][j] + (c[i][k] * b[k][j]); } } void reflection(int n,float c[][3]) {
MCC Bangalore, Master Of Computer Application Page 21
float b[10][3],a[10][3]; int i=0,ch,j; cleardevice(); printf("\n\t* * MENU * *"); printf("\n\t1) ABOUT X-AXIS"); printf("\n\t2) ABOUT Y-AXIS"); printf("\n\t3) ABOUT ORIGIN"); printf("\n\t4) ABOUT X=Y"); printf("\n\t5) ABOUT -X=Y"); printf("\n\tENTER YOUR CHOICE : "); scanf("%d",&ch); clrscr(); cleardevice(); disp(n,c); for(i=0;i<3;i++) for(j=0;j<3;j++) { b[i][j]=0; if(i==j) b[i][j]=1; } switch(ch) { case 1: b[1][1]=-1; break; case 2: b[0][0]=-1; break; case 3: b[0][0]=-1; b[1][1]=-1; break; case 4: b[0][0]=0; b[1][1]=0; b[0][1]=1; b[1][0]=1; break; case 5: b[0][0]=0; b[1][1]=0; b[0][1]=-1; b[1][0]=-1; break; default:printf("\n\tINVALID CHOICE ! "); break; } mul(n,b,c,a); setcolor(RED); disp(n,a); }
MCC Bangalore, Master Of Computer Application Page 22
void shearing(int n,float c[][3]) { float b[10][3],sh,a[10][3]; int i=0,ch,j; cleardevice(); printf("\n\t* * * MENU * * *"); printf("\n\t1) X SHEARING"); printf("\n\t2) Y SHEARING"); printf("\n\t3) EXIT "); printf("\n\tENTER YOUR CHOICE : "); scanf("%d",&ch); if(ch==3) return; printf("\n\tENTER THE VALUE for SHEARING:"); scanf("%f",&sh); clrscr(); cleardevice(); for(i=0;i<3;i++) for(j=0;j<3;j++) b[i][j]=0; for(i=0;i<3;i++) b[i][i]=1; switch(ch) { case 1: b[1][0]=sh; break; case 2: b[0][1]=sh; break; case 3: break; default:printf("\n\tINVALID CHOICE ! "); break; } mul(n,b,c,a); setcolor(RED); disp(n,a); } void main() { int i,j,k,cho,n,gd=DETECT,gm; float c[10][3],tx,ty,sx,sy,ra; initgraph(&gd,&gm,"E:\\tc\\bgi"); printf("\nEnter the number of vertices : "); scanf("%d",&n); for(i=0;i<n;i++) { printf("\nEnter the co-ordinates of the %d vertex :",i+1); scanf("%f%f",&c[i][0],&c[i][1]);
MCC Bangalore, Master Of Computer Application Page 23
c[i][2]=1; } do { clrscr(); cleardevice(); printf("\n\t\t\t * * * MENU * * *"); printf("\n\t 1) REFLECTION "); printf("\n\t 2) SHEARING"); printf("\n\t 3) EXIT"); printf("\n\t ENTER YOUR CHOICE: "); scanf("%d",&cho); switch(cho) { case 1: clrscr(); cleardevice(); setcolor(BLUE); disp(n,c); reflection(n,c); getch(); break; case 2: clrscr(); cleardevice(); setcolor(BLUE); disp(n,c); shearing(n,c); getch(); break; case 3: exit(0); break; default:printf("\n\tInvalid choice !!"); break; } }while(cho!=3); getch(); closegraph(); }
OUTPUT
Page 24
Page 25
Page 26
Page 27
Page 28
Page 29
Page 30
ProgNo:5
Date:
#include<stdio.h> #include<stdlib.h> #include<graphics.h> #include<math.h> void scale(int *,int *,int *,float,float,float,int,int,int); void rotate(int *,int *,int *,float,int,int,int); void translate(int *,int *,int *,int,int,int); main() { int driver,mode,ch; int x1,y1,z1,x2,y2,z2,x3,y3,z3,x4,y4,z4,tx,ty,tz; float refx,refy,refz,theta,Sx,Sy,Sz; char ans; detectgraph(&driver,&mode); initgraph(&driver,&mode,"c:\\tc\\bgi"); do { x1=150,y1=300,x2=250,y2=300,x3=200,y3=200,x4=250,y4=250; refx=200,refy=250,refz=300; cleardevice(); line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x1,y1,x3,y3); line(x2,y2,x4,y4); line(x3,y3,x4,y4); outtextxy(100,30,"Main Menu"); outtextxy(100,50,"1.Scaling"); outtextxy(100,70,"2.Rotation"); outtextxy(100,90,"3.Translation"); outtextxy(100,110,"Enter your choice"); scanf("%d",&ch); switch(ch) { case 1: outtextxy(100,130,"Enter the values for Sx and Sy and Sz"); scanf("%f %f %f",&Sx,&Sy,&Sz); scale(&x1,&y1,&z1,Sx,Sy,Sz,refx,refy,refz); scale(&x2,&y2,&z2,Sx,Sy,Sz,refx,refy,refz); scale(&x3,&y3,&z3,Sx,Sy,Sz,refx,refy,refz); scale(&x4,&y4,&z4,Sx,Sy,Sz,refx,refy,refz); break; case 2: outtextxy(100,130,"Enter the angle of rotation"); scanf("%f",&theta); rotate(&x1,&y1,&z1,theta,refx,refy,refz); rotate(&x2,&y2,&z2,theta,refx,refy,refz); rotate(&x3,&y3,&z3,theta,refx,refy,refz); rotate(&x4,&y4,&z4,theta,refx,refy,refz); MCC Bangalore, Master Of Computer Application Page 32
break; case 3: outtextxy(100,130,"Enter tx and ty and tz"); scanf("%d %d %d",&tx,&ty,&tz); translate(&x1,&y1,&z1,tx,ty,tz); translate(&x2,&y2,&z2,tx,ty,tz); translate(&x3,&y3,&z3,tx,ty,tz); translate(&x4,&y4,&z4,tx,ty,tz); break; } line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x1,y1,x3,y3); line(x2,y2,x4,y4); line(x3,y3,x4,y4); outtextxy(100,450,"One more transformation:"); ans=getche(); }while(ans=='y'||ans=='Y'); getch(); closegraph(); return; } void scale(int *xnew,int *ynew,int *znew,float Sx,float Sy,float Sz,int refx,int refy,int refz) { translate(xnew,ynew,znew,-refx,-refy,-refz); *xnew=*xnew*Sx; *ynew=*ynew*Sy; *znew=*znew*Sz; translate(xnew,ynew,znew,refx,refy,refz); return; } void rotate(int *xnew,int *ynew,int *znew,float angl,int refx,int refy,int refz) { int *xold,*yold,*zold; translate(xnew,ynew,znew,-refx,-refy,-refz); *xold=*xnew; *yold=*ynew; *zold=*znew; angl=angl*3.14159/180; *xnew=*xold*cos(angl)-*yold*sin(angl); *ynew=*yold*cos(angl)+*xold*sin(angl); *znew=*zold; translate(xnew,ynew,znew,refx,refy,refz); return; } void translate(int *xnew,int *ynew,int *znew,int tx,int ty,int tz) { *xnew=*xnew+tx; *ynew=*ynew+ty; *znew=*znew+tz; return; MCC Bangalore, Master Of Computer Application Page 33
OUTPUT
Page 34
Page 35
ProgNo:6 Date:
#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 { 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;
MCC Bangalore, Master Of Computer Application Page 36
} 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(x0,y0,x1,y1); rectangle(xwmin,ywmin,xwmax,ywmax); getch(); } int calcode (x,y,xwmin,ywmin,xwmax,ywmax) 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; return(code); }
MCC Bangalore, Master Of Computer Application Page 37
void main() { float x2,y2,x1,y1,xwmin,ywmin,xwmax,ywmax; int gd=DETECT,gm; clrscr(); initgraph(&gd,&gm,"e:\\tc\\bgi"); printf("\n\n\tEnter the co-ordinates of Line :"); printf("\n\n\tX1 Y1 : "); scanf("%f %f",&x1,&y1); printf("\n\n\tX2 Y2 : "); scanf("%f %f",&x2,&y2); printf("\n\tEnter the co_ordinates of window :\n "); printf("\n\txwmin , ywmin : "); scanf("%f %f",&xwmin,&ywmin); printf("\n\txwmax , ywmax : "); scanf("%f %f",&xwmax,&ywmax); clrscr(); line(x1,y1,x2,y2); rectangle(xwmin,ywmin,xwmax,ywmax); getch(); clrscr(); lineclip(x1,y1,x2,y2,xwmin,ywmin,xwmax,ywmax ); getch(); closegraph(); }
OUTPUT
Page 38
Page 39
Page 40
ProgNo:7 Date:
#include <stdio.h> #include <graphics.h> #include <conio.h> #include <math.h> #include <process.h> #define TRUE 1 #define FALSE 0 typedef unsigned int outcode; outcode CompOutCode(float x,float y); enum { TOP = 0x1, BOTTOM = 0x2, RIGHT = 0x4, LEFT = 0x8}; float xmin,xmax,ymin,ymax; void clip(float x0,float y0,float x1,float y1) { outcode outcode0,outcode1,outcodeOut; int accept = FALSE,done = FALSE; outcode0 = CompOutCode(x0,y0); outcode1 = CompOutCode(x1,y1); do { if(!(outcode0|outcode1)) { accept = TRUE; done = TRUE; } else if(outcode0 & outcode1) done = TRUE; else { float x,y; outcodeOut = outcode0?outcode0:outcode1; if(outcodeOut & TOP) { x = x0+(x1-x0)*(ymax-y0)/(y1-y0); y = ymax; } else if(outcodeOut & BOTTOM) { x = x0+(x1-x0)*(ymin-y0)/(y1-y0); y = ymin; } else
MCC Bangalore, Master Of Computer Application Page 41
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 = CompOutCode(x0,y0); } else { x1 = x; y1 = y; outcode1 = CompOutCode(x1,y1); } } }while(done==FALSE); if(accept) line(x0,y0,x1,y1); outtextxy(150,20,"POLYGON AFTER CLIPPING"); rectangle(xmin,ymin,xmax,ymax); } outcode CompOutCode(float x,float y) { outcode 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 main( ) { float x1,y1,x2,y2; int gdriver = DETECT, gmode, n,poly[14],i; clrscr( );
MCC Bangalore, Master Of Computer Application Page 42
printf("Enter the no of sides of polygon:"); scanf("%d",&n); printf("\nEnter the coordinates of polygon\n"); for(i=0;i<2*n;i++) { scanf("%d",&poly[i]); } poly[2*n]=poly[0]; poly[2*n+1]=poly[1]; printf("Enter the rectangular coordinates of clipping window\n"); scanf("%f%f%f%f",&xmin,&ymin,&xmax,&ymax); initgraph(&gdriver, &gmode, "c:\\tc\\bgi"); outtextxy(150,20,"POLYGON BEFORE CLIPPING"); drawpoly(n+1,poly); rectangle(xmin,ymin,xmax,ymax); getch( ); cleardevice( ); for(i=0;i<n;i++) clip(poly[2*i],poly[(2*i)+1],poly[(2*i)+2],poly[(2*i)+3]); getch( ); restorecrtmode( ); }
OUTPUT
Page 43
Page 44
ProgNo:8 Date:
Program to illustrate the sales of product in past 4 years using bar and pie charts.
#include<stdio.h> #include<math.h> #include<graphics.h> #define twopie 2*3.14159 int piecahrt(int,int,int,int data[]); int bargraph(int p[]); main() { int gd=DETECT,gm,ch; void drawlabels(int,int); void drawbars(int,int[12],int); char answer='Y'; int i,xcentre,ycentre, radius,data[4],n,w; initgraph(&gd,&gm,"c:\\tc\\bgi"); do { cleardevice(); setcolor(3); settextstyle(0,0,2); outtextxy(90,20,"PIE CHART AND BAR CHART"); settextstyle(3,0,2); setcolor(15); outtextxy(230,50,"MAIN MENU"); outtextxy(230,70,"**********"); settextstyle(2,0,5); outtextxy(100,110,"1. PIE CHART"); outtextxy(100,130,"2. BAR CHART"); outtextxy(100,150,"3. EXIT"); outtextxy(100,176,"Enter your choice(1/2): "); gotoxy(38,12); scanf("%d",&ch); cleardevice(); switch(ch) { case 1: //data[4]; xcentre=getmaxx()/2.0; ycentre=getmaxy()/2.0; for(i=0;i<4;i++) { printf("ENTER THE DATA : "); scanf("%d",&data[i]); }
MCC Bangalore, Master Of Computer Application Page 45
printf("ENTER THE RADIUS OF THE PIE CHART : "); scanf("%d",&radius); circle(xcentre,ycentre,radius); piechart(xcentre,ycentre,radius,data); delay(50); getch(); break; case 2: printf("\n\n Enter the number of months:"); scanf("%d",&n); printf("\n Enter the width:"); scanf("%d",&w); for(i=0;i<n;i++) { printf("\n ENTER DATA FOR MONTH(%d) (Range 1300):",i+1); scanf("%d",&data[i]); while(data[i]<0 || data[i]>300) { printf("INVALID DATA"); printf("\n ENTER DATA FOR MONTH(%d)(Range 1-300):",i+1); scanf("%d",&data[i]); } } cleardevice(); drawlabels(n,w); drawbars(n,data,w); break; case 3: closegraph(); exit(1); break ; } outtextxy(200,400,"Want to continue(y/n)??"); answer=getche(); } while(answer=='y'); closegraph(); return; } int piechart(int xcentre,int ycentre,int radius, int data[4]) { float x,y,stangle=0,endangle,total; int i; for(i=0;i<4;i++) total= total+data[i];
MCC Bangalore, Master Of Computer Application Page 46
for(i=0;i<4;i++) { endangle=twopie*data[i]/total+stangle; x=xcentre+radius*cos(endangle); setcolor(MAGENTA); y=ycentre+radius*sin(endangle); line(xcentre,ycentre,(int)x,(int)y); stangle=endangle; } getch(); return; } void drawlabels(int n,int w) { int i,s,ss; int x=40,y=450; char m[12] [5]={"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DE C"}; line(30,440,getmaxx(),440); line(30,440,30,1); settextstyle(SANS_SERIF_FONT,HORIZ_DIR,1); outtextxy(300,70,"BAR GRAPH"); for(i=0;i<n;i++) { outtextxy(x,y,m[i]); x=x+40+w; } } void drawbars(int n,int data[12],int w) { int i,r,b; int l=40,t=440; setfillstyle(SOLID_FILL,5); setcolor(5); for(i=0;i<n;i++) { r=l+w; b=t-data[i]; bar3d(l,t,r,b,10,10); rectangle(l,t,r,b); l=l+40+w; } }
OUTPUT
MCC Bangalore, Master Of Computer Application Page 47
Page 48
Page 49
ProgNo:9.a
MCC Bangalore, Master Of Computer Application Page 50
Date:
#include<conio.h> #include<stdio.h> #include<graphics.h> #include<dos.h> void fill_right(x,y) int x , y ; { if(getpixel(x,y) == 0) { putpixel(x,y,RED); fill_right(++x,y); x=x-1; fill_right(x,y-1); fill_right(x,y+1); } } void fill_left(x,y) int x , y ; { if(getpixel(x,y) == 0) { putpixel(x,y,RED); fill_left(--x,y); x=x+1; fill_left(x,y-1); fill_left(x,y+1); } } void main() { int x , y ,a[10][10]; int gd, gm ,n,i; detectgraph(&gd,&gm); initgraph(&gd,&gm,"c:\\tc\\bgi"); printf("\n\n\tEnter the no. of edges of polygon : "); scanf("%d",&n); printf("\n\n\tEnter the cordinates of polygon :\n\n\n "); for(i=0;i<n;i++) { printf("\tX%d Y%d : ",i,i); scanf("%d %d",&a[i][0],&a[i][1]); }
MCC Bangalore, Master Of Computer Application Page 51
a[n][0]=a[0][0]; a[n][1]=a[0][1]; printf("\n\n\tEnter the seed pt. : "); scanf("%d%d",&x,&y); cleardevice(); setcolor(WHITE); for(i=0;i<n;i++) /*- draw poly -*/ { line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]); } fill_right(x,y); fill_left(x-1,y); getch(); }
OUTPUT
Page 52
ProgNo:9.b Date:
#include<stdio.h> #include<conio.h> #include<graphics.h> void bfillcolor(int,int,int,int); void main() { int gdriver=DETECT,gmode; int xc,yc,r,fc,bc; clrscr(); initgraph(&gdriver,&gmode,"c:\\tc\\bgi"); printf("\n\t\tBOUNDARY FILL"); printf("\n\n\tEnter center:"); scanf("%d %d",&xc,&yc); printf("\n\tEnter the radius:"); scanf("%d",&r); printf("\tEnter boundry color :"); scanf("%d",&bc); printf("\tEnter fill color : "); scanf("%d",&fc); setcolor(bc); circle(xc,yc,r); bfillcolor(xc,yc,bc,fc); getch(); } void bfillcolor(int x,int y,int bc,int fc) { int cp; cp=getpixel(x,y); if(cp!=bc && cp!=fc) { delay(20); putpixel(x,y,fc); bfillcolor(x+1,y,bc,fc); bfillcolor(x-1,y,bc,fc); bfillcolor(x,y+1,bc,fc); bfillcolor(x,y-1,bc,fc); } }
OUTPUT
Page 54
ProgNo:10 Date:
Page 56