0% found this document useful (0 votes)
19 views

CGMT Lab Record

Uploaded by

playplay9928
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
19 views

CGMT Lab Record

Uploaded by

playplay9928
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 37
Alm: ‘To write a program in C to perform 2D translation Algoritiy Start the program. Initialize the variables. Call the initgraph() funetion. “Get the input forthe source image and Translation factors 5. Add the translatic n vector with the existing original co-ordinates: 6, Get the new co-ordinates for the resultant image: 7. Redraw the resultant image. 8, Stop the graphies driver 9, Stop the program, Hinelude Hinelude dinclude int n,ia{ L00}{100} t%.ty void input printenter the no of vertices") scantt*%d" Sn); for(i=0; cnjit+) { printQ"\n enter the co-ordinites Prantyoadoz” Sal LO} 8 ‘ \ } void output() it Sali 1}O} oi 1ILIDS cl for(indsiensit) tnocoisto}alil yafie 10} 0" IL yi } } void transtationO output print{(“enter the transformation vertex txty:")s scantt"%d%d" Sex, Sty); for(i=O;i<=nsi++) t afi}(O}=ali}[0]+4x; ed Ty; output; } void main() ‘ int gd-DETECT, gm; initgraph( &gd,Sgm,"Z\BGIN"); ‘input0; translation(); getch(); ) Output: enter the no of vertices:3 enter the co-ordinates:50 50 50 100 enter the co-ordinates:50 100 75 75 enter the co-ordinates:75 75 50 50 enter the transformation vertex tx,ty:10 10 “Thus the program in C to perform 2D translation was done successfully. ATION - ROTATI VAL TRANSFORM, To write a program in C to perform 2D Rotation. Algorithm: 1. Start the program. 2. Initialize the variables 3. Call the initgraph () fimetion, 4. Get the input for the source image and rotation angle from user 5.” Process the existing original co-ordinates with rotation angle. 6. Get the new co-ordinates for the resultant image. 7. Redraw the resultant image. 8. Stop the graphics driver. 9. Stop the program. #include #include ‘#include #include int n,ia[100]{100],t%,fy,y,k,temp; void input() { printf("enter the no of vertices scani"%d",&n); forli=O;i #include #include int n,,a{100][100},fx,fyloat sx.sy, void input() it printf("enter the no of vertices: scanf("%d",&n); forli=0;i #include #inclade #include int x1,y1,x2,y2,d,a[20]{2].n ‘void input(); void output(); void reflection); void inpus() t x1=0,yl=getmaxy()/2,x2=getmaxx(),y2=getmaxy()25 printf("enter the no of vertices:"); scant{"%d",é&n); for(i=0;i 1 #include tinelude ‘Hinelude void main() { int poly[15),iv; float sh.temp; int gd=DETECT, gm; initgraph(&ed,&gm,"Z:\\BGN\"); print{("Enter the number of vertices"); | min) seanf("%d",&v); print{(" Enter the number of co-ordinates: fortir0;i { seanf("%d",&polyli)); } iit+) printi("Enter the shearing constant: scank("%M",&sh); poly[0]; poly[2*v+1 }=poly[1); cleardevicet); setcolor(9); drawpoly(v+1,poly); oly: polyli}=poly{i]+sh*polyfi+ 1}; poly[it1 }=polyfit1}+sh*temp; : poly{2*v}=poly{0); Boly{2*v1}=poly(1]; | settextstyle(1,0,2); outtextxy(300,40, setcolor(5); drawpoly(v+1,poly); getch(); ) Output Enter the number of vertices:4 Enter the co-ordinates:50 50 100 50 Enter the co-ordinates: 100 50 100 100 Enter the co-ordinates: 100 100 50 100 Enter the co-ordinates:50 100 50 $0 he Shearing"); Enter the shear value:5 | Enter the fixed point :50 100 Enter the axis for shearing if x-axis then | 7 if y-axis then 0 ———— COMPOSITE 2D TRANSFORMATIONS, Aim: To write a C program to perform composite 2D transformation. 1, Start the program i 2, Declare the variables and initialise the graphics driver, 3. Get the translation factors, scaling factors, and angle of rotations form the user. 4. Call the input() function to draw the polygon. 5. Then perform the composite transformations, | 6. Display the corresponding outputs by calling the output(). { 7. Stop the driver, 8. Stop the program, rrogra lude #include #include finclude int nLa[20][2]. ft y,tx,ty,temp; float sx,sy,k; Void input() { rint{("enter the no of vertices: scan("%d", for(i=O;i #include #include typedef unsigned int outcode; enum { TOP=0x1, BOTTOM=0x2, RIGHT=0x4, LEFT=0x8 }; void lineclip(x0,y0,x1,y,xwmin,ywmin,xwmax.ywmax ) float x0,y0,x1,y1.21 in,ywmin,xwmax,ywmax; { int gd,gm; outcode code0,code! ,codeout; int accept = 0, done=0; code0 = calcode(x0,y0,xwmin, ywmin,xwmax,ywmax); codel = calcode(x,y1,xwmin,ywmin,xwmax.ywmax); do t ifl(code0 | codel)) { accept =1 ; done =1; 3 else iflcode0 & code!) done = else { float x,y; codeout = code0 ? code0 : codel; ifleodcout & TOP) { X= x0 + (x1-x0)*(ywmax-y0)(y1-y0); y= ywmax; } else if{ codeout & BOTTOM) { X= x0 + (x1-x0)*(ywmin-y0)/(yl-y0); y= ywmin; i } else if ( codeout & RIGHT) { x0=x; ¥0=y; Code0=calcode(x0,y0,xwmin, ywmin,xwmax,ywmax); else { xl= yl=y alcode(x1,y1,xwmin,ywmin,xwmax,ywmax); while( done = 0); iffaccept) line(x0,y0,x1,y1); rectangle(xwmin, ywmin, xwmax,ywmax); getchQ); } int caleode (x,y,xwmin, ywmin,xwmax,ywmax) float x,y,xwmin,ywmin,xwmax,ywmax; { int code =0; ifly> ywmax) code |-TOP; else if y xwmax) code |= RIGHT; else if(x< xwmin) code |= LEFT; return(code); | void main() ‘ float x2,y2,x1,y1,xwmin, ywmin, xwmax, ywmax; | int g¢=DETECT, gm; | etrserQ; | initgraph( &gd,&gm,"Z:\\bgi"); \ printf("\n\n\tEnter the co-ordinates of Line | Print{"\nin\tX YT :"; | Scant"%f%I",&x1,&yl); | { print{{"\nin\tX2 Y2 :"); | scanfl"%f %f",&x2,&y2); | | rint{("\nitEnter the co_ordinates of window :\n"); | print"\n\txwmin , ywmin :"); | seanf{"%F %f", &xwmin, &ywmnir Print{"\n\oowmax , ywmax : "); seanfl"%f %f",Sexwmax,&ywmax): cleardevice(); | Tine(x1,y1,x2,y2); Tectangle(xwmnin, ywmin, xwmax,ywmax); { getch(); cleardevice(); lineclip(x1,y1,x2,y2,xwmin,ywmin,xwmax,ywmax ); | getch(); closegraphQ; Output: Enter the co-ordinates of line: X1 yl:100 150 £ X2 y2:200 250 Enter the co-ordinates of window: Xwmin,ywmin: 110 140 Xwmax,ywmax:210 240 NX NI Result: ‘Thus the C program to implement Cohen Sutherland line clipping algorithm was done successfully, (OHEN SUTHERLAND WINDOWING ALGORITHM Aim: To write a C program to implement Cohen Sutherland windowing algorithm Algorith : 1. Start the program 2, Declare the variables and initialise the graphies driver. 3. Get the Window coordinates and end coordinates of the images inside the window form the user. 4. Call the clip function to find the points that are to be clipped by the window 5. Then perform the transformations. 6, Display the corresponding output of clipped objects 7. Stop the driver, 8. Stop the program. Program: #include #include finclude int getpoint; void draw(); void wintran(); void format(); void main() { int gd=DETECT, gm,errorcode; cliser(); initgraph(&gd,&gm,"E\\TC\BGIN\"); setbkeolor(13);, draw() format( getch(); wintran(); getch(); élosegraph(); } void draw() { setcolor(4); rectangle(80,80,520,478); rectangle(100,100,300,300); ‘outtextxy(250,250," graphics"); circle(250,250,40); rectangle(200,200,280,280); } void wintran() { int igs for(i 0;i<=80;i++) { for(j=0;j<=80;j++) { setcolor(3); getpoint=getpixel(i+200,j+200); putpixel(2%i+300,2*}+300,getpoint); } } getch(); setbkcolor(15); setviewport(200,200,280,280, 1); clearviewportQ; void format() { setcolor(4); settextstyle(1,0,4); outtextxy(40,40,"WINDOWING TRANSFORMATION"); } Output: graphics graphic ‘Thus the C program to implement Cohen Sutherland line clipping algorithm was done successfully. UL SUTHERLAND - IODGEMAN POLYGON CLIPPING ALGORITHM Aim: To write a C program to implement Sutherland Hodgeman polygon clipping algorithm. Algorith 1. Start the program 2. Declare the variables and initialise the graphics driver. 3. Get the Window coordinates and end coordinates of the polygon form the user. 4, Call the clip function to find the points that are to be clipped by the window. 5. Then perform the transformations. 6. Display the corresponding output of clipped sides of the polygon. 7. Stop the driver. 8. Stop the program. Program: #include ude Hinclude #define round(a) ((int)(a+0.5)) int k; float xmin, ymin,xmax,ymax,arr{20],m; void clipi(float x1, float yl, float x2,float y2) { if(x2-x1) me Q2-yIA2-K1); else m=100000; iffx>=xmin && x2>=xmin) } iffel=xmin) { an{k]=xmin; an{k+l}=yl+m*(xmin-x1); anfkt arr{k+3}=y2; ki=4; if{xl>=xmin && x2ymax && y2<=ymax) { am{kJ=x1-+m*(ymax-yl); am[k+t]=ymax; art{k+2]2; an{k+3}=y2; kt+=4; } iffyl<=ymax && y2>ymax) t an{kJ=x1-+m*(ymax-yl); arr[k+1]-ymax; k=: } } void clipr(float x1,float yl, float x2,float y2) { iffx2-x1) m(y2-y1)/G2-x1); « else m=100000; ifxl<=xmax && x2<=xmax) pee if{x1>xmax &8& x2<=xmax) anfk}=xmax; anfk#l }-yl+m*(xmax-x1); amfk+2]=x2; ank+3]=y2; ki=4; } if{xl<=xmax && x2>xmax) an{k]=xmax; ar{k+1]=yl+m*(xmax-x1); k+=2; } 4 void clipb(float x1,float yl,float x2,float y2) { iffy2-yl) m=(X2-x1)/(y2-y1); else m=100000; iffy!>=ymin && y2>=ymin) { ar{k]=x2; an{k+1}-y2; ki=2; } iflyl=ymin) { } iffy!>=ymin && y2 #include #include #include int maxx,maxy,midx,midy; void axis() { getch(); cleardevice(); line(midx,0,midx,maxy); Jine(0,midy,maxx,midy); } yoid main() { int gd,gm.x,y,2,0,x1 a 2 detectgraph(&gd,&gm); . initgraph(&gd,gm,"E:\TC\BGIN"); setfillstyle(0,getmaxcolor()); maxx=getmaxx(); maxy=getmaxy(); midx=maxx/2; midy=maxy/2; axis(); =i } bar3d(mids+50,midy. print"Enter Translat 100,midx+60,midy-90,5,1); nll" %I%d", x, ey, axis() print{("ARer Translation"); bar3d(midx+(x+50),midy-(y+100), axis(); bar3d(midx+50,midy+100,midx+60,midy-90,5,1); printf("Enter Scaling Factor"); scantt"%d%d%d" &x,&ry, 2), axis(); print{("After Scaling"); imidx+x+60,midy-(y+90),5,1); bar3d(midx+(x*50),midy-(y* 100), midx+(<*60),midy-(y*90),5*2,1)s axis(); bar3d(midx+50,midy-100,midx+60,midy-90,5,1); print{("Enter Rotating Angle") scant{"%d",8&o); x1=50*cos(0*3.14/180)-100*sin(o*3.14/180); ’*cos(o*3.14/180)+100*sin(o*3.14/180) *sin(o*3.14/180)-90*cos(o*3.14/180); }*sin(o*3.14/180)+90*cos(0*3.14/180); axis(); print{("\nA ter Rotation about Z Axis"); bar3d(midxtx1,midy-yl,midx+x2,midy-y2,5,1); axis(); printi("\nA fter Rotation about X Axis"); bar3d(midx+50,midy-x1,midx+60,midy-x2,5,1); axis); printf("\WwAfter Rotation about Y Axis"); bar3d(midx+x 1,midy-100,midx+x2,midy-90,5,1); getch(); closegraphQ; Outpu Translation Enter Translation Factor : 20 20 20 After Translation OG , {2 | Sealing Enter Scaling Factor : 225 Rotation Enter Rotating Angle : 60 | After Scaling After Rotation about Z-Axis After Rotation about X-Axis After Rotation about Y-Axis Thus the C program to implement 3D transformations was done successfully COMPOSITE THREE DIMENSIONAL TRANSFORMATIONS To write aC program to implement composite 3D transformations. 1, Start the program, 2, Declare the variables and initialise the graphics driver. 3. Get the translation factors, sealing factors form the user 4. Call the function to draw the 3¢ bar, 5. Then perform the composite transformations. wi 6. Display the corresponding outputs. 7. Stop the driver. 8. Stop the program. Program: Hinclude #include Hinclude #includesctype.h> #includecmath.h> fincludesstdlib.h> * void draw_bar3d() { intxL,x2,yLy2i,tx,ty,t2,25 float sx,Sy,52; x1=200;y1=200;x2=250;y2=250,2=20; bar3d(x1,y1,x2,y2,2,1); printf("Enter translation factor and scale facto scanf{"%d%edved%f%I%F", Bite, Sty, &tz,&sx,Bsy,Bs2); getch(); cleardevice(); printf("After translation:\n"); setcolor(111); xlexLttyyl=ylttyix2=x24bcy2=y2+ty; bar3d(x,y1,32,y2,2,1); getch(); cleardevice(); xlex1*sxylayltsy;2=24s2; printf("After scaling:\n"); bar3d(x1,y1,x2,y22,2); getch(); Void main() { int x1,y1,x2,y2,ch; int gdriver=DETECT,gmode,gerror; initgraph(&gdriver,&gmode,"D:\\TC\\BGI\\") draw_bar3d\); pial getchl); ) Outp Enter translation factor and scale factor: 100 100 20 1.50.5 1 . Oo After translation: Miter scaling: successfully Cy Thus the C program to implement composite 3D transformations was done

You might also like