You are on page 1of 72

REG NO: 40508104067

LINE DRAWING USING DDA ALGORITHM

AIM: To write a C program to draw a line using DDA algorithm.

ALGORITHM: 1. Start the program. 2. Read the starting and ending coordinates xa, ya, xb, and yb. 3. Find the x-coordinate difference and y-coordinate difference. dx=xb-xa & dy=yb-ya. 4. Compare the difference and decide the step value. if(dx>dy) step=dx else step=dy 5. Find the increment values of coordinates. xi=dx/step yi=dy/step 6. Display the starting point using the function putpixel(xa,ya,4). 7. Find adjacent pixels using the formula xa=xa+xi & ya=ya+yi. 8. Repeat the steps till reaching the endpoints i.e, ya=yb & xa=xb. 9. Stop the program.

REG NO: 40508104067

PROGRAM: #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> void main() { int gd=DETECT,gm; float xadj,yadj,xa,ya,xb,yb,dx,dy,step,xi,yi,d,t1,t2; initgraph(&gd,&gm,""); d=1; printf("Enter the starting coordinates:"); scanf("%f %f",&xa,&ya); printf("Enter the ending coordinates:"); scanf("%f %f",&xb,&yb); dx=xb-xa; dy=yb-ya; if(abs(dx)>abs(dy)) step=abs(dx); else step=abs(dy); xi=dx/step; yi=dy/step; while(d==1) { putpixel(xa,ya,4); xadj=xa+xi; yadj=ya+yi; xa=xadj; ya=yadj; if((xa==xb)&&(ya==yb)) d=0; } getch(); closegraph(); }

REG NO: 40508104067

OUTPUT:

Enter the starting coordinates: 100 100 Enter the ending coordinates: 200 200

RESULT: Thus a line is drawn successfully using DDA algorithm in C.

REG NO: 40508104067

LINE DRAWING USING BRESENHAMS ALGORITHM

AIM: To write a program in C to draw a line using Bresenhams algorithm.

ALGORITHM: 1. 2. 3. 4. 5. Start the program. Read the starting and ending coordinates xa, ya, xb, yb. Find the x-coordinate difference and y-difference. dx=xb-xa & dy=yb-ya Calculate decision parameter p value. p= 2dy-dx Fix the starting and ending coordinates. xstart=xa if(xa<xb) ystart=ya xend=xb yend=yb else xstart=xb ystart=yb xend=xa yend=ya Display the starting point using the function putpixel(xa,ya,4). Find adjacent pixels and display it using the formula given below : i. x=xstart & y=ystart ii. while(x<xend) x=x+1 if(p<0) p=p+2*dy else p=p+2*(dy-dx) y=y+1 iii. putpixel(x,y,1) Repeat the step 7 till reaching the end points. Stop the program.

6. 7.

8. 9.

REG NO: 40508104067

PROGRAM: #include<stdio.h> #include<conio.h> #include<graphics.h> void main() { int gd=DETECT,gm; int xa, xb, ya, yb, dx, dy, x, y, xstart, ystart, xend, yend, p; initgraph(&gd,&gm,""); clrscr(); printf("Enter the xa & ya value:"); scanf("%d %d",&xa,&ya); printf("Enter the xb & yb value:"); scanf("%d %d",&xb,&yb); dx=abs(xa-xb); dy=abs(ya-yb); p=2*dy-dx; if(xa<xb) { xstart=xa; ystart=ya; xend=xb; yend=yb; } else { xstart=xb; ystart=yb; xend=xa; yend=xa; } putpixel(xstart,ystart,4); x=xstart; y=ystart; while(x<xend) { x=x+1; if(p<0) p=p+(2*dy); else { p=p+2*(dy-dx); y=y+1; } putpixel(x,y,1); } getch(); closegraph(); }

REG NO: 40508104067

OUTPUT: Enter the xa & ya value: 200 200 Enter the xb & yb value: 350 450

RESULT: Thus a line is drawn successfully using Bresenhams algorithm in C.

REG NO: 40508104067

CIRCLE DRAWING USING BRESENHAMS CIRCLE ALGORITHM

AIM: To draw a circle using Bresenhams circle drawing algorithm in C.

ALGORITHM: 1. 2. 3. Start the program. Get the radius and center of the circle r, xc, yc. Obtain the first point on the circumference of a circle centered on the origin as (X0,Y0)=(0,r) 4. 5. Calculate the initial value of the decision parameter as p =5/4-r At each xk position, starting at k=0, perform the following test if(pk<0), the next point along the circle centered on (0,0) is (xk+1,yk+1) and pk+1=pk+2xk+1+1 Otherwise the next point along the circle is (xk+1,yk-1) and pk+1=pk+2xk+1+1-2yk+1 , where 2xk+1 =2xk+2 and 2yk+1 =2yk 2. 6. 7. Determine symmetry points in other seven octants. Move each calculated pixel position (x,y) onto the circular path centered on (xc,yc) and plot the coordinate values x=x+xc & y=y+yc. 8. 9. Repeat the steps 5 to 7 until x>=y. Stop the program.

REG NO: 40508104067

PROGRAM: #include<stdio.h> #include<conio.h> #include<graphics.h> float xc, yc, r, x, y, p; void plotpoint(); void main() { int gd=DETECT,gm; initgraph(&gd,&gm,""); printf("Enter the xc value:"); scanf("%f",&xc); printf("Enter the yc value:"); scanf("%f",&yc); printf("Enter the radius:"); scanf("%f",&r); x=0; y=r; plotpoint(); p=1-r; while(x<y) { if(p<0) x++; else { x++; y--; } if(p<0) p=p+2*(x+1); else p=p+2*(x-y)+1; plotpoint(); } getch(); closegraph(); }

REG NO: 40508104067

void plotpoint() { putpixel(xc+x,yc+y,1); putpixel(xc-x,yc+y,1); putpixel(xc+x,yc-y,1); put pixel(xc-x,yc-y,1); putpixel(xc+y,yc+x,1); putpixel(xc-y,yc+x,1); putpixel(xc+y,yc-x,1); putpixel(xc-y,yc-x,1); }

REG NO: 40508104067

OUTPUT: Enter the xa value: 200 Enter the ya value: 200 Enter the radius: 50

RESULT: Thus a circle is drawn successfully using Bresenhams circle drawing algorithm in C.

REG NO: 40508104067

ELLIPSE DRAWING ALGORITHM AIM: To write a program in C to draw an ellipse using midpoint ellipse drawing algorithm.

