Вы находитесь на странице: 1из 62

CONTENTS

EXPT NO DATE NAME OF THE EXPERIMENT PAGE NO REMARKS

Ex. No: 1 Date: LINE DRAWING USING DDA AND BRESENHAM ALGORITHM Aim To draw a line on the screen using DDA and Bresenhams algorithm, given two points. Algorithm Bresenhamss Algorithm: 1. Input the two end points and store the left end point in (xo,yo). 2. Load (xo,yo) into the frame buffer. 3. Calculate the constants dx,dy,2dy and -2dx and obtain starting value for decision parameters as Po = 2dy dx. 4. At each xk along the line, starting at k=0, perform the following test:i) If Pk < 0, the next point to plot is (xk+1,yk) and Pk+1 = Pk + 2dy. ii) Otherwise, the next point to plot is (xk+1,yk+1) and Pk+1 = Pk + 2dy 2dx. 5. Repeat step4 for dx times. DDA Algorithm: 1. 2. 3. 4. 5. 6. 7. Input the two end points and store the left end point in (xo,yo). Calculate the difference dx=xb-xa and dy=yb-ya. Check if dx>dy then, steps=dx. If the condition is false then, steps=dy. Calculate xinc value and yinc value. Assign xa to x and ya to y. Round off the x and y value by calling putpixel(ceil(x),ceil(y),WHITE). Initialise k=1 in for loop and continue until k<=steps. Calculate x=x+xinc and y=y+yinc. Round off the x and y value by calling putpixel(ceil(x),ceil(y),WHITE).

CODING #include<stdio.h> #include<conio.h> #include<graphics.h> #include<stdlib.h> #include<math.h> #include<iostream.h> void main() { int xmin,ymin,xmax,ymax; char c; void bresen(int xmin,int ymin,int xmax,int ymax); void dda(int xmin,int ymin,int xmax,int ymax); int gdriver=DETECT,gmode,errorcode; initgraph(&gdriver,&gmode," "); errorcode=graphresult(); if(errorcode!=grOk) { printf("graphics error:%s\n",grapherrormsg(errorcode)); printf("press any key to halt"); getch(); exit(1); } do { int c1,i; printf("\n1.dda\n2.bresenham\n3.exit"); printf("\n enter the choice"); scanf("%d",&c1); printf("\nenter the first end points"); scanf("%d%d",&xmin,&ymin); printf("\n enter the second end points"); scanf("%d%d",&xmax,&ymax); clrscr(); switch(c1) { case 1: dda(xmin,ymin,xmax,ymax); break; case 2: bresen(xmin,ymin,xmax,ymax); break;

case 3: printf("exit"); exit(1); } printf("do u want to continue(y/n)?"); scanf("%s",&c); clrscr(); }while(c=='y'); } void dda(int xmin,int ymin,int xmax,int ymax) { int step,dx,dy,i; float xincr,yincr,x,y; int gm,gd=DETECT; initgraph(&gd,&gm,""); dx=xmax-xmin; dy=ymax-ymin; if(abs(dx)>abs(dy)) step=dx; else step=dy; xincr=dx/step; yincr=dy/step; x=xmin; y=ymin; clrscr(); setbkcolor(7); putpixel(ceil(x),ceil(y),1); for(i=1;i<=step;i++) { x=x+xincr; y=y+yincr; putpixel(ceil(x),ceil(y),1); } getch(); closegraph(); } void bresen(int xmin,int ymin,int xmax,int ymax) { int xend,dx,dy,x,y,p; int gm,gd=DETECT; initgraph(&gd,&gm,""); dx=abs(xmax-xmin); dy=abs(ymax-ymin); p=(2*dy)-dx; if(xmin>xmax)

{ x=xmax; y=ymax; xend=xmin; } else { x=xmin; y=ymin; xend=xmax; } setbkcolor(7); putpixel(x,y,1); while(x<xend) { x=x+1; if(p<0) p=p+(2*dy); else { y=y+1; p=p+(2*(dy-dx)); } putpixel(ceil(x),ceil(y),1); } getch(); closegraph(); }

OUTPUT: 1. DDA 2. BREESENHAM 3. EXIT Enter the choice: 1 Enter the 1st end point 300 350 Enter the 2nd end point 500 550

RESULT: Thus bresenham and dda line algorithms were implemented.

Ex. No: 2 Date: MID POINT CIRCLE ALGORITHM Aim To write a program to generate a circle divided into sectors and a number of concentric circles around it using midpoint circle drawing algorithm. Algorithm 1. 2. 3. 4. 5. 6. 7. 8. 9. Include all header files. Declare the variables gd = DETECT and gm as integer. Get the value from the user for xc,yc,r. Get the value for n. Using algorithm draw the circles using for loop. Assign x=0, r=y and call plotpoint function. Calculate p using formula p = 1- r. While x is less than y and if p is less than zero then calculate x=x+1 and p = p+2*x +1. If p is not less than zero, x=x+1, y=y-1 and p=p+2*(x-y)+1. Call the plotpoint function. Draw 8 sectors calling sector function and by giving the starting and ending angles, and radius as parameters to form a circle. 10. Number the segments by using the function outtextxy. 11. Get the options 1 to 8 from the user. 12. The sectors are replaced from one pixel position to another by using set of functions such as setcolor(), setfillstyle(), sector(). 13. Each sector is replaced using switch case statement. 14. Close the graphics function using closegraph(). 15. Stop the execution of the program.

