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

INFANT JESUS COLLEGE OF ENGINEERING KEELAVALLANADU

Department of Computer Science & Engineering

CS 79 Computer Graphics Lab Manual

Prepared by Indrakumari R AP / IT

CS 79 COMPUTER GRAPHICS LABORATORY

1. Implementation of Bresenhams Algorithm Line, Circle, Ellipse.

2. Implementation of Line, Circle and ellipse attributes 3. Two Dimensional transformations - Translation, Rotation, Scaling, Reflection, Shear. 4. Composite 2D Transformations 5. Cohen Sutherland 2D line clipping and Windowing 6. Sutherland Hodgeman Polygon clipping Algorithm 7. Three dimensional transformations - Translation, Rotation, Scaling 8. Composite 3D transformations 9. Drawing three dimensional objects and Scenes 10. Generating Fractal images

LIST OF EQUIPMENTS: 1) Turbo C 2) Visual C++ with OPENGL 3) Any 3D animation software like 3DSMAX, Maya, Blender

BASIC GRAPHICS FUNCTION

1) Initgraph () initgraph() function initializes the graphics mode and clears the screen.

Declaration: void far initgraph(int far *driver, int far *mode, char far *path) 2) Detectgraph () Detectgraph function determines the graphics hardware in the system, if the function finds a graphics adapter then it returns the highest graphics mode that the adapter supports. Declaration: void far detectgraph(int far *driver, int far *mode) Integer that specifies the graphics driver to be used. You can give graphdriver a value using a constant of the graphics_drivers enumeration type. 3) Closegraph () closegraph() function switches back the screen from graphcs mode to text mode. It clears the screen also. A graphics program should have a closegraph function at the end of graphics. Otherwise DOS screen will not go to text mode after running the program. 4) Getpixel () getpixel function returns the color of pixel present at location(x, y). Declaration:int getpixel(int x, int y); 5) Putpixel () putpixel function plots a pixel at location (x, y) of specified color. Declaration:void putpixel(int x, int y, int color); For example if we want to draw a GREEN color pixel at (35, 45) then we will write putpixel(35, 35, GREEN); in our c program, putpixel function can be used to draw circles, lines and ellipses using various algorithms. 6) line()

line function is used to draw a line from a point(x1,y1) to point(x2,y2) i.e. (x1,y1) and (x2,y2) are end points of the line. Declaration :void line(int x1, int y1, int x2, int y2); 7) lineto() lineto function draws a line from current position(CP) to the point(x,y), you can get current position using getx and gety function. 8) circle() Circle function is used to draw a circle with center (x,y) and third parameter specifies the radius of the circle. Declaration :void circle(int x, int y, int radius); 9)ellipse() Ellipse is used to draw an ellipse (x,y) are coordinates of center of the ellipse, stangle is the starting angle, end angle is the ending angle, and fifth and sixth parameters specifies the X and Y radius of the ellipse. To draw a complete ellipse strangles and end angle should be 0 and 360 respectively. Declaration :void ellipse(int x, int y, int stangle, int endangle, int xradius, int yradius); 10) drawpoly() drawpoly function is used to draw polygons i.e. triangle, rectangle, pentagon, hexagon etc. Declaration :void drawpoly( int num, int *polypoints );

num indicates (n+1) number of points where n is the number of vertices in a polygon, polypoints points to a sequence of (n*2) integers . Each pair of integers gives x and y coordinates of a point on the polygon. We specify (n+1) points as first point coordinates should be equal to (n+1)th to draw a complete figure. To understand more clearly we will draw a triangle using drawpoly, consider for example the array :int points[] = { 320, 150, 420, 300, 250, 300, 320, 150}; points array contains coordinates of triangle which are (320, 150), (420, 300) and (250, 300). Note that last point(320, 150) in array is same as first. 11) outtext () outtext function displays text at current position. Declaration :void outtext(char *string); 12) outtextxy () outtextxy function display text or string at a specified point(x,y) on the screen. Declaration :void outtextxy(int x, int y, char *string); x, y are coordinates of the point and third argument contains the address of string to be displayed. 13)rectangle() Rectangle function is used to draw a rectangle. Coordinates of left top and right bottom corner are required to draw the rectangle. left specifies the X-coordinate of top left corner, top specifies the Y-coordinate of top left corner, right specifies the X-coordinate of right bottom corner, bottom specifies the Y-coordinate of right bottom corner. Declaration :void rectangle(int left, int top, int right, int bottom); 14) floodfill()

floodfill function is used to fill an enclosed area. Current fill pattern and fill color is used to fill the area.(x, y) is any point on the screen if (x,y) lies inside the area then inside will be filled otherwise outside will be filled,border specifies the color of boundary of area. Declaration :void floodfill(int x, int y, int border); 15)fillpoly() f illpoly function draws and fills a polygon. It require same arguments as drawpoly. Declaration :void drawpoly( int num, int *polypoints ); 16)fillellipse() f illellipse function draws and fills a polygon. Declaration:void fillellipse(int x, int y, int xradius, int yradius); x and y are coordinates of center of the ellipse, xradius and yradius are x and y radius of ellipse respectively.

An Example program using the basic graphic functions #include <graphics.h> #include <stdlib.h>

#include <stdio.h> #include <conio.h>

void main() { int gd=DETECT, gm; initgraph(&gd, &gm, " c:\\turboc\\bgi"); circle(100,100,50); outtextxy(75,170, "Circle"); rectangle(200,50,350,150); outtextxy(240, 170, "Rectangle"); ellipse(500, 100,0,360, 100,50); outtextxy(480, 170, "Ellipse"); line(100,250,540,250); outtextxy(300,260,"Line"); getch(); closegraph(); }

EX NO 1a AIM

BRESENHAMS LINE DRAWING ALGORITHM

To write a C program to draw a line using Bresenhams algorithm. In Bresenhams approach the pixel position along a line path are determined by sampling unit X intervals. Starting from the left end point(X0, Y0)of a given line we step to each successive columns and plot the pixel whose scan line

