CG

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 18

SYLLABUS COMPUTER GRAPHICS LAB 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. TOTAL = 45 PERIODS

EX NO 1a AIM

DDA ALGORITHM

To write a C program to draw a line using DDA algorithm. The digital differential analyzer is a scan conversion algorithm based on calculation either y or x using the following equations y = mx x = y / m Sample the line at unit intervals in one coordinate and determine corresponding integer values nearest the line path for the other coordinate. Sample at X intervals (x = 1) and compute each successive Y value as Yk+1 = Yk + m For lines with positive slope greater than 1, reverse the roles of X and Y. Sample at unit Y intervals (y = 1)and calculate each successive X value as Xk+1 = Xk + 1/m Algorithm Step 1: Input the line endpoints and store the left endpoint in (x1, y1) and right endpoint in (x2, y2) Step 2: Calculate the values of x and y using x = xb xa, y = yb ya Step 3: if the values of x > y assign values of steps as x otherwise the values of steps as y Step 4: Calculate the values of X increment and Y increment and assign the value x= xa and y = ya Step 5: for k=1 to steps do X = X + X increment Y= Y + Y increment Putpixel(ceil(x), ceil(y),15)

EX NO 1b 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(X 0, 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

EX NO 1c AIM

MIDPOINT CIRCLE DRAWING ALGORITHM

To write a C program to draw a circle using Bresenhams algorithm.

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

EX NO 1d AIM

MIDPOINT ELLIPSE DRAWING ALGORITHM

To write a C program to draw an ellipse using midpoint ellipse algorithm. Algorithm Step 1: Input radius rx, ry and ellipse center (Xc, Yc) and obtain the first point on the circumference of a circle centered on the origin as (X0, Y0) = (0, ry) Step 2: Calculate the initial values of the decision parameter in region 1 as

P10 = r2y r2x ry + 1/4 r2x Step 3: At each Xk position in region 1, starting at k = 0, perform the following test: If P1k < 0, the next point to plot is (Xk+1, Yk) and P1k+1 = P1k+2 r2yXk+1 + r2y Otherwise the next point is (Xk+1, Yk-1) and P1k+1 = P1k+2 r2yXk+1 - 2r2xYk+1 + r2y With 2 r2yXk+1=2 r2yXk+ 2r2y 2r2xYk+1=2r2xYk- 2r2x Step 4: Calculate the initial values of the decision parameter in region 2 as P20 = r2y(X0+1/2)2+ r2x(Y0 1)2- r2x r2y Step 5: At each position starting at Yk position in region 2, starting at k = 0, perform the following test: If P2k > 0, the next point to plot is (Xk, Yk-1) and P2k+1 = P2k - 2 r2yYk+1 + r2x Otherwise the next point is (Xk+1, Yk-1) and P2k+1 = P2k - 2 r2yXk+1 - 2r2xYk+1 + r2x Step 6: Determine symmetry points in the other three octants Step 7: 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 8: Repeat steps for region 1 until 2 r2yX>=2 r2xY

EX NO 2 IMPLEMENTATION OF LINE,CIRCLE & ELLIPSE ATTRIBUTES Aim: To write a C Program to display the various attributes of line, circle and ellipse. 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. Include the various attributes of line, circle and ellipse. 7. close the graph and run the program. 8. stop the program. #include <graphics.h> #include <stdlib.h> #include <string.h> #include <stdio.h> #include <conio.h> /* the names of the line styles supported */ char *lname[] = { "SOLID_LINE", "DOTTED_LINE", "CENTER_LINE", "DASHED_LINE", "USERBIT_LINE" }; int main(void) { /* request auto detection */ int gdriver = DETECT, gmode, errorcode; int style, midx, midy, userpat; char stylestr[40]; /* initialize graphics and local variables */ initgraph(&gdriver, &gmode, ""); /* read result of initialization */ errorcode = graphresult(); if (errorcode != grOk) /* an error occurred */

{ printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); /* terminate with an error code */ } midx = getmaxx() / 2; midy = getmaxy() / 2; /* a user defined line pattern */ /* binary: "0000000000000001" */ userpat = 1; for (style=SOLID_LINE; style<=USERBIT_LINE; style++) { /* select the line style */ setlinestyle(style, userpat, 1); /* convert style into a string */ strcpy(stylestr, lname[style]); /* draw a line */ line(0, 0, midx-10, midy); /* draw a rectangle */ rectangle(0, 0, getmaxx(), getmaxy()); /* output a message */ outtextxy(midx, midy, stylestr); /* wait for a key */ getch(); cleardevice();

} /* clean up */ closegraph(); return 0; } #include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> /* the names of the text styles supported */ char *fname[] = { "DEFAULT font", "TRIPLEX font", "SMALL font", "SANS SERIF font", "GOTHIC font" }; int main(void) { /* request auto detection */ int gdriver = DETECT, gmode, errorcode; int style, midx, midy; int size = 1; /* initialize graphics and local variables */ initgraph(&gdriver, &gmode, ""); /* read result of initialization */ errorcode = graphresult(); if (errorcode != grOk) /* an error occurred */ { printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); /* terminate with an error code */ } midx = getmaxx() / 2; midy = getmaxy() / 2; settextjustify(CENTER_TEXT, CENTER_TEXT); /* loop through the available text styles */ for (style=DEFAULT_FONT; style<=GOTHIC_FONT; style++)

