You are on page 1of 67

BRESENHAMS LINE DRAWING ALGORITHM Ex No: 1 a Date: Aim: To write a program in C language for Bresenhams line drawing algorithm.

Algorithm: 1. Input the lines two endpoint and store the left end point in (x1,y1) and right end point in(x2,y2). 2. Load (x1,y1) into the frame buffer; (i.e) plot the first point. 3. Calculate constants dx, dy, 2dy and 2dy-2dx and obtain the starting value for the decision parameter as P0=2dy-dx 4. At each xk along the line, starting at k=0, perform the following test: If pk<0, then next point to plot is (xk+1, yk) and Pk+1=pk+2dy Otherwise, the next point to plot is (xk+1,yk+1) Pk+1=pk+2dy+2dy-2dx 5. Repeat the step-4dx times. 6. Stop the Program.

Program #include<stdio.h> #include<conio.h> #include<stdlib.h> #include<graphics.h> void main() { int gd=DETECT,gm; int x1,y1,x2,y2; void linebres(int,int,int,int); clrscr(); initgraph(&gd,&gm," "); printf("Enter the starting point\n"); scanf("%d%d",&x1,&y1); printf("Enter the ending point"); scanf("%d%d",&x2,&y2); linebres(x1,y1,x2,y2); getch(); } void linebres(int xa,int ya,int xb,int yb) { int dx,dy,p,twody,twodydx,x,y,xend; dx=abs(xa); dy=abs(ya); p=2*(dy-dx); twody=2*dy; twodydx=2*(dy=dx); if(xa>xb) { x=xb; y=yb; xend=xa; } else { x=xa; y=ya; xend=xb; } putpixel(x,y,15); while(x<xend) { x++; if(p<0) p+=twody; else

{ y++; p+=twodydx; } putpixel((int)x,(int)y,15); } }

Output: Enter The Two Left Endpoints(xa,ya): Enter The Two Right Endpoints(xb,yb): 578 234 321 124

Result: Thus the bresanhams line drawing algorithm is executed and output is verified

BRESENHAMS CIRCLE DRAWING ALGORITHM Ex No: 1 b Date: Aim: To write a program in C language for Bresenhams circle drawing algorithm. Algorithm: 1. Input radius r and circle center (x1,y1) and obtain the first point on the circumference of the circle centered on the origin (x0,y0)= (0,r) 2. Calculate the initial value of the decision parameter as P0=5/4-r 3. At each xk position, starting at k=0, perform the following i. If pk<0, point along the circle centered on (0, 0) is (xk,yk) and Pk+1=Pk+2xk+1+1 ii. Otherwise, the point along the circle is (xk+1,yk-1) and Pk+1=Pk+xk+1+1-2yk+1 4. Determine symmetry points in the other seven octants. 5. Move each calculated pixel position (x, y) on to the circular path centered on (xc,yc) and plot the coordinate values x=x+c,y=y+c 6. Repeat step- 3 to step-5 until x>=y 7. Stop the Program.

Program: #include<stdio.h> #include<conio.h> #include<stdlib.h> #include<graphics.h> void main() { int gd=DETECT,gm; int x,y,r; int x1,y1; void circlemid(int,int,int); clrscr(); initgraph(&gd,&gm," "); printf("Enter the x and y values"); scanf("%d%d",&x1,&y1); printf("Enter the radius"); scanf("%d",&r); circlemid(x1,y1,r); getch(); } void circlemid(int xc,int yc,int r) { int x,y,p; void circleplot(int,int,int,int); x=0; y=r; p=1-r; while(x<y) { x++; if(p<0) p+=2*x+1; else { y--; p+=2*(x-y); } circleplot(xc,yc,x,y); } } void circleplot(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); }

Output:

Enter The Radius Value

80 230 260

Enter The xcenter and ycenter Values :

Result: Thus the bresanhams circle drawing algorithm is executed and output is verified

BRESENHAMS ELLIPSE DRAWING ALGORITHM Ex.No:1 c Date: Aim: To write a program in C language to implement Bresenhams ellipse drawing algorithm. Algorithm: 1. Input rx, ry and ellipse center (xcen, ycen) and obtain the first point on an ellipse centered on the origin as (x0,y0)= (0, r) 2. Calculate the initial value of the decision parameter 1 as P10=r2y-r2xry+1/4r2x 3. At each xk position, starting at k=0 perform the following If Pk<0, point along the circle centered on (0, 0) is (xk,yk) Pk+1=p1k+2r2xxk+1+r2y Otherwise, the next point along the circle is (xk+1,yk-1) P1k+1=p1k+2r2yxk+1-2r2xyk+1+r2y 4. Calculate the initial value of the decision parameter 2 as P20=r2y0(x0+1/2)2+r2x(y0-1)2-r2xr2y 5. At each xk position, starting at k=0 perform the following If P2k>0, next point along the circle centered on(0,0) is P2k+1=P2k-2r2xyk+1+r2x Otherwise the next point along circle (xk+1,yk-1) and P2k+1=P2k+2r2yxk+1-2rxyk+1+r2x 6. Determine symmetry Points in the other three octants. 7. Move each calculated pixel position (x, y) onto circular path centered on (xc, yc) and plot coordinate values X=x+xc,y=y+yc 8. Repeat steps of region 1 until 2r2yx>2rx2y 9. Stop the Program.