ALGORITHM: 1. Start the program. 2. Get the radius rx, ry and center of the ellipse xc, yc. 3. Obtain the first point on the ellipse centered on the origin as (x0,y0)=(0,ry) 4. Calculate the initial value of the decision parameter in region1 as p10=ry2-rx2ry+1/4rx2. 5. At each xk position in region1,starting at k=0,perform the following test If(p1k<0),the next point along the ellipse centered on (0,0) is (xk+1,yk) and p1k+1=p1k+2ry2xk+1+ry2 Otherwise the next point along the ellipse is (xk+1,yk-1) and p1k+1=p1k+2ry2xk+1+ry2-2rx2yk+1,where 2ry2xk+1=2ry2xk+2ry2 and 2rx2yk+1=2rx2yk-2rx2. 6. Calculate the value of the decision parameter in region2 using the point (x0,y0) as p20=ry2(x0+1/2)2+rx2(y0-1)2-rx2ry2. 7. At each yk position in region2,starting at k=0, perform the following test If(p2k>0), the next point along the ellipse centered on (0,0) is (xk, yk+1) and p2k+1=p2k-2rx2yk+1+rx2 Otherwise the next point along the ellipse is (xk+1,yk+1) and P2k+1=p2k+2ry2xk+1+rx2-2rx2yk+1, using the same incremental calculations for x & y as in region2. 8. Determine the symmetry points in other three quadrants. 9. Move each calculated pixel position (x,y) onto the elliptical path centered on (xc,yc) and plot the coordinate values, x=x+xc & y=y+yc. 10. Repeat the steps for region1 until 2ry2x>2rx2y. 11. Stop the program.

REG NO: 40508104067

PROGRAM: #include<stdio.h> #include<conio.h> #include<graphics.h> #define round(a) (int)(a+0.5) float xc, yc, x, y; void plotpoint(); void main() { int gd=DETECT,gm; float rx, ry, p, px, py, rx2, ry2, tworx2, twory2; initgraph(&gd,&gm,""); clrscr(); printf("Enter the xc value:"); scanf("%f",&xc); printf("Enter the yc value:"); scanf("%f",&yc); printf("Enter the x radius:"); scanf("%f",&rx); printf("Enter the y radius:"); scanf("%f",&ry); ry2=ry*ry; rx2=rx*rx; twory2=2*ry2; tworx2=2*rx2; x=0; y=ry; plotpoint(); p=round(ry2 - rx2 * ry + (0.25 * rx2)); px=0; py=tworx2*y; while(px<py) { x=x+1; px=px+twory2; if(p>=0){ y=y-1; py=py-tworx2; }

REG NO: 40508104067

if(p<0) p=p+ry2+px; else p=p+ry2+px-py; plotpoint(); } p=round(ry2 * (x+0.5) * (x+0.5) + rx2 * (y-1) * (y-1) - rx2 * ry2); while(y>0) { y=y-1; py=py-tworx2; if(p<=0) { x=x+1; px=px+twory2; } if(p>0) p=p+rx2-py; else p=p+rx2-py+px; plotpoint(); } getch(); closegraph(); } void plotpoint() { putpixel(xc+x,yc+y,1); putpixel(xc-x,yc+y,1); putpixel(xc+x,yc-y,1); putpixel(xc-x,yc-y,1); }

REG NO: 40508104067

OUTPUT: Enter the xa value: 200 Enter the ya value: 200 Enter the x radius: 100 Enter the y radius: 50

RESULT: Thus an ellipse is drawn successfully using midpoint ellipse drawing algorithm in C.

REG NO: 40508104067

IMPLEMENTATION OF LINE, CIRCLE AND ELLIPSE ATTRIBUTES

AIM: To write a C Program to display the various output primitives with its attributes.

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 chosen particular primitives. 6. Using switch cases 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.

REG NO: 40508104067

PROGRAM: #include<stdio.h> #include<conio.h> #include<graphics.h> #include<string.h> void main() { char ch; int gd=DETECT,gm,x1,y1,x2,y2,rad,sa,ea,xrad,yrad,i; initgraph(&gd,&gm,""); printf("enter the choice:"); scanf("%c",ch); do { 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': setlinestyle(SOLID_LINE,0,1); line(100,200,300,400); break; case '2': setfillstyle(SOLID_FILL,8); circle(200,200,75); break; case '3': setfillstyle(7,4); bar(100,300,200,100); break;

REG NO: 40508104067

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,"Graphics"); 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; } } while(ch=='y'|| ch=='Y'); getch(); }

REG NO: 40508104067

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

REG NO: 40508104067

Graphics

RESULT: Thus the program for implementing line, circle and ellipse attributes was executed successfully.

REG NO: 40508104067

REG NO: 40508104067

BASIC 2D TRANSFORMATIONS

AIM: To write a program to perform the basic 2D transformations like translation, rotation and scaling using transformation equation in C.

ALGORITHM: 1. Start the program. 2. Obtain the coordinates of the line xa,ya,xb,yb. 3. Get the translation factors tx, ty, rotation angle a & scaling factors sx, sy. 4. Find the translated coordinates by applying the angle as x1=x+tx & y1=y+ty. 5. Get the rotation coordinates by applying the angle as X1=abs(xa-xb)cosa-abs(ya-yb)sina Y1=abs(xa-xb)sina+abs(ya-yb)cosa 6. Scaling is applied as X1=x*sx Y1=y*sy 7. Draw the transformed line with the new coordinates (x1,y1). 8. Stop the program.

REG NO: 40508104067

PROGRAM: #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> void getdata(); int x1, y1, x2, y2; void main() { int gd=DETECT,gm; int x3, x4, y3, y4, tx, ty, ch; float sx, sy, a, t; initgraph(&gd,&gm,""); clrscr(); setcolor(1); do { printf("1.Translation\t2.Scaling\t3.Rotation\t4.Exit\nEnter your choice:"); scanf("%d",&ch); switch(ch) { case 1: getdata(); printf("Enter the translation factor in x-axis:"); scanf("%d",&tx); printf("Enter the translation factor in y-axis:"); scanf("%d",&ty); x3=x1+tx; x4=x2+tx; y3=y1+ty; y4=y2+ty; line(x3,y3,x4,y4); getch(); cleardevice(); break; case 2: getdata(); printf("Enter the scaling factor in x-axis:"); scanf("%f",&sx); printf("Enter the scaling factor in y-axis:"); scanf("%f",&sy); x3=x1*sx;

REG NO: 40508104067

