Cs1360 - Graphics and Multimedia Laboratory

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 76

CS1360 GRAPHICS AND MULTIMEDIA LABORATORY

LIST OF EXPERIMENTS 1. To implement Bresenhams algorithms for line, circle and ellipse drawing 2. To perform 2D Transformations such as translation, rotation, scaling,reflection and sharing. 3. To implement CohenSutherland 2D clipping and windowviewport mapping 4. To perform 3D Transformations such as translation, rotation and scaling. 5. To visualize projections of 3D images and Hidden Surface Elimination. 6. To convert between color models. 7. To implement text compression algorithm 8. To implement image compression algorithm 9. To perform animation using any Animation software 10. To perform basic operations on image using any image editing software

EX NO 1.a DATE: AIM

BRESENHAMS LINE DRAWING ALGORITHM

To write a C program to draw a line using Bresenhams algorithm. Algorithm Step 1: Input the line endpoints and store the left endpoint in (X0, Y0) Step 2: Load (X0, Y0) in to the frame buffer Step 3: Calculate constants x, y, 2y, and 2y -2x, and obtain the decision parameters as P0 = 2 y x Step 4 : At each Xk along the line, starting at k = 0, perform the following test. If Pk < 0, the next point to plot is (Xk+1, Yk) and Pk+1 = Pk+2y Otherwise, the next point to plot is (Xk+1, Yk+1) and Pk+1 = Pk+2 y - 2 x Step 5: Repeat step 4 x times

EX NO 1.b DATE: AIM

BRESENHAMS CIRCLE DRAWING ALGORITHM

To write a C program to draw a circle using Bresenhams algorithm.

Algorithm Step 1:Input radius r and circle center(Xc, Yc)and obtain the first point on the circumference of a circle centered on the origin as (X0, Y0) = (0, r) Step 2: Calculate the initial values of the decision parameter as P0 = 5/4 r Step 3: At each position starting at k perform the following test: If Pk < 0, the next point to plot is (Xk+1, Yk) and Pk+1 = Pk+2 Xk+1 + 1 Otherwise the next point is (Xk+1, Yk-1) and Pk+1 = Pk+2 Xk+1 + 1- 2Yk+1 where 2Xk+1=2Xk+2 and 2Yk+1=2Yk-2 Step 4: Determine symmetry points in the other seven octants Step 5: Move each pixel position(X, Y) onto the circular path centered on(Xc, Yc) and plot the coordinate values as X = X + Xc Y = Y + Yc Step 6: Repeat steps 3 through until X>=Y

EX NO 1.c DATE: AIM

MIDPOINT ELLIPSE DRAWING ALGORITHM

To write a C program to draw an ellipse using midpoint ellipse algorithm.

Algorithm Step 1: Input radius rx, ry and ellipse center (Xc, Yc) and obtain the first point on the circumference of a circle centered on the origin as (X0, Y0) = (0, ry) Step 2: Calculate the initial values of the decision parameter in region 1 as P10 = r2y r2x ry + 1/4 r2x Step 3: At each Xk position in region 1, starting at k = 0, perform the following test: If P1k < 0, the next point to plot is (Xk+1, Yk) and P1k+1 = P1k+2 r2yXk+1 + r2y Otherwise the next point is (Xk+1, Yk-1) and P1k+1 = P1k+2 r2yXk+1 - 2r2xYk+1 + r2y With 2 r2yXk+1=2 r2yXk+ 2r2y 2r2xYk+1=2r2xYk- 2r2x Step 4: Calculate the initial values of the decision parameter in region 2 as P20 = r2y(X0+1/2)2+ r2x(Y0 1)2- r2x r2y Step 5: At each position starting at Yk position in region 2, starting at k = 0, perform the following test: If P2k > 0, the next point to plot is (Xk, Yk-1) and P2k+1 = P2k - 2 r2yYk+1 + r2x Otherwise the next point is (Xk+1, Yk-1) and P2k+1 = P2k - 2 r2yXk+1 - 2r2xYk+1 + r2x Step 6: Determine symmetry points in the other three octants Step 7: Move each pixel position(X, Y) onto the circular path centered on (Xc, Yc) and plot the coordinate

values as X = X + Xc Y = Y + Yc Step 8: Repeat steps for region 1 until 2 r2yX>=2 r2xY

PROGRAM #include"stdlib.h" #include"conio.h" #include"iostream.h" #include"graphics.h" #include"math.h" #include"dos.h" void main() { int x1,x2,y1,y2,ch,h,k,r,a,b; void bres(int,int,int,int); void midcircle(int,int,int); void ellipse(int,int,int,int); clrscr(); while(1) { cout<<"MENU"; cout<<"-----"; cout<<"1.BRESENHAM LINE\n2.MIDCIRCLE\n3.ELLIPSE\n4.EXIT\n"; cout<<"enter your choice"; cin>>ch; switch(ch)

