COMPUTER GRAPHICS LAB (CS 2405) for the Practical Examination held on ... Display a BresenHam’s Line ... Display the BRESENHAM’S CIRCLE DRAWING ALGORITHM Stop the ...

  • Published on
    06-Mar-2018

  • View
    213

  • Download
    1

Transcript

THIRUMALAI ENGINEERING COLLEGEKILAMBI, KANCHIPURAM-631551DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERINGCOMPUTER GRAPHICS LAB(CS 2405)NAME : _________________________________REG. NUMBER : _________________________________SEMESTER : _________________________________YEAR : ___________________________________THIRUMALAI ENGINEERING COLLEGEKILAMBI, KANCHIPURAM-631551DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERINGBONAFIDE CERTIFICATEThis is to certify that this is a bonafide work done by Mr./Miss.______________________________ Reg.No._____________________ For the CS 2405 Computer Graphics Lab as a part of B.E., Computer Science and Engineering course in Thirumalai Engineering College, Kanchipuram during the year of 2013-2014. The Record is found to be completed and satisfactory.Head of the Department Staff In-Charge Submitted for the Practical Examination held on ________________ Internal Examiner External ExaminerS.NO.DATE NAME OF THE EXPERIMENTPAGE NOSIGN1.IMPLEMENTATION OF BRESENHAMS LINE, CIRCLE AND ELLIPSE DRAWING ALGORITHM2.IMPLEMENTATION OF LINE, CIRCLE AND ELLIPSE ATTRIBUTES3.IMPLEMENTATION OF TWO DIMENSIONAL TRANSFORMATIONS4.IMPLEMENTATION OF COMPOSITE TWO DIMENSIONAL TRANSFORMATIONS5.IMPLEMENTATION OF COHEN SUTHERLAND TWO DIMENSIONAL LINE CLIPPING6.IMPLEMENTATION OF SUTHERLAND HODGEMAN POLYGON CLIPPING7.IMPLEMENTATION OF THREE DIMENSIONAL TRANSFORMATIONS8.IMPLEMENTATION OF COMPOSITE THREE DIMENSIONAL TRANSFORMATIONS9.(a) DRAWING THREE DIMENSIONAL OBJECTS9.(b) DRAWING THREE DIMENSIONAL SCENES10. FRACTALSLIST OF EXPERIMENTSEX.NO:IMPLEMENTATION OF BRESENHAMS LINE DRAWING ALGORITHMDATE:AIM: To write a program for implementation of Bresenhams line Drawing Algorithm.ALGORITHM: Start the program Include the graphics header file and obtain graphics mode and driver. Get the co-ordinates of two end points of a line (x1,y1) & (x2,y2) and store left end point in (X1, y1). Load (x1, y1) into frame buffer that means plot the first point. Calculate constants dx, dy, 2dy, 2dy-2dx and obtain value for decision parameter p=2dy-dx. At each x along the line perform the test if pgh();x1=getmaxx()/2+(x1*20);x2=getmaxx()/2+(x2*20);y1=getmaxy()/2-(y1*20);y2=getmaxy()/2-(y2*20);bres(x1,y1,x2,y2);}void bres(int x1,int y1,int x2,int y2){int p,dx,dy,x,y,xend,m;dx=abs(x2-x1);dy=abs(y2-y1);m=abs(dy/dx);if(mx2){x=x2;y=y2;xend=x1;}else{x=x1;y=y1;xend=x2;}putpixel(x,y,15);while(x{x=x2;y=y2;xend=y1;}else{x=x1;y=y1;xend=y2;}putpixel(x,y,15);while(yRESULT: Thus the program for Implementation of Bresenhams Line drawing algorithm has been executed successfully.EX.NO:IMPLEMENTATION OF BRESENHAMS CIRCLE DRAWING ALGORITHMDATE:AIM: To write a program for implementation of Bresenhams Circle Drawing Algorithm.ALGORITHM: Start the program Include the graphics header file and obtain graphics mode and driver. Get the center point (x,y) and radius(r) of a circle. Initialize the variables Select f the following in circle, ddsym and Flower Plot the pixel to display the circle using put pixel function. Display the BRESENHAMS CIRCLE DRAWING ALGORITHM Stop the programPROGRAM:#include#include#include#includevoid plot(int,int,int,int);int f;void main(){int ch,r,xc,yc,x,y,x1,y1,x2,y2,r1,r2;float p;initgraph(&gd,&gm,"d:\\tc\\BGI");void plot(int x,int y,int xc,int yc);void semi(int xc,int yc,int r);printf("\nEnter the Center and radius:");printf("\nXcenter:");scanf("%d",&xc);printf("Ycenter:");scanf("%d",&yc);printf("Radius:");scanf("%d",&r);gh();xc=getmaxx()/2+(20*xc);yc=getmaxy()/2+(20*yc);r=r*20;f=1;setcolor(WHITE);printf("\nSelect any one of the following:");printf("\n1.Circle\n2.DDsym\n3.Flower\nOption:");scanf("%d",&ch);switch(ch){case 1:semi(xc,yc,r);x1=xc-r/2;r1=r/2;getch();break;case 2:semi(xc,yc,r);x1=xc-r/2;y1=yc;r1=r/2;getch();f=2;semi(x1,y1,r1);x2=xc+r/2;y2=yc;f=3;getch();semi(x2,y2,r1);getch();break;case 3:r1=r;r2=r1*1.42;x1=xc;y1=yc-r1;f=2;semi(x1,y1,r1);x1=xc;y1=yc+r1;f=3;semi(x1,y1,r1);x1=xc-r1;y1=yc;f=4;semi(x1,y1,r1);x1=xc+r;y1=yc;f=5;semi(x1,y1,r1);f=1;semi(xc,yc,r2);break;}getch();closegraph();}void semi(int xc,int yc,int r){int x,y,p;x=0;y=r;p=1-r;while(xputpixel(xc-x,yc-y,5);putpixel(xc+y,yc+x,5);putpixel(xc-y,yc+x,5);putpixel(xc+y,yc-x,5);putpixel(xc-y,yc-x,5);break;case 2:putpixel(xc+x,yc+y,2);putpixel(xc-x,yc+y,2);putpixel(xc+y,yc+x,2);putpixel(xc-y,yc+x,2);break;case 3:putpixel(xc+x,yc-y,2);putpixel(xc-x,yc-y,2);putpixel(xc+y,yc-x,2);putpixel(xc-y,yc-x,2);break;case 4:putpixel(xc+y,yc-x,2);putpixel(xc+x,yc+y,2);putpixel(xc+x,yc-y,2);putpixel(xc+y,yc+x,2);break;case 5:putpixel(xc-y,yc+x,2);putpixel(xc-x,yc-y,2);putpixel(xc-x,yc+y,2);putpixel(xc-y,yc-x,2);break;}delay(10);}OUTPUT:Enter the center and radius:Xcenter: 0Ycenter: 0Radius: 6Select any one of the following:1. Circle2. DDSym3. FlowerOptionOption: 1CircleOption: 2DDSymOption: 3FlowerRESULT: Thus the program for Implementation of Bresenhams Circle drawing algorithm has been executed successfully.EX.NO:IMPLEMENTATION OF BRESENHAMS ELLIPSE DRAWING ALGORITHMDATE:AIM: To write the program for implementation of Bresenhams Ellipse Drawing Algorithm.ALGORITHM: Start the program Include graphics header file and obtain the graphics mode and driver Get the radius and center point of the ellipse Initialize the variables Calculate the value for d1=rysq-rxsq*ry+(0.25*rxsq) Plot the pixel to display the ellipse by using put pixel function Calculate the value for d2, if the value d2>0 then calculate d2=d2-dy+rxsq else d2=d2+dx-dy+rxsq Close the graph function Stop the programPROGRAM:#include#include#includevoid main(){long d1,d2;int i,gd,gm,x,y,x0,y0;long rx,ry,rxsq,rysq,tworxsq,tworysq,dx,dy;initgraph(&gd,&gm,"d:\\tc\\BGI");clrscr();printf("Enter the X radius and Y radius of the ellipse:\n");scanf("%ld%ld",&rx,&ry);printf("\nEnter the center (x,y) of the ellipse:\n");scanf("%d%d",&x0,&y0);detectgraph(&gd,&gm);initgraph(&gd,&gm,"d:\\tc\\BGI");cleardevice();rxsq=rx*rx;rysq=ry*ry;tworxsq=2*rxsq;tworysq=2*rysq;x=0;y=ry;d1=rysq-rxsq*ry+(0.25*rxsq);dx=tworysq*x;dy=tworxsq*y;do{putpixel(x0+x,y0+y,15);putpixel(x0-x,y0-y,15);putpixel(x0+x,y0-y,15);putpixel(x0-x,y0+y,15);if(d1dy=dy-tworxsq;d2=d2-dy+rxsq;}else{x=x+1;y=y-1;dx=dx+tworysq;dy=dy-tworxsq;d2=d2+dx-dy+rxsq;}}while(y>0);getch();closegraph();}OUTPUT:Enter the X radius and Y radius of the ellipse:7550Enter the center (x, y) of the ellipse:300300RESULT: Thus the program for Implementation of Bresenhams Ellipse drawing algorithm has been executed successfully.EX.NO:IMPLEMENTATION OF LINE, CIRCLE AND ELLIPSE ATTRIBUTESDATE:AIM: To write a program for implementation of Line, Circle and Ellipse Attributes.ALGORITHM: Start the program Create the program for graph.c Include the graphics header file and get the graphics mode and driver Create the function gh() Include the file graph.c in attribute program Initialize the array variables Get the values for rectangle by using rectangle function Get the values for polygon by using drawpoly() function Get the values for circle and using pieslice() function Draw star and star flower with using pieslice() function Stop the program PROGRAM:#include#includevoid main(){int x1,y1,a,b;int r[15],r1[19];int poly[]={105,380,155,380,180,330,130,280,80,330,105,380};int r2[]={260,225,380,225,280,300,320,180,360,300,260,225};int r3[]={320,340,420,190,220,190,320,340};int r4[]={220,290,420,290,320,140,220,290};int r5[]={285,190,355,190,385,240,355,290,285,290,255,240};int r6[]={305,225,335,225,345,255,320,270,295,255,305,225};int r7[]={500,160,540,120,500,80,460,120,500,160};//RECTANGLErectangle(50,180,200,90);setfillstyle(HATCH_FILL,11);bar(50,180,200,90);getch();//SQUAREsetcolor(GREEN);rectangle(440,180,560,60);setfillstyle(CLOSE_DOT_FILL,GREEN);bar(440,180,560,60);setcolor(16);line(440,180,560,60);line(440,60,560,180);a=(int)(560-440)/3;r[0]=440+a;r[1]=180;r[2]=440;r[3]=180-a;r[4]=560-a;r[5]=60;r[6]=560;r[7]=60+a;r[8]=440+a;r[9]=180;setcolor(BROWN);drawpoly(5,r);setfillstyle(CLOSE_DOT_FILL,BROWN);fillpoly(5,r);r1[0]=560-a;r1[1]=180;r1[2]=560;r1[3]=180-a;r1[4]=440+a;r1[5]=60;r1[6]=440;r1[7]=60+a;r1[8]=560-a;r1[9]=180;setcolor(15);drawpoly(5,r1);setfillstyle(CLOSE_DOT_FILL,15);fillpoly(5,r1);setfillstyle(HATCH_FILL,RED);fillpoly(5,r7);getch();//POLYGONsetcolor(YELLOW);drawpoly(6,poly);setfillstyle(1,RED);fillpoly(6,poly);line(130,380,130,280);line(130,380,180,330);line(130,380,80,330);line(130,380,105,305);line(130,380,155,305);line(130,380,93,355);line(130,380,168,355);getch();//CIRCLEsetfillstyle(9,DARKGRAY);setcolor(BROWN);pieslice(500,330,0,360,66);setcolor(5);rectangle(480,268,520,393);rectangle(460,278,540,382);rectangle(438,350,562,310);rectangle(448,370,552,290);rectangle(500,395,500,265);rectangle(435,330,565,330);getch();//STARsetfillstyle(1,10);drawpoly(6,r2);fillpoly(6,r2);setfillstyle(9,4);fillpoly(5,r6);setcolor(BLACK);line(180,225,260,225);getch();//STAR FLOWERdrawpoly(4,r3);setfillstyle(1,9);setcolor(9);fillpoly(4,r3);drawpoly(4,r4);setfillstyle(1,GREEN);setcolor(GREEN);fillpoly(4,r4);setfillstyle(1,5);setcolor(5);pieslice(285,190,55,180,65);setfillstyle(1,6);setcolor(6);pieslice(355,190,0,123,65);setfillstyle(1,5);setcolor(5);pieslice(385,240,303,360,65);pieslice(385,240,0,55,65);setcolor(5);line(385,240,449,240);setfillstyle(1,5);setcolor(5);pieslice(355,290,236,360,65);setfillstyle(1,5);setcolor(5);pieslice(285,290,180,305,65);setfillstyle(1,6);setcolor(6);pieslice(255,240,125,235,65);setfillstyle(9,RED);setcolor(RED);fillpoly(6,r5);getch();}OUTPUT:RESULT: Thus the program for Implementation of Line, Circle and Ellipse Attributes has been executed successfully.EX.NO:IMPLEMENTATION OF TWO DIMENSIONAL TRANSFORMATIONSDATE:AIM: To write a program for implementation of Two Dimensional Transformations.ALGORITHM: Start the program. Include the graphics header file in 2D Transformation. Declare the function for input, output, shearing, reflection and translation, scaling, rotation. Initialize the values and get the graphics mode and driver. In input function get the co-ordinates point. In translation function get the translation vector tx,ty . In rotation function get the angle and pivot point fx,fy for rotation. Get the scaling factor sx,sy in scaling function and also for shearing. Define the function for reflection. Stop the program.PROGRAM:#include#include#include#include#include#includevoid menu();void input();void output();void shearing();void reflection();void translation();void scaling();void rotation();int a[10][2],i,x,option,temp,angle,tx,ty,fx,fy,sh,k,n,axis,y;float sx,sy;void main(){int gd=DETECT,gm;initgraph(&gd,&gm," ");menu();getch();}void menu(){printf("Menu\n");printf("1.Translation\n2.Rotation\n3.Scaling\n4.Shearing\n5.Reflection\n6.Exit\nEnter your choice:");scanf("%d",&option);switch(option){case 1:input();translation();break;case 2:input();rotation();break;case 3:input();scaling();break;case 4:input();shearing();break;case 5:input();reflection();break;case 6:exit(0);break;}}void input(){printf("\nEnter the number of vertices:");scanf("%d",&n);for(i=0;ivoid scaling(){output();printf("Enter the scaling factor\n");scanf("%f%f",&sx,&sy);printf("Enter the fixed point:");scanf("%d%d",&fx,&fy);for(i=0;itemp=a[i][0];a[i][0]=a[i][1];a[i][1]=temp;}output();delay(10);menu();}OUTPUT:Menu1. Translation2. Rotation3. Scaling4. Shearing5. Reflection6. ExitTRANSLATIONEnter the choice: 1Enter the number of Vertices: 3Enter the coordinates: 30 150 10 200Enter the coordinates: 10 200 60 200Enter the coordinates: 60 200 30 150Enter the translation vector Tx, Ty: 90 60ROTATIONEnter the choice: 2Enter the number of Vertices: 3Enter the coordinates: 30 150 10 200Enter the coordinates: 10 200 60 200Enter the coordinates: 60 200 30 150Enter the Rotating Angle: 90Enter the Pivot Point : 100 200`SCALINGEnter the choice: 3Enter the number of Vertices: 3Enter the coordinates: 30 150 10 200Enter the coordinates: 10 200 60 200Enter the coordinates: 60 200 30 150Enter the scaling Factor: 0.3 0.4Enter the Fixed Point : 100 200SHEARINGEnter the choice: 4Enter the number of Vertices: 3Enter the coordinates: 30 150 10 200Enter the coordinates: 10 200 60 200Enter the coordinates: 60 200 30 150Enter the shear Value: 5Enter the fixed point: 50 100Enter the Axis for shearing if x-axis then 1 if y-axis then 0REFLECTIONEnter the choice: 5Enter the number of Vertices: 3Enter the coordinates: 30 150 10 200Enter the coordinates: 10 200 60 200Enter the coordinates: 60 200 30 150RESULT: Thus the program for Implementation of Two Dimensional Transformations has been executed successfully.EX.NO:IMPLEMENTATION OF COMPOSITE TWO DIMENSIONAL TRANSFORMATIONSDATE:AIM: To write a program for implementation of Composite 2D Transformations.ALGORITHM: Start the program Include the graphics header file in composite 2D transformation Initialize the variables and midentity() function Create the translate function and get the values for translate vectors tx,ty Create the scale function and get the values for scalar vector sx,sy For rotation get the values of angle and pixel points In main function get the graphic mode and driver Call the translate function and display after the translation by outextxy() Call the rotation function and display after the rotation by outextxy() Stop the programPROGRAM:#include#include#include#include#includetypedef float matrix[3][3];typedef struct{float x,y;}wept;matrix tm;void midentity(matrix m){int i,j;for(i=0;im[i][j]=(i==j);}void mpremul(matrix a,matrix b){int r,c;matrix temp;for(r=0;rvoid transformpoints(int npts,wept *pts){int k;float temp;for(k=0;ksleep(2);midentity(tm);translate(0,150);ptstopts(pts,tpts);outtextxy(50,280,"after translation");getch();}OUTPUT:RESULT: Thus the program for Implementation of Composite Two Dimensional Transformations has been executed successfully.EX.NO:IMPLEMENTATION OF COHEN SUTHERLAND TWO DIMENSIONAL LINE CLIPPINGDATE:AIM: To write a program for implementation of Cohen Sutherland 2D Line Clipping.ALGORITHM: Start the program. Create the file graph.c and include the graphics header file. Get the graphic mode and driver for the file. Include the graph file in this 2D line clipping. Initialize the variables and declare dx,dy. Get the no of lines, starting point and ending point. To calculate max, min by using predefined method getxx() and getmaxy(). Use the method clip for calculating the lines dx, dy, a, b, c, d. If a is min then calculate b=b+dy*(xmin-a)/dx then if a is max,calculate b=d+dy*(xmax-a)/dx. If c is min, calculate d=d+dy*(xmin-c)/dx then if c is max calculate d=d+dy*(xmax-c)/dx. Display the clipped window. Stop the program.PROGRAM:#include#include#includeint xmin=-4,ymin=2,xmax=4,ymax=-2;int dx,dy;void clip(int a,int b,int c,int d){dy=c-a;dx=d-b;if(ab=b+dy*(xmin-a)/dx;a=xmin;}if(a>xmax){b=b+dy*(xmax-a)/dx;a=xmax;}if(cxmax){d=d+dx*(xmax-c)/dy;c=xmax;}if(bymax){a=a+dx*(xmax-b)/dy;b=ymax;}if(dymax){c=c+dx*(ymax-d)/dy;d=ymax;}setcolor(WHITE);line(a,b,c,d);}void main(){int x1[50],y1[5],x2[50],y2[50],x[50],y[50];int i,n;int gd=DETECT,gm;initgraph(&gd,&gm,"d:\\tc\\BGI");printf("Enter the number of lines");scanf("%d",&n);for(i=1;iEnter the number of lines: 1Enter the starting point: 0 0Enter the ending point: 6 6RESULT: Thus the program for Implementation of Cohen Sutherland Two Dimensional Line Clipping has been executed successfully.EX.NO:IMPLEMENTATION OF SUTHERLAND HODGEMAN POLYGON CLIPPINGDATE:AIM: To write the program for implementation of Sutherland Hodgeman Polygon Clipping.ALGORITHM: Start the program. Include the graphics header file in this Hodgeman polygon clipping. Initialize the value for TOP, BOTTOM, LEFT and RIGHT. Declare the variable for xmin,xmax,ymin and ymax. Define the clip function and compute x0, x1 by ComputeOutCode () function. If it is lies TOP then y is ymax or else ymin. If it is RIGHT then x ix xmax else xmin. Define the main function and call all its function. Get the graphics mode and driver in polygon clipping. Display the polygon after the clipping. Stop the program.PROGRAM:#include #include #include #include #include #define TRUE 1#define FALSE 0typedef 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;}elseif(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;}elseif(outcodeOut & BOTTOM){x = x0+(x1-x0)*(ymin-y0)/(y1-y0);y = ymin;}elseif(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;elseif(yxmax)code|=RIGHT;elseif(xscanf("%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);/* initialize graphics and local variables */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;iRESULT: Thus the program for Implementation of Sutherland Hodgeman Polygon Clipping has been executed successfully.EX.NO:IMPLEMENTATION OF THREE DIMENSIONAL TRANSFORMATIONSDATE:AIM:To write the program for implementation of three dimensional transformations.ALGORITHM: Start the program. Initialize the variables maxx,maxy,midx,midy. Assign the variables gd,gm,x,y,z,o,x1,x2,y1,y2 in main method. Assign getmaxx() to maxx variable and getmaxy() to maxy variable. The method bar3d () is initialized. Enter the Translation, Scaling & Rotation factor in printf() method. Calculate x1, x2, y1, y2 and display results in respective co-ordinates. Display the results after performing Translation, Rotation &Scaling. Stop the program.PROGRAM:#include#include#include#includeint maxx,maxy,midx,midy;void axis(){getch();cleardevice();line(midx,0,midx,maxy);line(0,midy,maxx,midy);}void main(){int gd,gm,x,y,z,o,x1,x2,y1,y2;detectgraph(&gd,&gm);initgraph(&gd,&gm,"d:\\tc\\BGI");setfillstyle(0,getmaxcolor());maxx=getmaxx();maxy=getmaxy();midx=maxx/2;midy=maxy/2;axis();bar3d(midx+50,midy-100,midx+60,midy-90,5,1);printf("Enter Translation factor");scanf("%d%d%d",&x,&y,&z);axis();printf("After translation");bar3d(midx+(x+50),midy-(y+100),midx+x+60,midy-(y+90),5,1);axis();getch();bar3d(midx+50,midy+100,midx+60,midy-90,5,1);printf("Enter scaling factor");scanf("%d%d%d",&x,&y,&z);axis();printf("After scaling");bar3d(midx+(x*50),midy-(y*100),midx+(x*60),midy-(y*90),5*z,1);axis();getch();bar3d(midx+50,midy-100,midx+60,midy-90,5,1);printf("Enter Rotating Angle");scanf("%d",&o);x1=50*cos(o*3.14/180)-100*sin(o*3.14/180);y1=50*cos(o*3.14/180)+100*sin(o*3.14/180);x2=60*sin(o*3.14/180)-90*cos(o*3.14/180);y2=60*sin(o*3.14/180)+90*cos(o*3.14/180);axis();printf("After Rotation about Z axis");bar3d(midx+x1,midy-y1,midx+x2,midy-y2,5,1);axis();printf("After Rotation about X axis");bar3d(midx+50,midy-x1,midx+60,midy-x2,5,1);axis();printf("After Rotation about Y axis");bar3d(midx+x1,midy-100,midx+x2,midy-90,5,1);getch();closegraph();}OUTPUT:Translation: Enter Translation Factor: 50 60 70After Translation Scaling:Enter Scaling Factor: 80 90 95After Scaling Rotation:Enter Rotating Angle: 60 After Rotation about Z-Axis:After Rotation about X-Axis:After Rotation about Y-Axis:RESULT: Thus the program for Implementation of Three Dimensional Transformations has been executed successfully.EX.NO:IMPLEMENTATION OF COMPOSITE THREE DIMENSIONAL TRANSFORMATIONSDATE:AIM: To write a program for implementation of composite three dimensional transformations.ALGORITHM: Start the program Define all necessary headers for three dimensional transformations. Declare the variables i,j,k,temp in MultiMatrix() method. Using malloc the memory can be allocated for column size. Using for loop to instantiate row and column. In ResetMatrix () method the matrix are represented in three dimensional form. The methods movto2 (), lineto2 (), outtextxy (), Draw (), Rotate (), Move () are defined. Initialize the graphic interface. In switch statement the values are checked in each case. Display the output in composite three dimensional transformations. Stop the program.PROGRAM:#include#include #include #include#include #include #define ROTATE_X 0#define ROTATE_Y 1#define ROTATE_Z 2#define R_STEP 2.0double CAMERA_D =800;double CAMERA_Z =1000;#define KEY_PAGEUP 73#define KEY_PAGEDOWN 81#define KEY_UP 72#define KEY_DOWN 80#define KEY_LEFT 75#define KEY_RIGHT 77#define KEY_ESC 27double house[28][4]={0,0,0,1,0,0,0,1,-20,0,100,1,20,0,100,1,20,-100,-100,1,-20,-100,-100,1,-20,100,-100,1,20,100,-100,1,0,0,0, 1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,20,0,-100,1,-20,0,-100,1,0,0,0,1,0,0,0,1,20,-100,100,1,-20,-100,100,1,-20,100,100,1,20,100,100,1,0, 0, 0, 1, /* center row=24 */100, 0, 0, 1, /* x axis */0, 100, 0, 1, /* y axis */0, 0, 100, 1, /* z axis */};void MultiMatrix(double mat1[],double mat2[][4],int row,int col){int i,j,k;double *temp;temp=(double*)malloc(col*sizeof(double));for(i=0;iMatrix[0][0]=1.0; Matrix[0][1]=0.0; Matrix[0][2]=0.0; Matrix[0][3]=0.0;Matrix[1][0]=0.0; Matrix[1][1]=1.0; Matrix[1][2]=0.0; Matrix[1][3]=0.0;Matrix[2][0]=0.0; Matrix[2][1]=0.0; Matrix[2][2]=1.0; Matrix[2][3]=0.0;Matrix[3][0]=0.0; Matrix[3][1]=0.0; Matrix[3][2]=0.0; Matrix[3][3]=1.0;}void lineto2(double x,double y,double z){z+=CAMERA_Z; if (zsetlinestyle(DOTTED_LINE,0,3);setcolor(BROWN);moveto2(house[6*4][0],house[6*4][1],house[6*4][2]);lineto2(house[6*4+1][0],house[6*4+1][1],house[6*4+1][2]);outtextxy2(house[6*4+1][0],house[6*4+1][1],house[6*4+1][2],"X");setcolor(GREEN);moveto2(house[6*4][0],house[6*4][1],house[6*4][2]);lineto2(house[6*4+2][0],house[6*4+2][1],house[6*4+2][2]);outtextxy2(house[6*4+2][0],house[6*4+2][1],house[6*4+2][1],"Y");setcolor(CYAN);moveto2(house[6*4][0],house[6*4][1],house[6*4][2]);lineto2(house[6*4+3][0],house[6*4+3][1],house[6*4+3][2]);outtextxy2(house[6*4+3][0],house[6*4+3][1],house[6*4+3][2],"Z");}}void Rotate(double house[28][4],double Theta,int Axis){double Ta[4][4];double Rx[4][4];double c,v,b,a;double len;Theta=Theta*3.14159265/180.0;ResetMatrix(Ta);ResetMatrix(Rx);Ta[3][0]=-house[24][0];Ta[3][1]=-house[24][1];Ta[3][2]=-house[24][2];if(Axis>2) Axis=2;if(Axis=0.000001){Rx[1][1]=c/v;Rx[1][2]=b/v;Rx[2][1]=-b/v;Rx[2][2]=c/v;MultiMatrix((double*)Ta,Rx,4,4);}ResetMatrix(Rx); Rx[0][0]=v;Rx[0][2]=a;Rx[2][0]=-a;Rx[2][2]=v;MultiMatrix((double*)Ta,Rx,4,4);ResetMatrix(Rx); Rx[0][0]=cos(Theta);Rx[0][1]=sin(Theta);Rx[1][0]=-sin(Theta);Rx[1][1]=cos(Theta);MultiMatrix((double*)Ta,Rx,4,4);ResetMatrix(Rx); Rx[0][0]=v;Rx[0][2]=-a;Rx[2][0]=a;Rx[2][2]=v;MultiMatrix((double*)Ta,Rx,4,4);if(v>=0.000001){ResetMatrix(Rx); /* act as Rx-1 */Rx[1][1]=c/v;Rx[1][2]=-b/v;Rx[2][1]=b/v;Rx[2][2]=c/v;MultiMatrix((double*)Ta,Rx,4,4);}ResetMatrix(Rx); /* act as Ta-1 */Rx[3][0]=house[24][0];Rx[3][1]=house[24][1];Rx[3][2]=house[24][2];MultiMatrix((double*)Ta,Rx,4,4);MultiMatrix((double*)house,Ta,28,4);}void Move(double house[28][4],double x,double y,double z){double SMatrix[4][4];ResetMatrix(SMatrix);SMatrix[3][0]=x;SMatrix[3][1]=y;SMatrix[3][2]=z;MultiMatrix((double*)house,SMatrix,28,4);}void Scale(double house[28][4],double sx,double sy,double sz){ double SMatrix[4][4]; ResetMatrix(SMatrix); SMatrix[0][0]=sx; SMatrix[1][1]=sy;SMatrix[2][2]=sz;SMatrix[3][0]=(1-sx)*house[24][0];SMatrix[3][1]=(1-sy)*house[24][1];SMatrix[3][2]=(1-sz)*house[24][2];MultiMatrix((double*)house,SMatrix,28,4);}void InitGraph(){ int G_Driver=DETECT,G_Mode;initgraph(&G_Driver,&G_Mode,"");}void main(){char c;InitGraph();while(1){cleardevice();Draw(house,CYAN,1);c=getch();if(c==0) c=getch();if(c==KEY_ESC) break;switch(c){ case 'a':Move(house,10,0,0);break;case 'd':Move(house,-10,0,0);break;case 'w':Move(house,0,10,0);break;case 's':Move(house,0,-10,0);break;case 'X':Rotate(house,R_STEP,ROTATE_X);break;case 'Y':Rotate(house,R_STEP,ROTATE_Y);break;case 'Z':Rotate(house,R_STEP,ROTATE_Z);break;case 'x':Rotate(house,-R_STEP,ROTATE_X);break;case 'y':Rotate(house,-R_STEP,ROTATE_Y);break;case 'z':Rotate(house,-R_STEP,ROTATE_Z);break;case KEY_PAGEUP:CAMERA_D+=10;break;case KEY_PAGEDOWN:CAMERA_D-=10;break;default:exit(0); break;}}closegraph();}OUTPUT:TRANSLATION:Translation towards right (On pressing a)Translation towards left (on pressingd)Translation towards top (on pressingw)Translate towards bottom (On pressings)Rotation about Positive X-Axis (On pressing X)Rotation about Positive Y-Axis (on pressingY)Rotation about Positive Z-Axis (on pressingZ)Rotation about negative X-Axis (on pressingx)Rotation about Negative Y-Axis (on pressingy)Rotation about Negative Z-Axis (on pressingz)Scaling Increasing (on pressingPage up)Scaling Decreasing (On Pressingpage down)RESULT: Thus the program for Implementation of Composite Three Dimensional Transformations has been executed successfully.EX.NO:DRAWING THREE DIMENSIONAL OBJECTSDATE:AIM: To write a program for drawing three dimensional objects.ALGORITHM: Start the program. Initialize the graphics interface. Assign values to rectangle () & lines (). Using the methods setcolor () & setfillstyle () to fill colors in bar chart. Assign values to bar3d () method to draw bar chart. The methods settextstyle () & setlinestyle () are used to represent line style and text style. Display the bar chart in 3d view. Stop the program.PROGRAM:#include#includeint main(){int gd=DETECT,gm;initgraph(&gd,&gm,"d:\\tc\\BGI");setcolor(YELLOW);rectangle(0,30,639,450);settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);setcolor(WHITE);outtextxy(275,0,"Bar Chart");setlinestyle(SOLID_LINE,0,2);line(100,420,100,60);line(100,420,600,420);line(90,70,100,60);line(110,70,100,60);line(590,410,600,420);line(590,430,600,420);outtextxy(95,35,"Y");outtextxy(610,405,"X");outtextxy(85,415,"O");setfillstyle(LINE_FILL,BLUE);bar3d(150,100,200,419,12,1);setfillstyle(XHATCH_FILL,RED);bar3d(225,150,275,419,12,1);setfillstyle(WIDE_DOT_FILL,GREEN);bar3d(300,200,350,419,12,1);setfillstyle(INTERLEAVE_FILL,MAGENTA);bar3d(375,125,425,419,12,1);setfillstyle(HATCH_FILL,BROWN);bar3d(450,175,500,419,12,1);getch();return(0);}OUTPUTRESULT: Thus the program for Drawing Three Dimensional Objects has been executed successfully.EX.NO:DRAWING THREE DIMENSIONAL SCENESDATE:AIM: To write a program to draw three dimensional scenes.ALGORITHM: Start the program. Declare the variables i, j=0, gd. Initialize the graphic interface. Include the settextstyle () method to set the text and outtextxy () to view the moving car. Values are assigned to setviewport (). Using for loop the values are assigned to rectangle, circle. The delay () method is used to produce delay while the car is moving. Display the three dimensional scene. Stop the program.PROGRAM:#include#includeint main(){int i,j=0,gd=DETECT,gm;initgraph(&gd,&gm,"");settextstyle(DEFAULT_FONT,HORIZ_DIR,2);outtextxy(25,240,"Press any key to view the moving car");getch();setviewport(0,0,639,440,1);for(i=0;isetcolor(j);delay(100);if(i==420)break;clearviewport();}getch();closegraph();return(0);}OUTPUT:RESULT: Thus the program for Drawing Three Dimensional Scenes has been executed successfully.EX.NO:FRACTALSDATE:AIM:To write a program to generate fractal imagesALGORITHM: Start the program. Initialize the method drawsierpinski (). Assign the values VGA & VGAHI to the variables gd, gm. Declare the variables direct, iterate, x1, x2, y1, y2. In the for loop the fractal image points are calculated using x1=x2=320 and y1=y2=0. Check the direct values 0, 1, 2 using if statement. Display the fractal image. Stop the program.PROGRAM:#include#include#include#include#includevoid drawsierpinski(void);void main(void){int gd=VGA;int gm=VGAHI;initgraph(&gd,&gm,"d:\\tc\\BGI");drawsierpinski();getch();}void drawsierpinski(void){char direct;int iterate;unsigned int x1,y1,x2,y2;x1=x2=320;y1=y2=0;for(iterate=0;iterateOUTPUT:RESULT: Thus the program for Generating Fractal images has been executed successfully.

Recommended

View more >