CODING #include<stdio.h> #include<conio.h> #include<iostream.h> #include<graphics.h> void midpoint(int xc,int yc,int r) { int x,y,p; void plotpoints(int xc,int yc,int x,int y); x=0; y=r; plotpoints(xc,yc,x,y); p=1-r; while(y>x) { if(p<0) { x=x+1; p=p+(2*x)+1; } else { p=p+(2*(x-y))+1; x=x+1; y=y-1; } plotpoints(xc,yc,x,y); } getch(); } void plotpoints(int xc,int yc,int x,int y) { putpixel(xc+x,yc+y,15); putpixel(xc-x,yc+y,15); putpixel(xc+x,yc-y,15); putpixel(xc-x,yc-y,15); putpixel(xc+y,yc+x,15); putpixel(xc-y,yc+x,15); putpixel(xc+y,yc-x,15); putpixel(xc-y,yc-x,15); }

void main() { char choice; int r1,r2,gd=DETECT,gm,ch,xc,yc,step; initgraph(&gd,&gm,""); cout<<"Enter the two radius value & step values"; cin>>r1>>r2>>step; xc=getmaxx()/2; yc=getmaxy()/2; for(int i=r1;i<r2;i=i+step) midpoint(xc,yc,i); setcolor(4); setfillstyle(SOLID_FILL,2); sector(xc,yc,0,45,r1,r1); setcolor(2); setfillstyle(SOLID_FILL,3); sector(xc,yc,45,90,r1,r1); setcolor(3); setfillstyle(SOLID_FILL,4); sector(xc,yc,90,135,r1,r1); setcolor(7); setfillstyle(SOLID_FILL,5); sector(xc,yc,135,180,r1,r1); setcolor(5); setfillstyle(SOLID_FILL,6); sector(xc,yc,180,225,r1,r1); setcolor(6); setfillstyle(SOLID_FILL,7); sector(xc,yc,225,270,r1,r1); setcolor(8); setfillstyle(SOLID_FILL,8); sector(xc,yc,270,315,r1,r1); setcolor(9); setfillstyle(SOLID_FILL,9); sector(xc,yc,315,360,r1,r1); gotoxy(xc,yc); setcolor(15); outtextxy(xc+20,yc-10,"1"); outtextxy(xc-20,yc-10,"4"); outtextxy(xc+20,yc+10,"8"); outtextxy(xc-20,yc+10,"5"); outtextxy(xc+10,yc-20,"2"); outtextxy(xc-10,yc-20,"3"); outtextxy(xc+10,yc+10,"7"); outtextxy(xc-10,yc+20,"6"); do

{ cout<<"Enter the sector choice"; cin>>ch; switch(ch) { case 1: { setcolor(0); setfillstyle(SOLID_FILL,0); sector(xc,yc,0,45,r1,r1); setcolor(2); setfillstyle(SOLID_FILL,2); sector(xc+(3*r1),yc+(3*r1),0,45,r1,r1); break; } case 2: { setcolor(0); setfillstyle(SOLID_FILL,0); sector(xc,yc,45,90,r1,r1); setcolor(3); setfillstyle(SOLID_FILL,3); sector(xc+(3*r1),yc+(3*r1),45,90,r1,r1); break; } case 3: { setcolor(0); setfillstyle(SOLID_FILL,0); sector(xc,yc,90,135,r1,r1); setcolor(7); setfillstyle(SOLID_FILL,4); sector(xc+(3*r1),yc+(3*r1),90,135,r1,r1); break; } case 4: { setcolor(0); setfillstyle(SOLID_FILL,0); sector(xc,yc,135,180,r1,r1); setcolor(5); setfillstyle(SOLID_FILL,5); sector(xc+(3*r1),yc+(3*r1),135,180,r1,r1); break; } case 5:

{ setcolor(0); setfillstyle(SOLID_FILL,0); sector(xc,yc,180,225,r1,r1); setcolor(6); setfillstyle(SOLID_FILL,6); sector(xc+(3*r1),yc+(3*r1),180,225,r1,r1); break; } case 6: { setcolor(0); setfillstyle(SOLID_FILL,0); sector(xc,yc,225,270,r1,r1); setcolor(7); setfillstyle(SOLID_FILL,7); sector(xc+(3*r1),yc+(3*r1),225,270,r1,r1); break; } case 7: { setcolor(0); setfillstyle(SOLID_FILL,0); sector(xc,yc,270,315,r1,r1); setcolor(8); setfillstyle(SOLID_FILL,8); sector(xc+(3*r1),yc+(3*r1),270,315,r1,r1); break; } case 8: { setcolor(0); setfillstyle(SOLID_FILL,0); sector(xc,yc,315,360,r1,r1); setcolor(9); setfillstyle(SOLID_FILL,9); sector(xc+(3*r1),yc+(3*r1),315,360,r1,r1); break; } } cout<<"Do u want to coninue(y/n)?"; cin>>choice; } while(choice=='y'); closegraph(); }

OUTPUT: Enter the two radius values of step values 20 40 10 Enter the sector choice 1

RESULT: Thus the mid point circle algorithm was implemented.

Ex.No: 3 Date: MIDPOINT ELLIPSE ALGORITHM Aim To write a C++ program to draw an ellipse. Algorithm 1. 2. 3. 4. Start the program. Initialize the graphics driver. Input the major and minor axis for an ellipse. Call the plotpoints function recursively as long as the condition fx < fy is true in such a way that i) if p < 0, then next point is (xk+1,yk) and p = p+fx+mi2. ii) Else next point is (xk+1,yk-1) and p=p+fx+mi2-fy 5. Similar processing is done in region 2. 6. Repeat steps 4 & 5 in the other quadrants also. 7. Stop the program.