{ case 1: cout<<"enter 1'st coordinate"; cin>>x1>>y1; cout<<"enter 2'nd coordinate"; cin>>x2>>y2; clrscr(); bres(x1,y1,x2,y2); break; case 2: cout<<"enter center"; cin>>h>>k; cout<<"enter radius"; cin>>r; clrscr(); midcircle(h,k,r); break; case 3: cout<<"enter center"; cin>>h>>k; cout<<"enter major & minor axis"; cin>>a>>b; clrscr(); ellipse(h,k,a,b); break;

case 4: exit(0); default: continue; } } } void bres(int x1, int y1, int x2, int y2) { int interchange,e,s1,s2,temp,x,y,dx,dy,i; int gd=DETECT,gm; initgraph(&gd,&gm,"c:\\tcplus\\bgi"); x=x1; y=y1; dx=abs(x2-x1); dy=abs(y2-y1); if(s1<0) s1=-1; else if(s1==0) s1=0; else s1=1; if(s2<0)

s2=-1; else if(s2==0) s2=0; else s2=1; if(dy>dx) { temp=dx; dx=dy; dy=temp; interchange=1; } else interchange=0; e=2*dy-dx; for(i=1;i<=dx;i++) { putpixel(x,y,1); while(e>=0) { if(interchange==0) x=x+s1; else y=y+s2;

e=e-2*dx; } if(interchange==1) y=y+s2; else x=x+s1; e=e+2*dy; } getch(); // endgraph(); } void midcircle(int h,int k,int r) { int gd=DETECT,gm; initgraph(&gd,&gm,"c:\\tcplus\\bgi"); int x,y,d; x=0; y=r; d=3-(2*r); for(;x<=y;) { if(d<0) { d+=4*x+6; x++;

} else { d+=4*(x-y)+10; x++; y--; } putpixel(x+h,y+k,15); putpixel(x+h,y+k,1); putpixel(-x+h,y+k,1); putpixel(x+h,-y+k,1); putpixel(y+h,x+k,1); putpixel(-y+h,x+k,1); putpixel(y+h,-x+k,1); putpixel(-y-h,-x+k,1); } // endgraph(); } void ellipse(int h,int k,int a,int b) { int gd=DETECT,gm; initgraph(&gd,&gm,"c:\\tcplus\\bgi"); int x,y,xend;

x=0;

xend=a; while(x<=xend) { y=b*sqrt(1-(pow(x,2))/(pow(a,2))); putpixel(x+h,y+k,15); putpixel(x+h,y+k,1); putpixel(-x+h,-y+k,1); putpixel(-x+h,y+k,1); putpixel(x+h,-y+k,1); x=x+1; } // endgraph(); // getch(); }

SAMPLE OUTPUT MENU ---------1.BRESENHAM LINE 2.MIDCIRCLE 3.ELLIPSE 4.EXIT Enter your choice: 1 Enter 1'st coordinate: 100 150 Enter 2'nd coordinate: 200 250

MENU ---------1.BRESENHAM LINE 2.MIDCIRCLE 3.ELLIPSE 4.EXIT

Enter your choice: 2 Enter center: 100 100 Enter radius: 75

MENU ---------1.BRESENHAM LINE 2.MIDCIRCLE 3.ELLIPSE 4.EXIT Enter your choice: 3 Enter center: 200 200 Enter major & minor axis: 50 30

RESULT:

EX NO 2 DATE: AIM

2 -DIMENSIONAL TRANFORMATIONS

To perform the various 2-dimensional transformations such as translation, scaling, rotation, reflection and shearing.

Algorithm Step 1: Input the figure. Step 2: Display the menu as 1.Translation 2.Scaling 3.Rotation 4.Reflection 5.Shearing 6.Exit Step 3: Get the choice from the user. Step 4: If the choice is 1 get the translation vector. Add the translation vector to the original coordinate position to move to a new position. Step 5: If the choice is 2 get the scaling factor. Multiply the coordinate values of each vertex by scaling factors to produce the transformed coordinates. Step 6: If the choice is 3 get the rotation angle. Rotate the figure with respect to the specified angle. Step 7: If the choice is 4 get the axis of reflection. Mirror image is generated relative to an axis of reflection by rotating the object 180 about the reflection axis. Step 8: If the choice is 5 shearing is done which causes the transformation that distorts the shape of an object. Step 9: If choice is 6 exit the program.