Program: #include<iostream.h> #include<conio.h> #include<graphics.h> #define ROUND(a) ((int)(a+0.5)) void main() { void ellipseMidpoint(int,int,int,int); int gd=DETECT,gm,xcen,ycen,rx,ry; initgraph(&gd,&gm,""); cout<<"\nEnter the x radius and Y-radius:"; cin>>rx>>ry; xcen=getmaxx()/2; ycen=getmaxy()/2; ellipseMidpoint(xcen,ycen,rx,ry); getch(); } void ellipseMidpoint(int xCenter,int yCenter,int Rx,int Ry) { int Rx2; Rx2 = Rx*Ry; int Ry2 = Ry*Ry; int twoRx2; twoRx2 = 2 * Rx2; int twoRy2; twoRy2 = 2 * Ry2; int p; int x=0; int y=Ry; int px=0; int py = twoRx2*y; void ellipsePlotPoints(int,int,int,int); ellipsePlotPoints(xCenter,yCenter,x,y); p = ROUND (Ry2 - (Rx2 * Ry) + (.25 * Rx2)); while(px<py) { x++; px += twoRy2; if(p<0) p+= Ry2+px; else { y--; py-= twoRx2; p+= Ry2+px-py; }

ellipsePlotPoints(xCenter,yCenter,x,y); } p= ROUND (Ry2*(x+0.5)*(x+0.5) + Rx2*(y-1)*(y-1) - Rx2*Ry2); while(y>0) { y--; py -= twoRx2; if(p>0) p += Rx2-py; else { x++; px += twoRy2; p += Rx2-py+px; } ellipsePlotPoints(xCenter,yCenter,x,y); } } void ellipsePlotPoints(int xCenter,int yCenter,int x,int y) { putpixel(xCenter+x,yCenter+y,15); putpixel(xCenter-x,yCenter+y,15); putpixel(xCenter+x,yCenter-y,15); putpixel(xCenter-x,yCenter-y,15); }

Output: Enter The Radius Value(Rx,Ry) : 10 300 30 150

Enter The xcenter and ycenter Values :

Result: Thus the bresanhams ellipse drawing algorithm is executed and output is verified

OUTPUT PRIMITIVES Ex.No : 2 Date Aim: To write a C Program to display the output primitives. Algorithm: 1. Start the program . 2. Initialize the variables. 3. Call the initgraph() function 4. Set color for the output primitives. 5. Using Outtextxy() display the choosen particular primitives. 6. Using switch case mention the various primitives and their attributes. 7. The various primitives are arc, line ,circle, rectangle and ellipse. 8. close the graph and run the program. 9. stop the program. :

Program: #include<stdio.h> #include<conio.h> #include<graphics.h> #include<string.h> void main() { char ch='y'; int gd=DETECT,gm,x1,y1,x2,y2,rad,sa,ea,xrad,yrad,i; initgraph(&gd,&gm,""); while(ch=='y') { cleardevice(); setbkcolor(9); outtextxy(100,150,"Enter 1 to get line"); outtextxy(100,170,"2.Circle"); outtextxy(100,190,"3.Box"); outtextxy(100,210,"4.Arc"); outtextxy(100,230,"5.Ellipse"); outtextxy(100,250,"6.Rectangle"); outtextxy(100,270,"7.Exit"); ch=getch(); cleardevice(); switch(ch) { case '1': line(100,200,300,400); break; case '2': circle(200,200,100); break; case '3': setfillstyle(5,4); bar(100,300,200,100); break; case '4': setfillstyle(5,4); arc(200,200,100,300,100); break; case '5': setfillstyle(5,4); fillellipse(100,100,50,100); break; case '6': settextstyle(DEFAULT_FONT,0,2);

outtextxy(120,140,"UDAYA SCHOOL OF ENGINEERING"); line(100,100,100,300); line(300,300,100,300); line(100,100,300,100); line(300,100,300,300); break; case '7': closegraph(); return; } ch='y'; getch(); } }

Output: 1.Enter 1 to get line 2.Circle 3.Box 4.Arc 5.Ellipse 6.Rectangle 7.Exit 1

Result: Thus the program is executed successfully.

TWO DIMENSIONAL TRANSFORMATION Ex.No : 3 Date Aim: To perform the 2D transformation such as translation, rotation, scaling, shearing, Reflection Algorithm: 1. Declare the variables xa,ya,xa1,ya1 of array type. 2. Declare the variables gd,gm,n,i,op,tx,ty,xf,yf,rx,ry. 3. Initialise the graphics function. 4. Input the number of points. 5. Input the value of co-ordinate according to number of points. 6. Using switch statement selects the option to perform translation, rotation, scaling, reflection and shearing. 7. Translation: a).input the translation vector b).add the translation vectors with the coordinates xa1[i]=xa[i]=tx, ya1[i]=ya[i]=ty, c).using the function line,display the object before and after translation. 8. Rotation a). input the rotation angle b). using formula theta=(theta*3.14)/180 c).input the value of reference point d). calculate new coordinate point using formula xa1[i]=xf+(xa[i]-xf)*cos(theta)-(ya[i]-yf)*sin(theta), ya1[i]=yf+(xa[i]-xf)*sin(theta)-(ya[i]-yf)*cos(theta), e). using the function line,display the object before and after rotation. 9. Scaling: a).input the scaling factor and reference point b).calculate new coordinate point using formula :