CODING #include<iostream.h> #include<conio.h> #include<graphics.h> #include<dos.h> #include<math.h> class mellipse { private: long xc,yc,ma,mi; public: void getdata(); void midellipse(); void plotpoints(int,int); }; void mellipse::getdata() { cout<<"\nenter the center point for ellipse:\n"; cin>>xc>>yc; cout<<"\nenter the major and minor axis for the ellipse:\n"; cin>>ma>>mi; } void mellipse::midellipse() { long x=0,y=mi,fx=0,fy=ma*ma*2*mi; long p=ceil(mi*mi-ma*ma*mi+0.25*ma*ma); while(fx<=fy) { plotpoints(x,y); x=x+1; fx=fx+mi*mi*2; if(p<0) { p=p+fx+mi*mi; } else { y=y-1; fy=fy-ma*ma*2; p=p+fx+mi*mi-fy; } } plotpoints(x,y); p=ceil(mi*mi*(x+0.5)*(x+0.5)+ma*ma*(y-1)*(y-1)-ma*ma*mi*mi);

while(y>0) { y=y-1; fy=fy-ma*ma*2; if(p>0) p=p-fy+ma*ma; else { x=x+1; fx=fx+mi*mi*2; p=p+fx-fy+ma*ma; } plotpoints(x,y); } } void mellipse::plotpoints(int x,int y) { putpixel(xc+x,yc+y,12); putpixel(xc+x,yc-y,13); putpixel(xc-x,yc-y,14); putpixel(-x+xc,y+yc,15); delay(100); } void main() { mellipse e; int gdriver=DETECT,gmode; initgraph(&gdriver,&gmode,"c:\\tc\\bgi"); e.getdata(); cout<<"\nthe ellipse is\n"; e.midellipse(); getch(); }

OUTPUT:

Enter the center points for ellipse 400 200 Enter the major and minor axis for ellipse 100 150 The ellipse is

RESULT: Thus the mid-point ellipse algorithm is implemented.

Ex. No: 4 Date: 2D TRANSFORMATION Aim To write a program to perform translation, scaling and rotation on a 2dimensional object. Algorithm 1. Include all the header files. 2. Input rectangle co-ordinates for 2d object. 3. Then define rotate() function. Declare the variable newx1, newy1 as integer. Then find the transformed co-ordinates as xco=newx1*cos(pi)-newy1*sin(pi), yco=newx1*sin(pi) +newy1*cos(pi). 4. Define translate function. Translate the object to x=x+tx and y=y+ty, where tx and ty are the translation factors. 5. Define rotate function. Rotate the object by a value obtained from the user. 6. Define the scale function. Redraw the object using the values x=x*sx and y=y*sy, where sx and sy are the scaling factors. 7. Define the main function with menu options 1.Translate 2.Rotate 3.Translate & Rotate 4.Scaling 5.All three 6.exit 8. Perform the appropriate transformation corresponding to the input received from the user.

18

CODING #include<stdio.h> #include<conio.h> #include<graphics.h> #include<iostream.h> #include<process.h> #include<dos.h> #include<math.h> int x,y,x1,y1; int deg,tem1,tem2,md1,md2; void rotate(int x,int y, int x1, int y1,double i) { int newx1, newy1; double xco,yco; double pi=((22.0/7.0)/180.0)*i; newx1=x1; newy1=y1; newx1=newx1-x; newy1=newy1-y; xco=newx1*cos(pi)-newy1*sin(pi); yco=newx1*sin(pi)+newy1*cos(pi); newx1=floor(xco)+x; newy1=floor(yco)+y; tem1=newx1; tem2=newy1; } void translate(int xa, int ya, int xb,int yb) { int tx,ty; printf("Enter translation factor[tx and ty]\n"); scanf("%d%d", &tx,&ty); xa=xa+tx; xb=xb+tx; ya=ya+ty; yb=yb+ty; rectangle(xa,ya,xb,yb); x=xa; y=ya; x1=xb;y1=yb; } void rotation(int x11,int y11, int x22, int y22) { int degree; int a[10]; printf("How many degree do u want to rotate\n"); scanf("%d", &degree);

19