{ cleardevice(); if (style == TRIPLEX_FONT) size = 4; /* select the text style */ settextstyle(style, HORIZ_DIR, size); /* output a message */ outtextxy(midx, midy, fname[style]); getch(); } /* clean up */ closegraph(); return 0; } EX NO 3a AIM To perform the various 2-dimensional transformations such as translation, rotation. Algorithm Step 1: Input the figure. Step 2: Display the menu as 1.Translation 2.Rotation 3.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 rotation angle. Rotate the figure with respect to the specified angle. Step 9: If choice is 3 exit the program. 2 -D TRANFORMATIONS- TRANSLATION & ROTATION

EX NO 3b AIM

2 DIMENSIONAL TRANFORMATIONS

To perform the various 2-dimensional transformations such as scaling, reflection and shearing. Algorithm Step 1: Input the figure. Step 2: Display the menu as 1.Scaling 2.Reflection 3.Shearing 4.Exit Step 3: Get the choice from the user. Step 4: If the choice is 1 get the scaling factor. Multiply the coordinate values of each vertex by scaling factors to produce the transformed coordinates. Step 5: If the choice is 2 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 6: If the choice is 3 shearing is done which causes the transformation that distorts the shape of an object. Step 7: If choice is 4 exit the program.

EX NO 4

2 DIMENSIONAL COMPOSITE TRANSFORMATION

(REFER UR OBSERVATION )

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 x = x1 + y y1 m


y = yw y = yw

min

max

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.

EX NO 6 AIM:

SUTHERLAND HODGEMAN ALGORITHM

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

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. EX NO 8 AIM To perform the various 3-dimensional transformations such as translation, scaling, rotation. Algorithm A rotation matrix for any axis that does not coincide with a coordinate axis can be set up as a composite transformation involving combination of translations and the coordinate-axes rotations. 1. Translate the object so that the rotation axis passes through the coordinate origin 2. Rotate the object so that the axis rotation coincides with one of the coordinate axes 3. Perform the specified rotation about that coordinate axis 4. Apply inverse rotation axis back to its original orientation 5. Apply the inverse translation to bring the rotation axis back to its original position ... COMPOSITE TRANSFORMATIONS

STUDY OF OPENGL Example 1 /* This program draws a Line.*/ #include <glut.h> /* glut.h includes gl.h and glu.h*/ void display() { /* clear window */ glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_LINES); glVertex2f(100, 100);//line start point glVertex2f(200, 200);// line end point glEnd(); /* flush GL buffers */ glFlush(); } void init() { glClearColor(1.0,1.0,1.0,0); glColor3f(0.0,0.0,0.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0 , 640 , 0 , 480); } void main(int argc, char** argv) { /* Initialize mode and open a window in upper left corner of /* screen */ /* Window title is name of program (arg[0]) */ glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(640, 480); glutInitWindowPosition(0, 0); glutCreateWindow("simple"); glutDisplayFunc(display); init(); glutMainLoop(); } Example 2 #include <iostream.h> #include <math.h> #include <glut.h> GLdouble X1, Y1, X2, Y2;

void LineDDA(void) { GLdouble dx=X2-X1 , dy=Y2-Y1,steps; float xInc,yInc,x=X1,y=Y1; steps=(abs(dx)>abs(dy))?abs(dx):abs(dy); xInc=dx/(float)steps; yInc=dy/(float)steps; glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_POINTS); glVertex2d(x,y); for(int k=0;k<steps;k++) { x+=xInc; y+=yInc; glVertex2d(x,y); } glEnd(); glFlush(); } void Init() { glClearColor(1.0,1.0,1.0,0); glColor3f(0.0,0.0,0.0); glViewport(0 , 0 , 640 , 480); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0 , 640 , 0 , 480); } void main(int argc, char **argv) { cout<<"Enter Two Points for Draw LineDDA:\n"; cout<<"\nEnter Point1( X1 , Y1):"; cin>>X1>>Y1; cout<<"\nEnter Point2( X2 , Y2):"; cin>>X2>>Y2; glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(640,480); glutInitWindowPosition(0,0); glutCreateWindow("LineDDA"); Init(); glutDisplayFunc(LineDDA); glutMainLoop();

You might also like