xa1[i]=(xa[i]*sx+rx*(1-sx), ya1 [i] = (ya[i]*sy+ry*(1-sy) c). using the function line, display the object before and after scaling. 10. Shearing: a).input the shearing value and reference point. b). input the shear direction x or y i).if direction x xa1[i]=xa[i]+shx*(ya[i]-yref) ii).otherwise ya1[i]=ya[i]+shy*(xa[i]-xref) iii). using the function line, display the object before and after shearing. 11. Reflection: a).display the object before reflection using the function line b). display the object after reflection using the function line 12. Stop.

Program: #include<iostream.h> #include<conio.h> #include<math.h> #include<graphics.h> #include<stdlib.h> void main() { int gd,gm,n,i,xa[10],ya[10],op,tx,ty,xa1[10],ya1[10],theta,xf,yf,rx,ry,sx,sy,shx,shy,xref,yref; char d; gd=DETECT; initgraph(&gd,&gm,""); cout<<"enter the no of points"; cin>>n; for(i=0;i<n;i++) { cout<<"enter the coordinates"<<i+1; cin>>xa[i]>>ya[i]; } do { cout<<"menu"; cout<<"\n1.translation\n2.rotation\n3.scaling\n4.shearing\n5.reflection\n6.exit"; cin>>op; switch(op) { case 1: cout<<"enter the translation vector"; cin>>tx>>ty; for(i=0;i<n;i++) { xa1[i]=xa[i]+tx; ya1[i]=ya[i]+ty; } cout<<"before translation"; for(i=0;i<n;i++) { line(xa[i],ya[i],xa[(i+1)%n],ya[(i+1)%n]); } cout<<"after translation"; for(i=0;i<n;i++) { line(xa1[i],ya1[i],xa1[(i+1)%n],ya1[(i+1)%n]); }

getch(); cleardevice(); break; case 2: cout<<"enter the rotation angle"; cin>>theta; theta=(theta*3.14)/180; cout<<"enter the reference points"; cin>>xf>>yf; for(i=0;i<n;i++) { xa1[i]=xf+(xa[i]-xf)*cos(theta)-(ya[i]-yf)*sin(theta); ya1[i]=yf+(xa[i]-xf)*sin(theta)-(ya[i]-yf)*cos(theta); } cout<<"before rotation"; for(i=0;i<n;i++) { line(xa[i],ya[i],xa[(i+1)%n],ya[(i+1)%n]); } cout<<"after rotation"; for(i=0;i<n;i++) { line(xa1[i],ya1[i],xa1[(i+1)%n],ya1[(i+1)%n]); } getch(); cleardevice(); break; case 3: cout<<"enter the scaling factor"; cin>>sx>>sy; cout<<"enter the reference point"; cin>>rx>>ry; for(i=0;i<n;i++) { xa1[i]=xa[i]*sx+rx*(1-sx); ya1[i]=ya[i]*sy+ry*(1-sy); } cout<<"before scaling"; for(i=0;i<n;i++) { line(xa[i],ya[i],xa[(i+1)%n],ya[(i+1)%n]); } cout<<"after scaling"; for(i=0;i<n;i++) {

line(xa1[i],ya1[i],xa1[(i+1)%n],ya1[(i+1)%n]); } getch(); cleardevice(); break; case 4: cout<<"enter the shear value"; cin>>shx>>shy; cout<<"enter the reference point"; cin>>xref>>yref; cout<<"enter the shear direction x or y"; cin>>d; if(d=='x') { for(i=0;i<n;i++) { xa1[i]=xa[i]+shx*(ya[i]-yref); ya1[i]=ya[i]; } } cout<<"before shearing"; for(i=0;i<n;i++) { line(xa[i],ya[i],xa[(i+1)%n],ya[(i+1)%n]); } cout<<"after shearing"; for(i=0;i<n;i++) { line(xa1[i],ya1[i],xa1[(i+1)%n],ya1[(i+1)%n]); } getch(); cleardevice(); break; case 5: cout<<"before reflection"; for(i=0;i<n;i++) { line(xa[i],ya[i],xa[(i+1)%n],ya[(i+1)%n]); } cout<<"after reflection"; for(i=0;i<n;i++) { line(ya[i],xa[i],ya[(i+1)%n],xa[(i+1)%n]); } getch(); cleardevice();

break; case 6: exit(0); break; } }while(op!=6); }

Output: Menu 1. 2. 3. 4. 5. 6. Translation Rotation Scaling Shearing Reflection Exit

Translation Enter the choice : 1 Enter the number of Vertices: 3 Enter the coordinates : Enter the coordinates : Enter the coordinates : 30 10 60 150 200 200 10 60 30 200 200 150

