SYLLABUS
CS2405 COMPUTER GRAPHICS LAB L T P C
0 0 3 2
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 1 OUTPUT PRIMITIVES
Aim:
To write a C Program to display the output primitives.
Algorithm:
1. Start the program .
2. Initialize the variables.
3. Call the initgraph() function
4. Set color for the output primitives.
5. Using Outtextxy() display the chosen particular primitives.
6. Using switch case mention the various primitives and their attributes.
7. The various primitives are arc, line ,circle, rectangle and ellipse.
8. close the graph and run the program.
9. stop the program.
EX NO 2a DDA ALGORITHM
AIM
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 = m∆x
∆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 2b BRESENHAM’S LINE DRAWING ALGORITHM
AIM
To write a C program to draw a line using Bresenham’s algorithm.
In Bresenham’s 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(Xk, 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, 2∆y, and 2∆y -2∆x, 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+2∆y
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 3 MIDPOINT CIRCLE DRAWING ALGORITHM
AIM
To write a C program to draw a circle using Bresenham’s 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 4 MIDPOINT ELLIPSE DRAWING ALGORITHM
AIM
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 5 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.
EX NO 6a 2 -D TRANFORMATIONS- TRANSLATION & ROTATION
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.
EX NO 6b 2 -DIMENSIONAL TRANFORMATIONS
AIM
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 7 COHEN-SUTHERLAND ALGORITHM
AIM:
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.
- 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
- Once we have established region codes for all line endpoints, we can quickly determine which lines are completely outside or inside the clip window.
- Lines that cannot be identified as completely inside or outside a clip window are checked for intersection with boundaries.
- Intersection points with a clipping boundary can be calculated using the slope-intercept form of the line equation.
- The y coordinate of the intersection point at vertical line
- The x coordinate of the intersection point at horizontal line
EX NO 8 WINDOW TO VIEWPORT MAPPING
AIM:
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 9 SUTHERLAND HODGEMAN ALGORITHM
AIM:
To clip a Polygon using Sutherland Hodgeman Algorithm.
Algorithm
- Input Coordinates of all vertices of the polygon
- Input coordinates of the clipping window
- Consider the left edge of the window
- Compare the vertices of each edge of the polygon , individually with the clipping plane
- 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
- 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
- Stop
EX NO 10 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 11 COMPOSITE TRANSFORMATIONS
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
...
EX NO 12 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();
}
***********************************************************