Y-value is closest to the line path. Assuming the Kth step in process, determined that the pixel at(X k, Yk)decide which pixel to plot in column Xk+1.The choices are (Xk+1, Yk) and (Xk+1,Yk+1) Algorithm Step 1: Input the line endpoints and store the left endpoint in (X0, Y0) Step 2: Load (X0, Y0) in to the frame buffer Step 3: Calculate constants x, y, 2y, and 2y -2x, and obtain the decision parameters as P0 = 2 y x Step 4 : At each Xk along the line, starting at k = 0, perform the following test. If Pk < 0, the next point to plot is (Xk+1, Yk) and Pk+1 = Pk+2y Otherwise, the next point to plot is (Xk+1, Yk+1) and Pk+1 = Pk+2 y - 2 x Step 5: Repeat step 4 x times #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> void main() { int gd = DETECT, gm; int x,y,x1,y1,x2,y2,p,dx,dy,twody,twodydx,xend; initgraph(&gd,&gm,"..\\BGI:"); printf("\nEnter the x-coordinate of the starting point :"); scanf("%d",&x1); printf("\nEnter the y-coordinate of the starting point :"); scanf("%d",&y1); printf("\nEnter the x-coordinate of the Ending point :"); scanf("%d",&x2); printf("\nEnter the y-coordinate of the ending point :"); scanf("%d",&y2); clrscr(); dx=x2-x1; dy=y2-y1; p=2*dy-dx; twody=2*dy; twodydx=2*(dy-dx); if (x1>x2) { x=x2; y=y2; xend=x1; } else { x=x1; y=y1; xend=x2;

} putpixel(x,y,RED); while(x<xend) { x++; if (p<0) p=p+twody; else { y=y+1; p=p+twodydx; } putpixel(x,y,RED); } getch(); closegraph(); }

Output Enter the x-coordinate of the starting point Enter the y-coordinate of the starting point Enter the x-coordinate of the Ending point Enter the y-coordinate of the ending point : 100 : 100 : 200 : 200

EX NO 1b AIM

BRESENHAMS CIRCLE DRAWING ALGORITHM

To write a C program to draw a circle using Bresenhams algorithm. Circles have the property of being highly symmetrical; Bresenham's circle algorithm calculates the locations of the pixels in the first 45 degrees. It assumes that the circle is centered on the origin. Algorithm

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

#include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> int x,y,r,p,xcenter, ycenter; void circleplot(int,int,int,int); void main() { int gd = DETECT, gm; initgraph(&gd,&gm,"..\\BGI:"); printf("\nEnter the x-coordinate of the centre point :"); scanf("%d",&xcenter); printf("\nEnter the y-coordinate of the centre point :"); scanf("%d",&ycenter); printf("\nEnter the radius :"); scanf("%d",&r); x=0; y=r; p=3-(2*r);

while (x<y) { x++; if (p<0) p=p+(4*x)+6; else { y--; p=p+10+4*(x-y); } circleplot(xcenter,ycenter,x,y); } getch(); closegraph(); } void circleplot(int xcenter, int ycenter,int x, int y) { putpixel(xcenter+x,ycenter+y,10); putpixel(xcenter-x,ycenter+y,10); putpixel(xcenter+x,ycenter-y,10); putpixel(xcenter-x,ycenter-y,10); putpixel(xcenter+y,ycenter+x,10); putpixel(xcenter-y,ycenter+x,10); putpixel(xcenter+y,ycenter-x,10); putpixel(xcenter-y,ycenter-x,10); }

Output

Enter the x-coordinate of the centre point: 100

Enter the y-coordinate of the centre point: 100 Enter the radius: 50

EX NO 1c AIM

BRESENHAMS ELLIPSE DRAWING ALGORITHM

To write a C program to draw an ellipse using Bresenhams algorithm. The main object in the algorithm is to perform analyze and manipulate linear equation so integer arithmetic is used in the calculations. Integer Arithmetic has the advantages of speed and precision; Algorithm Step 1: Declare the variable p, px, py, x, y, ry2, rx2, twory2, tworx2, xc, yc, rx, ry, gd, gm. Step 2: Initialize the graphics function. Step 3: Inputs the value of inter major axis and minor axis. Step 4: Calculate the values of ry2, rx2, try2, trx2. Step 5: Call the function plot points to plot the pixel along the circular path. Step 6:Check whether px and py if there then perform the following steps Increment the value of x Check if p>0 Decrement value of y and calculate py Otherwise calculate p using p=p+xy2+px Call the function plot points.

Step 7 : Check whether if value of y is greater than 0. Decrement the value y and calculate py. Check if p<0. Increment the value y and calculate py. Check if p<0. Step 8: Stop the program. Program #include<graphics.h> #include<stdio.h> #include<conio.h> #include<stdlib.h> void ellipseplot(int,int,int,int); int xcenter,yc,rx,ry; void main() { int gd=DETECT,gm; long xcenter,ycenter,rx,ry; long rx2,ry2,tworx2,twory2,d1,d2,x,y,dx,dy; initgraph(&gd,&gm,"..\\BGI:"); printf("\nEnter the x-coordinate of the centre point :"); scanf("%ld",&xcenter); printf("\nEnter the y-coordinate of the centre point :"); scanf("%ld",&ycenter); printf("\nEnter the x-coordinate of the radius :"); scanf("%ld",&rx); printf("\nEnter the y-coordinate of the radius :"); scanf("%ld",&ry); clrscr(); rx2=rx*rx; ry2=ry*ry; twory2=2*ry2; tworx2=2*rx2; x=0; y=ry; d1=ry2-rx2*ry+(0.25*rx2); dx=twory2*x; dy=tworx2*y; do { ellipseplot(xcenter,ycenter,x,y); if(d1<0) { x=x+1; y=y; dx=dx+twory2; d1=d1+dx+ry2; } else Calculate p and call the function plot points to point pixel along the path.