Enter the translation vector Tx, Ty : 90

60

Rotation Enter the choice : 2 Enter the number of Vertices: 3 Enter the coordinates : Enter the coordinates : Enter the coordinates : 30 10 60 150 200 200 10 60 30 200 200 150

Enter the Rotating Angle : Enter the Pivot Point :

90 100 200

Scaling Enter the choice : 3 Enter the number of Vertices: 3

Enter the coordinates : Enter the coordinates : Enter the coordinates :

30 10 60

150 200 200

10 60 30

200 200 150

Enter the scaling Factor : 0.3 Enter the Fixed Point :

0.4 100

200

Shearing Enter the choice : 4 Enter the number of Vertices: 3 Enter the coordinates : Enter the coordinates : Enter the coordinates : 30 10 60 150 200 200 10 60 30 200 200 150

Enter the shear Value : Enter the fixed point

5 : 50

100 if x-axis then 1 if y-axis then 0

Enter the Axis for shearing

Reflection Enter the choice : 5 Enter the number of Vertices: 3 Enter the coordinates : Enter the coordinates : Enter the coordinates : 30 10 60 150 200 200 10 60 30 200 200 150

Result: Thus the C program for implementation of two dimensional transformation is written and output was verified.

TWO DIMENSIONAL COMPOSITE TRANSFORMATION Ex.No:4 Date: Aim : To write a Program in C to implement the 2-Dimensional composite transformations such as translation, rotation, scaling. Algorithm: 1. Start the Program. 2. Enter the option to select the transformation to be performed. 3. Translate the object by adding the translation vector tx1,ty1,tx2and ty2 to the x and y coordinates Xi+1=Xi+tx1+tx2 coordinates. Xi+1=Xi*sx1*sx2 successive rotating angle Xi+1=Xref+ (Xi-Xref)cos(?) - (Yi-Yref) sin (?) Yi+1=Yref+ (Xi-Xref) sin (?)- (Yi-Yref)cos(?) 6. Stop the Program. Yi+1=Yi*sy1*sy2 5. Rotation is performed by rotating the object with respect to the reference point and tow Yi+1=Yi+ty1+ty2 4. Scaling is performed by multiplying the scaling vector sx1,sy1,sx2and sy2 to the x and y

Program: #include<stdio.h> #include<conio.h> #include<graphics.h> #include<dos.h> #include<math.h> #include<stdlib.h> void menu(); void input(); void output(); void comtranslation(); void comrotation(); void comscaling(); int a[10][2],i,x,option,temp,angle,tx,ty,fx,fy,sh,k,n,axis,y; int c,b,tx1,ty1,tx2,ty2; float sx1,sy1,sx2,sy2,sx,sy; void menu() { printf("menu\n"); printf("1.Translation\n"); printf("2.rotation\n"); printf("3.scaling\n"); printf("4.exit\n"); printf("enter the choice:"); scanf("%d",&option); switch(option) { case 1: input(); comtranslation(); break; case 2: input(); comrotation(); break; case 3: input(); comscaling(); break; case 4: exit(0); break; } } void input()

{ printf("enter the number of vertices:" ); scanf("%d",&n); for(i=0;i<n;i++) { printf("enter the coordinates:"); scanf("%d%d%d%d",&a[i][0],&a[i][1],&a[i+1][0],&a[i+1][1]); } } void output() { cleardevice(); for(i=0;i<n;i++) { line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]); } } void comtranslation() { output(); printf("enter the tranformation vertex tx1,ty1,tx2,ty2:\n"); scanf("%d%d%d%d",&tx1,&ty1,&tx2,&ty2); tx=tx1+tx2; ty=ty1+ty2; for(i=0;i<=n;i++) { a[i][0]=a[i][0]+tx; a[i][1]=a[i][1]+ty; } output(); delay(10); menu(); } void comrotation() { output(); printf("enter the two successive rotating angle:"); scanf("%d%d",&c,&b); printf("enter the pivot point:"); scanf("%d%d",&fx,&fy); y=c+b; k=(y*3.14)/180; for(i=0;i<=n;i++) { a[i][0]=fx+(a[i][0]-fx)*cos(k)-(a[i][1]-fy)*sin(k);

a[i][1]=fy+(a[i][0]-fx)*sin(k)-(a[i][1]-fy)*cos(k); } output(); delay(10); menu(); } void comscaling() { output(); printf("enter the two successive scaling factors\n"); scanf("%f%f%f%f",&sx1,&sy1,&sx2,&sy2); sx=sx1*sx2; sy=sy1*sy2; printf("enter the fixed point:"); scanf("%d%d",&fx,&fy); for(i=0;i<=n;i++) { a[i][0]=a[i][0]*sx+fy*(1-sx); a[i][1]=a[i][1]*sy+fy*(1-sy); } output(); delay(10); menu(); } void main() { int gd=DETECT,gm; initgraph(&gd,&gm,"c:\\tcplus\\bgi"); menu(); getch(); }

Output: Menu 1. 2. 3. 4. Translation Rotation Scaling Exit