md1=(x11+x22)/2; md2=(y11+y22)/2; rotate(md1,md2, x,y, degree); a[0]=tem1; a[1]=tem2; rotate(md1, md2, x,y1,degree); a[2]=tem1; a[3]=tem2; rotate(md1, md2, x1,y,degree); a[4]=tem1; a[5]=tem2; rotate(md1,md2,x1,y1, degree); a[6]=tem1; a[7]=tem2; line(a[0],a[1],a[2],a[3]); line(a[2],a[3],a[6],a[7]); line(a[6],a[7],a[4],a[5]); line(a[0],a[1],a[4],a[5]); } void scale(int xa, int ya, int xb, int yb) { float sx,sy; printf("enter scaling factor[sx and sy]\n"); scanf("%f%f", &sx, &sy); xa=ceil(xa*sx); xb=ceil(xb*sx); ya=ceil(ya*sx); yb=ceil(yb*sx); setcolor(15); rectangle(xa,ya,xb,yb); x=xa;y=ya;x1=xb;y1=yb; } void main() { int gd=DETECT, gm,n; clrscr(); initgraph(&gd,&gm, " "); printf("Enter coordinates for rectangle\n"); scanf("%d%d%d%d", &x,&y,&x1,&y1); md1=(x+x1)/2; md2=(y+y1)/2; rectangle(x,y,x1,y1); getch(); do { printf("\ndo u want to \n 1. Translate\n 2. Rotate\n 3. Translate and rotate\n 4. Scaling \n 5. All the three\n 6. exit\n"); printf(" enter ur choice"); scanf("%d",&n);

20

switch(n) { case 1: getch(); setcolor(5); translate(x,y,x1,y1); break; case 2: getch(); setcolor(12); rotation(x,y,x1,y1); break; case 3: getch(); setcolor(2); translate(x,y,x1,y1); setcolor(6); rotation(x,y,x1,y1); break; case 4: getch(); scale(x,y,x1,y1); break; case 5: getch(); translate(x,y,x1,y1); getch(); rotation(x,y,x1,y1); getch(); scale(x,y,x1,y1); getch(); //rotation(x,y,x1,y1); break; } getch(); } while(n<6); }

21

OUTPUT: Enter the co-ordinates for rectangle: 100 200 300 400 Do you want to 1.Translate 2.Rotate 3.Translate & Rotate 4.scaling 5.All the three 6.Exit Enter your choice:5 Enter the translation factor[tx,ty] : 20 20 How many degrees do you want to rotate: 45 Enter the scaling factor [sx,sy]: 1.5 1.5

RESULT: Thus 2D transformation has been implemented.

22

Ex.No: 5 Date: LINE CLIPPING Aim To write a C program to perform line clipping. Algorithm 1. 2. 3. 4. Input the co-ordinates of the clipping window. Input the end points of the line to be clipped. Check if the two end points are within the region of the clipping window. Set the 4 digit code for each of the points based on their distance from the window coordinates. 5. If points are inside the window, then no change is needed. 6. Else if points are completely outside the window, the line need not be displayed. 7. Else if line cuts through the window, then redraw the line until it the window.The part of line outside the window is cut off.

23

CODING #include<iostream.h> #include<conio.h> #include<graphics.h> #include<dos.h> #include<math.h> float a1,b1,a2,b2; struct clip { float x,y; int code[4]; }; clip region(clip p); clip newp(clip p1,clip p2); int clipp(clip p1,clip p2); int main() { int v; int gdriver= DETECT, gmode; initgraph(&gdriver,&gmode,"c:\\tc\\bgi"); cout<<"\n enter the window coordinate \n"; cout<<"\n enter bottom coordinate \n"; cin>>a1>>b1; cout<<"\n enter top coordinate \n"; cin>>a2>>b2; rectangle(a1,b1,a2,b2); getch(); clip p1,p2; cout<<"\n enter line points \n"; cin>>p1.x>>p1.y>>p2.x>>p2.y; setcolor(5); line(p1.x,p1.y,p2.x,p2.y); getch(); p1=region(p1); p2=region(p2); v=clipp(p1,p2); switch(v) { case 0: cout<<"line is inside the window \n"; getch(); cleardevice(); rectangle(a1,b1,a2,b2); line(p1.x,p1.y,p2.x,p2.y); getch(); break;

24

case 1: cout<<"\n line is outside the window";


getch(); cleardevice(); rectangle(a1,b1,a2,b2); getch(); break; case 2: cout<<"\n intersection of lines \n"; getch(); p1=newp(p1,p2); p2=newp(p2,p1); cleardevice(); rectangle(a1,b1,a2,b2); line(p1.x,p1.y,p2.x,p2.y); getch(); break; } getch(); return(0); } clip region(clip p) { clip temp; if(p.y<b1) temp.code[0]=1; else temp.code[0]=0; if(p.y>b2) temp.code[1]=1; else temp.code[1]=0; if(p.x>a2) temp.code[2]=1; else temp.code[2]=0; if(p.x<a1) temp.code[3]=1; else temp.code[3]=0; temp.x=p.x; temp.y=p.y; return(temp); } int clipp(clip p1,clip p2) { int flag=0; for(int i=0;i<4;i++) { if((p1.code[i]!=0)||(p2.code[i]!=0)) { flag=1; }

25

} 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); } clip newp(clip p1,clip p2) { int x,y; float k,m; clip temp; if(p1.code[3]==1) x=a1; if(p1.code[2]==1) x=a2; 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(int i=0;i<4;i++) temp.code[i]=p1.code[i]; if((temp.y<=b2)&&(temp.y>=b1)) return(temp); } if(p1.code[0]==1) y=b1; if(p1.code[1]==1) y=b2; 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(int i=0;i<4;i++) temp.code[i]=p1.code[i]; return(temp); } else return(p1); }

26

OUTPUT: Enter the top window coordinate 150 200 Enter the bottom coordinates 300 400 Enter the line points 250 300 350 450 Before clipping

After clipping

RESULT:

27

Thus the line clipping algorithm was implemented. Cohen Sutherland algorithm #include<iostream.h> #include<conio.h> #include<graphics.h> class cohen { private: int xmin,ymin,xmax,ymax,m,x1,y1,x2,y2,c1[4],c2[4]; public: void getdata(); void codegen(int,int,int,int); int accept(int c1[],int c2[]); int reject(int c1[],int c2[]); int ptinside(int c1[]); void swap(int,int,int,int,int c1[],int c2[]); void clipline(); void draw(); }; void cohen::draw() { cout<<"\nBefore clipping"; rectangle(xmin,ymin,xmax,ymax); line(x1,y1,x2,y2); } void cohen::getdata() { cout<<"\nEnter the top left coordinate of the clip window\n"; cin>>xmin>>ymin; cout<<"\nEnter the bottom right coordinates of the clip window\n"; cin>>xmax>>ymax; cout<<"\nEnter the first coordinate of the line\n"; cin>>x1>>y1; cout<<"\nEnter the second coordinate of the line\n"; cin>>x2>>y2; } void cohen::codegen(int a,int b,int c,int d) { for(int i=1;i<=4;i++) { c1[i]=0; c2[i]=0; } if(a<xmin) c1[1]=1; if(a>xmax) c1[2]=1; if(b>ymax) c1[3]=1;