PROGRAM FOR TRANSLATION, SCALING, ROTATION: # include<stdio.h> # include<math.h> # include<graphics.h> # include<conio.h> # include<stdlib.h> typedef float matrix[3][3]; matrix thematrix; static struct wcpt { int x; int y; }; int k; typedef struct wcpt wcpt2; void main() { int gdriver=DETECT,gmode,ch,y; int pts1[10],pts2[10],c; void matrixsetidentity(matrix m); void rotate(float a,wcpt2 refpt); void matrixpremultiply(matrix a,matrix b); void translate(int tx,int ty); void scale(float sx,float sy,wcpt2 refpt); void transformpoints(int npts,wcpt2 pts[]);

wcpt2 pts[6]={50.0,50.0,100.0,50.0,100.0,100.0}; wcpt2 refpt={100.0,100.0}; initgraph(&gdriver,&gmode,"d:\\tc\\bgi"); do { printf("1.Scale\n2.Rotate\n3.Translate\n"); printf("Enter the choice:"); scanf("%d",&ch); cleardevice(); for(k=0,c=0;k<3;k++) { pts2[c]=pts[k].x; c++; pts2[c]=pts[k].y; c++; } switch(ch) { case 1: cleardevice(); outtextxy(100,15,"Before Scaling"); fillpoly(3,pts2); matrixsetidentity(thematrix); getch(); cleardevice();

outtextxy(100,15,"After Scaling"); scale(2,2,refpt); transformpoints(3,pts2); fillpoly(3,pts2); break; case 2: cleardevice(); outtextxy(100,15,"Before Rotation"); fillpoly(3,pts2); matrixsetidentity(thematrix); getch(); cleardevice(); outtextxy(100,15,"After Rotation"); rotate(90.0,refpt); transformpoints(3,pts2); fillpoly(3,pts2); break; case 3: cleardevice(); outtextxy(100,15,"Before Translation"); fillpoly(3,pts2); matrixsetidentity(thematrix); getch(); cleardevice(); outtextxy(100,15,"After Translation");

translate(100,0); transformpoints(3,pts2); fillpoly(3,pts2); break; default: printf("\nEnter correct choice:"); } getch(); clrscr(); printf("\nTo continue press 1\nTo exit type 0"); getch(); scanf("%d",&y); }while(y); } void matrixsetidentity(matrix m) { int i,j; matrix temp; for(i=0;i<3;i++) for(j=0;j<3;j++) m[i][j]=(i==j); } void matrixpremultiply(matrix a,matrix b) { int r,c;

matrix tmp; for(r=0;r<3;r++) for(c=0;c<3;c++) tmp[r][c]=a[r][0]*b[0][c]+a[r][1]*b[1][c]+a[r][2]*b[2][c]; for(r=0;r<3;r++) for(c=0;c<3;c++) b[r][c]=tmp[r][c]; } void translate(int tx,int ty) { matrix m; matrixsetidentity(m); m[0][2]=tx; m[1][2]=ty; matrixpremultiply(m,thematrix); } void scale(float sx,float sy,wcpt2 refpt) { matrix m; matrixsetidentity(m); m[0][0]=sx; m[0][2]=(1-sx)*refpt.x; m[1][1]=sy; m[1][2]=(1-sy)*refpt.y; matrixpremultiply(m,thematrix);

} void rotate(float b,wcpt2 refpt) { matrix m; float a; matrixsetidentity(m); a=3.1617/180*b; m[0][0]=cos(a); m[0][1]=-sin(a); m[0][2]=refpt.x*(1-cos(a))+refpt.y*sin(a); m[1][0]=sin(a); m[1][1]=cos(a); m[1][2]=refpt.x*(1-cos(a))-refpt.y*sin(a); matrixpremultiply(m,thematrix); } void transformpoints(int npts,wcpt2 pts[]) { int k; float tmp; for(k=0;k<npts;k++) {

tmp=thematrix[0][0]*pts[k].x+thematrix[0][1]*pts[k].y+thematrix[0][2];

pts[k].y=thematrix[1][0]*pts[k].x+thematrix[1][1]*pts[k].y+thematrix[1][2]; pts[k].x=tmp;

SAMPLE OUTPUT:

1. Scale 2. Rotate 3. Translate

Enter the Choice

Before Scaling

After Scaling

Enter the Choice

Before Rotation

After Rotation

Enter Choice

Before Translation

After Translation

REFLECTION

#include <stdio.h> #include <graphics.h> #include <stdlib.h> #include <conio.h> #include <math.h> //void mul(int,float,float,float); 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 reflection(int n,float c[][3]) { float b[10][3],a[10][3]; int i=0,j,ch;

do { cleardevice(); printf("\n\t**MENU**\n\t1)ABOUT X-AXIS\n\t2)ABOUT Y-AXIS\n\t3)ABOUT ORIGIN\n\t4)ABOUT X=Y\n\t5)ABOUT X=-Y\n\t6)EXIT"); printf("\n\n\tenter your choice:");

scanf("%d",&ch); 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]=b[1][1]=-1; break; case 4: b[0][0]=b[1][1]=0; b[0][1]=b[1][0]=1; break;

case 5: b[0][0]=b[1][1]=0; b[0][1]=b[1][0]=-1; break; case 6: exit(0); break; default: printf("\n\tINVALID CHOICE"); break; } mul(n,b,c,a); setcolor(RED); disp(n,a); getch(); } while(ch!=6); }