Translation Enter the choice : 1 Enter the number of Vertices: 3 Enter the coordinates : Enter the coordinates : Enter the coordinates : 30 10 60 150 200 200 10 60 30 200 200 150

enter the transformation vertex tx1,ty1,tx2,ty2: 30 18 60 12

Rotation Enter the choice : 2 Enter the number of Vertices: 3 Enter the coordinates : Enter the coordinates : Enter the coordinates : 30 10 60 150 200 200 10 60 30 200 200 150

enter the two successive rotating angle: 50 40 Enter the Pivot Point : 100 200

Scaling Enter the choice : 3 Enter the number of Vertices: 3

Enter the coordinates : Enter the coordinates : Enter the coordinates :

30 10 60

150 200 200

10 60 30

200 200 150

enter the two successive scaling factors: 0.15 Enter the Fixed Point : 100 200

0.2 0.15 0.2

Result: Thus the C program for implementation of two dimensional composite transformation is written and output was verified.

COHEN-SUTHERLAND LINE CLIPPING ALGORITHM Ex.No.: 5 Date: Aim: To clip a line using Cohen-Sutherland clipping algorithm. Algorithm: 1. The method speeds up the processing of line segments by performing initial tests that reduce the number of intersections that must be calculated. 2. 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. 3. 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 4. 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 5. Once we have established region codes for all line endpoints, we can quickly determine which lines are completely outside or inside the clip window. 6. Lines that cannot be identified as completely inside or outside a clip window are checked for intersection with boundaries. 7. Intersection points with a clipping boundary can be calculated using the slope-intercept form of the line equation.
m = ( y2 y1 ) ( x2 x1 )

The y coordinate of the intersection point at vertical line

y = y1 + m( x x1 )
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<graphics.h> #define left 0 #define right 1 #define bottom 2 #define top 3 struct coord { int x,y; }winmin={200,200},winmax={400,400},pts1[10],pts2[10]; int code[4]; void encode(struct coord pt) { if(pt.x<winmin.x) code[left]=1; else code[left]=0; if(pt.x>winmax.x) code[right]=1; else code[right]=0; if(pt.y<winmin.y) code[bottom]=1; else code[bottom]=0; if(pt.y>winmax.y) code [top]=1; else code[top]=0; } int accept(int c1[],int c2[]) { int ac=1,k; for(k=0;k<=3;k++) { if(c1[k]||c2[k]) ac=0; } return ac; } int reject (int c1[],int c2[])

{ int rj=0,k; for(k=0;k<=3;k++) { if(c1[k]&&c2[k]) rj=1; } return rj; } int ptinside(int c[]) { if(c[left]||c[right]||c[bottom]||c[top]) return 0; else return 1; } void swappts(struct coord *p1,struct coord *p2) { struct coord tmp; tmp=*p1; *p1=*p2; *p2=tmp; } void swapcodes(int *c1,int *c2) { int tmp; tmp=*c1; *c1=*c2; *c2=tmp; } void clipline(struct coord p1,struct coord p2) { int k,done=0,draw=0,code1[4],code2[4]; float m; while(!done) { encode(p1); for(k=0;k<=3;k++) code1[k]=code[k]; encode(p2); for(k=0;k<=3;k++) code2[k]=code[k]; if(accept(code1,code2)) {

done =draw=1; } else if(reject(code1,code2)) { done=1; } else { if(ptinside(code1)) { swappts(&p1,&p2); swapcodes(code1,code2); } if(p2.x!=p1.x) m=(float)(p2.y-p1.y)/(p2.x-p1.x); if(code1[left]) { p1.y=p1.y+(winmin.x-p1.x)*m; p1.x=winmin.x; } else if(code1[right]) { p1.y=p1.y+(winmax.x-p1.x)*m; p1.x=winmax.x; } else if(code1[bottom]) { if(p2.x!=p1.x) p1.x=p1.x+(winmin.y-p1.y)/m; p1.y=winmin.y; } else if(code1[top]) { if(p2.x!=p1.x) p1.x=p1.x+(winmax.y-p1.y)/m; p1.y=winmax.y; } } }if(draw)line(p1.x,p1.y,p2.x,p2.y); } void main() { int gd=DETECT,gm; int n,i; printf("\nEnter the No of lines:"); scanf("%d",&n);

for(i=0;i<n;i++) { printf("\nEnter the start pointfor the line %d",i+1); scanf("%d%d",&pts1[i].x,&pts1[i].y); printf("\nEnter the End point for line%d:",i+1); scanf("%d%d",&pts2[i].x,&pts2[i].y); } initgraph(&gd,&gm,""); for(i=0;i<n;i++) line(pts1[i].x,pts1[i].y,pts2[i].x,pts2[i].y); rectangle(200,200,400,400); getch(); cleardevice(); rectangle(200,200,400,400); for(i=0;i<n;i++) clipline(pts1[i],pts2[i]); getch(); closegraph(); }

Output:
Before Clippping Please Enter Left , Bottom , Right , Top Of The Clip window 200 200 400 400 Please Enter The Line Coordinates (X1, Y1, X2, Y2) 150 300 400 450

After Clipping

Result: Thus the given C program for is implemented and output was verified.