x4=x2*sx; y3=y1*sy; y4=y2*sy; line(x3,y3,x4,y4); getch(); cleardevice(); break; case 3: getdata(); printf("Enter the rotation angle:"); scanf("%f",&a); t=a*(3.14159/180); x4=x1+abs(x2-x1)*cos(t)-abs(y2-y1)*sin(t); y4=y1+abs(x2-x1)*sin(t)+abs(y2-y1)*cos(t); line(x1,y1,x4,y4); getch(); cleardevice(); break; case 4: exit(0); break; } }while(ch<4); getch(); closegraph(); } void getdata() { cleardevice(); printf("Enter the x1 value:"); scanf("%d",&x1); printf("Enter the y1 value:"); scanf("%d",&y1); printf("Enter the x2 value:"); scanf("%d",&x2); printf("Enter the y2 value:"); line(x1,y1,x2,y2); }

scanf("%d",&y2);

REG NO: 40508104067

OUTPUT: 1.Translation 2.Scaling 3.Rotation 4.Exit Enter the choice: 1 Enter the x1 value: 100 Enter the y1 value: 100 Enter the x2 value: 200 Enter the y2 value: 200 Enter the translation factor in x-axis: 50 Enter the translation factor in y-axis: 0

1.Translation 2.Scaling 3.Rotation Enter the choice: 2 Enter the x1 value: 100 Enter the y1 value: 100 Enter the x2 value: 200 Enter the y2 value: 200 Enter the scaling factor in x-axis: 1 Enter the scaling factor in y-axis: 2

4.Exit

REG NO: 40508104067

1.Translation 2.Scaling 3.Rotation Enter the choice: 3 Enter the x1 value: 100 Enter the y1 value: 100 Enter the x2 value: 200 Enter the y2 value: 200 Enter the rotation angle: 45

4.Exit

RESULT: Thus the program for performing basic 2D transformations using transformation equation is successfully executed in C.

REG NO: 40508104067

2D TRANSFORMATIONS (REFLECTION AND SHEARING)

AIM: To write a program to perform the other 2D transformations like reflection and shearing in C.

ALGORITHM: 1. Start the program. 2. For reflection obtain the coordinates of the triangle xa, ya, xb, yb, xc, yc. 3. Calculate the reflection as, x`=x+2*(320-x) y`=y+2*(240-y) 4. For shearing obtain the coordinates of the square xa, ya, xb, yb, xc, yc, xd, yd. 5. Shearing points can be calculated as x`=x+shx & y`=y+shy, where shx, shy are the shearing factors. 6. Draw the transformed objects with the new coordinates (x`, y`). 7. Stop the program.

REG NO: 40508104067

PROGRAM: #include<stdio.h> #include<conio.h> #include<graphics.h> void reflection(); void shearing(); void main() { int gd=DETECT,gm,ch; initgraph(&gd,&gm,""); do { cleardevice(); printf("1.Reflection\t2.Shearing\t3.Exit\nEnter your choice:"); scanf("%d",&ch); switch(ch) { case 1: cleardevice(); reflection(); break; case 2: cleardevice(); shearing(); break; case 3: exit(0); break; } } while(ch<3); getch(); closegraph(); } void reflection() { int xa, ya, xb, yb, xc, yc, x1, y1, x2, y2, x3, y3, ch; printf("Enter the xa&ya values:"); scanf("%d\n%d",&xa,&ya);

REG NO: 40508104067

printf("Enter the xb&yb values:"); scanf("%d\n%d",&xb,&yb); printf("Enter the xc&yc values:"); scanf("%d\n%d",&xc,&yc); line(xa,ya,xb,yb); line(xb,yb,xc,yc); line(xc,yc,xa,ya); do { printf("1.About x-axis\t2.About y-axis\t3.About both\t4.exit\nEnter the choice:"); scanf("%d",&ch); switch(ch) { case 1: cleardevice(); line(xa,ya,xb,yb); line(xb,yb,xc,yc); line(xc,yc,xa,ya); y1=ya+2*(getmaxy()/2-ya); y2=yb+2*(getmaxy()/2-yb); y3=yc+2*(getmaxy()/2-yc); line(xa,y1,xb,y2); line(xb,y2,xc,y3); line(xc,y3,xa,y1); break; case 2: cleardevice(); line(xa,ya,xb,yb); line(xb,yb,xc,yc); line(xc,yc,xa,ya); x1=xa+2*(getmaxy()/2-xa); x2=xb+2*(getmaxy()/2-xb); x3=xc+2*(getmaxy()/2-xc); line(x1,ya,x2,yb); line(x2,yb,x3,yc); line(x3,yc,x1,ya); break;

REG NO: 40508104067

case 3: cleardevice(); line(xa,ya,xb,yb); line(xb,yb,xc,yc); line(xc,yc,xa,ya); x1=xa+2*(getmaxy()/2-xa); x2=xb+2*(getmaxy()/2-xb); x3=xc+2*(getmaxy()/2-xc); y1=ya+2*(getmaxy()/2-ya); y2=yb+2*(getmaxy()/2-yb); y3=yc+2*(getmaxy()/2-yc); line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x3,y3,x1,y1); break; case 4: break; } } while(ch<4); } void shearing() { float xa, xb, xc, xd, ya, yb, yc, yd, xe, xf, ye, yf, shx, shy; int ch; void rect(); printf("Enter the xa&ya values:"); scanf("%f\n%f",&xa,&ya); printf("Enter the xb&yb values:"); scanf("%f\n%f",&xb,&yb); printf("Enter the xc&yc values:"); scanf("%f\n%f",&xc,&yc); printf("Enter the xd&yd values:") scanf("%f\n%f",&xd,&yd); do { printf("1.About x-axis\t2.About y-axis\t3.About both\t4.Exit\nEnter the choice:"); scanf("%d",&ch);

REG NO: 40508104067

switch(ch) { case 1: cleardevice(); line(xa,ya,xb,yb); line(xb,yb,xc,yc); line(xc,yc,xd,yd); line(xd,yd,xa,ya); printf("Enter the shearing factor for x:"); scanf("%f",&shx); xe=xc+shx; xf=xd+shx; line(xa,ya,xb,yb); line(xb,yb,xe,yc); line(xe,yc,xf,yd); line(xf,yd,xa,ya); break; case 2: cleardevice(); printf("Enter the shearing factor for y:"); scanf("%f",&shy); ye=yb+shy; yf=yc+shy; line(xa,ya,xb,ye); line(xb,ye,xc,yf); line(xc,yf,xd,yd); line(xd,yd,xa,ya); break; case 3: cleardevice(); printf("Enter the shearing factor for x:"); scanf("%f",&shx); printf("Enter the shearing factor for y:"); scanf("%f",&shy); xe=xc+shx; xf=xd+shx; ye=yb+shy; yf=yc+shy;

REG NO: 40508104067

line(xa,ya,xb,ye); line(xb,ye,xe,yf); line(xe,yf,xf,yd); line(xf,yd,xa,ya); break; case 4: break; } } while(ch<4); cleardevice(); }