void main() { int i,n,gd=DETECT,gm; float c[10][3]; initgraph(&gd,&gm,""); printf("\nEnter the no of vertices:");

scanf("%d",&n); for(i=0;i<n;i++) { printf("\nEnter co-ordinates of the %d vertex:",i+1); scanf("%f%f",&c[i][0],&c[i][1]); c[i][2]=1; } cleardevice(); printf("\n\tREFLECTION"); cleardevice(); setcolor(BLUE); disp(n,c); reflection(n,c); getch(); closegraph(); }

SAMPLE OUTPUT: Enter the number of vertices: 3 Enter the co-ordinates of 1 vertex: 0 40 Enter the co-ordinates of 1 vertex: 40 0 Enter the co-ordinates of 1 vertex: 20 40 Enter the reflection value about X-Axis: 2

SHEARING

#include <stdio.h> #include <graphics.h> #include <stdlib.h> #include <conio.h> #include <math.h> //void mul(int,float,float,float); 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 shearing(int n,float c[][3]) { float b[10][3],sh,a[10][3]; int i=0,ch,j; do {

cleardevice(); printf("\n\t**MENU**\n\t1)X-SHEARING\n\t2)Y-SHEARING\n\t3)EXIT\n\t"); printf("Enter the choice:");

scanf("%d",&ch); if(ch==3) return; printf("\nEnter the value for shearing:"); scanf("%f",&sh); 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: exit(0); break; default: printf("\n\tInvalid choice"); break;

} mul(n,b,c,a); disp(n,c); setcolor(RED); disp(n,a); getch(); } while(ch!=3); } void main() { int i,n,gd=DETECT,gm; float c[10][3]; initgraph(&gd,&gm,""); printf("\nEnter the no of vertices:"); scanf("%d",&n);

for(i=0;i<n;i++) { printf("\nEnter co-ordinates of the %d vertex:",i+1);

scanf("%f%f",&c[i][0],&c[i][1]); c[i][2]=1; } cleardevice();

setcolor(BLUE); shearing(n,c); disp(n,c); getch(); closegraph(); }

SAMPLE OUTPUT: Enter the number of vertices: 3 Enter the co-ordinates of 1 vertex: 0 40 Enter the co-ordinates of 1 vertex: 40 0 Enter the co-ordinates of 1 vertex: 20 40

Enter the shearing factor of X-Axis: 2

RESULT

EX NO 3a
DATE: AIM:

COHEN-SUTHERLAND ALGORITHM

To clip a line using Cohen-Sutherland clipping algorithm. Algorithm: The method speeds up the processing of line segments by performing initial tests that reduce the number of intersections that must be calculated. 1. Every line endpoint is assigned a four digit binary code, called region code, that identifies the location of the point relative to the boundaries of the clipping rectangle. 2. Each bit position in the region code is used to indicate one of the four relative coordinate positions of the point with respect to the clip window. Bit 1: left Bit 2: right Bit 3: below Bit 4: above 3. Bit values in the region code are determined by comparing endpoint coordinates values (x, y) with respect to the clip boundaries. eg.Bit 1 is set to 1 if x<xwmin 4. Once we have established region codes for all line endpoints, we can quickly determine which lines are completely outside or inside the clip window. 5. Lines that cannot be identified as completely inside or outside a clip window are checked for intersection with boundaries. 6. Intersection points with a clipping boundary can be calculated using the slope-intercept form of the line equation.
m ( y2 y1 ) ( x2 x1 )

7. The y coordinate of the intersection point at vertical line

y y1 m( x x1 )

8. The x coordinate of the intersection point at horizontal line

x x1

y y1 m

y yw y yw

min

max

PROGRAM #include<stdio.h> #include<conio.h> #include<stdlib.h> #include<math.h> #include<graphics.h> typedef struct coordinate { int x,y; char code[4]; }pt; void drawwindow(); void drawline(pt p1,pt p2,int cl); pt setcode(pt p); int visibility(pt p1,pt p2); pt resetendpoint(pt p1,pt p2); int main() { int gd=DETECT,gm,v;