WINDOW TO VIEWPORT MAPPING Ex.No.: 6 Date: Aim: To write a C program to perform Window to Viewport transformation. Algorithm: 1. Draw a world coordinate area selected for display called as window. This window defines what is to be viewed. 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. 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<graphics.h> #include<math.h> main() { float sx,sy; int w1,w2,w3,w4,x1,x2,x3,x4,y1,y2,y3,y4,v1,v2,v3,v4; int gd=DETECT,gm; initgraph(&gd,&gm,"c:\\tc\\bgi"); printf("Enter The Coordinate x1,y1,x2,y2,x3,y3\n"); scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3); cleardevice(); w1=5; w2=5; w3=635; w4=465; rectangle(w1,w2,w3,w4); line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x3,y3,x1,y1); getch(); v1=425; v2=75; v3=550; v4=250; sx=(float)(v3-v1)/(w3-w1); sy=(float)(v4-v2)/(w4-w2); rectangle(v1,v2,v3,v4); x1=v1+floor(((float)(x1-w1)*sx)+.5); x2=v1+floor(((float)(x2-w1)*sx)+.5); x3=v1+floor(((float)(x3-w1)*sx)+.5); y1=v2+floor(((float)(y1-w2)*sy)+.5); y2=v2+floor(((float)(y2-w2)*sy)+.5); y3=v2+floor(((float)(y3-w2)*sy)+.5); line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x3,y3,x1,y1); getch(); return 0; }

Output Enter The Coordinate x1,y1,x2,y2,x3,y3 100 200 300 400 500 350

Result: Thus the C program for window to view-port is written and output was verified.

SUTHERLAND HODGEMAN POLYGON CLIPPINGALGORITHM Ex.No.7 Date: Aim: To clip a Polygon using Sutherland Hodgeman Algorithm. Algorithm: 1. Input Coordinates of all vertices of the polygon 2. Input coordinates of the clipping window 3. Consider the left edge of the window 4. Compare the vertices of each edge of the polygon , individually with the clipping plane 5. Save the resulting intersections and vertices in the new list of vertices according to four possible relationships between the edge and the clipping boundary discussed earlier 6. Repeat the steps 4 and 5 for remaining edges of the clipping window. Each time the resultant list of vertices is successively passed to process the next edge of the clipping window 7. Stop program

Program: #include<stdio.h> #include<conio.h> #include<graphics.h> #define round(a) ((int)(a+0.5)) int k; float xmin,ymin,xmax,ymax,arr[20],m; void clipl(float x1,float y1,float x2,float y2) { if(x2-x1) m=(y2-y1)/(x2-x1); else m=100000; if(x1>=xmin && x2>=xmin) { arr[k]=x2; arr[k+1]=y2; k+=2; } if(x1<xmin && x2>=xmin) { arr[k]=xmin; arr[k+1]=y1+m*(xmin-x1); arr[k+2]=x2; arr[k+3]=y2; k+=4; } if(x1>=xmin && x2<xmin) { arr[k]=xmin; arr[k+1]=y1+m*(xmin-x1); k+=2; } } void clipt(float x1,float y1,float x2,float y2) { if(y2-y1) m=(x2-x1)/(y2-y1); else m=100000; if(y1<=ymax && y2<=ymax) { arr[k]=x2; arr[k+1]=y2; k+=2;

} if(y1>ymax && y2<=ymax) { arr[k]=x1+m*(ymax-y1); arr[k+1]=ymax; arr[k+2]=x2; arr[k+3]=y2; k+=4; } if(y1<=ymax && y2>ymax) { arr[k]=x1+m*(ymax-y1); arr[k+1]=ymax; k+=2; } } void clipr(float x1,float y1,float x2,float y2) { if(x2-x1) m=(y2-y1)/(x2-x1); else m=100000; if(x1<=xmax && x2<=xmax) { arr[k]=x2; arr[k+1]=y2; k+=2; } if(x1>xmax && x2<=xmax) { arr[k]=xmax; arr[k+1]=y1+m*(xmax-x1); arr[k+2]=x2; arr[k+3]=y2; k+=4; } if(x1<=xmax && x2>xmax) { arr[k]=xmax; arr[k+1]=y1+m*(xmax-x1); k+=2; } } void clipb(float x1,float y1,float x2,float y2)