REG NO: 40508104067

OUTPUT: 1.Reflection 2.Shearing 3.Exit Enter the choice: 1 Enter the xa&ya value: 200 100 Enter the xb&yb value: 200 200 Enter the xc&yc value: 100 200 1.About x-axis 2.About y-axis Enter the choice: 1 3.About both 4.Exit

1.About x-axis 2.About y-axis Enter the choice: 2

3.About both 4.Exit

1.About x-axis 2.About y-axis Enter the choice: 3

3.About both 4.Exit

REG NO: 40508104067

1.About x-axis 2.About y-axis Enter the choice: 4 1.Reflection 2.Shearing 3.Exit Enter the choice: 2 Enter the xa&ya value: 200 200 Enter the xb&yb value: 300 200 Enter the xc&yc value: 300 300 Enter the xd&yd value: 200 300

3.About both 4.Exit

1.About x-axis 2.About y-axis 3.About both 4.Exit Enter the choice: 1 Enter the shearing factor for x:50

1.About x-axis 2.About y-axis 3.About both 4.Exit Enter the choice: 2 Enter the shearing factor for y:50

1.About x-axis 2.About y-axis Enter the choice: 3 Enter the shearing factor for x: 50 Enter the shearing factor for y: 50

3.About both 4.Exit

REG NO: 40508104067

Enter the choice: 4 Enter the choice: 3

RESULT: Thus the program for performing 2D transformations reflection and shearing is successfully executed in C.

REG NO: 40508104067

COMPOSITE 2D TRANSFORMATIONS

AIM: To write a program to perform the composite 2D transformations like successive translation, rotation and scaling using transformation equation in C. ALGORITHM: 1. Start the program. 2. Obtain the coordinates of the line xa,ya,xb,yb. 3. Get the translation factors tx1, ty1, tx2, ty2 rotation angles a1, a2 & scaling factors sx1, sy1, sx2, sy2. 4. Find the translated coordinates by applying the formula as below. x1=x+(tx1+tx2) & y1=y+(ty1+ty2). It possesses associative property and successive translations are additive. 5. Get the rotation coordinates by applying the formula as X1=abs(xa-xb)cos(a1+a2)-abs(ya-yb)sin(a1+a2) Y1=abs(xa-xb)sin(a1+a2)+abs(ya-yb)cos(a1+a2) It possesses associative property and successive rotations are additive. 6. Scaling is applied as X1=x*(sx1*sx2) Y1=y*(sy1*sy2) It possesses associative property and successive scaling is multiplicative. 7. Draw the transformed line with the new coordinates (x1,y1). 8. Stop the program.

REG NO: 40508104067

PROGRAM: #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> void getdata(); int x1, y1, x2, y2; void main() { int gd=DETECT, gm; int x3, x4, y3, y4, tx1, ty1, tx2, ty2, ch; float sx1, sy1, sx2, sy2, a1, a2, t1, t2; initgraph(&gd,&gm,""); clrscr(); setcolor(1); do { printf("1.Successive Translation\t2.Successive Scaling\t3.Successive Rotation\t4.Exit\nEnter your choice:"); scanf("%d",&ch); switch(ch) { case 1: getdata(); printf("Enter the translation factors in x-axis:"); scanf("%d%d",&tx1,&tx2); printf("Enter the translation factors in y-axis:"); scanf("%d%d",&ty1,&ty2); x3=x1+tx1+tx2; x4=x2+tx1+tx2; y3=y1+ty1+ty2; y4=y2+ty1+ty2; line(x3,y3,x4,y4); getch(); cleardevice(); break;

REG NO: 40508104067

case 2: getdata(); printf("Enter the scaling factors in x-axis:"); scanf("%f%f",&sx1,&sx2); printf("Enter the scaling factors in y-axis:"); scanf("%f%f",&sy1,&sy2); x3=x1*sx1*sx2; x4=x2*sx1*sx2; y3=y1*sy1*sy2; y4=y2*sy1*sy2; line(x3,y3,x4,y4); getch(); cleardevice(); break; case 3: getdata(); printf("Enter the rotation angle:"); scanf("%f%f",&a1,&a2); t1=a1*(3.14159/180); t2= a2*(3.14159/180); x4=x1+abs(x2-x1)*cos(t1+t2)-abs(y2-y1)*sin(t1+t2); y4=y1+abs(x2-x1)*sin(t1+t2)+abs(y2-y1)*cos(t1+t2); line(x1,y1,x4,y4); getch(); cleardevice(); break; case 4: exit(0); break; } } while(ch<4); getch(); closegraph(); } void getdata() { cleardevice();

REG NO: 40508104067

printf("Enter the x1 value:"); scanf("%d",&x1); printf("Enter the y1 value:"); scanf("%d",&y1); printf("Enter the x2 value:"); scanf("%d",&x2); printf("Enter the y2 value:"); scanf("%d",&y2); line(x1,y1,x2,y2); }

REG NO: 40508104067

OUTPUT: 1.Successive Translation 2. Successive Scaling 3. Successive Rotation 4.Exit Enter the choice: 1 Enter the x1 value: 100 Enter the y1 value: 100 Enter the x2 value: 200 Enter the y2 value: 200 Enter the translation factor in x-axis: 25 25 Enter the translation factor in y-axis: 0 0

1.Successive Translation 2. Successive Scaling 3. Successive Rotation 4.Exit Enter the choice: 2 Enter the x1 value: 100 Enter the y1 value: 100 Enter the x2 value: 200 Enter the y2 value: 200 Enter the scaling factor in x-axis: 0.5 0.5 Enter the scaling factor in y-axis: 1 1

REG NO: 40508104067

1.Successive Translation 2. Successive Scaling 3. Successive Rotation 4.Exit Enter the choice: 3 Enter the x1 value: 100 Enter the y1 value: 100 Enter the x2 value: 200 Enter the y2 value: 200 Enter the rotation angle: 20 25

RESULT: Thus the program for performing composite 2D transformations using transformation equation is successfully executed in C.

REG NO: 40508104067

LINE CLIPPING USING COHEN-SUTHERLAND ALGORITHM

