Lab Exercise
Lab Exercise
Lab Exercise
LINE : ALGORITHM :
Step 1 : Start. Step 2 : Initialize the graphics header files and functions. Step 3 : Declare the required variables and functions. Step 4 : Get the four points for drawing a line namely x1,x2,y1,y2. Step 5 : Draw the line using the algorithm. Step 6 : Display the output. Step 7 : stop.
PROGRAM :
#include "stdio.h" #include "conio.h" #include "math.h" #include "graphics.h" main() { int gd=DETECT,gm; int xa,xb,ya,yb; int dx,dy,x,y,xend,p; initgraph(&gd,&gm,"c:\\tc\\bgi"); printf("Enter The Two Left Endpoints(xa,ya):\n");
scanf("%d%d",&xa,&ya); printf("Enter The Two Right Endpoints(xb,yb):\n"); scanf("%d%d",&xb,&yb); dx=abs(xa-xb); dy=abs(ya-yb); p=2*dy-dx; if(xa>xb) { x=xb; y=yb; xend=xa; } else { x=xa; y=ya; xend=xb; } putpixel(x,y,6); while(x<xend) { x=x+1; if(p<0) { p=p+2*dy;
2
OUTPUT : Enter The Two Left Endpoints(xa,ya): 234 124 Enter The Two Right Endpoints(xb,yb): 578 321
RESULT: Thus the Bresenhams algorithm was executed and verified successfully.
CIRCLE : ALGORITHM :
Step 1 : Start. Step 2 : Initialize the graphics header files and functions. Step 3 : Declare the required variables and functions. Step 4 : Get the co-ordinates and radius of the circle. Step 5 : Draw the circle using the algorithm. Step 6 : Display the output. Step 7 : stop.
PROGRAM :
#include<stdio.h> #include<conio.h> #include<math.h> #include<graphics.h> main() { int gd=DETECT,gm; int xcenter,ycenter,radius; int p,x,y; initgraph(&gd,&gm,"c:\\tc\\bgi"); x=0; printf("Enter The Radius Value:\n"); scanf("%d",&radius); y=radius; printf("Enter The xcenter and ycenter Values:\n");
4
scanf("%d%d",&xcenter,&ycenter); plotpoints(xcenter,ycenter,x,y); p=1-radius; while(x<y) { if(p<0) x=x+1; else { x=x+1; y=y-1; } if(p<0) p=p+2*x+1; else p=p+2*(x-y)+1; plotpoints(xcenter,ycenter,x,y); } getch(); return(0); } int plotpoints(int xcenter,int ycenter,int x,int y) { putpixel(xcenter+x,ycenter+y,1); putpixel(xcenter-x,ycenter+y,1);
5
OUTPUT : Enter The Radius Value : 80 Enter The xcenter and ycenter Values : 230 260
RESULT: The Bresenhams algorithm for circle was executed and verified successfully.
ELLIPSE : ALGORITHM :
Step 1 : Start. Step 2 : Initialize the graphics header files and functions. Step 3 : Declare the required variables and functions. Step 4 : Get the co-ordinates and radius of the ellipse. Step 5 : Draw the ellipse using the algorithm. Step 6 : Display the output. Step 7 : stop.
PROGRAM :
#include "stdio.h" #include "conio.h" #include "math.h" #include "graphics.h" main() { int gd=DETECT,gm; int xcenter,ycenter,rx,ry; int p,x,y,px,py,rx1,ry1,rx2,ry2; initgraph(&gd,&gm,"c:\\tc\\bgi"); printf("Enter The Radius Value:\n"); scanf("%d%d",&rx,&ry); printf("Enter The xcenter and ycenter Values:\n"); scanf("%d%d",&xcenter,&ycenter); ry1=ry*ry; rx1=rx*rx;
7
ry2=2*ry1; rx2=2*rx1; x=0; y=ry; plotpoints(xcenter,ycenter,x,y); p=(ry1-rx1*ry+(0.25*rx1)); px=0; py=rx2*y; while(px<py) { x=x+1; px=px+ry2; if(p>=0) y=y-1; py=py-rx2; if(p<0) p=p+ry1+px; else p=p+ry1+px-py; plotpoints(xcenter,ycenter,x,y); p=(ry1*(x+0.5)*(x+0.5)+rx1*(y-1)*(y-1)-rx1*ry1); while(y>0) { y=y-1; py=py-rx2; if(p<=0) { x=x+1; px=px+ry2; } if(p>0)
8
p=p+rx1-py; else p=p+rx1-py+px; plotpoints(xcenter,ycenter,x,y); } } getch(); return(0); } int plotpoints(int xcenter,int ycenter,int x,int y) { putpixel(xcenter+x,ycenter+y,6); putpixel(xcenter-x,ycenter+y,6); putpixel(xcenter+x,ycenter-y,6); putpixel(xcenter-x,ycenter-y,6); }
OUTPUT : Enter The Radius Value(Rx,Ry) : 10 30 Enter The xcenter and ycenter Values : 300 150
RESULT: The Bresenhams algorithm for ellipse was executed and verified successfully.
9
EX.NO:2 DATE: TWO DIMENSIONALTRANSFORMATION AIM To write a c program to implement 2D transformation of image. ALGORITHM
Step 1:Start the program. Step 2:Draw the image with default parameters. Step 3 : Get the choice from the user. Step 4: Get the parameters for transformation. Step 5 : Perform the transformation. Step 6 : Draw the image. Step 7 : Stop the program.
PROGRAM:
# include<graphics.h> # include<stdio.h> # include<conio.h> # include<math.h> void main() { int gd=DETECT,gm,i,j,k,ch; float tx,ty,x,y,ang,n,temp; float a[5][3],si,co,b[5][3],c[5][3]; initgraph(&gd,&gm,"e:\\tcpp\\bgi"); n=4;
10
a[0][0]=0; a[0][1]=0; a[1][0]=100; a[1][1]=0; a[2][0]=100; a[2][1]=100; a[3][0]=0; a[3][1]=100; a[4][0]=0; a[4][1]=0; while(1) { cleardevice(); gotoxy(1,8); printf("\n\t******** Program to perform 2-D Transformations ********"); printf("\n\t\t\t 1. Accept the polygon"); printf("\n\t\t\t 2. Perform translation"); printf("\n\t\t\t 3. Perform scaling"); printf("\n\t\t\t 4. Perform rotation"); printf("\n\t\t\t 5. Perform reflection"); printf("\n\t\t\t 6. Perform shearing"); printf("\n\t\t\t 7. Exit"); printf("\n\t\t\t Enter your choice::"); scanf("%d",&ch); switch(ch)
11
{ case 1: cleardevice(); gotoxy(1,1); printf("\n\tEnter no of points.:"); scanf("%f",&n); for(i=0;i<n;i++) { printf("\n\t Enter x,y co-ordinates for %d::",i+1); scanf("%f %f",&a[i][0],&a[i][1]); } a[i][0]=a[0][0]; a[i][1]=a[0][1]; cleardevice(); for(i=0;i<n;i++) line(320+a[i][0],240-a[i][1],320+a[i+1][0],240-a[i+1][1]); line(0,240,639,240); line(320,0,320,479); getch(); break; case 2: cleardevice(); for(i=0;i<n;i++) line(320+a[i][0],240-a[i][1],320+a[i+1][0],240-a[i+1][1]); line(0,240,639,240);
12
line(320,0,320,479); gotoxy(1,1); printf("Enter translation vectors tx and ty\n\t"); scanf("%f %f",&x,&y); cleardevice(); for(i=0;i<n;i++) line(320+a[i][0]+x,240-(a[i][1]+y),320+a[i+1][0]+x,240-(a[i+1][1]+y)); line(0,240,639,240); line(320,0,320,479); getch(); break; case 3: cleardevice(); for(i=0;i<n;i++) line(320+a[i][0],240-a[i][1],320+a[i+1][0],240-a[i+1][1]); line(0,240,639,240); line(320,0,320,479); gotoxy(1,1); printf("Enter scaling vectors tx and ty\n\t"); scanf("%f %f",&x,&y); if(x==0) x=1; if(y==0) y=1; cleardevice();
13
for(i=0;i<n;i++) line(320+(a[i][0]*x),240-(a[i][1]*y),320+(a[i+1][0]*x),240-(a[i+1][1]*y)); line(0,240,639,240); line(320,0,320,479); getch(); break; case 4: cleardevice(); for(i=0;i<n;i++) line(320+a[i][0],240-a[i][1],320+a[i+1][0],240-a[i+1][1]); line(0,240,639,240); line(320,0,320,479); gotoxy(1,1); printf("Enter the angle of rotation\n\t"); scanf("%f",&ang); ang=ang*0.01745; gotoxy(1,3); printf("Enter point of rotation\n\t"); scanf("%f %f",&x,&y); gotoxy(1,5); printf("1.clockwise 2.anticlockwise\n\t"); scanf("%d",&k); si=sin(ang); co=cos(ang); for(i=0;i<n+1;i++)
14
{ c[i][0]=a[i][0]; c[i][1]=a[i][1]; c[i][2]=1; } b[0][0]=co; b[0][1]=si; b[0][2]=0; b[1][0]=(-si); b[1][1]=co; b[1][2]=0; b[2][0]=(-x*co)+(y*si)+x; b[2][1]=(-x*si)-(y*co)+y; b[2][2]=1; if(k==1) { b[0][1]=(-si); b[1][0]=(si); b[2][0]=(-x*co)-(y*si)+x; b[2][1]=(-x*si)+(y*co)+y; } for(i=0;i<n+1;i++) { for(j=0;j<3;j++) {
15
a[i][j] = 0 ; for(k=0;k<3;k++) a[i][j] = a[i][j] + c[i][k] * b[k][j] ; } } cleardevice(); for(i=0;i<n;i++) line(320+a[i][0],240-a[i][1],320+a[i+1][0],240-a[i+1][1]); line(0,240,639,240); line(320,0,320,479); getch(); break; case 5: cleardevice(); for(i=0;i<n;i++) line(320+a[i][0],240-a[i][1],320+a[i+1][0],240-a[i+1][1]); line(0,240,639,240); line(320,0,320,479); gotoxy(1,1); printf("\n1.Reflection about Y-axis"); printf("\n2.Reflection about X-axis"); printf("\n3.Reflection about origin"); printf("\n4.Reflection about line y=x"); printf("\n5.Reflection about line y=-x"); printf("\nEnter your choice:"); scanf("%d",&ch);
16
switch(ch) { case 1: for(i=0;i<n+1;i++) a[i][0]=a[i][0]*(-1); /* Plot the polygon */ cleardevice(); for(i=0;i<n;i++) line(320+a[i][0],240-a[i][1],320+a[i+1][0],240-a[i+1][1]); line(0,240,639,240); line(320,0,320,479); getch(); break; case 2: for(i=0;i<n+1;i++) a[i][1]=a[i][1]*(-1); cleardevice(); for(i=0;i<n;i++) line(320+a[i][0],240-a[i][1],320+a[i+1][0],240-a[i+1][1]); line(0,240,639,240); line(320,0,320,479); getch(); break; case 3: for(i=0;i<n+1;i++)
17
{ a[i][1]=a[i][1]*(-1); a[i][0]=a[i][0]*(-1); } cleardevice(); for(i=0;i<n;i++) line(320+a[i][0],240-a[i][1],320+a[i+1][0],240-a[i+1][1]); line(0,240,639,240); line(320,0,320,479); getch(); break; case 4: for(i=0;i<n+1;i++) { temp=a[i][0]; a[i][0]=a[i][1]; a[i][1]=temp; } cleardevice(); for(i=0;i<n;i++) line(320+a[i][0],240-a[i][1],320+a[i+1][0],240-a[i+1][1]); line(0,240,639,240); line(320,0,320,479); line(0,479,639,0); getch();
18
break; case 5: for(i=0;i<n+1;i++) { temp=a[i][0]; a[i][0]=a[i][1]; a[i][1]=temp; } for(i=0;i<n+1;i++) { a[i][1]=a[i][1]*(-1); a[i][0]=a[i][0]*(-1); } cleardevice(); for(i=0;i<n;i++) line(320+a[i][0],240-a[i][1],320+a[i+1][0],240-a[i+1][1]); line(0,240,639,240); line(320,0,320,479); line(0,0,639,479); getch(); break; default: break; } break;
19
case 6: cleardevice(); for(i=0;i<n;i++) line(320+a[i][0],240-a[i][1],320+a[i+1][0],240-a[i+1][1]); line(0,240,639,240); line(320,0,320,479); gotoxy(1,1); printf("\n1.X shear with y reference line"); printf("\n2.Y shear with x reference line"); printf("\nEnter your choice:"); scanf("%d",&ch); switch(ch) { case 1: printf("\nEnter the x-shear parameter value:"); scanf("%f",&temp); printf("\nEnter the yref line"); scanf("%f",&ty); b[0][0]=1; b[0][1]=0; b[0][2]=0; b[1][0]=temp; b[1][1]=1; b[1][2]=0; b[2][0]=(-temp)*(ty); b[2][1]=0;
20
b[2][2]=1; for(i=0;i<n+1;i++) a[i][2]=1; for(i=0;i<n+1;i++) { for(j=0;j<3;j++) { c[i][j] = 0 ; for(k=0;k<3;k++) c[i][j] = c[i][j] + a[i][k] * b[k][j] ; } } cleardevice(); for(i=0;i<n;i++) line(320+c[i][0],240-c[i][1],320+c[i+1][0],240-c[i+1][1]); line(0,240,639,240); line(320,0,320,479); getch(); break; case 2: printf("\nEnter the y-shear parameter value:"); scanf("%f",&temp); printf("\nEnter the xref line"); scanf("%f",&tx); b[0][0]=1;
21
b[0][1]=temp; b[0][2]=0; b[1][0]=0; b[1][1]=1; b[1][2]=0; b[2][0]=0; b[2][1]=(-temp)*(tx); b[2][2]=0; for(i=0;i<n+1;i++) a[i][2]=1; for(i=0;i<n+1;i++) { for(j=0;j<3;j++) { c[i][j] = 0 ; for(k=0;k<3;k++) c[i][j] = c[i][j] + a[i][k] * b[k][j] ; } } cleardevice(); for(i=0;i<n;i++) line(320+c[i][0],240-c[i][1],320+c[i+1][0],240-c[i+1][1]); line(0,240,639,240); line(320,0,320,479); getch();
22
break; default: break; } break; case 7: exit(1); closegraph(); restorecrtmode(); break; default: break; } } }
OUTPUT
1. Translation 2. Rotation 3. Scaling 4. Shearing 5. Reflection 6. Exit
23
TRANSLATION Enter the choice: 1 Enter the number of Vertices: 3 Enter the co-ordinates: 30 150 10 200 Enter the co-ordinates: 10 200 60 200 Enter the co-ordinates: 60 200 30 150
ROTATION Enter the choice : 2 Enter the number of Vertices: 3 Enter the coordinates : 30 150 10 200
24
Enter the coordinates : 10 200 60 200 Enter the coordinates : 60 200 30 150
Enter the Rotating Angle : 90 Enter the Pivot Point : 100 200
SCALING Enter the choice : 3 Enter the number of Vertices: 3 Enter the coordinates : 30 150 10 200 Enter the coordinates : 10 200 60 200 Enter the coordinates : 60 200 30 150
25
Enter the scaling Factor : 0.3 0.4 Enter the Fixed Point : 100 200
SHEARING Enter the choice : 4 Enter the number of Vertices: 3 Enter the coordinates : 30 150 10 200 Enter the coordinates : 10 200 60 200 Enter the coordinates : 60 200 30 150
26
Enter the shear Value : 5 Enter the fixed point : 50 100 Enter the Axis for shearing if x-axis then 1 if y-axis then 0
27
REFLECTION Enter the choice : 5 Enter the number of Vertices: 3 Enter the coordinates : 30 150 10 200 Enter the coordinates : 10 200 60 200 Enter the coordinates : 60 200 30 150
RESULT Thus the program was completed and the output was obtained successfully.
28
EX. NO:3 DATE: COHEN SUTHERLAND LINE CLIPPING ALGORITHM AIM : To write a C program to perform line clipping using Cohen Sutherland Algorithm. ALGORITHM :
Step 1: Start Step 2: Get the bottom-left coordinate of view port from the user. Step 3: Get the top-right coordinate of view port from the user. Step 4: Get the coordinates of 1st end point of line from the user. Step 5: Get the coordinates of 2nd endpoint of line from the user. Step 6: Print the region code of the first and second point. Step 7: If the points lie inside the view port, print The line is totally visible. Step 8: If the starting point lies outside the view port, print The line is invisible Step 9: If the starting point lies inside the view port and the ending point lies outside the view port, print The line is partially visible Step 10: Clip the line present outside the view port. Step 11: Draw the clipped line present inside the view port. Step 12: Stop PROGRAM: #include<stdio.h> #include<graphics.h> //#include<process.h> void main() { int gd=DETECT, gm;
29
float i,xmax,ymax,xmin,ymin,x11,y11,x22,y22,m; float a[4],b[4],c[4],x1,y1; clrscr(); initgraph(&gd,&gm,"c:\\tc\\bgi"); printf("\nEnter the bottom-left coordinate of viewport: "); scanf("%f %f",&xmin,&ymin); printf("\nEnter the top-right coordinate of viewport: "); scanf("%f %f",&xmax,&ymax); rectangle(xmin,ymin,xmax,ymax); printf("\nEnter the coordinates of 1st end point of line: "); scanf("%f %f",&x11,&y11); printf("\nEnter the coordinates of 2nd endpoint of line: "); scanf("%f %f",&x22,&y22); line(x11,y11,x22,y22); for(i=0;i<4;i++) { a[i]=0; b[i]=0; } m=(y22-y11)/(x22-x11); if(x11<xmin) a[3]=1; if(x11>xmax) a[2]=1; if(y11<ymin)
30
a[1]=1; if(y11>ymax) a[0]=1; if(x22<xmin) b[3]=1; if(x22>xmax) b[2]=1; if(y22<ymin) b[1]=1; if(y22>ymax) b[0]=1; printf("\nRegion code of 1st pt "); for(i=0;i<4;i++) { printf("%f",a[i]); } printf("\nRegion code of 2nd pt "); for(i=0;i<4;i++) { printf("%f",b[i]); } printf("\nAnding : "); for(i=0;i<4;i++) { c[i]=a[i]&&b[i];
31
} for(i=0;i<4;i++) printf("%f",c[i]); getch(); if((c[0]==0)&&(c[1]==0)&&(c[2]==0)&&(c[3]==0)) { if((a[0]==0)&&(a[1]==0)&&(a[2]==0)&&(a[3]==0)&& (b[0]==0)&&(b[1]==0)&&(b[2]==0)&&(b[3]==0)) { clrscr(); clearviewport(); printf("\nThe line is totally visible\nand not a clipping candidate"); rectangle(xmin,ymin,xmax,ymax); line(x11,y11,x22,y22); getch(); } else { clrscr(); clearviewport(); printf("\nLine is partially visible"); rectangle(xmin,ymin,xmax,ymax); line(x11,y11,x22,y22); getch(); if((a[0]==0)&&(a[1]==1))
32
{ x1=x11+(ymin-y11)/m; x11=x1; y11=ymin; } else if((b[0]==0)&&(b[1]==1)) { x1=x22+(ymin-y22)/m; x22=x1; y22=ymin; } if((a[0]==1)&&(a[1]==0)) { x1=x11+(ymax-y11)/m; x11=x1; y11=ymax; } else if((b[0]==1)&&(b[1]==0)) { x1=x22+(ymax-y22)/m; x22=x1; y22=ymax; } if((a[2]==0)&&(a[3]==1)) { y1=y11+m*(xmin-x11);
33
y11=y1; x11=xmin; } else if((b[2]==0)&&(b[3]==1)) { y1=y22+m*(xmin-x22); y22=y1; x22=xmin; } if((a[2]==1)&&(a[3]==0)) { y1=y11+m*(xmax-x11); y11=y1; x11=xmax; } else if((b[2]==1)&&(b[3]==0)) { y1=y22+m*(xmax-x22); y22=y1; x22=xmax; } clrscr(); clearviewport(); printf("\nAfter clippling:"); rectangle(xmin,ymin,xmax,ymax);
34
line(x11,y11,x22,y22); getch(); } } else { clrscr(); clearviewport(); printf("\nLine is invisible"); rectangle(xmin,ymin,xmax,ymax); getch(); } closegraph(); getch(); }
OUTPUT
Enter the bottom-left coordinate of viewport: 100 100 Enter the top-right coordinate of viewport: 200 100 Enter the coordinates of 1st end point of line: 110 150 Enter the coordinates of 2nd endpoint of line: 200 250
The line is partially visible.
35
After clipping.
RESULT Thus the program to perform line clipping by Cohen Sutherland algorithm was executed and the output was obtained.
36
ALGORITHM :
Step 1 : Start. Step 2 : Draw an image with default parameters. Step 3 : Get the choice from user. Step 4 : Get the parameters for transformation. Step 5 : Perform the transformation. Step 6 : Display the output. Step 7 : Stop.
PROGRAM :
#include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h>
37
line(0,midy,maxx,midy); } void main() { int gd,gm,x,y,z,o,x1,x2,y1,y2; detectgraph(&gd,&gm); initgraph(&gd,&gm," "); 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(); 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");
38
bar3d(midx+(x*50),midy-(y*100),midx+(x*60),midy-(y*90),5*z,1); axis(); 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(); }
39
After Translation
40
Scaling
Enter Scaling Factor : 80 90 95
After Scaling
Rotation
Enter Rotating Angle : 60
41
RESULT :
Thus the program was executed and the output was obtained successfully.
42
ALGORITHM :
Step 1 : start. Step 2 : Draw any image in the three dimensional plane. Step 3 : Get the choice of axis as input from the user. Step 4 : Perform the projection about the desired axis. Step 5 : Display the projected image. Step 6 : Stop.
PROGRAM:
#include<stdio.h> #include<math.h> #include<conio.h> #include<stdlib.h> #include<graphics.h> int gd=DETECT,gm; double x1,x2,y1,y2; void draw_cube(double edge[20][3]) { int i; initgraph(&gd,&gm,"..\bgi");
43
clearviewport(); for(i=0;i<19;i++) { x1=edge[i][0]+edge[i][2]*(cos(2.3562)); y1=edge[i][1]-edge[i][2]*(sin(2.3562)); x2=edge[i+1][0]+edge[i+1][2]*(cos(2.3562)); y2=edge[i+1][1]-edge[i+1][2]*(sin(2.3562)); line(x1+320,240-y1,x2+320,240-y2); } line(320,240,320,25); line(320,240,550,240); line(320,240,150,410); getch(); closegraph(); } void perspect(double edge[20][3]) { int ch; int i; float p,q,r; clrscr(); printf("\n -=[ Perspective Projection About ]=-"); printf("\n 1:==>X-Axis "); printf("\n 2:==>Y-Axis "); printf("\n 3:==>Z-Axis ");
44
printf("\n Enter Your Choice :="); scanf("%d",&ch); switch(ch) { case 1: printf("\n Enter P :="); scanf("%f",&p); for(i=0;i<20;i++) { edge[i][0]=edge[i][0]/(p*edge[i][0]+1); edge[i][1]=edge[i][1]/(p*edge[i][0]+1); edge[i][2]=edge[i][2]/(p*edge[i][0]+1); } draw_cube(edge); break; case 2: printf("\n Enter Q :="); scanf("%f",&q); for(i=0;i<20;i++) { edge[i][1]=edge[i][1]/(edge[i][1]*q+1); edge[i][0]=edge[i][0]/(edge[i][1]*q+1); edge[i][2]=edge[i][2]/(edge[i][1]*q+1); } draw_cube(edge);
45
break; case 3: printf("\n Enter R :="); scanf("%f",&r); for(i=0;i<20;i++) { edge[i][2]=edge[i][2]/(edge[i][2]*r+1); edge[i][0]=edge[i][0]/(edge[i][2]*r+1); edge[i][1]=edge[i][1]/(edge[i][2]*r+1); } draw_cube(edge); break; } closegraph(); } void main() { int choice; double edge[20][3]= { 100,0,0,100,100,0,0,100,0,0,100,100,0,0,100,0,0,0, 100,0,0,100,0,100,100,75,100,75,100,100,100,100,75, 100,100,0,100,100,75,100,75,100,75,100,100,0,100,100, 0,100,0,0,0,0,0,0,100,100,0,100 }; clrscr(); draw_cube(edge); perspect(edge);
46
closegraph(); }
OUTPUT :
47
RESULT : Thus the projections on the three dimensional images was performed successfully and the output was verified.
48
PROGRAM :
#include "math.h" #include <stdio.h> #define MIN(a,b) (a<b?a:b) #define MAX(a,b) (a>b?a:b) #define NO_HUE -1 void rgbtohsv(float r,float g,float b,float *h,float *s,float *v) { float max=MAX(r,MAX(g,b)),min=MIN(r,MIN(g,b)); float delta=max-min; *v=max; if(max!=0.0)
49
*s=delta/max; else *s=0.0; if(*s==0.0) *h=NO_HUE; else { if(r==max) *h=(g-b)/delta; else if(g==max) *h=2+(b-r)/delta; else if(b==max) *h=4+(r-g)/delta; *h*=60.0; if(*h<0) *h+=360.0; *h/=360.0; } printf("H=%f S= %f V=%f",*h,*s,*v); } void main() { float a,b,c,d,e,f; clrscr(); printf("Enter the RGB and HSV values :");
50
OUTPUT :
Enter RGB and HSV Values: 0.1 0.2 0.3 0.4 0.5 0.6 H= 0.5833 S= 0.6667 V= 0.3000
51
PROGRAM :
#include "stdio.h" #include "conio.h" #include "math.h" void hsvtorgb(float h,float s,float v,float *r,float *g,float *b) { int i; float aa,bb,cc,f; if(s==0) *r=*g=*b=v; else { if(h==1.0)h=0; h*=6.0; i=floor(h); f=h-i; aa=v*(1-s); bb=v*(1-(s*f)); cc=v*(1-(s*(1-f))); switch(i) { case 0: *r=v; *g=cc; *b=aa;
52
printf("R= %f G=%f B=%f",*r,*g,*b); break; case 1: *r=bb; *g=v; *b=aa; printf("R= %f G=%f B=%f",*r,*g,*b); break; case 2: *r=aa; *g=v; *b=cc; printf("R= %f G=%f B=%f",*r,*g,*b); break; case 3: *r=aa; *g=bb; *b=v; printf("R= %f G=%f B=%f",*r,*g,*b); break; case 4: *r=cc; *g=aa; *b=v; printf("R= %f G=%f B=%f",*r,*g,*b);
53
break; case 5: *r=v; *g=aa; *b=bb; printf("R= %f G=%f B=%f",*r,*g,*b); break; } } } void main() { float a,b,c,d,e,f; clrscr(); printf("\n Enter HSV and RGB Values :"); scanf("%f%f%f%f%f%f",&a,&b,&c,&d,&e,&f); hsvtorgb(a,b,c,&d,&e,&f); getch(); }
54
OUTPUT :
Enter HSV and RGB Values: 0.1 0.2 0.3 0.4 0.5 0.6 R= 0.3000 G= 0.2760 B= 0.2400
RESULT :
Thus the program was executed and the output is obtained.
55
ALGORITHM :
Step 1 : Start. Step 2 : Create the input file and get the text to be compressed as input from the user. Step 3 : Using the file pointer scan the input file. Step 4 : Compress the text using the algorithm. Step 5 : Print the compressed code in the output file. Step 6 : Stop.
PROGRAM :
#include<stdio.h> #include<conio.h> #include<stdlib.h> void main() { int a[20],b[20],c[20],d[20],e[10],k[10],q[10],r[10]; int max,m,n,temp,temp1,x=0,y=0,count=0,count1=0; char c1,u; char /*c1='A',*/a1,b1; char z[20][10]={{"0"},{"1"},{"00"},{"01"},{"10"},{"11"},{"000"},{"001"},{"010"},{"011"},{"1 00"},{"101"},{"110"},{"111"}}; int i,j;
56
FILE *fp; char alp; clrscr(); fp=fopen("HUFF.TXT","r"); while((c1=fgetc(fp))!='$') count++; fclose(fp); fp=fopen("HUFF.TXT","r"); for(i=0;i<count;i++) { c1=fgetc(fp); a[i]=c1; } for(i=0;i<count;i++) { for(j=0;j<count;j++) { if(a[i]<a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } }
57
for(i=0;i<count;i++) { for(j=0;j<count;j++) { if(a[j]==u) count++; } b[i]=count1; u++; count1=0; } i=0; while(b[i]!=0) { y++; i++; } for(i=0;i<y;i++) { if(b[i]!=0) d[i]=b[i]; x++; } alp=a[0]; x=0; k[0]=a[0];
58
for(i=1;i<count;i++) { if(a[i]!=alp) { k[++x]=a[i]; alp=a[i]; } } for(i=0;i<x+1;i++) { for(j=0;j<i;j++) { if(d[i]==d[j]) { temp=d[i]; d[i]=d[j]; d[j]=temp; } temp1=k[i]; k[i]=k[j]; k[j]=temp1; fclose(fp); fp=fopen("HUFF.TXT","r"); printf("\nCode After Huffmam Compression"); for(i=0;i<count;i++)
59
OUTPUT :
Text input : AAABBBCCC$ Coded output : 1110001111
RESULT :
Thus a program to implement text compression was executed and the output was verified.
60
ALGORITHM :
Step 1 : Start. Step 2 : Initialize the graphics header files and functions. Step 3 : Get an image from user as input. Step 4 : If the + button is pressed ZOOM IN the object. Step 5 : If the button is pressed ZOOM OUT the object. Step 6 : Quit the graphics mode by close graph function. Step 7 : Stop.
PROGRAM :
#include<graphics.h> #include<stdlib.h> #include<stdio.h> #include<conio.h> int main(void) { int gdriver=DETECT,gmode,errorcode; int midx,midy; int a,r1; char c;
61
int radius=100; int rleft=10,rright=10,rn1=6,rn2=10; initgraph(&gdriver,&gmode," "); errorcode=graphresult(); if(errorcode!=grOk) { printf("graphicserror:%s\n",grapherrormsg(errorcode)); printf("press any key to halt"); getch(); exit(1); } midx=getmaxx()/2; midy=getmaxy()/2; setcolor(getmaxcolor()); cleardevice(); circle(midx,midy,radius); ellipse(midx,midy,0,360,rn1,rn2); circle(midx-50,midy-50,rleft); circle(midx+50,midy-50,rright); ellipse(midx,midy+50,0,360,rn2,rn1); for(a=0;a<midx;a++) { c=getch(); if(c=='+') {
62
cleardevice(); circle(midx,midy,(radius=radius+2)); ellipse(midx,midy,0,360,rn1++,rn2++); r1=radius/2; circle(midx-radius+r1,midy-radius+r1,rleft++); circle(midx+radius-r1,midy-radius+r1,rright++); ellipse(midx,midy+r1,0,360,rn2++,rn1++); } else if(c=='-') { cleardevice(); circle(midx,midy,(radius=radius-2)); ellipse(midx,midy,0,360,rn1--,rn2--); r1=radius/2; circle(midx-radius+r1,midy-radius+r1,rleft--); circle((midx+radius-r1),(midy-radius+r1),rright--); ellipse(midx,midy+r1,0,360,rn2--,rn1--); } else break; } closegraph(); return 0; }
63
OUTPUT :
ZOOM IN :
ZOOM OUT :
Ex.no. :9 Date :
BOUNCING BALL
AIM:
To create a flash document for Bouncing a Ball. ALGORITHM: Step 1: New Document Lets begin by opening a new document (File > New). Just a Flash File (ActionScript 2.0) document is fine; the Actionscript version isnt relevant as we wont be using any. Step 2: Document Settings Use the Properties Inspector at the bottom of your screen to enter the Document Properties, click the dimensions button. First, give your document a title, then give the Stage (the area of your document which will be visible when published) dimensions of 600px by 400px. The background color well leave white, but change the Frame Rate to 21 fps (Frames Per Second). 21 fps gives a quick, smooth rate of animation.
Step 3: Layer Organization is key when working with Flash, so before we start drawing our ball, lets name the layer its going to sit on. Double click Layer 1 and call it "Ball."
65
Step 4: Ball Select the Oval Tool, define a stroke and a fill color, such as those shown. Next draw a uniform circle (by holding Shift and dragging the mouse). Also, use the Properties Inspector to give the Stroke a 3px weight. Step 5: Symbol Creation By creating symbols in Flash you can use multiple instances of the same objects within one file. Symbols are all accessible from the Library palette (Window > Library). By changing a symbol, all instances of that symbol within the file will be changed. Were going to turn our circle into a symbol which will become our ball. Select the whole circle and press F8. Name it "mc_ball," choose to turn it into a Movie Clip symbol and click OK.
Step 6: Shadow Add a second layer underneath the "Ball" layer and call it "Shadow." Within the first frame of this layer draw an ellipse to form a shadow under the ball. Remove the stroke and color it #E2E0E5. Turn it into a symbol just as you did before with the ball, then name it "mc_shadow."
66
Step 7: Position Position the two symbols at roughly the correct height to suggest that the ball is sitting on the ground. In the Align palette (Window > Align) check To Stage and center the two symbols horizontally.
Step 8: Add Keyframes Weve created our symbols in their most basic form, now lets begin to animate them. Hold Shift and click on frame 20 of both layers on the Timeline. Go to Modify > Timeline > Convert to Keyframes or press F6 to turn these frames into Keyframes. Keyframes represent points along the timeline where something happens. This point on the Timeline will be the end of the animation; in other words the ball will have bounced up from the ground, reached its apex and returned to this starting position. The same applies to our shadow: it will have faded as the ball rose and returned to this starting state at frame 20.
67
Step 9: High Point Now lets add the point at which the ball is at its highest. Select frame 10 on both layers and press F6 to turn them into Keyframes.
Step 10: Move it! With the Slider still at frame 10, move the ball symbol vertically upwards to some point on the Stage. Select the Free Transform Tool and make the instance of the "shadow_main" symbol wider.
Finally, with the shadow selected, go to Color on the Properties Inspector and change it to an Alpha value of 30%.
Step 11: Tweening Tweening is the process by which Flash automatically generates all the stages between an objects or shapes two states. Were going to tween the frames between our first frame and halfway along the animation at frame 10. Select any frame between 1 and 10 on both layers and refer once again to the
68
Properties Inspector. Choose Motion from the Tween select and youll see the resultant frames turn purple. Step 12: Easy Tiger Move the Slider back and forth along the timeline with your mouse and youll see the ball and shadow nicely animated. The speed of this movement is however completely uniform. We need the ball to decelerate as it reaches its apex and we can do this with ease (ha ha). Again, with one frame of each layer selected (within the tweened area) refer to the Properties Inspector. Change the Tweening to Ease out with a value of 100. This will slow down our climbing ball.
Step 13: Back Down to Earth Having successfully animated the rising of the ball, repeat the tweening steps for frames 11 20. This time ease the motion tween in -100, causing the ball to accelerate as it approaches the ground.
OUTPUT: Press Command + Enter to view your animated ball so far. It should look like the animation shown below.
69
ALGORITHM:
Step 1: First create a new Flash document and find an image to use as your background, I use this one (300x224 pixels). Resize your document to fit with your background and set the frame rate to 20 and click OK. Step 2: Drag and drop your photo onto your document, make sure it lies inside your document Step 3: Create a new layer, use the Oval Tool with empty border color and white fill color to draw a small circle.
Step 4: Use the Selection Tool to select it and press F8 to convert it to a movie clip type and name it mc_snow.
Step 5: Press Ctrl-F3 to bring up the Properties box if it is not showed up and name it snow in the Instance name input
70
Step 6: Select the Selection Tool (V) and click once on the Movie Clip (circle) to select it. Then, go to the Action Script Panel (F9) and enter this code inside the actions panel: onClipEvent (load) { this._xscale = this._yscale = Math.random() * 40 + 20; this._x = Math.random() * Stage.width; this._y = Math.random() * 200 - 200; this._alpha = Math.random() * 20 + 80; speed = Math.random()*3 + 2; } onClipEvent (enterFrame) { this._y += speed; if (this._y>=Stage.height) { this._y = -5; } }
Step 7: Create a new layer and name it action, click on its first frame and press F9 to bring up the Action panel then type in: for(k=0;k<100;k++) { duplicateMovieClip("snow","snow"+k,k); } OUTPUT: Below is the result when pressing Ctrl-Enter to test:
RESULT: Thus the above program was successfully executed and verified .
71
ALGORITHM:
Step 1: Create a new document and press Ctrl-J to open Document Properties dialog, set your document to 450px widthand 192px height to fit with the road image
Next you will need to find two images car and road or use the images below Car:
Road:
72
Step 2: Go to File>Import>Import to Library and select these images from the dialog. Press Ctrl-L to open up the Librarypanel if you don't see it
Step 3: Place the road image on Layer 1 (the image has the same dimensions as your document), rename this layer Road. Create a new layer and place your car on the right hand side of the road image
73
Step 4: Use the Free Transform Tool(Q) to rotate our car to fit with the road
Step 5: Create a new Motion Guide layer and use the Pen Tool(P) to draw a path like this (its color is not important)
Step 6: Click on frame 45 and press F6 to place a keyframe there, repeat this step with car and road layers
74
Step 7: Click on the frame 45 of the Car layer and move our car to the left hand side of the road. Use the Free Transform Tool to rotate our car a bit
Step 8: Right click on any frame between 0 and 45 of the car layer and select Create Motion Tween. Now you can pressCtrl-Enter to test your result. Notice that you will see our car moving along the path but its direction is not correct Click on frame 4 of Car layer and press F6 to place a keyframe here, then select the Free Transform Tool to rotate it a bit Repeat this step with some other frames. You can see my process below
75
RESULT: Thus the above program was successfully executed and verified .
76
ALGORITHM:
Step1:Now that weve set the stage for our E-card, lets go ahead and animate it. For the first part of that, were going to use a new kind of symbol called a Movie Clip. Thus far weve been creating our symbols as the Graphic type; that is, a simple shape or group of shapes locked in as a single static symbol that can be dropped from the library and into our movie, and manipulated as we see fit.
Step2: A movie clip is the same as graphic, but its animated with its own separate internal timeline; with a movie clip, you can capture an animation as part of a neat little package and duplicate it for continuous playback in as many instances as youd likeits like having a minimovie inside a movie. It also allows you to manipulate that animation in ways that might be more difficult otherwise, because you can tween the movie clip symbol in the same ways that you can tween a graphic symbol. Step3:Now were going to animate our fireworks in two parts. The first part will involve creating an animation inside the fireworks symbol using a shape tween; so lets do that part first, to create a continuous loop of the fireworks growing and shrinking.
77
Step4:To do that, you can get inside the fireworks symbol to animate it by right-clicking on the listing for the fireworks symbol (youll notice it has a different icon than the graphic symbols) and clicking Edit. The rest of your scene will vanish and youll have a blank stage; the shapes/fills that make up your fireworks will be accessible. Above your stage youll see that your working area is now titled Scene 1 (the main stage youre working on) and then Pink Fireworks (in my case, anyway), signifying that youre one level deeper and youre now on the stage for your symbol alone. Your timeline will now have only one layer. To leave this area and get back to your regular scene, you can click the blue arrow to the left, or the text that says Scene 1. Ste5:To create our shape tween, well need to create a new keyframe. I think two seconds would be a good interval over which to complete our cycle; thats 12 frames per second, 24 frames total. Well start the burst small, then expand it, then shrink it down to vanish again, so wel l need two separate segments. So for right now, well make two new keyframes at the halfway point.
78
Step6:So now youve got 13 frames of an animated movie clip that...well...isnt really animated. To take care of that, lets go back to frame 1. Click on that frame in your timeline, and then use your Arrow Tool to select all of the shapes/fills of the fireworks burst on your stage. (Make sure youre on frame 1 when you do this.) Then use the Free Transform tool to scale the fireworks in the first frame down as small as you can, to the point of near-invisiblity.
Step7:To complete the shape tween and make the fireworks burst shrink to nothing again, all you have to do is copy the first framewhere you already scaled the shape downto the last frame, frame 24. Then repeat the process in the last step, and select frames 13-24 (dont select frame 12!) and apply a shape tween to them in the same way. When you drag along the numbers above the timeline you should be able to watch your animation in full as the burst grows, then shrinks again. Dont forget to save your work when youre done, and then exit out of the symbols editing stage by clicking on the blue arrow in the upper left hand corner of the stage.
79
Step8:Now were going to use a Motion Tween in a new way: to animate the opacity of the fireworks burst. Well animate them fading in and then fading out over the same time period that they grow and shrink; that means that well need the fireworks layer to last for 24 frames.
Step9:Now go back to the first frame, and select the symbol for your fireworks burst; I know that
can be kind of hard since its so small, but the easy way to do it is to make sure that all of your layers are locked except for the one with the symbol on it, and then drag your arrow tool across the entire canvas.
Step10:Once youve selected the symbol, look in the Properties tab; you should see a dropdown
menu with the label Color that says, by default, None. This lets you set the brightness of
80
your selected shape (Brightness), adjust the color (Tint), adjust the opacity (Alpha), or adjust all three at once using the Advanced settings options.
Step11:The last part is to use a Motion Tween so that the opacity fades in from frames 1 to 12
and then fades out from frames 13 to 24. This is the easiest part of all, if youll remember fromLesson 1. Just right-click between frames 1 and 12, and click Create Motion Tween; then do the same between frames 13 to 24. You havent animated any motion, but you have animated the changing opacity. Try previewing it to see how it affects the movie clip.
OUTPUT: After that, all that remains is to copy the entire set of 24 frames and then paste it again in the same layer, after the first, to extend it to fill all 48 frames. Once thats done then you can play with any other fireworks symbols that youve made, to create different iterations and colors, and different burst frequencies. I used my second fireworks symbol to make a quicker burst, and started it a few frames later than the original so they wouldnt explode at the exact same time.
RESULT : Thus the image was edited using the editing software and the output was verified.
81