{ x=x+1; y=y-1; dx=dx+twory2; dy=dy-tworx2; d1=d1+dx-dy+ry2; } } while(dx<dy); d2=ry2*(x+0.5)*(x+0.5)+rx2*(y-1)*(y-1)-rx2*ry2; do { ellipseplot(xcenter,ycenter,x,y); if(d2>0) { x=x; y=y-1; dy=dy-tworx2; d2=d2-dy+rx2; } else { x=x+1; y=y-1; dy=dy-tworx2; dx=dx+twory2; d2=d2+dx-dy+rx2; } } while(y>0); getch(); closegraph(); } void ellipseplot(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 x-coordinate of the centre point : 100 Enter the y-coordinate of the centre point : 125 Enter the x-coordinate of the radius : 50 Enter the y-coordinate of the radius : 60

QUESTIONS 1. Difference between DDA and Bresenham's line drawing algorithm DDA uses float numbers and uses operators such as division and multiplication in its calculation. Bresenhams algorithm uses ints and only uses addition and subtraction. Due to the use of only addition subtraction and bit shifting (multiplication and division use more resources and processor power) bresenhams algorithm is faster than DDA in producing the line. Im not sure, though if i remember right, they still produce the same line in the end. 2. What are the advantages of Bresenhams algorithm? The main advantage of Bresenham's algorithm is speed. It uses only integer addition/subtraction and bit shifting. 3.What are the disadvantages of Bresenhams algorithm? The disadvantage of such a simple algorithm is that it is meant for basic line drawing. 4.What is the drawback of DDA? Time consuming 5. Define Computer graphics. Computer graphics remains one of the most existing and rapidly growing computer fields. Computer graphics may be defined as a pictorial representation or graphical representation of objects in a computer

EX NO 2a

IMPLEMENTATION OF LINE, CIRCLE AND ELLIPSE ATTRIBUTES

FUNCTIONS USED: Line() line (x1,y1,x2,y2) : Line function draws a line between two specified points (x,y) towards (x2,y2).This function comes handy if you want to draw box like shapes or just plotting the graphs etc. e.g. line(100,50,100,400); You can set the line style using setlinestyle functions. This function specifies the type of line, pattern & the thickness that is going to appear on the screen. You have options like solid,dotted,centered,dashed etc. The other attributes with the command are as follows: setlinestyle(style,0,1);SetLinetype(lt),setLinewidthScaleFactor(lw),sePol ylinecolourIn dex(lc) Color palettes

The graphics.h has declaration about 16 colors.In order to use the color in your program you have to use the functions like setcolor( ) ,setbkcolor( ) & setfillstyle( ).The function setcolor( ) sets the value of current drawing color to color.setfillstyle( ) sets the current fill pattern and fill color.setbkcolor( ) sets the value for background color,which is by default black. Below is the table that describes the value for each color that are declared in the graphics.h file. Color Black Blue GREEN Cyan RED MAGENTA BROWN LIGHTGRAY DARKGRAY LIGHTBLUE LIGHTGREEN LIGHTCYAN LIGHTRED LIGHTMAGENTA YELLOW WHITE Value 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

CIRCLE DRAWING FUNCTIONS USED: Circle() The function circle() is used to draw a circle using(x,y) as centre point. Syntax: circle (x,y,radius) The other attributes with the command are as follows: Setfillstyle(style,lc),setcolor(lc) ELLIPSE DRAWING

FUNCTIONS USED: ellipse (x,y,stangle,endangle,xrad,yrad) : This function draws an elliptical arc. Here (x,y) are the co-ordinates of center of the ellipse. (stangle,endangle) are the starting and ending angles. If stangle=0 and endangle=360 then this function draws complete ellipse. eg.ellipse(100,150,0,360,100,50); The other attributes with the command are as follows: setfillcolor(lc),setfillstyle(ls),setlinecolor(lc),setlinewidth(lw)
Aim

To write a program to draw a Line,circle,ellipse.


Algorithm

Step 1: Start Step 2: Draw the line. Set the attributes like style,type,width etc. Step 3: Draw the circle. Set the attributes using commands. Step 4: Draw the ellipse. Set the attributes like fillcolour, fillstyle, linecolour, linewidth Step 5: stop the program
Program #include <graphics.h> #include <stdlib.h> #include <string.h> #include <stdio.h> #include <conio.h> void main() { int gd=DETECT,gm; int ch; clrscr(); while(1) { printf("******Line Styles*******\n"); printf("\n1.Solid Line"); printf("\n2.Dotted Line"); printf("\n3.Center Line"); printf("\n4.Dashed Line"); printf("\n5.Userbit Line"); printf("\n6.Exit"); printf("\n\nEnter your choice:\n"); scanf("%d",&ch); switch(ch)

{ case 1: clrscr(); initgraph(&gd,&gm," "); setlinestyle(0,1,3); line(100,30,250,250); getch(); cleardevice();closegraph(); break; case 2: clrscr(); initgraph(&gd,&gm," "); clrscr(); setlinestyle(1,1,3); line(100,30,250,250); getch(); cleardevice();closegraph(); break; case 3: clrscr(); initgraph(&gd,&gm," "); setlinestyle(2,1,3); line(100,30,250,250); getch(); cleardevice();closegraph(); break; case 4: clrscr(); initgraph(&gd,&gm," "); setlinestyle(3,1,3); line(100,30,250,250); getch(); cleardevice(); closegraph(); break; case 5: clrscr(); initgraph(&gd,&gm," "); setlinestyle(4,1,3); line(100,30,250,250); getch(); cleardevice();closegraph(); break; case 6: exit(0); } } } OUTPUT ******Line Styles******* 1.Solid Line 2.Dotted Line 3.Center Line 4.Dashed Line

5.Userbit Line 6.Exit Enter Ur Choice: 1 Solid Line

Entrer ur Choice: 2 Dotted Line ..

EX No. 2b Program #include <graphics.h> #include <stdlib.h> #include <string.h> #include <stdio.h> #include <conio.h> void main() { int gd=DETECT,gm; int ch; clrscr(); while(1) { printf("******Circle Attributes*******\n"); printf("\n1.Empty Fill"); printf("\n2.Soild Fill"); printf("\n3.Line Fill"); printf("\n4.Wide dot Fill"); printf("\n5.close dot Fill"); printf("\n6.User Fill"); printf("\n7.Exit"); printf("\n\nEnter your choice:\n"); scanf("%d",&ch);