AIM: To write a program in C to clip a line using Cohen-Sutherland line clipping algorithm. ALGORITHM: 1. Start the program. 2. Obtain the coordinates of the clipping window xmin,ymin,xmax,ymax. 3. Get the coordinates of the line to be clipped xa,ya,xb,yb. 4. Region codes are assigned to the line end points according to relative position with respect to the clipping rectangle. 5. The two region codes of the line end points which passes through the clipping rectangle are ANDed. 6. If the result is one (1 1 1 1),then the line is entirely outside the clipping window. 7. If the result is zero (0 0 0 0), then the line is completely inside the window hence we can save it for display. 8. For the intermediate results we have to find intersection point using line equation. The slope of the line is given by the equation m=(yb-ya)/(xb-xa). 9. The point at which the line intersects the clipping window can be obtained using the equation x<=x1+m(y-y1) & y<=y1+m(x-x1) ,where x is set either xmin or xmax and y is either ymin or ymax. 10. Using the intersection point the part of the outside the clipping window are clipped off. 11. Stop the program.

REG NO: 40508104067

PROGRAM: #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> void main() { int gd=DETECT,gm; float xmin,ymin,xmax,ymax,x1,y1,x2,y2,x,y,slope,yl1,yr1,xa1,xb1; float xa2, xr2; float yl2,xb2,xl2,xe,ye,yr2; int l1,r1,b1,a1,l2,r2,b2,a2,l,r,b,a; initgraph(&gd,&gm,""); clrscr(); printf("Enter the coordinates for rectangle:"); scanf("%f\n%f\n%f\n%f",&xmin,&ymin,&xmax,&ymax); printf("Enter the coordinates for line:"); scanf("%f\n%f\n%f\n%f",&x1,&y1,&x2,&y2); setcolor(3); rectangle(xmin,ymin,xmax,ymax); line(x1,y1,x2,y2); if((x1-xmin)<0) l1=1; else l1=0; if((xmax-x1)<0) r1=1; else r1=0; if((ymax-y1)<0) b1=1; else b1=0; if((y1-ymin)<0) a1=1; else a1=0;

REG NO: 40508104067

if((x2-xmin)<0) l2=1; else l2=0; if((xmax-x2)<0) r2=1; else r2=0; if((ymax-y2)<0) b2=1; else b2=0; if((y2-ymin)<0) a2=1; else a2=0; if((l1*l2)==0) l=0; else l=1; if((r1*r2)==0) r=0; else r=1; if((b1*b2)==0) b=0; else b=1; if((a1*a2)==0) a=0; else a=1; if((a==0)&&(b==0)&&(l==0)&&(r==0)) { slope=(y2-y1)/(x2-x1); if(y1<ymin) { xa1=x1; xb2=x2;

REG NO: 40508104067

else xa1=x1+(ymin-y1)/slope; getch(); cleardevice(); rectangle(xmin,ymin,xmax,ymax); line(xa1,ymin,x2,y2); xe=xa1; ye=ymin; } if(x1<xmin) { yl1=y1+slope*(xmin-x1); getch(); cleardevice(); rectangle(xmin,ymin,xmax,ymax); line(xmin,yl1,x2,y2); xe=xmin; ye=yl1; } if(x1>xmax) { yr1=y1+slope*(xmax-x1); getch(); cleardevice(); rectangle(xmin,ymin,xmax,ymax); line(xmax,yr1,x2,y2); xe=xmax; ye=yr1; } if(y1>ymax) { xb1=x1+(ymax-y1)/slope; getch(); cleardevice(); rectangle(xmin,ymin,xmax,ymax); line(xb1,ymax,x2,y2); xe=xb1; ye=ymax; } if(y2<ymin) { if((x2-x1)==0) { xa2=x2;

xb2=x2;

REG NO: 40508104067

else xa2=x2+(ymin-y2)/slope; getch(); cleardevice(); rectangle(xmin,ymin,xmax,ymax); line(xa2,ymin,xe,ye); } if(x2>xmax) { yr2=y2+slope*(xmax-x2); getch(); cleardevice(); rectangle(xmin,ymin,xmax,ymax); line(xmax,yr2,xe,ye); } if(x2<xmin) { yl2=y2+slope*(xmin-x2); getch(); cleardevice(); rectangle(xmin,ymin,xmax,ymax); line(xmin,yl2,xe,ye); } if(y2>ymax) { if((x2-x1)==0) xb2=x2; else xb2=x2+(ymax-y2)/slope; getch(); cleardevice(); rectangle(xmin,ymin,xmax,ymax); line(xb2,ymax,xe,ye); } getch(); closegraph(); }

REG NO: 40508104067

OUTPUT: Enter the coordinates for rectangle: 200 200 300 300 Enter the coordinates for line:150 200 350 450

RESULT: Thus the program to clip a line using Cohen-Sutherland algorithm has written and successfully executed in C.

REG NO: 40508104067

IMPLEMENTATION OF SUTHERLAND HODGEMAN POLYGON CLIPPING ALGORITHM

AIM: To write a C program to implement Sutherland Hodgeman polygon clipping algorithm.

ALGORITHM: 1. Start the program. 2. Declare the variables for defining clip window & polygon. 3. While clipping a polygon following four possible cases to be considered: If the first vertex is outside the window boundary and the second vertex inside. If the first vertex is inside the window boundary and the second vertex outside If both are outside If both are inside

4. Clip the polygon against the window boundary in the order left, right, bottom & above using intersection calculation. 5. Check the vertices that formed the polygon lies inside or on the boundary. If that is inside or on the boundary save that point otherwise discard it. 6. Stop the program.

REG NO: 40508104067

PROGRAM: #include <stdio.h> #include <graphics.h> #include <conio.h> #include <math.h> #include <process.h> #define TRUE 1 #define FALSE 0 typedef unsigned int outcode; outcode CompOutCode(float x,float y); enum { TOP = 0x1, BOTTOM = 0x2, RIGHT = 0x4, LEFT = 0x8 }; float xmin,xmax,ymin,ymax; void clip(float x0,float y0,float x1,float y1) { outcode outcode0,outcode1,outcodeOut; int accept = FALSE,done = FALSE; outcode0 = CompOutCode(x0,y0); outcode1 = CompOutCode(x1,y1); do { if(!(outcode0|outcode1)) { accept = TRUE; done = TRUE; } else if(outcode0 & outcode1) done = TRUE; else { float x,y; outcodeOut = outcode0?outcode0:outcode1;

REG NO: 40508104067

if(outcodeOut & TOP) { x = x0+(x1-x0)*(ymax-y0)/(y1-y0); y = ymax; } else if(outcodeOut & BOTTOM) { x = x0+(x1-x0)*(ymin-y0)/(y1-y0); y = ymin; } else if(outcodeOut & RIGHT) { y = y0+(y1-y0)*(xmax-x0)/(x1-x0); x = xmax; } else { y = y0+(y1-y0)*(xmin-x0)/(x1-x0); x = xmin; } if(outcodeOut==outcode0) { x0 = x; y0 = y; outcode0 = CompOutCode(x0,y0); } else { x1 = x; y1 = y; outcode1 = CompOutCode(x1,y1); } } }while(done==FALSE); if(accept) line(x0,y0,x1,y1); outtextxy(150,20,"POLYGON AFTER CLIPPING"); rectangle(xmin,ymin,xmax,ymax); }