pt p1,p2,temp; initgraph(&gd,&gm,"d:\\tc\\bgi"); clrscr(); cleardevice(); printf("\nEnter endpoint 1(x,y):"); scanf("%d%d",&p1.x,&p1.y); printf("\nEnter endpoint 2(x,y):"); scanf("%d%d",&p2.x,&p2.y); printf("\nClipping Window"); drawwindow(); getch(); clrscr(); printf("BEFORE CLIPPING"); drawwindow(); drawline(p1,p2,5); getch(); p1=setcode(p1); p2=setcode(p2); v=visibility(p1,p2); switch(v) { case 0: clrscr(); drawwindow(); drawline(p1,p2,5);

break; case 1: clrscr(); drawwindow(); break; case 2: clrscr(); printf("AFTER CLIPPING"); p1=resetendpoint(p1,p2); p2=resetendpoint(p2,p1); drawwindow(p1,p2,5); drawline(p1,p2,5); break; } getch(); closegraph(); return(0); } void drawwindow() { setcolor(RED); line(150,100,450,100); line(450,100,450,350); line(450,350,150,350); line(150,350,150,100);

} void drawline(pt p1,pt p2,int cl) { setcolor(cl); line(p1.x,p1.y,p2.x,p2.y); } pt setcode(pt p) { pt ptemp; if(p.y<100) ptemp.code[0]='1'; else ptemp.code[0]='0'; if(p.y>350) ptemp.code[1]='1'; else ptemp.code[1]='0'; if(p.x>450) ptemp.code[2]='1'; else ptemp.code[2]='0'; if(p.x<150) ptemp.code[3]='1'; else ptemp.code[3]='0';

ptemp.x=p.x; ptemp.y=p.y; return(ptemp); } int visibility(pt p1,pt p2) { int i,flag=0; for(i=0;i<4;i++) { if((p1.code[i]!='0')||(p2.code[i]!='0')) flag=1; } if(flag==0) return(0); for(i=0;i<4;i++) { if((p1.code[i]==p2.code[i])&&(p1.code[i]=='1')) flag=0; }if(flag==0) return(1); return(2); } pt resetendpoint(pt p1,pt p2) { pt temp;

int x,y,i; float m,k; if(p1.code[3]=='1') x=150; if(p1.code[2]=='1') x=450; if((p1.code[3]=='1')||(p1.code[2]=='1')) { m=(float)(p2.y-p1.y)/(p2.x-p1.x); k=(p1.y+(m*(x-p1.x))); temp.y=k; temp.x=x; for(i=0;i<4;i++) temp.code[i]=p1.code[i]; if(temp.y<=350&&temp.y>=100) return(temp); } if(p1.code[0]=='1') y=100; if(p1.code[1]=='1') y=350; if((p1.code[0]=='1')||(p1.code[1]=='1')) { m=(float)(p2.y-p1.y)/(p2.x-p1.x); k=(float)p1.x+(float)(y-p1.y)/m;

temp.x=k; temp.y=y; for(i=0;i<4;i++) temp.code[i]=p1.code[i]; return(temp); } else return(p1); } SAMPLE Output: Clipping Window

Before Clipping

After Clipping

RESULT:

EX NO 3b DATE: AIM:

WINDOW TO VIEWPORT MAPPING

To write a C program to perform Window to Viewport transformation.

Algorithm Step1: Draw a world coordinate area selected for display called as window. This window defines what is to be viewed. Step 2: Draw an area on a display device to which a window is mapped called as Viewport. The viewport defines where it is to be displayed. Step 3: Now perform the mapping of a part of a world-coordinate scene to device coordinates referred as viewing transformation.

PROGRAM: #include<stdio.h> #include<conio.h> #include<dos.h> #include<graphics.h> #include<math.h> void main() { int xwmin,ywmin,xwmax,ywmax,xv1,yv1; int xvmin,xvmax,yvmin,yvmax,xw,yw,xv,yv; int gd=DETECT,gm;