switch(ch) { case 1: clrscr(); initgraph(&gd,&gm," "); setfillstyle(EMPTY_FILL, RED); circle(100, 100, 50); floodfill(100, 100, WHITE); getch(); cleardevice(); closegraph(); break; case 2: clrscr(); initgraph(&gd,&gm," "); setfillstyle(SOLID_FILL, RED); circle(100, 100, 50); floodfill(100, 100, WHITE); getch(); cleardevice(); closegraph(); break; case 3: clrscr(); initgraph(&gd,&gm," "); setfillstyle(LINE_FILL, RED); circle(100, 100, 50); floodfill(100, 100, WHITE); getch(); cleardevice(); closegraph(); break; case 4: clrscr(); initgraph(&gd,&gm," "); setfillstyle(WIDE_DOT_FILL, RED); circle(100, 100, 50); floodfill(100, 100, WHITE); getch(); cleardevice();closegraph(); break; case 5: clrscr(); initgraph(&gd,&gm," "); setfillstyle(CLOSE_DOT_FILL, RED); circle(100, 100, 50); floodfill(100, 100, WHITE); getch(); cleardevice();closegraph(); break; case 6: clrscr(); initgraph(&gd,&gm," "); setfillstyle(USER_FILL, RED); circle(100, 100, 50); floodfill(100, 100, WHITE);

getch(); cleardevice();closegraph(); break; case 7: exit(0); } } } OUTPUT "******Circle Attributes******* 1.Empty Fill 2.Soild Fill 3.Line Fill 4.Wide dot Fill 5.close dot Fill 6.User Fill 7.Exit Enter your choice: 1

Enter Ur Choice:2

Enter Ur Choice:3

Entrer ur Choice:6

EX.No.2c Program: #include <graphics.h> #include <stdlib.h> #include <string.h> #include <stdio.h> #include <conio.h> void main() { int gd=DETECT,gm; int ch; clrscr(); while(1) { printf("******Ellipse Attributes*******\n"); printf("\n1.Empty Fill"); printf("\n2.Soild Fill"); printf("\n3.Line Fill"); printf("\n4.Wide dot Fill"); printf("\n5.close dot Fill"); printf("\n6.User Fill"); printf("\n7.Exit"); printf("\n\nEnter your choice:\n"); scanf("%d",&ch); switch(ch) { case 1: clrscr(); initgraph(&gd,&gm," "); setfillstyle(EMPTY_FILL, RED); ellipse(100, 100,0,360,50,25); floodfill(100, 100, WHITE); getch(); cleardevice(); closegraph(); break; case 2: clrscr(); initgraph(&gd,&gm," "); setfillstyle(SOLID_FILL, RED); ellipse(100, 100,0,360,50,25); floodfill(100, 100, WHITE); getch(); cleardevice(); closegraph(); break; case 3: clrscr();

initgraph(&gd,&gm," "); setfillstyle(LINE_FILL, RED); ellipse(100, 100,0,360,50,25); floodfill(100, 100, WHITE); getch(); cleardevice(); closegraph(); break; case 4: clrscr(); initgraph(&gd,&gm," "); setfillstyle(WIDE_DOT_FILL, RED); ellipse(100, 100,0,360,50,25); floodfill(100, 100, WHITE); getch(); cleardevice(); closegraph(); break; case 5:clrscr(); initgraph(&gd,&gm," "); setfillstyle(CLOSE_DOT_FILL, RED); ellipse(100, 100,0,360,50,25); floodfill(100, 100, WHITE); getch(); cleardevice(); closegraph(); break; case 6: clrscr(); initgraph(&gd,&gm," "); setfillstyle(USER_FILL, RED); ellipse(100, 100,0,360,50,25); floodfill(100, 100, WHITE); getch(); cleardevice();closegraph(); break; case 7: exit(0); } } } OUTPUT:

"******Ellipse Attributes******* 1.Empty Fill 2.Soild Fill 3.Line Fill 4.Wide dot Fill 5.close dot Fill 6.User Fill 7.Exit

Entrer ur choice: 1

Entrer ur choice: 2

Enter ur choice : 6

EX NO 3 AIM

2 -DIMENSIONAL TRANFORMATIONS

To perform the various 2-dimensional transformations such as translation, scaling, rotation, reflection and shearing. Algorithm Step 1: Input the figure. Step 2: Display the menu as 1.Translation 2.Scaling 3.Rotation 4.Reflection 5.Shearing 6.Exit Step 3: Get the choice from the user. Step 4: If the choice is 1 get the translation vector. Add the translation vector to the original coordinate position to move to a new position. Step 5: If the choice is 2 get the scaling factor. Multiply the coordinate values of each vertex by scaling factors to produce the transformed coordinates. Step 6: If the choice is 3 get the rotation angle. Rotate the figure with respect to the specified angle. Step 7: If the choice is 4 get the axis of reflection. Mirror image is generated relative to an axis of reflection by rotating the object 180 about the reflection axis. Step 8: If the choice is 5 shearing is done which causes the transformation that distorts the shape of an object. Step 9: If choice is 6 exit the program. Program #include"stdio.h" #include"conio.h" #include"graphics.h" #include"math.h" int gd=0,gm,theta,xf,yf,x1,y1,x2,y2,xa1,xa2,ya1,ya2,a; int midx,midy,tx,ty,choice,sx,sy,sh,xa[10],ya[10],first; int i,n=5; int xf,yf; int xb[10],yb[10]; void main() { do { first=0; org(); getch(); printf("\n1.Translation\n2.Rotation\n3.Scaling\n4.Reflection\n5.Shear\n6.Exit\nenter ur choice\n"); scanf("%d",&choice); //input options switch(choice) { case 1: //translation printf("Enter the translation factor (x,y) :"); //get the factor (x,y) scanf("%d%d",&tx,&ty); org();

rectangle(midx+tx,midy-100+ty,midx+100+tx,midy+ty); //add with x,y getch(); closegraph(); break; case 2: //rotation printf("Enter the angle :"); scanf("%d",&a); org(); rotation(); //call rotation function break; case 3: //scaling printf("Enter the scaling factor (x,y):"); //get the factor scanf("%d%d",&sx,&sy); org(); x1=0; y1=-100; x2=100; y2=0; xa1=x1*sx+midx; ya1=y1*sy+midy; xa2=x2*sx+midx; ya2=y2*sy+midy; rectangle(xa1,ya1,xa2,ya2); getch(); closegraph(); break; case 4: org(); rectangle(midx,midy,midx+100,midy+100); getch(); closegraph(); break; case 5: //Shear printf("enter the shear value"); scanf("%d",&sh); cleardevice(); org(); for(i=0;i < n;i++) //multiply with shear factor { xb[i]=xa[i]+sh*(ya[i]-yf); yb[i]=ya[i]; } for(i=0;i < n;i++) line(xb[i],yb[i],xb[(i+1)%n],yb[(i+1)%n]); getch(); //multiply with x,y