28

if(b<ymin) c1[4]=1; if(c<xmin) c2[1]=1; if(c>xmax) c2[2]=1; if(d>ymax) c2[3]=1; if(d<ymin) c2[4]=1; cout<<c1[1]<<c1[2]<<c1[3]<<c1[4]; cout<<c2[1]<<c2[2]<<c2[3]<<c2[4]; } int cohen::accept(int c1[],int c2[]) { int flag=1; for(int i=1;i<=4;i++) if((c1[i]||c2[i])==1) flag=0; return(flag); } int cohen::reject(int c1[],int c2[]) { int flag=0; for(int i=1;i<=4;i++) if((c1[i]&&c2[i])==1) flag=1; return(flag); } int cohen::ptinside(int c1[]) { int flag=0; if((c1[1]||c1[2]||c1[3]||c1[4])==0) flag=1; return(flag); } void cohen::swap(int a,int b,int c,int d,int c1[],int c2[]) { int t1,t2,t[4]; t1=a;a=c;c=t1; t2=b;b=d;d=t2; for(int i=1;i<=4;i++) { t[i]=c1[i]; c1[i]=c2[i]; c2[i]=t[i]; } } void cohen::clipline() { int done=0,draw=0; while(!done) { codegen(x1,y1,x2,y2);

29

m=(float)(y2-y1)/(x2-x1); if((accept(c1,c2)==1)) { done=1; draw=1; } else if((reject(c1,c2)==1)) done=1; else { if(ptinside(c1)==1) swap(x1,y1,x2,y2,c1,c2); if(c1[1]==1) { y1=y1+(xmin-x1)*m; x1=xmin; rectangle(xmin,ymin,xmax,ymax); line(x1,y1,x2,y2); getch(); } else if(c1[2]==1) { y1=y1+(xmax-x1)*m; x1=xmax; rectangle(xmin,ymin,xmax,ymax); line(x1,y1,x2,y2); getch(); } else if(c1[3]==1) { x1=x1+(ymin-y1)/m; y1=ymin; rectangle(xmin,ymin,xmax,ymax); line(x1,y1,x2,y2); getch(); } else if(c1[4]==1) { x1=x1+(ymax-y1)/m; y1=ymax; rectangle(xmin,ymin,xmax,ymax); line(x1,y1,x2,y2); getch(); } /*done=1;*/ } } if(draw==1)

30

{ cout<<"\nAfter clipping\n"; rectangle(xmin,ymin,xmax,ymax); line(x1,y1,x2,y2); } else { cout<<"\nAfter clipping\n"; rectangle(xmin,ymin,xmax,ymax); } } void main() { int gd,gm; gd=DETECT; initgraph(&gd,&gm,"d:\\tueboc\\bgi"); cohen c; c.getdata(); c.draw(); getch(); cleardevice(); c.clipline(); getch(); }

31

OUTPUT: Enter the top left coordinate of the clip window 150 150 Enter the bottom right coordinate of the clip window 350 350 Enter the first coordinate of the line 100 100 Enter the second coordinate of the line 200 200 Before clipping

After clipping

10010000 RESULT:

32

Thus the Cohen Sutherland algorithm for line clipping was implemented. Ex.No.: 6 Date: IMAGE COMPRESSION AND DECOMPRESSION USING JAVA

Aim To compress and decompress a given image(jpeg). Algorithm COMPRESSION: 8. Start the program. 9. Initialise the given file using File Input Stream class object and the target file using Deflater Output Stream object. 10. Read character by character from input file and copy into target file. 11. Display the size of the two files as output. 12. Stop the program. DECOMPRESSION: 1. Start the program. 2. Initialise the given file using Inflater Input Stream class object and the target file using File Output Stream object. 3. Read character by character from input file and copy into target file. 4. Display the size of the two files as output. 5. Stop the program.

33

CODING COMPRESSION: import java.io.*; import java.util.zip.*; class Dzip { public static void main(String args[]) throws Exception { FileInputStream fin=new FileInputStream("Ashes.bmp"); DeflaterOutputStream dout=new DeflaterOutputStream( new FileOutputStream("Ashes.zip")); int b; while((b=fin.read())!=-1) dout.write(b); fin.close(); dout.close(); File f1=new File("Ashes.bmp"); File f2=new File("Ashes.zip"); System.out.println("Before " + f1.length()); System.out.println("After "+f2.length()); } } DECOMPRESSION: import java.io.*; import java.util.zip.*; class Izip { public static void main(String args[])throws Exception { InflaterInputStream in=new InflaterInputStream(new FileInputStream("Ashes.zip")); FileOutputStream sout=new FileOutputStream("Ashes.bmp"); int b; while((b=in.read())!=-1) sout.write(b); in.close(); sout.close(); System.out.println("The size of the unzipped file is"); File f1=new File("Ashes.zip"); File f2=new File("Ashes.bmp"); System.out.println("The size of unzipped file is "+f2.length()); System.out.println("The size of zipped file is "+f1.length()); } }