REG NO: 40508104067

outcode CompOutCode(float x,float y) { outcode code = 0; if(y>ymax) code|=TOP; else if(y<ymin) code|=BOTTOM; if(x>xmax) code|=RIGHT; else if(x<xmin) code|=LEFT; return code; } void main( ) { float x1,y1,x2,y2; /* request auto detection */ int gdriver = DETECT, gmode, n,poly[14],i; clrscr( ); printf("Enter the no of sides of polygon:"); scanf("%d",&n); printf("\nEnter the coordinates of polygon\n"); for(i=0;i<2*n;i++) { scanf("%d",&poly[i]); } poly[2*n]=poly[0]; poly[2*n+1]=poly[1]; printf("Enter the rectangular coordinates of clipping window\n"); scanf("%f%f%f%f",&xmin,&ymin,&xmax,&ymax); initgraph(&gdriver, &gmode, "c:\\tc\\bgi"); outtextxy(150,20,"POLYGON BEFORE CLIPPING"); drawpoly(n+1,poly); rectangle(xmin,ymin,xmax,ymax); getch( ); cleardevice( ); for(i=0;i<n;i++) clip(poly[2*i],poly[(2*i)+1],poly[(2*i)+2],poly[(2*i)+3]); getch( ); restorecrtmode( ); }

REG NO: 40508104067

OUTPUT: Enter the no of sides of polygon:5 Enter the coordinates of polygon 50 50 200 100 350 350 80 200 40 80 Enter the rectangular coordinates of clipping window 150 150 300 300

POLYGON BEFORE CLIPPING

POLYGON AFTER CLIPPING

RESULT: Thus the C program to implement Sutherland Hodgeman polygon clipping was executed and verified.

REG NO: 40508104067

3D TRANSFORMATION(TRANSLATION,ROTATION AND SCALING)

AIM: To write a C program to perform translation,rotation and scaling on 3D objects. ALGORITHM: 1. Start the program. 2. Store the coordinate values in a homogeneous matrix. 3. Draw a 3D object with a specified coordinate value stored in a homogeneous matrix. 4. Perform Translation with the use of translation matrix given below 1 0 0 tx 0 1 0 ty 0 0 1 tz 0 0 0 1 where tx,ty,tz are the translation factors. 5. Perform rotation with the rotation matrix given by 1 0 0 0 0 cost -sint 0 0 sint cost 0 0 0 0 0 6. Perform scaling with the scaling matrix given by sx 0 0 0 0 sy 0 0 0 0 sz 0 0 0 0 1 where sx,sy,sz are scaling factors. 7. Stop the program.

REG NO: 40508104067

PROGRAM: #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> void main() { int tx,ty,z[4][4],t1,i,j,p,k,l,shx; float a,zr[4][4],yf,xf,zf,m,n,sx,sy,sz; int pts[10],pts1[10],ch,x[10],y[10]; int gd=DETECT,gm; void scaling(); initgraph(&gd,&gm,""); do { cleardevice(); printf("3D-TRANSFORMATION\n"); printf("1.Translation\t2.Rotation\t3.Scaling\t4.Exit\n"); printf("Enter the choice:"); scanf("%d",&ch); switch(ch) { case 1: cleardevice(); pts[0]=50;pts[1]=50;pts[2]=250;pts[3]=50;pts[4]=250; pts[5]=150;pts[6]=50;pts[7]=150;pts[8]=50;pts[9]=50; printf("Enter the vector:"); scanf("%d %d",&tx,&ty); for(i=0;i<4;i++) for(j=0;j<4;j++) if(i==j) z[i][j]=1; else z[i][j]=0; z[0][3]=tx; z[1][3]=ty;

REG NO: 40508104067

for(k=0;k<10;k++) { pts1[k]=z[0][0]*pts[k]+z[0][2]*pts[k+1]+z[0][3]; if(k==9) break; pts1[k+1]=z[2][1]*pts[k]+z[2][2]*pts[k+1]+z[2][3]; } getch(); cleardevice(); outtextxy(200,3,"Translation"); drawpoly(5,pts); for(i=0;i<10;i+=2) line(pts[i],pts[i+1],pts[i]+50,pts[i+1]+50); drawpoly(5,pts1); for(i=0;i<10;i++) { pts[i]+=50; pts1[i]+=50; } drawpoly(5,pts); drawpoly(5,pts1); for(i=0;i<10;i++) line(pts1[i],pts1[i+1],pts1[i]-50,pts1[i+1]-50); getch(); cleardevice(); break; case 2: pts[0]=50;pts[1]=50;pts[2]=250;pts[3]=50;pts[4]=250; pts[5]=150;pts[6]=50;pts[7]=150;pts[8]=50;pts[9]=50; xf=150.0;yf=300.0; printf("%5.2f\t%5.2f\n",xf,yf); printf("Enter the rotation angle:"); scanf("%f",&a); cleardevice(); outtextxy(200,3,"Rotation"); for(i=0;i<3;i++) { zr[i][3]=0; zr[3][i]=0; } zr[3][3]=1; p=a;

REG NO: 40508104067

for(l=0;l<=p;l+=10) { a=(float)l; a=a*3.14159/180; zr[1][1]=cos(a); zr[2][1]=sin(a); zr[1][2]=(-1)*sin(a); zr[2][2]=cos(a); m=xf*(1-cos(a))+yf*sin(a); n=yf*(1-cos(a))-xf*sin(a); k=0; for(k=0;k<10;k+=2) { pts1[k]=zr[1][1]*pts[k]+zr[1][2]*pts[k+1]+m; if(k==9) break; pts1[k+1]=zr[2][1]*pts[k]+zr[2][2]*pts[k+1]+n; } delay(500); cleardevice(); drawpoly(5,pts); for(i=0;i<10;i+=2) line(pts[i],pts[i+1],pts[i]+50,pts[i+1]+50); drawpoly(5,pts); for(i=0;i<10;i+=2) line(pts[i],pts[i+1],pts[i]+50,pts[i+1]+50); drawpoly(5,x); for(i=0;i<10;i++) { x[i]=pts[i]+50; y[i]=pts1[i]+50; } drawpoly(5,pts1); for(i=0;i<10;i+=2) line(pts1[i],pts[i+1],pts1[i]+50,pts1[i+1]+50); drawpoly(5,y); delay(500); } getch(); cleardevice(); break;

REG NO: 40508104067