initgraph(&gd,&gm,""); printf("Enter the window coordinates xwmin,xwmax,ywmin,ywmax\n"); scanf("%d\t%d\t%d\t%d",&xwmin,&xwmax,&ywmin,&ywmax); line(xwmin-25,xwmin-25,xwmin-25,ywmax+50); line(xwmin-40,ywmax+25,xwmax+50,ywmax+25); outtextxy(xwmin+5,ywmax+5,"Window"); line(xwmin,ywmin,xwmin,ywmax); line(xwmin,ywmax,xwmax,ywmax); line(xwmax,ywmax,xwmax,ywmin); line(xwmax,ywmin,xwmin,ywmin); xvmax=xwmax/2; xvmin=xwmin/2; yvmin=ywmin/2; yvmax=ywmax/2; line(xvmin+455,xvmin+455,xvmin+455,yvmax+325); line(xvmin+255,yvmax+315,xvmax+325,yvmax+315); outtextxy(xvmin+305,yvmax+305,"Viewport"); line(xvmin+300,yvmin+300,xvmin+300,yvmax+300); line(xvmin+300,yvmax+300,xvmax+300,yvmax+300); line(xvmax+300,yvmax+300,xvmax+300,yvmin+300); line(xvmax+300,yvmin+300,xvmin+300,yvmin+300); xw=xwmin+50; yw=ywmin+50; putpixel(xw,yw,4); xv1=((xvmax-xvmin)/(xwmax-xwmin))*(xw-xwmin);

xv=xv1+xvmin; yv1=((yvmax-yvmin)/(ywmax-ywmin))*(yw-ywmin); yv=yv1+yvmin; putpixel(xv+325,yv+325,2); getch(); closegraph(); }

SAMPLE OUTPUT:

Enter the window co-ordinates xwmin, xwmax, ywmin, ywmax: 50,70,90,120

WINDOW PORT

VIEW PORT

EX.NO.4 DATE

3D TRANSFORMATIONS

AIM To perform the various 3-dimensional transformations such as translation, scaling, rotation.

Algorithm Step 1: Input the figure. Step 2: Display the menu as 1.Translation 2.Scaling 3.Rotation 4.Exit Step 3: Get the choice from the user. Step 4: If the choice is 1 a point or an object is translated from position P to position P' with the operation P'=T.P where tx,ty and tz specifying translation distances. x'=x+ tx y'=y+ ty z'=z+ tz Step 5: If the choice is 2 the scaling transformation of a position P can be written as P'=S.P where scaling parameters sx,sy and sz are assigned any positive values. x'=x.sx y'=y.sy z'=z.sz

Step 6: If the choice is 3 get the rotation angle. Rotate the figure with respect to the axis of rotation. a: About z axis rotation x'=xcos-ysin y'=xsin+ycos z'=z Rotation can be expressed as P'=Rz().P b: About x axis rotation y'=ycos-zsin z'=ysin+zcos x'=x Rotation can be expressed as P'=Rx().P c: About y axis rotation z'=zcos-xsin x'=zsin+xcos y'=y Rotation can be expressed as P'=Ry().P Step 7: If choice is 4 exit the program.

PROGRAM #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> #include<dos.h> void main() { int t,r,l,b,d,ch,tx,ty,sx,sy,deg; float j; int a,f,c,e; int gd=DETECT,gm; initgraph(&gd,&gm,""); printf("Enter the points right,top,left, bottom,depth: "); scanf("%d%d%d%d%d",&r,&t,&l,&b,&d); printf("Enter the choice:1.Translation2.Rotation3.Scaling....."); printf("\nEnter the choice here..."); scanf("%d",&ch); switch(ch) { case 1: cleardevice(); printf("Enter the value of tx,ty....."); scanf("%d%d",&tx,&ty); bar3d(r,t,b,l,d,l); getch(); bar3d(r+tx,r+ty,l=tx,b+ty,d,l); getch(); break; case 2: cleardevice(); printf("Enter the value of T:"); scanf("%d",&deg); j=(float)deg*3.1459/180; a=abs(l*cos(j)+t*sin(j)); f=abs(t*cos(j)-l*sin(j)); c=abs(r*cos(j)+b*sin(j)); e=abs(b*cos(j)-r*sin(j)); bar3d(r,t,l,b,d,l); getch(); bar3d(a,f,c,e,d,l); getch(); break;

case 3: cleardevice(); printf("Enter the value of sx and sy"); scanf("%d%d",&sx,&sy); bar3d(r,t,l,b,d,l); getch(); bar3d(r*sx,t*sy,l*sx,b*sy,d,l); getch(); break; } }

SAMPLE OUTPUT:
Enter The Values of Right, Top, Left, Bottom, Depth: 100 110 120 130 50

Enter Your Choice: 1.Translation 2.Rotation 3.Scaling

Enter Your Choice Here1 Enter The Values of tx, ty..3 5

Enter The Values of Right, Top, Left, Bottom, Depth: 100 110 120 130 50

Enter Your Choice: 1.Translation 2.Rotation 3.Scaling Enter Your Choice Here2

Enter The Rotation angle value T:25