34

Ex.No.: 7 Date: TEXT COMPRESSION USING JAVA Aim: To compress a given line of text using java. Algorithm: 1. 2. 3. 4. Import the i/o and lang packages. Declare a class txtTemp. Input the text to be compressed from the user into the String object s. For every character of the input string, do a. Check if the characters following the current character are the same and increment the count variable c for every match. b. If c = 0 (i.e. if there is no match), then copy the current character to the output string s1. c. Else i. Let d = c-1 ii. Convert d into string representation using valueOf() function and store in string object s2. iii. If (d<10) then copy the current character twice into the output string and then copy the first character of s2. iv. Else copy the current character twice into the ouput string and copy the first two characters of s2. d. Continue the processing with the remaining part of the string. 5. Print the input string and its size. 6. Print the output string i.e. compressed string and its size.

35

CODING import java.io.*; import java.lang.*; class tcomp { public static void main(String args[]) throws IOException { String s; StringBuffer s1=new StringBuffer(" "); DataInputStream in = new DataInputStream(System.in); int i,j,l,c,k=0; s1.setLength(100); for(int h=0;h<8;h++) System.out.println(" "); System.out.println("\t Text Compression"); System.out.println("\nText to be compressed:"); s=in.readLine(); l=s.length(); for(i=0;i<l;i++) { c=0; for(j=i+1;j<l;j++) { if(s.charAt(i)==s.charAt(j)) c++; else break; } if(c==0) { s1.setCharAt(k,s.charAt(i)); k++; } else if(c>0) { int d=c-1; String s2=String.valueOf(d); if(d<10) { s1.setCharAt(k,s.charAt(i)); k++; s1.setCharAt(k,s.charAt(i)); k++; s1.setCharAt(k,s2.charAt(0)); k++; } else {

36

s1.setCharAt(k,s.charAt(i)); k++; s1.setCharAt(k,s.charAt(i)); k++; s1.setCharAt(k,s2.charAt(0)); k++; s1.setCharAt(k,s2.charAt(1)); k++; } } i=j-1; } System.out.println("Original Text is "+s); System.out.println("\tCompressed text is"+s1); System.out.println("\tTotal bytes"+l); System.out.println("\t Text compressed "+k+"bytes"); } }

37

OUTPUT: Text compression Text to be compressed: aaaaaaaaaabbbbbbbbbb Original text is aaaaaaaaaabbbbbbbbbb Compressed text is aa8bb8 Total bytes 20 Text compressed 6 bytes

RESULT: Thus the given text was compressed.

38

Ex.No:8 Date:

APPLET DRAWING Aim To create an applet to display basic shapes. Algorithm 1. Set the applet window dimensions to 100 and 300 for the height and width in the applet tag. 2. In the paint method, a. Set the pen color to blue. b. Draw a line 150 units long. c. Draw a rectangle. d. Draw an empty oval and fill oval using the Graphics class methods.

CODING 39

import java.awt.*; import java.io.*; import java.applet.Applet; /*<applet code = sp width=500 height=600> </applet>*/ public class sp extends Applet { public void paint(Graphics g) { g.setColor(Color.blue); g.drawLine(50,100,200,100); g.drawRect(100,150,460,100); g.drawOval(150,150,200,200); g.fillOval(200,100,100,100); } }

OUTPUT:

40

RESULT: Thus the applet was executed successfully.

Ex.No.: 9 41

Date: 3D TRANSFORMATION Aim: To write a program to translate a 3D object from one position to another and to scale it from one size to another. Algorithm: 1. 2. 3. 4. 5. Start the program Input the eight sets of values for x coordinates and y coordinates respectively. Input the users choice of transformation Draw the cuboid using the given dimensions. If the choice is 1, then a. Get the translation factors for x axis(tx) and y(ty) axis from user b. Add tx to the eight coordinate values and ty to the eight y coordinate values. c. Redraw the cuboid using the new values of the cuboid coordinates. 6. Else a. Get the scaling factors for x axis(sx) and y axix(sy) from user. b. Multiply both values by a factor of 20. c. Add the sx and sy values to the sets x and y coordinates respectively. d. Redraw the cuboid using the new values of the cuboid coordinates.

42

CODING: #include<stdio.h> #include<conio.h> #include<graphics.h> #include<stdlib.h> //#include "graph.c" int x1,x2,x3,x4,x5,x6,x7,x8,x,y,y1,y2,y3,y4,y5,y6,y7,y8; void main() { void translation(); void scaling(); int ch,gd=DETECT,gm; initgraph(&gd,&gm," "); printf("enter the value of x coordinate:"); scanf("%d%d%d%d%d%d%d%d",&x1,&x2,&x3,&x4,&x5,&x6,&x7,&x8); printf("enter the value of y coordinate:"); scanf("%d%d%d%d%d%d%d%d",&y1,&y2,&y3,&y4,&y5,&y6,&y7,&y8); printf("enter 1-trans, 2-scaling:"); scanf("%d",&ch); x1=getmaxx()/2+(x1*20); x2=getmaxx()/2+(x2*20); x3=getmaxx()/2+(x3*20); x4=getmaxx()/2+(x4*20); x5=getmaxx()/2+(x5*20); x6=getmaxx()/2+(x6*20); x7=getmaxx()/2+(x7*20); x8=getmaxx()/2+(x8*20); y1=getmaxy()/2-(y1*20); y2=getmaxy()/2-(y2*20); y3=getmaxy()/2-(y3*20); y4=getmaxy()/2-(y4*20); y5=getmaxy()/2-(y5*20); y6=getmaxy()/2-(y6*20); y7=getmaxy()/2-(y7*20); y8=getmaxy()/2-(y8*20); switch(ch) { case 1: translation(); break; case 2: scaling(); break; } getch(); closegraph(); } void translation()