//Reflection

} }while(choice<6); getch(); } org() { initgraph(&gd,&gm,""); midx=getmaxx()/2; midy=getmaxy()/2; xf=midx;

yf=midy; xa[0]=midx; ya[0]=midy-100; xa[1]=midx+100; ya[1]=midy-100; xa[2]=midx+100; ya[2]=midy; xa[3]=midx; ya[3]=midy; xa[4]=midx; ya[4]=midy-100; cleardevice(); setcolor(WHITE); line(0,midy,2*midx,midy); line(midx,0,midx,2*midy); setcolor(4); if(first==0) { setlinestyle(SOLID_LINE,1,1); first=1; } else { setlinestyle(DASHED_LINE,1,1); } rectangle(midx,midy-100,midx+100,midy); setlinestyle(SOLID_LINE,1,1); return; } rotation() { theta=(a*2*3.14)/180; for(i=0;i < n;i++) { xb[i]=xf+(xa[i]-xf)*cos(theta)-(ya[i]-yf)*sin(theta); yb[i]=yf+(xa[i]-xf)*sin(theta)-(ya[i]-yf)*cos(theta); } for(i=0;i < n;i++) line(xb[i],yb[i],xb[(i+1)%n],yb[(i+1)%n]); getch(); cleardevice(); return; } Output 1. Translation 2. Rotation 3. Scaling 4. Reflection

5. Shear 6. Exit

Enter your Choice : 1 Enter the Translation Factor (x,y) : 100 100

Enter your Choice : 6

EX NO 4 Aim:

2D COMPOSITE TRANSFORMATION

To create a program to transform, rotate and scale using for the 2D composite transformation. Algorithm: Step 1: Start the Program. Step 2: Include the graphics header file Step 3: Initialize graphics using initgraph () Step 4: Initialize the variable Step 5: Enter the choice for transformation. Step 6: If choice=2 Translation & Rotation i.e. changing the co-ordinates and rotate the object is performed x`=x+tx y`=y+ty Step 7: If choice=3 Translation & Scaling i.e. Translate & Scaling the object is performed

x`=x*cos-y*sin y`=x*sin+y*cos Step 8: If choice=4 Scaling & Rotation i.e. Scaling & Rotate the object is performed x`=x*sx y`=y*sy Step 9: Stop the Program. Program Program: /*2D COMPOSITE TRANSFORMATION*/ #include<stdio.h> #include<stdlib.h> #include<math.h> #include<graphics.h> float tx,ty,sx,sy,r; float x[15][15],y[15][15]; int i,j=2,choice; void transform(); void rotate(); void scale(); void transform() { x[i][j-1]=x[i][j-1]+tx; x[i][j]=x[i][j]+tx; y[i][j-1]=y[i][j-1]+ty; y[i][j]=y[i][j]+ty; } void rotate() { x[i][j-1]=(x[i][j-1]*cos(r))-(y[i][j-1]*sin(r)); y[i][j-1]=(x[i][j-1]*sin(r))+(y[i][j-1]*cos(r)); x[i][j]=(x[i][j]*cos(r))-(y[i][j]*sin(r)); y[i][j]=(x[i][j]*sin(r))+(y[i][j]*cos(r)); } void scale() { x[i][j-1]=x[i][j-1]*sx; x[i][j]=x[i][j]*sx; y[i][j-1]=y[i][j-1]*sy; y[i][j]=y[i][j]*sy; } int main() { int gd=DETECT,gm; initgraph(&gd,&gm,"c:\\tc\\bgi"); x[1][1]=100,x[1][2]=200,y[1][1]=200,y[1][2]=300; x[2][1]=200,x[2][2]=200,y[2][1]=300,y[2][2]=200; x[3][1]=100,x[3][2]=200,y[3][1]=200,y[3][2]=200; textattr(0); for(i=1;i<=3;i++) line(x[i][j-1],y[i][j-1],x[i][j],y[i][j]); while(choice!=4) { printf("2D COMPOSITE");

printf("\n1.TRANSLATION & ROTATION"); printf("\n2.TRANSLATION & SCALING"); printf("\n3.SCALING & ROTATION"); printf("\n4.EXIT"); printf("\n ENTER UR CHIOCE:"); scanf("%d",&choice); switch(choice) { case 1: printf("\n enter x & y vector:"); scanf("%f%f",&tx,&ty); printf("\n enter rotation angle:"); scanf("%f",&r); clrscr(); r=(r*3.14/180); for(i=1;i<=3;i++) { transform(); rotate(); line(x[i][j-1],y[i][j-1],x[i][j],y[i][j]); delay(500); } break; case 2: printf("\n enter x & y vector:"); scanf("%f%f",&tx,&ty); printf("\n enter x & y vector:"); scanf("%f%f",&sx,&sy); clrscr(); for(i=1;i<=3;i++) { transform(); scale(); line(x[i][j-1],y[i][j-1],x[i][j],y[i][j]); delay(500); } break; case 3: printf("\n enter x & y vector:"); scanf("%f%f",&sx,&sy); printf("\n enter rotation angle:"); scanf("%f",&r); clrscr(); r=(r*3.14/180); for(i=1;i<=3;i++) { scale(); rotate(); line(x[i][j-1],y[i][j-1],x[i][j],y[i][j]); delay(500); } break; case 4: exit(0); } }

getch(); return 0; } Output: 2D COMPOSITE 1.TRANSLATION & ROTATION 2.TRANSLATION & SCALING 3.SCALING & ROTATION 4.EXIT ENTER UR CHIOCE: 1 Enter x & y Vector: 1 1 Enter Rotation Angle: 10 ENTER UR CHIOCE: 2 Enter x & y Vector: 2 1 Enter x & y Vector: 3 1

ENTER UR CHIOCE: 3 Enter x & y Vector: 1 1 Enter Rotation Angle: 20

ENTER UR CHIOCE: 4

Result: Thus the above program 2D Composite Transformations and its operations Translation & Rotation, Translation & Scaling; Scaling & Rotation has been executed successfully using Graphics C Language.