{ if(y2-y1) m=(x2-x1)/(y2-y1); else m=100000; if(y1>=ymin && y2>=ymin) { arr[k]=x2; arr[k+1]=y2; k+=2; } if(y1<ymin && y2>=ymin) { arr[k]=x1+m*(ymin-y1); arr[k+1]=ymin; arr[k+2]=x2; arr[k+3]=y2; k+=4; } if(y1>=ymin && y2<ymin) { arr[k]=x1+m*(ymin-y1); arr[k+1]=ymin; k+=2; } } void main() { int gdriver=DETECT,gmode,n,poly[20],i; float xi,yi,xf,yf,polyy[20]; initgraph(&gdriver,&gmode,""); clrscr(); printf("Coordinates of rectangular clip window :\nxmin,ymin scanf("%f %f",&xmin,&ymin); printf("\nxmax,ymax :"); scanf("%f %f",&xmax,&ymax); printf("\n\nPolygon to be clipped :\nNumber of sides :"); scanf("%d",&n); printf("Enter the coordinates :"); for( i=0;i<2*n;i++) scanf("%f",&polyy[i]); polyy[i]=polyy[0]; polyy[i+1]=polyy[1]; for(i=0;i<2*n+2;i++) poly[i]=round(polyy[i]);

:");

setcolor(RED); rectangle(xmin,ymax,xmax,ymin); printf("\t\tUNCLIPPED POLYGON"); setcolor(WHITE); fillpoly(n,poly); getch(); cleardevice(); k=0; for(i=0;i<2*n;i+=2) clipl(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]); n=k/2; for(i=0;i<k;i++) polyy[i]=arr[i]; polyy[i]=polyy[0]; polyy[i+1]=polyy[1]; k=0; for(i=0;i<2*n;i+=2) clipt(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]); n=k/2; for(i=0;i<k;i++) polyy[i]=arr[i]; polyy[i]=polyy[0]; polyy[i+1]=polyy[1]; k=0; for(i=0;i<2*n;i+=2) clipr(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]); n=k/2; for(i=0;i<k;i++) polyy[i]=arr[i]; polyy[i]=polyy[0]; polyy[i+1]=polyy[1]; k=0; for(i=0;i<2*n;i+=2) clipb(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]); for(i=0;i<k;i++) poly[i]=round(arr[i]); if(k) fillpoly(k/2,poly); setcolor(RED); rectangle(xmin,ymax,xmax,ymin); printf("\tCLIPPED POLYGON"); getch(); closegraph(); }

Output: Coordinates of rectangular clip window : xmin,ymin xmax,ymax : 100 100 : 250 250

Polygon to be clipped : Number of sides :3

Enter the coordinates : 50 200 200 225 150 50

Result: Thus the C program is written and output was verified.

THREE DIMENSIONAL TRANSFORMATION Ex.No:8 Date: Aim : To write a Program in C to implement the basic 3-Dimensional transformations such as translation, rotation, scaling. Algorithm: 1. Start the Program. 2. Enter the option to select the transformation to be performed. 3. Translate the object by adding the translation vector tx , ty and tz to the x ,y and z coordinates Xi+1=Xi+tx coordinates. Xi+1=Xi*sx 6. Stop the Program Yi+1=Yi*sy Zi+1=Zi*sz 5. Rotation is performed by rotating the object with respect to x-axis, y-axis and z-axis Yi+1=Yi+ty Zi+1=Zi+tz 4. Scaling is performed by multiplying the scaling vector sx, sy and sz to the x , y and z

Program: #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> int maxx,maxy,midx,midy; void axis() { getch(); cleardevice(); line(midx,0,midx,maxy); line(0,midy,maxx,midy); } void main() { int gd,gm,x,y,z,o,x1,x2,y1,y2; detectgraph(&gd,&gm); initgraph(&gd,&gm," "); setfillstyle(0,getmaxcolor()); maxx=getmaxx(); maxy=getmaxy(); midx=maxx/2; midy=maxy/2; axis(); bar3d(midx+50,midy-100,midx+60,midy-90,5,1); printf("Enter Translation Factor"); scanf("%d%d%d",&x,&y,&z); axis(); printf("after translation"); bar3d(midx+(x+50),midy-(y+100),midx+x+60,midy-(y+90),5,1); axis(); bar3d(midx+50,midy+100,midx+60,midy-90,5,1); printf("Enter Scaling Factor"); scanf("%d%d%d",&x,&y,&z); axis(); printf("After Scaling"); bar3d(midx+(x*50),midy-(y*100),midx+(x*60),midy-(y*90),5*z,1); axis(); bar3d(midx+50,midy-100,midx+60,midy-90,5,1); printf("Enter Rotating Angle"); scanf("%d",&o); x1=50*cos(o*3.14/180)-100*sin(o*3.14/180); y1=50*cos(o*3.14/180)+100*sin(o*3.14/180);

x2=60*sin(o*3.14/180)-90*cos(o*3.14/180); y2=60*sin(o*3.14/180)+90*cos(o*3.14/180); axis(); printf("After Rotation about Z Axis"); bar3d(midx+x1,midy-y1,midx+x2,midy-y2,5,1); axis(); printf("After Rotation about X Axis"); bar3d(midx+50,midy-x1,midx+60,midy-x2,5,1); axis(); printf("After Rotation about Y Axis"); bar3d(midx+x1,midy-100,midx+x2,midy-90,5,1); getch(); closegraph(); }

Output Translation Enter Translation Factor : 50 60 70

After Translation

Scaling Enter Scaling Factor : 80 90 95

After Scaling

Rotation Enter Rotating Angle : 60

After Rotation about Z-Axis

After Rotation about X-Axis

After Rotation about Y-Axis :

Result: Thus the C program for implementation of three dimensional transformation is written and output was verified.