Enter The Values of Right, Top, Left, Bottom, Depth: 100 110 120 130 50 Enter Your Choice: 1.Translation 2.Rotation 3.Scaling Enter Your Choice Here2 Enter The Values of sx, sy..1,2

RESULT

EX NO 5 DATE: AIM

PROJECTIONS OF 3D IMAGES

To write a C program to show the perspective projection of a 3D image.

Algorithm Step1: Get the coordinates to draw the cube. Step 2: Read the reference point. Step 3: Read the view plane. Step 4: For a perspective projection object positions are transformed to the view plane along lines that converge to a point called the projection reference point. Step 5: The projected view of an object is determined by calculating the intersection point of the projection lines with the view plane.

PROGRAM

#include<iostream.h> #include<conio.h> #include<stdio.h> #include<math.h> #include<graphics.h> #include<process.h> int gd=DETECT,gm; double x1,x2,y1,y2;

void show_message() { char*mess[]={"-","=",",","[",",","3","D","","t","r","a","n","s","f","o","r","m","a","t","i","o","n",",",",","]","-","-"}; int xx=28,xxx=52,i,j; _setcursortype(_NOCURSOR); for(i=0;i<15;i++) { for(j=24;j>=12;j++) { gotoxy(xx,1); cout<<mess[i]; xx++; gotoxy(xxx,1); cout<<mess[i]; xxx--; } } setcursortype(_NORMALCURSOR); } void draw_cube(double edge[20][3]) { initgraph(&gd,&gm,""); int i; 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; double p,q,r; clrscr(); cout<<"\nPrespectpective Projection about"<<endl; cout<<"1:==>x axis"<<endl; cout<<"2:==>y axis"<<endl; cout<<"3:==>z axis"<<endl; cout<<"Enter your choice"; cin>>ch;

switch(ch) { case 1: cout<<"\nEnter p"; cin>>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: cout<<"Enter q"; cin>>q; for(i=0;i<20;i++) { edge[i][1]=edge[i][1]/(q*edge[i][1]*q+1); edge[i][0]=edge[i][0]/(q*edge[i][1]*q+1); edge[i][2]=edge[i][2]/(q*edge[i][1]*q+1); } draw_cube(edge); break; case 3:

cout<<"Enter r:"; cin>>r; for(i=0;i<20;i++) { edge[i][2]=edge[i][2]/(r*edge[i][2]*r+1); edge[i][0]=edge[i][0]/(r*edge[i][2]*r+1); edge[i][1]=edge[i][1]/(r*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,1 00,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}; while(1) { clrscr(); cout<<"1.==>drawcube"<<endl; cout<<"2.==>perspect projection"<<endl; cout<<"3.==>Exit"<<endl; cout<<"Enter your choice";

cin>>choice; switch(choice) { case 1: draw_cube(edge); break; case 2: perspect(edge); break; case 3: exit(0); default: cout<<"press a valid key"; getch(); closegraph(); } } }

RESULT:

EX NO 6 DATE

HSV TO RGB COLOR MODEL

AIM To write a C program to convert HSV to RGB color model.

Algorithm

void HSVtoRGB( float *r, float *g, float *b, float h, float s, float v ) { int i; float f, p, q, t; if( s == 0 ) { // achromatic (grey) *r = *g = *b = v; return; } h /= 60; // sector 0 to 5 i = floor( h ); f = h - i; // factorial part of h p = v * ( 1 - s ); q = v * ( 1 - s * f ); t = v * ( 1 - s * ( 1 - f ) ); switch( i ) { case 0: *r = v; *g = t; *b = p; break; case 1: *r = q; *g = v; *b = p; break; case 2: *r = p; *g = v; *b = t; break; case 3: *r = p;

*g = q; *b = v; break; case 4: *r = t; *g = p; *b = v; break; default: *r = v; *g = p; *b = q; break; } } // case 5:

PROGRAM #include<stdio.h> #include<math.h> #include<conio.h> float r,g,b;

void hsvtorgb(float h,float s,float v) { int i; float aa,bb,cc,f; if(s==0) r=g=b=v; else { if(h==1.0)

h=0; h*=6; 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;break; case 1: r=bb;g=v;b=aa;break; case 2: r=aa;g=v;b=cc;break; case 3: r=aa;g=bb;b=v;break; case 4: r=cc;g=aa;b=v;break; case 5: r=v;g=aa;b=bb;break; } } }

void main() { float h1,s1,v1; clrscr(); printf("Enter the value for H : "); scanf("%f",&h1);

printf("\nEnter the value for S : "); scanf("%f",&s1); printf("\nEnter the value for V : "); scanf("%f",&v1); hsvtorgb(h1,s1,v1); printf("\n\nThe value of R : %f",r);

printf("\n\nThe value of G : %f",g); printf("\n\nThe value of B : %f",b); getch(); }

//Range of input between 0 and 1 SAMPLE OUTPUT Enter the value for H:.2 Enter the value for S:.3 Enter the value for V:.1 The value of R : 0.094000 The value of G : 0.100000 The value of B : 0.070000

RESULT

EX NO 7 DATE: AIM

TEXT COMPRESSION ALGORITHM

To write a C program to compress the text using Run length encoding.

Algorithm Step 1: create a new file that has the input in the form 0s and 1s and save it with the name input .txt Step 2: Create a new file that has no data and save the file as output.txt Step 3: The data in the input file is compressed and the compressed data is written in the output.txt file. Step 4: After execution the result is viewed by opening the output.txt file

PROGRAM #include<stdio.h> #include<string.h> #include<conio.h> main() { char text[4097],text1[4096],text2[4096],x; int i,count; FILE *f2; f2=fopen("g:\maheshd\media\com.txt","w"); clrscr(); strcpy(text1," ");

strcpy(text2," "); printf("enter the input :"); gets(text); i=0; while(text[i]!='\0') { count=0; strcpy(text1,text2); x=text[i]; while(x==text[i]&&text[i]!='\0') { count++; i++; } i--; sprintf(text2,"%s%c%d",text1,x,count); if(text[i]=='\0') break; i++; } fprintf(f2,"%s",text2); fprintf(f2,"%d",count); printf("\n \t compressed data :%s",text2); getch(); return(0);

SAMPLE OUTPUT: Enter the input:

KKKKKLLLLLUUUTTTEEW

Compressed data: K5L5U3T3E2W1

EX NO 8 DATE: AIM:

ANIMATION

To demonstrate simple animation using Macromedia Flash.

Algorithm:

One of the basic animation techniques is moving an object from one location to another. This is accomplished in Flash by applying a Motion Tween between the start and the end frames. Step 1 Open a new file. You'll notice that this movie has only one layer called Layer 1. It is always a good practice to put descriptive names of your choosing for the layers in a Flash file. To rename Layer 1, double click on its name and then type the new name, something like"MotionTween".

Step 2 Our stage is set and it is now time to create an object. Hightlight the first frame in the Time line and using the Oval Tool somewhere on the stage. Step 3 To animated this circle so that it moves from one place to another, we have to convert it into a Symbol. and draw a circle

Click on the Arrow tool

and select the circle you have just drawn. Flash places a sort of 'net'

over selected objects. The image below shows what a selected and an unselected circle looks like.

Now click on Insert - Convert to Symbol. The Symbol Properties window pops up. In the name text field type "Ball" which is the name for our symbol and also make sure that the Behavior is set to Graphic (that is the default value). Click OK to convert the circle into a symbol.

Step 4 If the Symbol Library is not open, click Window - Library to display the library in which you will find your newly created symbol, "Ball". Click on its name to view the thumbnail of the object. Step 5 Place the circle (which is now a symbol) somewhere near the top-left corner of the stage. Now right-click frame number 36 in the time line and select Insert Keyframe

This adds a key frame at frame 36. With this frame selected, move the circle symbol and place it near the top-right corner of the stage. Thus, the circle is placed at its staring point (top-left) in frame 1 and at its ending point (top-right) in frame 36.

Step 6 The last step is creating a Motion Tween between frame 1 and frame 36. Right-click at any frame between 1 and 36, and from the drop down menu select Create Motion Tween

With the motion tween applied between frames 1 and 36, the layer should look something like:

To see how it works, click on Control - Rewind and then Control - Play.

EX NO 9 DATE Aim :

IMAGE EDITING SOFTWARE

To demonstrate the various features of PhotoShop CS Image Editing Tools using a JPEG image.

Algorithm :

Step 1 : Open the PhotoShop CS.

Step 2 : Create a frame which contain the jpeg image.

Step 3 : Create a interval to perform animation.

Step 4 : Demonstrate Blur Tool.

Step 5 : Demonstrate Sharpen Tool.

Step 6 : Demonstrate Texture Tool.

Step 7 : Demonstrate Pixelate Tool.

Step 8 : Demonstrate Stylize Tool.

Step 9 : Demonstrate Photo Editing Tool.

Step 10 :

Copy the new JPEG and create new layer.

Step 11:

Now, paste the image in the new layer.

Output: Blur Tool

Sharpen Tool

Texture Tool [Pattern Tool]

Pixelate Tool [Crystallize]

Stylize Tool [Find Edges]

Photo Editing

You might also like