EX NO 5a AIM:

COHEN-SUTHERLAND ALGORITHM

To clip a line using Cohen-Sutherland clipping algorithm. Algorithm: The method speeds up the processing of line segments by performing initial tests that reduce the number of intersections that must be calculated.

1. Every line endpoint is assigned a four digit binary code, called region code, that identifies the location of
the point relative to the boundaries of the clipping rectangle. 2. Each bit position in the region code is used to indicate one of the four relative coordinate positions of the point with respect to the clip window. Bit 1: left Bit 2: right Bit 3: below Bit 4: above

3. Bit values in the region code are determined by comparing endpoint coordinates values (x, y) with respect
to the clip boundaries. eg.Bit 1 is set to 1 if x<xwmin 4. Once we have established region codes for all line endpoints, we can quickly determine which lines are completely outside or inside the clip window. 5. Lines that cannot be identified as completely inside or outside a clip window are checked for intersection with boundaries. 6. Intersection points with a clipping boundary can be calculated using the slope-intercept form of the line equation.

m = ( y2 y1 ) ( x2 x1 )

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

y = y1 +m( x x1 )

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

Program

x = x1 +

y y1 m

y = yw y = yw

min

max

#include<stdio.h> #include<conio.h> #include<graphics.h> void main() { int gd=DETECT, gm; float i,xmax,ymax,xmin,ymin,x1,y1,x2,y2,m; float start[4],end[4],code[4]; clrscr(); initgraph(&gd,&gm,"");

printf("\n\tEnter the bottom-left coordinate of viewport: "); scanf("%f %f",&xmin,&ymin); printf("\n\tEnter the top-right coordinate of viewport: "); scanf("%f %f",&xmax,&ymax); printf("\n\tEnter the coordinates for starting point of line: "); scanf("%f %f",&x1,&y1); printf("\n\tEnter the coordinates for ending point of line: "); scanf("%f %f",&x2,&y2); for(i=0;i<4;i++) { start[i]=0; end[i]=0; } m=(y2-y1)/(x2-x1); if(x1<xmin) start[0]=1; if(x1>xmax) start[1]=1; if(y1>ymax) start[2]=1; if(y1<ymin) start[3]=1; if(x2<xmin) end[0]=1; if(x2>xmax) end[1]=1; if(y2>ymax) end[2]=1; if(y2<ymin) end[3]=1;

for(i=0;i<4;i++) code[i]=start[i]&&end[i]; if((code[0]==0)&&(code[1]==0)&&(code[2]==0)&&(code[3]==0)) { if((start[0]==0)&&(start[1]==0)&&(start[2]==0)&&(start[3]==0)&& (end[0]==0)&&(end[1]==0)&&(end[2]==0)&&(end[3]==0)) { cleardevice(); printf("\n\t\tThe line is totally visible\n\t\tand not a clipping candidate"); rectangle(xmin,ymin,xmax,ymax); line(x1,y1,x2,y2); getch(); } else { cleardevice(); printf("\n\t\t\t\tLine is partially visible"); rectangle(xmin,ymin,xmax,ymax); line(x1,y1,x2,y2); getch(); if((start[2]==0)&&(start[3]==1)) { x1=x1+(ymin-y1)/m; y1=ymin; } if((end[2]==0)&&(end[3]==1)) { x2=x2+(ymin-y2)/m; y2=ymin; }

if((start[2]==1)&&(start[3]==0)) { x1=x1+(ymax-y1)/m; y1=ymax; } if((end[2]==1)&&(end[3]==0)) { x2=x2+(ymax-y2)/m; y2=ymax; } if((start[1]==0)&&(start[0]==1)) { y1=y1+m*(xmin-x1); x1=xmin; } if((end[1]==0)&&(end[0]==1)) {

y2=y2+m*(xmin-x2); x2=xmin; } if((start[1]==1)&&(start[0]==0)) { y1=y1+m*(xmax-x1); x1=xmax; } if((end[1]==1)&&(end[0]==0)) { y2=y2+m*(xmax-x2); x2=xmax;

} clrscr(); cleardevice(); printf("\n\t\tAfter clippling:"); rectangle(xmin,ymin,xmax,ymax); line(x1,y1,x2,y2); getch(); } } else { clrscr(); cleardevice(); printf("\nLine is invisible"); rectangle(xmin,ymin,xmax,ymax); } getch(); closegraph(); } OUTPUT:

Enter the bottom-left coordinate of viewport:50 50 Enter the top-right coordinate of viewport:200 200 Enter the coordinates for starting point of line:25 25 Enter the coordinates for ending point of line:220 220

EX NO 5b AIM:

WINDOW TO VIEWPORT MAPPING

To write a C program to perform Window to Viewport transformation. Algorithm Step1: Draw a world coordinate area selected for display called as window. This window defines what is to be viewed. Step 2: Draw an area on a display device to which a window is mapped called as Viewport. The viewport defines where it is to be displayed. Step 3: Now perform the mapping of a part of a world-coordinate scene to device coordinates referred as viewing transformation. Program #include<stdio.h> #include<conio.h> #include<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 Co-ordinates 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)+0.5); x2=v1+floor(((float)(x2-w1)*sx)+0.5); x3=v1+floor(((float)(x3-w1)*sx)+0.5); y1=v2+floor(((float)(y1-w2)*sy)+0.5); y2=v2+floor(((float)(y2-w2)*sy)+0.5); y3=v2+floor(((float)(y3-w2)*sy)+0.5); line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x3,y3,x1,y1); getch(); return 0; }

Ex NO 6