case 3: scaling(); break; case 4: exit(0); } } while(ch!=5); closegraph(); } void scaling() { int xx1,yy1,xx2,yy2,xx3,yy3,xx4,yy4,tx,ty, x1,y1,x2,y2,x3,y3,x4,y4; x1=100;y1=100;x2=50;y2=150;x3=100;y3=125;x4=150;y4=150; printf("Enter the scaling factor:"); scanf("%d %d",&tx,&ty); cleardevice(); settextstyle(0,0,2); outtextxy(200,50,"scaling"); line(x1,y1,x2,y2); line(x1,y1,x4,y4); line(x2,y2,x4,y4); setlinestyle(1,1,1); line(x2,y2,x3,y3); line(x3,y3,x1,y1); line(x3,y3,x4,y4); xx1=x1*tx; yy1=y1*ty; xx2=x2*tx; yy2=y2*ty; xx3=x3*tx; yy3=y3*ty; xx4=x4*tx; yy4=y4*ty; setlinestyle(0,1,1); line(xx1,yy1,xx2,yy2); line(xx1,yy1,xx4,yy4); line(xx2,yy2,xx4,yy4); setlinestyle(1,1,1); line(xx2,yy2,xx3,yy3); line(xx3,yy3,xx1,yy1); line(xx3,yy3,xx4,yy4); getch(); }

REG NO: 40508104067

OUTPUT: 3D-TRANSFORMATIONS 1.Translation 2.Rotation 3.Scaling 4.Exit Enter the choice: 1

1.Translation 2.Rotation Enter the choice: 2

3.Scaling 4.Exit

REG NO: 40508104067

1.Translation 2.Rotation Enter the choice: 3

3.Scaling 4.Exit

1.Translation 2.Rotation Enter the choice: 4

3.Scaling 4.Exit

RESULT: Thus the program to perform translation, rotation and scaling on 3D objects were written and executed successfully in C.

REG NO: 40508104067

COMPOSITE 3D- TRANSFORMATIONS

AIM: To perform Composite 3D transformations such as successive translation & rotation, fixed point scaling.

ALGORITHM: 1. Start the program. 2. Use the function draw cube to draw a cube using eight points by means of line functions. 3. Declare the variables x1,y1,x2,y2,x3,y3, in array type which of data type int. 4. Declare the necessary variables for performing transformations. 5. Initialize graphics functions. 6. Input the first point in the cube. 7. Input the size of the edge. 8. Using switch operation selects the operation to perform successive translation, successive rotation, and fixed point scaling. 9. Successive Translation a) input the translation vector tx1, ty1, tz1 & tx2, ty2, tz2. b) calculate points using formula x3[i]=x1[i]+tx1+tx2. y3[i]=y1[i]+ty1+ty2 z3[i]=z1[i]+tz1+tz2. x4[i]=x3[i]+z3[i]/2 y4[i]=y3[i]+z3[i]/2 c) using the function line, display the object before and after translation.

REG NO: 40508104067

10.Successive Rotation: a) input the rotation angles b) Using formula theta=((theta1+theta2)*3.14)/180 c) If the direction is along z axis, z3[i]=z1[i]. x3[i]=x1[i]*cos(theta)-y1[i]*sin(theta). y3[i]=x1[i]*sin(theta)-y1[i]*cos(theta). Apply cyclic permutation and compute for the rest of the directions. d) Calculate the points using the formula x4[i]=x3[i]+z3[i]/2 y4[i]=y3[i]+z3[i]/2 e) Using the function line, display the object before and after rotation. 12. Fixed Point Scaling: a) Input the scaling factor and reference point b) Calculate coordinates point using formula x3[i]=xf+(x1[i]*sx+xf*(1-sx), y3 [i] =yf+ (y1[i]*sy+yf*(1-sy) z3 [i] =zf+ (z1[i]*sz+zf*(1-sz) c) Calculate the points using the formula x4[i]=x3[i]+z3[i]/2 y4[i]=y3[i]+z3[i]/2 d) Using the function line, display the object before and after scaling. 13. Stop the program.

REG NO: 40508104067

PROGRAM: #include<graphics.h> #include<math.h> #include<conio.h> #include<stdlib.h> void drawcube(int x1[],int y1[]); void main() { int i,x1[8],y1[8],x2[8],y2[8],z1[8],x3[8],y3[8],z3[8],x4[8],y4[8],t,t1,t2,op; int ch,tx1,ty1,tz1,tx2,ty2,tz2,sx,sy,sz,xf,yf,zf,x,y,z,s; int driver=DETECT; int mode; initgraph(&driver,&mode,""); printf("Enter the points on the cube:"); scanf("%d%d%d",&x,&y,&z); printf("Enter the size of the edge:"); scanf("%d",&s); x1[0]=x1[3]=x; x1[1]=x1[2]=x+s; x1[4]=x1[7]=x; x1[5]=x1[6]=x+s; y1[0]=y1[1]=y; y1[2]=y1[3]=y+s; y1[4]=y1[5]=y; y1[6]=y1[7]=y+s; z1[1]=z1[2]=z1[3]=z1[0]=z; z1[4]=z1[5]=z1[6]=z1[7]=z-s; for(i=0;i<8;i++) { x2[i]=x1[i]+z1[i]/2; y2[i]=y1[i]+z1[i]/2; } getch(); cleardevice();

REG NO: 40508104067

do { printf("menu"); printf("\n1.Successive Transformation\n2.Successive Rotation \n 3.Fixed Point Scaling\n4.exit\n"); printf("Enter thr choice:"); scanf("%d",&ch); switch(ch) { case 1: printf("Enter thr translation vectors:"); scanf("%d%d%d%d%d%d",&tx1, &ty1, &tz1, &tx2, &ty2, &tz2); for(i=0;i<8;i++) { x3[i]=x1[i]+tx1+tx2; y3[i]=y1[i]+ty1+ty2; z3[i]=z1[i]+tz1+tz2; } for(i=0;i<8;i++) { x4[i]=x3[i]+z3[i]/2; y4[i]=y3[i]+z3[i]/2; } cleardevice(); printf("before Translation"); drawcube(x2,y2); getch(); cleardevice(); printf("After Translation"); drawcube(x4,y4); getch(); cleardevice(); break; case 2: printf("Enter the Rotation angle:"); scanf("%d%d",&t1,&t2);

REG NO: 40508104067

t=((t1+t2)*3.14)/180; printf("Enter the dirrection"); printf("\n1.Rotation about x-axis\n2.Rotation about yaxis\n3.Rotation about z axis"); scanf("%d",&op); if(op==1) { for(i=0;i<8;i++) { x3[i]=x1[i]; y3[i]=y1[i]*cos(t)-z1[i]*sin(t); z3[i]=y1[i]*sin(t)+z1[i]*cos(t); } } else if(op==2) { for(i=0;i<8;i++) { y3[i]=y1[i]; x3[i]=z1[i]*cos(t)-x1[i]*sin(t); z3[i]=z1[i]*sin(t)+x1[i]*cos(t); } } else if(op==3) { for(i=0;i<8;i++) { z3[i]=z1[i]; x3[i]=x1[i]*cos(t)-y1[i]*sin(t); y3[i]=x1[i]*sin(t)+y1[i]*cos(t); } } else printf("Enter correct option"); for(i=0;i<8;i++) { x4[i]=x3[i]+z3[i]/2; y4[i]=y3[i]+z3[i]/2; } cleardevice(); printf("before Translation");

REG NO: 40508104067

drawcube(x2,y2); getch(); cleardevice(); printf("After Rotation"); drawcube(x4,y4); getch(); cleardevice(); break; case 3: printf("Enter the scaling factor"); scanf("%d%d%d",&sx,&sy,&sz); printf("Enter the reference point"); scanf("%d%d%d",&xf,&yf,&zf); for(i=0;i<8;i++) { x3[i]=xf+(x1[i]*sx)+xf*(1-sx); y3[i]=yf+(y1[i]*sy)+yf*(1-sy); z3[i]=zf+(z1[i]*sz)+zf*(1-sz); } for(i=0;i<8;i++) { x4[i]=x3[i]+z3[i]/2; y4[i]=y3[i]+z3[i]/2; } cleardevice(); printf("before scaling"); drawcube(x2,y2); getch(); cleardevice(); printf("After scaling"); drawcube(x4,y4); getch(); cleardevice(); break; case 4: exit(0); break; }

REG NO: 40508104067

} while(op!=4); getch(); } void drawcube(int x1[],int y1[]) { int i; for(i=0;i<4;i++) { if(i<3) line(x1[i],y1[i],x1[i+1],y1[i+1]); line(x1[0],y1[0],x1[3],y1[3]); } for(i=4;i<8;i++) { if(i<7) line(x1[i],y1[i],x1[i+1],y1[i+1]); line(x1[4],y1[4],x1[7],y1[7]); } for(i=0;i<4;i++) { line(x1[i],y1[i],x1[i+4],y1[i+4]); } }