43

{ int x,y; //gh(); line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x3,y3,x4,y4); line(x1,y1,x4,y4); line(x5,y5,x6,y6); line(x6,y6,x7,y7); line(x7,y7,x8,y8); line(x5,y5,x8,y8); line(x1,y1,x5,y5); line(x2,y2,x6,y6); line(x3,y3,x7,y7); line(x4,y4,x8,y8); printf("enter the x&y displacement:"); scanf("%d%d",&x,&y); x=(x*20); y=(y*20); line(x1+x,y1-y,x2+x,y2-y); line(x2+x,y2-y,x3+x,y3-y); line(x3+x,y3-y,x4+x,y4-y); line(x4+x,y4-y,x1+x,y1-y); line(x5+x,y5-y,x6+x,y6-y); line(x6+x,y6-y,x7+x,y7-y); line(x7+x,y7-y,x8+x,y8-y); line(x5+x,y5-y,x8+x,y8-y); line(x1+x,y1-y,x5+x,y5-y); line(x2+x,y2-y,x6+x,y6-y); line(x3+x,y3-y,x7+x,y7-y); line(x4+x,y4-y,x8+x,y8-y); } void scaling() { int xc,yc,xd,yd,sx,sy,sf; //gh(); line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x3,y3,x4,y4); line(x1,y1,x4,y4); line(x5,y5,x6,y6); line(x6,y6,x7,y7); line(x7,y7,x8,y8); line(x5,y5,x8,y8); line(x1,y1,x5,y5); line(x2,y2,x6,y6); line(x3,y3,x7,y7); line(x4,y4,x8,y8); printf("enter the scaling factor:");

44

scanf("%d",&sf); sf=sf*20; line(x1-sf,y1-sf,x2+sf,y2-sf); line(x2+sf,y2-sf,x3+sf,y3+sf); line(x3+sf,y3+sf,x4-sf,y4+sf); line(x4-sf,y4+sf,x1-sf,y1-sf); line(x5-sf,y5-sf,x6+sf,y6-sf); line(x6+sf,y6-sf,x7+sf,y7+sf); line(x7+sf,y7+sf,x8-sf,y8+sf); line(x8-sf,y8+sf,x5-sf,y5-sf); line(x1-sf,y1-sf,x5-sf,y5-sf); line(x2+sf,y2-sf,x6+sf,y6-sf); line(x3+sf,y3+sf,x7+sf,y7+sf); line(x4-sf,y4+sf,x8-sf,y8+sf); } /*graph.c*/ #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> #include<stdlib.h> #include<dos.h> void gh() { int gd=DETECT,gm,i; initgraph(&gd,&gm," "); for(i=0;i<getmaxx();i+=20); { setcolor(GREEN); line(0,i,getmaxx(),i); line(i,0,i,getmaxy()); } setcolor(WHITE); line(0,getmaxy()/2,getmaxx(),getmaxy()/2); line(getmaxx()/2,0,getmaxx()/2,getmaxy()); getch(); }

45

OUTPUT: Enter the x coordinate 13312442 Enter the y coordinate 22446688 enter 1-trans 2-scaling: 1 Enter the x and y displacement: 0.2 0.2

RESULT: Thus 3D transformation was implemented. 46

Ex.No.: 10 Date: CONVERSION BETWEEN COLOUR MODELS Aim: To convert HSV colour parameters to RGB parameters and vice versa. Algorithm: RGB to HSV conversion 1. Input the values for R,G and B 2. The value for V is the max(R,G,B); 3. The value for S is S= (max(R,G,B) min(R,G,B))/max(R,G,B) if V!=0 else S=0. 4. The value for H is a. If S=0, then H=0 b. Else i. If R=max(R,G,B) then H=(G-B)/delta ii. Else if G=max(R,G,B) then H=2+(B-R)/delta iii. Else H=4+(R-G)/delta 5. H=H*60.0 ; If H<0 then H=H/360.0 HSV to RGB conversion 1. Input the h,s,v values 2. if s=0, then r=g=b=v 3. else if h=1 then a. h=h*6.0 b. i=floor(h), f=h-i ; a = v(1-s) ; b1=v(1-(s*f)) ; c=v(1-(s(1-f))) c. switch(i): i. case 0 : r=v, g=c, b=a ii. case 1 : r=b1, g= v, b=a iii. case 2 : r=a, g=v, b=c iv. case 3 : r=a, g=b1, b=v v. case 4 : r=c, g=a, b=v vi. case 5 : r=v, g=a, b=b1

47