Polygon Clipping

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

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); } 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); /* initialize graphics and local variables */ 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( ); } /* 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

EX NO 7

3-D TRANSFORMATIONS

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

Algorithm Step 1: Input the figure. Step 2: Display the menu as 1.Translation 2.Scaling 3.Rotation 4.Exit Step 3: Get the choice from the user. Step 4: If the choice is 1 a point or an object is translated from position P to position P' with the operation P'=T.P where tx,ty and tz specifying translation distances. x'=x+ tx y'=y+ ty z'=z+ tz Step 5: If the choice is 2 the scaling transformation of a position P can be written as P'=S.P where scaling parameters sx,sy and sz are assigned any positive values. x'=x.sx y'=y.sy z'=z.sz Step 6: If the choice is 3 get the rotation angle. Rotate the figure with respect to the axis of rotation. Step 6a: About z axis rotation x'=xcos-ysin y'=xsin+ycos z'=z Rotation can be expressed as P'=Rz().P Step 6b: About x axis rotation

y'=ycos-zsin z'=ysin+zcos x'=x Rotation can be expressed as P'=Rx().P Step 6c: About y axis rotation z'=zcos-xsin x'=zsin+xcos y'=y Rotation can be expressed as P'=Ry().P Step 7: If choice is 4 exit the program. Program #include <stdio.h> #include <stdlib.h> #include<graphics.h> #include<conio.h> #include<math.h> #define yorg getmaxy()-100 #define xorg 100 void draw3d(int s,int x[20],int y[20],int d); int x[20],y[20],x1[20],y1[20],tx,ty,sx,sy,sz,i,s,d; void main() { int gd=DETECT,gm,ch,a,theta; initgraph(&gd,&gm,""); printf("\tEnter the No of sides : "); scanf("%d",&s); for(i=0;i<s;i++) { printf("\t(x%d , y%d) :",i,i); scanf("%d%d",&x[i],&y[i]);

} printf("\tDepth :"); scanf("%d",&d); clrscr(); cleardevice(); axis(); setcolor(RED); draw3d(s,x,y,d); getch(); do { clrscr(); cleardevice(); setcolor(WHITE); printf("\t\t\t3D TRANSFORMATIONS \n"); printf("\t1.TRANSLATION\n\t2.SCALING\n\t3.ROTATION\n\t4.EXIT\n\tENTER YOUR CHOICE :"); scanf("%d",&ch); switch(ch) { case 1: printf("\tEnter the translation factor (tx,ty) :"); scanf("%d%d",&tx,&ty); clrscr(); cleardevice(); axis(); setcolor(RED); draw3d(s,x,y,d); getch(); cleardevice(); for(i=0;i<s;i++)

{ x1[i]=x[i]+tx; y1[i]=y[i]+ty; } axis(); setcolor(DARKGRAY); setlinestyle(DASHED_LINE,1,1); draw3d(s,x,y,d); setlinestyle(SOLID_LINE,1,1); setcolor(RED); draw3d(s,x1,y1,d); getch(); break; case 2: printf("\tEnter the Scaling factor (sx,sy,sz) :"); scanf("%d%d%d",&sx,&sy,&sz); clrscr(); cleardevice(); axis(); setcolor(RED); draw3d(s,x,y,d); getch(); cleardevice(); for(i=0;i<s;i++) { x1[i]=x[i]*sx; y1[i]=y[i]*sy; } axis(); setcolor(DARKGRAY);

setlinestyle(DASHED_LINE,1,1); draw3d(s,x,y,d); setlinestyle(SOLID_LINE,1,1); setcolor(RED); draw3d(s,x1,y1,sz*d); getch(); break; case 3: printf("\tRotation angle :"); scanf("%d",&a); clrscr(); cleardevice(); axis(); setcolor(RED); draw3d(s,x,y,d); getch(); cleardevice(); theta=(a*2*3.14)/180; for(i=0;i<s;i++) { x1[i]=x[0]+((x[i]-x[0])*cos(theta))-((y[i]-y[0])*sin(theta)); y1[i]=y[0]+((y[i]-y[0])*cos(theta))+((x[i]-x[0])*sin(theta)); } axis(); setcolor(DARKGRAY); setlinestyle(DASHED_LINE,1,1); draw3d(s,x,y,d); setlinestyle(SOLID_LINE,1,1); setcolor(RED); draw3d(s,x1,y1,d);

getch(); break; default : break; } }while(ch<4); closegraph(); } axis() { setcolor(WHITE); line(100,0,100,getmaxy()-100); outtextxy(90,10,"x"); line(100,getmaxy()-100,getmaxx(),getmaxy()-100); outtextxy(getmaxx()-10,getmaxy()-110,"y"); line(100,getmaxy()-100,0,getmaxy()); outtextxy(10,getmaxy()-10,"z"); return; } void draw3d(int s,int x[20],int y[20],int d) { int i,j,k=0; for(j=0;j<2;j++) { for(i=0;i<s-1;i++) { line(x[i]+xorg-k,yorg-y[i]+k,x[i+1]+xorg-k,yorg-y[i+1]+k); } line(x[i]+xorg-k,yorg-y[i]+k,x[0]+xorg-k,yorg-y[0]+k); k=d;

} for(i=0;i<s;i++) { line(x[i]+xorg,yorg-y[i],x[i]+xorg-d,yorg-y[i]+d); } } OUTPUT: Enter the No of sides: 4 (x0 , y0):100 100 (x1 , y1):200 100 (x2, y2) :200 200 (x3 , y3):100 200 Depth : 45 ENTER THE CHOICE: 1 Enter the translation factor (tx,ty) :15 15 ENTER THE CHOICE: 2 Enter the Scaling factor (sx,sy,sz) :2 ENTER THE CHOICE: 3 Rotation angle: 75 1 1

EX NO 8

Composite 3D transformations

AIM: To write a program for implementing translation , rotation & scaling the object using 3D-composite transformation ALGORITHM: Start the program.

Declare the variables.

Declare the following functions. transform()

rotate() scale()

Translate the object one co-ordinate to another using transform() function and rotate the object using rotate function.

Translate the object one co-ordinate to another using transform() function and change the size of object using scale() function.

Change the size of object using scale() function and translate the object one co-ordinate to another using transform() function

Display the object.

Stop the program.

PROGARM: #include<stdio.h> #include<stdlib.h> #include<conio.h> #include<math.h> #include<graphics.h> float tx,ty,tz,sx,sy,sz,r; float x[15][15],y[15][15],z[15][15]; int i,j=2,ch; void transform() { x[i][j-1]=x[i][j-1]+tx; x[i][j]=x[i][j]+tx; y[i][j-1]=y[i][j-1]+ty; y[i][j]=y[i][j]+ty; z[i][j-1]=z[i][j-1]+tz; z[i][j]=z[i][j]+tz; } void rotate() { if(i<=8) { x[i][j-1]=(x[i][j-1]*cos(r))-(y[i][j-1]*sin(r)); y[i][j-1]=(x[i][j-1]*sin(r))+(y[i][j-1]*cos(r));

x[i][j]=(x[i][j]*cos(r))-(y[i][j]*sin(r)); y[i][j]=(x[i][j]*sin(r))+(y[i][j]*cos(r)); } else { z[i][j-1]=(z[i][j-1]*cos(r))-(y[i][j-1]*sin(r)); y[i][j-1]=(z[i][j-1]*sin(r))+(y[i][j-1]*cos(r)); z[i][j]=(z[i][j]*cos(r))-(y[i][j]*sin(r)); y[i][j]=(z[i][j]*sin(r))+(y[i][j]*cos(r)); } } void scale() { x[i][j-1]=x[i][j-1]*sx; x[i][j]=x[i][j]*sx; y[i][j-1]=y[i][j-1]*sy; y[i][j]=y[i][j]*sy; z[i][j-1]=z[i][j-1]*sz; z[i][j]=z[i][j]*sz; } int main() { int gd=DETECT,gm; initgraph(&gd,&gm,"c:\\tc\\bgi"); x[1][1]=200,x[1][2]=250,y[1][1]=200,y[1][2]=200; x[2][1]=200,x[2][2]=200,y[2][1]=200,y[2][2]=150; x[3][1]=200,x[3][2]=250,y[3][1]=150,y[3][2]=150; x[4][1]=250,x[4][2]=250,y[4][1]=150,y[4][2]=200; x[5][1]=220,x[5][2]=270,y[5][1]=140,y[5][2]=140; x[6][1]=270,x[6][2]=270,y[6][1]=140,y[6][2]=190; x[7][1]=220,x[7][2]=220,y[7][1]=140,y[7][2]=190; x[8][1]=220,x[8][2]=270,y[8][1]=190,y[8][2]=190; z[9][1]=200,z[9][2]=220,y[9][1]=150,y[9][2]=140; z[10][1]=250,z[10][2]=270,y[10][1]=150,y[10][2]=140; z[11][1]=250,z[11][2]=270,y[11][1]=200,y[11][2]=190; z[12][1]=220,z[12][2]=200,y[12][1]=190,y[12][2]=200; textattr(0); for(i=1;i<=12;i++) { if(i<=8) line(x[i][j-1],y[i][j-1],x[i][j],y[i][j]); else line(z[i][j-1],y[i][j-1],z[i][j],y[i][j]); } while(ch!=4) { printf("\n 3D-COMPOSITE TRANSFORMATION"); printf("\n1.TRANSLATION & ROTATION"); printf("\n2.TRANSLATION & SCALING"); printf("\n3.SCALING & ROTATION"); printf("\n4.EXIT"); printf("\nENTER YOUR CHOICE:");

scanf("%d",&ch); switch(ch) { case 1: printf("\nENTER X Y & Z VALUES:"); scanf("%f%f%f",&tx,&ty,&tz); printf("\nENTER ROTATION ANGLE:"); scanf("%f",&r); clrscr(); r=(r*3.14/180); for(i=1;i<=12;i++) { transform(); rotate(); if(i<=8) line(x[i][j-1],y[i][j-1],x[i][j],y[i][j]); else line(z[i][j-1],y[i][j-1],z[i][j],y[i][j]); delay(500); } break; case 2: printf("\nENTER X Y & Z VALUES:"); scanf("%f%f%f",&tx,&ty,&tz); printf("\nENTER X Y & Z VECTORS:"); scanf("%f%f%f",&sx,&sy,&sz); clrscr(); for(i=1;i<=12;i++) { transform(); scale(); if(i<=8) line(x[i][j-1],y[i][j-1],x[i][j],y[i][j]); else line(z[i][j-1],y[i][j-1],z[i][j],y[i][j]); delay(500); } break; case 3: printf("\nENTER X Y & Z VECTORS:"); scanf("%f%f%f",&sx,&sy,&sz); printf("\nENTER ROTATION ANGLE:"); scanf("%f",&r); clrscr(); r=(r*3.14/180); for(i=1;i<=12;i++) { scale(); rotate(); if(i<=8) line(x[i][j-1],y[i][j-1],x[i][j],y[i][j]); else line(z[i][j-1],y[i][j-1],z[i][j],y[i][j]); delay(500);

} break; case 4: printf("\nEND OF PROGRAM"); exit(0); break; } } getch(); return 0; } OUTPUT: /* 3D-COMPOSITE TRANSFORMATION */ 1. TRANSLATION & ROTATION 2.TRANSLATION & SCALING 3.SCALING & ROTATION 4.EXIT ENTER YOUR CHOICE:

ENTER YOUR CHOICE:1 ENTER YOUR X,Y &Z VECTORS: 1 1 1 ENTER ROTATION ANGLE : 20

1. TRANSLATION & ROTATION 2.TRANSLATION & SCALING 3.SCALING & ROTATION 4.EXIT ENTER YOUR CHOICE:2

ENTER YOUR X ,Y & Z VALUES: 2 2 2 ENTER YOUR X ,Y & Z VECTORS : 3 2

1. TRANSLATION & ROTATION

2.TRANSLATION & SCALING 3.SCALING & ROTATION 4.EXIT ENTER YOUR CHOICE:3 ENTER YOUR X,Y & Z VECTORS :1.3 1.4 1.3 ENTER ROTATION ANGLE : 25

1. TRANSLATION & ROTATION 2.TRANSLATION & SCALING 3.SCALING & ROTATION 4.EXIT ENTER YOUR CHOICE:4 END OF THE PROGRAM RESULT: Thus the program has been executed and verified successfully.

EX NO 10

Generating Fractal images

AIM: To generate fractal images. ALGORITHM 1. The sierpinski Triangle is created by infinite removals 2. Each triangle is divided into 4 smaller upside down triangles 3. The center of the 4 triangles is removed 4. As the process is iterated infinite number of times, the total area of the set goes to infinity as the size of the each new triangle goes to zero 5. After closer examination magnification factor is 2. 6. With each magnification there are 3 divisions of a triangle Dimension D=ln(3)/ln(2) D=1.5850 Program
#include <stdio.h> #include <conio.h>

#include <stdlib.h> #include <math.h> #include <graphics.h> void DrawSierpinski(void); void main(void) { 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; } Output : }