REG NO: 40508104067

DRAWING 3D- OBJECTS

AIM: To write a C program to compose a scene by using three dimensional objects.

ALGORITHM: 1. Start the program. 2. Initialize graphics functions. 3. Use the function bar3d to depict three dimensional bars on the screen. 4. Call sphere in the program by specifying necessary variables. 5. Compose a scene in which the bars and the sphere move on the screen by using the relevant control structure. 6. Stop the program.

REG NO: 40508104067

PROGRAM: #include<stdio.h> #include<conio.h> #include<graphics.h> void main() { int gd=DETECT,gm,i,j; initgraph(&gd,&gm,""); bar3d(200,200,325,250,31,1); getch(); for(i=1;i<=750;i++) { cleardevice(); setcolor(2); setfillstyle(SLASH_FILL,2); bar3d(200,i-200,325,i-250,31,1); setcolor(5); setfillstyle(CLOSE_DOT_FILL,5); bar3d(i+200,200,i+325,250,31,1); setcolor(6); setfillstyle(BKSLASH_FILL,6); bar3d(400,i+200,525,i+250,31,1); setcolor(3); setfillstyle(SOLID_FILL,3); bar3d(i-200,200,i-325,250,31,1); setcolor(7); setfillstyle(INTERLEAVE_FILL,7); bar3d(i-200,i+200,i-325,i+250,31,1); setcolor(4); setfillstyle(LTSLASH_FILL,4); bar3d(i+400,200,i+525,250,31,1); setcolor(5); setfillstyle(XHATCH_FILL,5); bar3d(0,i-200,25,i-250,31,1); setcolor(4); setfillstyle(HATCH_FILL,4);

REG NO: 40508104067

bar3d(i+300,i-100,i+125,i-150,31,1); setcolor(8); setfillstyle(LINE_FILL,8); bar3d(i-400,i+200,i-525,i+250,31,1); setcolor(5); setfillstyle(BKSLASH_FILL,5); bar3d(i+100,i-200,i+250,i-250,31,1); setcolor(3); setfillstyle(SOLID_FILL,3); fillellipse(i+100,100,50,50); fillellipse(450-i,400,50,50); setcolor(6); setfillstyle(SOLID_FILL,6); fillellipse(100,i+100,50,50); fillellipse(450,i-500,50,50); setcolor(2); setfillstyle(SOLID_FILL,2); fillellipse(i+100,i+100,50,50); fillellipse(450-i,i-400,50,50); delay(25); } getch(); closegraph(); }

REG NO: 40508104067

OUTPUT:

RESULT: Thus a C program for composing a scene using three dimensional objects was executed and verified.

REG NO: 40508104067

GENERATING FRACTALS

AIM:

To write a C program to generate a fractal image. ALGORITHM: 1. 2. 3. Start the program. Initialize graphics functions. In this program a sierpinski gasket is chosen as a fractal image and is drawn using the function drawsierpinski(). 4. Sierpinski triangle is created by infinite removals. Each triangle is divided into four smaller triangles, each of them half the length (height, etc.) of the original. 5. Each smaller triangle is reduced iteratively in linear dimension from the original by a factor of n=2, and keep k=3 smaller triangles at each step. Hence the Sierpinski Triangle has a dimension of 1.5850 6. Stop the program.

REG NO: 40508104067

PROGRAM: #include<stdio.h> #include<conio.h> #include<stdlib.h> #include<math.h> #include<graphics.h> void DrawSierpinski(void); void main() { int gd=VGA; int gm=VGAHI; initgraph(&gd,&gm,"\\tc\\bgi"); DrawSierpinski(); getch(); } void DrawSierpinski(void) { char Direct; int iterate; unsigned int x1,y1,x2,y2; x1=x2=320; y1=y2=0; for(iterate=0;iterate<10000;iterate++) { Direct=random(3); if(Direct==0) { x1=(x2+320)/2; y1=(y2+0)/2; else if(Direct==1) { x1=(x2+0)/2; y1=(y2+480)/2; else if(Direct==2) { x1=(x2+640)/2; y1=(y2+480)/2; putpixel(x1,y1,WHITE); x2=x1; y2=y1; } }

} } }

REG NO: 40508104067

OUTPUT:

RESULT: Thus a C program to draw sierpinski gasket was written and executed.

You might also like