CODING //<applet code="colour.java"height=500 width=500></applet> import java.awt.*; import javax.swing.*; import java.applet.*; public class colour extends JApplet { public int opt; public float r,g,b,h,s,v; public float min,max,delta; public void init() { opt=Integer.parseInt(JOptionPane.showInputDialog("Select The option 1.rgb-hsv\n2.hsv-rgb")); switch(opt) { case 1: r=Float.parseFloat(JOptionPane.showInputDialog("Enter the value of red")); g=Float.parseFloat(JOptionPane.showInputDialog("Enter the value of Green")); b=Float.parseFloat(JOptionPane.showInputDialog("Enter the value of Blue")); break; case 2: h=Float.parseFloat(JOptionPane.showInputDialog("Enter the value of H")); s=Float.parseFloat(JOptionPane.showInputDialog("Enter the value of S")); v=Float.parseFloat(JOptionPane.showInputDialog("Enter the value of V")); break; }} public void rgbtohsv(Graphics o) { o.drawRect(100,100,200-100,200-100); o.setColor(new Color(r,g,b)); o.fillRect(100,100,200-100,200-100); o.drawString("R="+r+"G="+g+"B="+b,20,50); if(r>=g && r>=b) max=r; else if(g>=r && g>=b) max=g; else if(b>=r && b>=g) max=b; if(r<=g && r<=b) min=r; else if(g<=r && g<=b) min=g; else if(b<=r && b<=g)

48

min=b; delta=max-min; v=max; if(max!=0.0) s=(float)(delta/max); else s=(float)(0.0); if(s==0.0) h=(float)(1); else { if(r==max) h=(float)((g-b)/delta); else if(g==max) h=(float)(2+((b-r)/delta)); else if(b==max) h=(float)(4+((r-g)/delta)); h=(float)(h/6.0); if(h<0) h=(float)(h/360); } o.drawRect(100,300,200-100,200-100); o.setColor(new Color(h,s,v)); o.fillRect(100,300,200-100,200-100); o.drawString("H="+h+"S="+s+"V="+v,20,80); } public void hsvtorgb(Graphics o)//HSV-RGB { o.drawRect(100,300,200-100,200-100); o.setColor(new Color(h,s,v)); o.fillRect(100,300,200-100,200-100); o.drawString("H="+h+"S="+s+"V="+v,20,50); float a,ba,c,f; int i; if(s==0) { r=v; g=v; b=v; } else { if(h==1.0) h=(float)(0.0); h=(float)(h*6.0); i=(int)(h); f=(float)(h-i);

49

a=(float)(v*(1-s)); ba=(float)(v*(1-(s*f))); c=(float)(v*(1-(s*(1-f)))); switch(i) { case 0: r=v;g=c;b=a; break; case 1: r=ba;g=v;b=a; break; case 2: r=a;g=v;b=c; break; case 3: r=a;g=ba;b=v; break; case 4: r=c;g=a;b=v; break; case 5: r=v;g=a;b=ba; break; } } o.drawRect(100,100,200-100,200-100); o.setColor(new Color(r,g,b)); o.fillRect(100,100,200-100,200-100); o.drawString("R="+r+"G="+g+"B="+b,20,80); } public void paint(Graphics o) { if(opt==1){ rgbtohsv(o);} else { hsvtorgb(o); } } }

50

OUTPUT: RGB to HSV conversion:

51

HSV to RGB conversion:

RESULT: Thus conversion between the color models HSV and RGB was implemented and displayed using appletviewer.

52

FLASH MX

53

Ex.No: 1 Date:

MOVING CAR ANIMATION Aim To design a car and animate it using macromedia flash. Algorithm 13. Open Start -> Programs -> Macromedia -> Flash 14. Open a new document. 15. Create a background with road, bush, flowers, clouds, butterflies etc in layers. 16. Insert another layer and draw a car. 17. Do the above 2 steps in a keyframe. 18. Create another keyframe after 30 frames. 19. Move your car to the last keyframe. 20. Right frame and select create motion tweening. 21. Test your animation by pressing Ctrl + Enter.

54

OUTPUT:

RESULT: Thus the animation was created using macromedia.

55

Ex.No: 2 Date: FISH TANK ANIMATION Aim To create a fish tank and animate it using macromedia. Algorithm 22. Open Start -> Programs -> Macromedia -> Flash 23. Open a new document. 24. Create a background with fish tank containing water, plant, pebbles etc in layer1. 25. Create various layers for each fish using keyframes. 26. Create another keyframe after 50 frames. 27. Move the fish in various layers to another position. 28. Right click and create motion tween. 29. Click Ctrl+ Enter to see the result.

56

OUTPUT:

RESULT: Thus the animation was created using macromedia. Ex.No: 3 57

Date:

KEYFRAME ANIMATION Aim To perform keyframe animation using macromedia. Algorithm 30. Open Start -> Programs -> Macromedia -> Flash 31. Open a new document. 32. Enter the text in the layer1. 33. Add the keyframe at the end of the text. 34. Break the text by giving Ctrl + B at the first and last keyframe. 35. Select a single letter and perform free transform in a keyframe. 36. Repeat the process for all the letters. 37. Click Ctrl + Enter to see the result.

58

OUTPUT:

RESULT: Thus the key frame animation was created using macromedia.

59

Ex.No: 4 Date:

MASKING AND MORPHING Aim To perform masking and morphing using macromedia. Algorithm

Masking: 38. Open Start -> Programs -> Macromedia -> Flash 39. Open a new document. 40. Create a text or picture in layer1. 41. Create a ball in another layer and set the keyframe. 42. Create motion tween for the ball and move it across the text. 43. Right click the layer that has the ball and give mask. 44. Click Ctrl + Enter to see the result. Morphing: 1. 2. 3. 4. 5. Open Start -> Programs -> Macromedia -> Flash . Open a new document. Draw 2 different pictures in the same layer at different keyframes. In properties dialog box select shape tween at the first and last keyframes. Click Ctrl + Enter to see the result.

60

OUTPUT:

61

MASKING:

RESULT: Thus the masking and morphing were completed.

62

Вам также может понравиться