Graphics Lab Manual

74
CONTENTS EXPT NO DATE NAME OF THE EXPERIMENT PAGE NO REMARKS

Transcript of Graphics Lab Manual

Page 1: Graphics Lab Manual

CONTENTS

EXPT NO

DATE NAME OF THE EXPERIMENT

PAGE NO

REMARKS

Page 2: Graphics Lab Manual
Page 3: Graphics Lab Manual

Ex. No: 1Date:

LINE DRAWING USING DDA AND BRESENHAM ALGORITHM

Aim

To draw a line on the screen using DDA and Bresenham’s algorithm, given two points.

Algorithm

Bresenhams’s 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. Input the two end points and store the left end point in (xo,yo).2. Calculate the difference dx=xb-xa and dy=yb-ya.3. Check if dx>dy then, steps=dx. If the condition is false then, steps=dy.4. Calculate xinc value and yinc value. Assign xa to x and ya to y.5. Round off the x and y value by calling putpixel(ceil(x),ceil(y),WHITE).6. Initialise k=1 in for loop and continue until k<=steps.7. Calculate x=x+xinc and y=y+yinc. Round off the x and y value by calling

putpixel(ceil(x),ceil(y),WHITE).

Page 4: Graphics Lab Manual

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;

Page 5: Graphics Lab Manual

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;elsestep=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)

Page 6: Graphics Lab Manual

{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();}

Page 7: Graphics Lab Manual

OUTPUT:

1. DDA2. BREESENHAM 3. EXIT

Enter the choice: 1Enter the 1st end point 300 350Enter the 2nd end point 500 550

RESULT:

Thus bresenham and dda line algorithms were implemented.

Page 8: Graphics Lab Manual

Ex. No: 2Date:

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. Include all header files.2. Declare the variables gd = DETECT and gm as integer.3. Get the value from the user for xc,yc,r. Get the value for n.4. Using algorithm draw the circles using for loop.5. Assign x=0, r=y and call plotpoint function. Calculate p using formula p = 1- r.6. While x is less than y and if p is less than zero then calculate x=x+1 and p = p+2*x +1.7. If p is not less than zero, x=x+1, y=y-1 and p=p+2*(x-y)+1.8. Call the plotpoint function.9. 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.

Page 9: Graphics Lab Manual

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);

}

Page 10: Graphics Lab Manual

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

Page 11: Graphics Lab Manual

{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:

Page 12: Graphics Lab Manual

{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();}

Page 13: Graphics Lab Manual

OUTPUT:

Enter the two radius values of step values20 40 10Enter the sector choice 1

RESULT:

Thus the mid point circle algorithm was implemented.

Page 14: Graphics Lab Manual

Ex.No: 3Date:

MIDPOINT ELLIPSE ALGORITHM

Aim

To write a C++ program to draw an ellipse.

Algorithm

1. Start the program.2. Initialize the graphics driver.3. Input the major and minor axis for an ellipse.4. 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.

Page 15: Graphics Lab Manual

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);

Page 16: Graphics Lab Manual

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(); }

Page 17: Graphics Lab Manual

OUTPUT:

Page 18: Graphics Lab Manual

Enter the center points for ellipse400200

Enter the major and minor axis for ellipse100150

The ellipse is

RESULT:

Thus the mid-point ellipse algorithm is implemented.

Page 19: Graphics Lab Manual

Ex. No: 4Date:

2D TRANSFORMATION

AimTo 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.exit8. Perform the appropriate transformation corresponding to the input received from the user.

19

Page 20: Graphics Lab Manual

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);md1=(x11+x22)/2;md2=(y11+y22)/2;

20

Page 21: Graphics Lab Manual

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);

switch(n)

21

Page 22: Graphics Lab Manual

{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);}

22

Page 23: Graphics Lab Manual

OUTPUT:

Enter the co-ordinates for rectangle: 100 200 300 400Do you want to 1.Translate2.Rotate3.Translate & Rotate4.scaling5.All the three6.Exit

Enter your choice:5Enter the translation factor[tx,ty] : 20 20How many degrees do you want to rotate: 45Enter the scaling factor [sx,sy]: 1.5 1.5

RESULT: Thus 2D transformation has been implemented.

23

Page 24: Graphics Lab Manual

Ex.No: 5Date:

LINE CLIPPING

Aim

To write a C program to perform line clipping.

Algorithm

1. Input the co-ordinates of the clipping window.2. Input the end points of the line to be clipped.3. Check if the two end points are within the region of the clipping window.4. Set the 4 digit code for each of the points based on their distance from the window co-

ordinates.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.

24

Page 25: Graphics Lab Manual

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;case 1: cout<<"\n line is outside the window";

25

Page 26: Graphics Lab Manual

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;}

} if(flag==0) return(0);

26

Page 27: Graphics Lab Manual

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);}

27

Page 28: Graphics Lab Manual

OUTPUT:

Enter the top window coordinate 150 200Enter the bottom coordinates300 400Enter the line points250 300 350 450

Before clipping

After clipping

RESULT:

Thus the line clipping algorithm was implemented.

28

Page 29: Graphics Lab Manual

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;if(b<ymin) c1[4]=1;if(c<xmin) c2[1]=1;if(c>xmax) c2[2]=1;

29

Page 30: Graphics Lab Manual

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); m=(float)(y2-y1)/(x2-x1); if((accept(c1,c2)==1)) {

30

Page 31: Graphics Lab Manual

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){cout<<"\nAfter clipping\n";rectangle(xmin,ymin,xmax,ymax);

31

Page 32: Graphics Lab Manual

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();}

32

Page 33: Graphics Lab Manual

OUTPUT:

Enter the top left coordinate of the clip window150 150Enter the bottom right coordinate of the clip window350 350Enter the first coordinate of the line 100 100Enter the second coordinate of the line200 200

Before clipping

After clipping

10010000

RESULT:

Thus the Cohen Sutherland algorithm for line clipping was implemented.

33

Page 34: Graphics Lab Manual

Ex.No.: 6Date:

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.

34

Page 35: Graphics Lab Manual

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());}}

Ex.No.: 7

35

Page 36: Graphics Lab Manual

Date:

TEXT COMPRESSION USING JAVA

Aim:To compress a given line of text using java.

Algorithm:

1. Import the i/o and lang packages.2. Declare a class txtTemp.3. Input the text to be compressed from the user into the String object s.4. 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. Elsei. 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.

36

Page 37: Graphics Lab Manual

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 {

37

Page 38: Graphics Lab Manual

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");}}

38

Page 39: Graphics Lab Manual

OUTPUT:

Text compression Text to be compressed:aaaaaaaaaabbbbbbbbbbOriginal text is aaaaaaaaaabbbbbbbbbbCompressed text is aa8bb8Total bytes 20Text compressed 6 bytes

RESULT:

Thus the given text was compressed.

39

Page 40: Graphics Lab Manual

Ex.No:8Date:

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.

40

Page 41: Graphics Lab Manual

CODING

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);}}

41

Page 42: Graphics Lab Manual

OUTPUT:

RESULT:

Thus the applet was executed successfully.

42

Page 43: Graphics Lab Manual

Ex.No.: 9Date:

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. Start the program2. Input the eight sets of values for x coordinates and y coordinates respectively.3. Input the user’s choice of transformation4. Draw the cuboid using the given dimensions.5. 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.

43

Page 44: Graphics Lab Manual

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()

44

Page 45: Graphics Lab Manual

{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:");

45

Page 46: Graphics Lab Manual

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();}

46

Page 47: Graphics Lab Manual

OUTPUT:

Enter the x coordinate 1 3 3 1 2 4 4 2Enter the y coordinate2 2 4 4 6 6 8 8 enter 1-trans 2-scaling: 1

Enter the x and y displacement: 0.2 0.2

RESULT:

Thus 3D transformation was implemented.

47

Page 48: Graphics Lab Manual

Ex.No.: 10Date:

CONVERSION BETWEEN COLOUR MODELS

Aim:To convert HSV colour parameters to RGB parameters and vice versa.

Algorithm:

RGB to HSV conversion1. Input the values for R,G and B2. 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=0b. Else

i. If R=max(R,G,B) then H=(G-B)/deltaii. Else if G=max(R,G,B) then H=2+(B-R)/deltaiii.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 values2. if s=0, then r=g=b=v3. else if h=1 then

a. h=h*6.0b. 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=aii. case 1 : r=b1, g= v, b=aiii.case 2 : r=a, g=v, b=civ. case 3 : r=a, g=b1, b=vv. case 4 : r=c, g=a, b=vvi. case 5 : r=v, g=a, b=b1

48

Page 49: Graphics Lab Manual

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;elseif(g>=r && g>=b)max=g;elseif(b>=r && b>=g)max=b;if(r<=g && r<=b)min=r;elseif(g<=r && g<=b)min=g;elseif(b<=r && b<=g)

49

Page 50: Graphics Lab Manual

min=b;delta=max-min;v=max;if(max!=0.0)s=(float)(delta/max);elses=(float)(0.0);if(s==0.0)h=(float)(1);else{if(r==max)h=(float)((g-b)/delta);elseif(g==max)h=(float)(2+((b-r)/delta));elseif(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);

50

Page 51: Graphics Lab Manual

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);}}}

51

Page 52: Graphics Lab Manual

OUTPUT:

RGB to HSV conversion:

52

Page 53: Graphics Lab Manual

HSV to RGB conversion:

RESULT:

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

53

Page 54: Graphics Lab Manual

FLASH MX

54

Page 55: Graphics Lab Manual

Ex.No: 1Date:

MOVING CAR ANIMATION

Aim

To design a car and animate it using macromedia flash.

Algorithm

13. Open Start -> Programs -> Macromedia -> Flash14. 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.

55

Page 56: Graphics Lab Manual

OUTPUT:

RESULT:

Thus the animation was created using macromedia.

56

Page 57: Graphics Lab Manual

Ex.No: 2Date:

FISH TANK ANIMATION

Aim

To create a fish tank and animate it using macromedia.

Algorithm

22. Open Start -> Programs -> Macromedia -> Flash23. 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.

57

Page 58: Graphics Lab Manual

OUTPUT:

RESULT:Thus the animation was created using macromedia.

Ex.No: 3

58

Page 59: Graphics Lab Manual

Date:

KEYFRAME ANIMATION

Aim

To perform keyframe animation using macromedia.

Algorithm

30. Open Start -> Programs -> Macromedia -> Flash31. 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.

59

Page 60: Graphics Lab Manual

OUTPUT:

RESULT:

Thus the key frame animation was created using macromedia.

60

Page 61: Graphics Lab Manual

Ex.No: 4Date:

MASKING AND MORPHING

Aim

To perform masking and morphing using macromedia.

Algorithm

Masking:38. Open Start -> Programs -> Macromedia -> Flash39. 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. Open Start -> Programs -> Macromedia -> Flash .2. Open a new document.3. Draw 2 different pictures in the same layer at different keyframes.4. In properties dialog box select shape tween at the first and last keyframes.5. Click Ctrl + Enter to see the result.

61

Page 62: Graphics Lab Manual

OUTPUT:

62

Page 63: Graphics Lab Manual

MASKING:

RESULT:

Thus the masking and morphing were completed.

63