GENERATING FRACTAL IMAGES Ex.No.:9 Date: Aim: To write a C program to generate fractal images. Algorithm: 1.Start the program. 2.Initialize the value for Mandelbrot and Julia set. 3.Initialize the EGA driver for displaying the color. 4.First, the pixel must be converted to its actual plane value (x,y). Then it is passed through the function the number of times in the global variable iterations. If the break condition is reached, which has been mathematically determined so that if the value of Z is larger than that, the function will continue for ever to infinity. 5.Depending on the number of times the function was iterated before the break value was reached, the color displayed on the screen changes. If the break value is never reached, the point is considered to be part of the set. The color for that is black (0). 6.In julia set z is the point taken consideration 7.The mandelbrot function is the same as the Julia function except that the Z is 0 and the constant is the point under consideration. 8.Stop the program.

Program: #include <stdlib.h> #include <graphics.h> #include <conio.h> #include <iostream.h> #include <stdio.h> void menu(void); void init(void); void mandel(int, int); void julia(int _x,int _y); void options(void); float frac=1; float conx=-0.74; float cony=0.1; float Maxx=2; float Minx=-2; float Maxy=1; float Miny=-1; float initer=50; float pixcorx; float pixcory; int scrsizex; int scrsizey; int newcolor; int lastcolor; int main(void) { starting: menu(); init(); int j=0; do { int i=0; do //Start horizontal loop { if (frac) { julia(i,j); if (lastcolor!=newcolor) julia(i-1,j); else putpixel(i-1,j,lastcolor); } else { mandel(i,j); if (lastcolor!=newcolor) mandel(i-1,j); else putpixel(i-1,j,lastcolor);

} newcolor=lastcolor; i+=2;} while ( (i<scrsizex) && !kbhit() ); j++;} while ( (j<scrsizey) && !kbhit() ); getch(); closegraph(); goto starting; return 0; } void menu(void) { start: clrscr(); printf("\t\t\tFractal Generator\n"); printf("\t\t\t For Julia and Mandelbrot fractals"); printf("\n\n\n 1.Display Fractal"); printf("\n 2.Change Display Options"); printf("\n 3.Exit"); int ink=getch(); if (ink=='3') exit(1); if (ink=='1') return; if (ink=='2') options(); goto start; } void options(void) { startop: clrscr(); printf("1.Fractal type: %f\n",frac); printf("Mandelbrot = 0 Julia 1\n"); printf("2.Real constant: %f\n",conx); printf("3.Imaginary constant: %f\n",cony); printf("4.Max real value: %f\n",Maxx); printf("5.Min real value: %f\n",Minx); printf("6.Max imaginary value: %f\n",Maxy); printf("7.Min imaginary value: %f\n",Miny); printf("8.Initerations: %f\n",initer); printf("9.Exit"); int ans=getch(); if (ans=='9') return; printf("\nEnter New Value:"); float newval; scanf("%f",&newval); if (ans=='1') frac=newval;

else if (ans=='2') conx=newval; else if (ans=='3') cony=newval; else if (ans=='4') Maxx=newval; else if (ans=='5') Minx=newval; else if (ans=='6') Maxy=newval; else if (ans=='7') Miny=newval; else if (ans=='8') initer=newval; goto startop; } void init(void) { int gdriver=DETECT,gmode,errorcode; int driver,mode; driver=EGA; mode=EGAHI; initgraph(&gdriver,&gmode, ""); int errorcode = graphresult(); if (errorcode != grOk) { printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to exit:"); getch(); exit(1); } scrsizex= getmaxx() +1; scrsizey= getmaxy() +1; pixcorx=(Maxx-Minx)/scrsizex; pixcory=(Maxy-Miny)/scrsizey; } void julia(int xpt, int ypt) { long double x=xpt*pixcorx+Minx; long double y=Maxy-ypt*pixcory; long double xnew=0; long double ynew=0; for(int k=0;k<=initer;k++) { xnew=x*x-y*y + conx; ynew=2*x*y + cony; x=xnew; y=ynew; if ( (x*x+y*y)>4 ) break; int color = k; if (color>15) color=color%15; if (k>=initer) putpixel(xpt,ypt,0);

else putpixel(xpt,ypt,color); newcolor=color; } void mandel(int xpt, int ypt) { long double x=0; long double y=0; long double xnew=0; long double ynew=0; for(int k=0;k<=initer;k++) { xnew=x*x-y*y + xpt*pixcorx+Minx; ynew=2*x*y + Maxy-ypt*pixcory; x=xnew; y=ynew; if ( (x*x+y*y)>4 ) break; } int color = k; if (color>15) color=color%15; if (k>=initer) putpixel(xpt,ypt,0); else putpixel(xpt,ypt,color); newcolor=color; }

Output Fractal Generator For Julia and Mandelbrot fractals 1.Display Fractal 2.Change Display Options 3.Exit

1.Fractal type: 1.000000 Mandelbrot = 0 2.Real constant: -0.740000 3.Imaginary constant: 0.100000 4.Max real value: 2.000000 5.Min real value: -2.000000 6.Max imaginary value: 1.000000 7.Min imaginary value: -1.000000 8.Initerations: 50.000000 9.Exit

Julia = 1

Result: Thus the C program for implementation of fractal generation is written and output was verified.

You might also like