Professional Documents
Culture Documents
Cs2405 Cglab Manual Onlyalgorithms
Cs2405 Cglab Manual Onlyalgorithms
ENGINNERING
Prepared by:
SATHISHKUMAR.N
MAHALAKSHMI.S
Asst Prof/ CSE
LIST OF EXPERIMENT
COMPUTER GRAPHICS LABORATORY (CS2405)
1. To implement Bresenhams algorithms for line, circle and ellipse drawing
2. To implement the basic attributes of Line, Circle and Ellipse
3. To perform 2D Transformations such as translation, rotation, scaling,
reflection and shearing.
4. To perform composite 2D Transformations
5. To implement Cohen-Sutherland 2D clipping and window-viewport
mapping
6. To implement Sutherland Hodgeman Polygon clipping algorithm.
7. To perform 3D Transformations such as translation, rotation and scaling.
8. To perform composite 3D Transformations
9. To draw three Dimensional objects and scenes.
10. To generate fractal images
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 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.
EX.NO:1.1
BRESENHAMS LINE DRAWING
Aim:
To implement Bresenhams line drawing Algorithm for drawing lines.
Functions used:
line ()
initgraph ().
EX.NO:1.2
BRESENHAMS CIRCLE DRAWING
Aim:
To write a program to draw a circle using Bresenhams circle drawing
Algorithm.
Functions used:
Circle()
initgraph ().
Syntax:
Initgraph (gd,gm,path)
Putpixel ()
10
EX.NO:1.3
BRESENHAMS ELLIPSE DRAWING
Aim:
To write a program to draw a ellipse using Bresenhams ellipse drawing
Algorithm.
Functions used:
initgraph().
Putpixel ()
11
Step 1: Start
Step 2: Get the center point as(x1, y1)
Step 3: Get the length of semi-major, semi-minor axes as r1 & r2
Step 4: Calculate t=pi/180
Step 5: Initialise i=0;
Step 6: Compute d=i*t
Step 7: Compute x=x1+y1*sin(d), y=y1+r2*cos(d).
Step 8: Put a pixel on(x,y)
Step 9: Increment I by 1
Step 10: Repeat steps(6) to (9) until i<360
Step 11: Stop
EX.NO:2
IMPLEMENTATION OF LINE,CIRCLE & ELLIPSE
ATTRIBUTES
Aim:
To implement the basic attributes of Line, Circle and Ellipse
Procedure:
Implementation of Line,Circle & Ellipse attributes
(x,y) are the interior points, boundary is the boundary color and fill_color is the
color to be filled. Following is a recursive method for boundary fill.
1. present_color = getcolor() // a function which returns the current color of (x,y)
2. if present_color <> boundary and if present_color <> fill_color then repeat steps
3-7
3. set_pixel (x,y, fill_color)
4. call the algorithm recursively for points (x + 1, y)
5. call the algorithm recursively for points (x 1,y)
6. call the algorithm recursively for points (x,y + 1)
7. call the algorithm recursively for points (x,y - 1)
8. stop
Algorithm:
Line,Circle & Ellipse attributes
Step 1. Input n, number of vertices of polygon
Step 2. input x and y coordinated of all vertices i array x[n] and y[n]
Step 3. find ymin and yma x
12
Step 4. Store the initail x values(x1) y values y1 and y2 for two endpoints and x
increment Dx from
scan line to scan line for each edge in the array edges [n] [4] while doing this
check that y1 >
y2 , if not interchange y1 and y2 and corresponding x1 and x2 so that for each
edge , y1
represents its maximum y coordinate and y2 represents it minimum y
coordiante
Step 5. Sort the rows of array , edges [n] [4] in descending order of y1 ,descending
order of y2 and
ascending order of
Step 6. Set y = yma x
Step 7. Find the active edges and update active edge list:if( y > y2 and y<= y1 ) then
edge is
activeOtherwise edge is not active
Step 8. Compute the x intersects for all active edges for current y values [ initially
x-intersect is and x
intersects for successive y values can be given asxi+1 = x i + Dx Where Dx = 1/m and m= y2 y1 / x2 - x1 i.e slope of a line segment
Step 9. VertexxIf x intersects is vertex i.e. X-intersect = x1 and y = y1 then apply
vertex test to check
whether to consider one intersect or two intersects. Store all x-intersect in the xintersect [ ]
array
Step 10. Store x-intersect [ ] array in the ascending order
Step 11. Extract pairs of intersects from the sorted x-intersect [ ] array
Step 12. Pass pair of x values to line drawing routine to draw corresponding line
segments
13
EX.NO:3
2D TRANSFORMATION
Aim:
To perform the 2D transformation such as translation, rotation, scaling,
shearing and reflection.
Functions Used:
Line()
The function line() is used to draw a line from(x1,y1)to (x2,y2)
Syntax:
line (x1,y1,x2,y2)
initgraph().
This function takes thee arguments and they are
i).the video driver to be used (gd).
ii).the graphics mode (gm).
iii).the path name.
Syntax:
Initgraph(gd,gm,path)
Algorithm:
Step1. Declare the variables xa,ya,xa1,ya1 of array type.
Step2.Declare the variables gd,gm,n,i,op,tx,ty,xf,yf,rx,ry.
Step3. Initialise the graphics function.
Step4. Input the number of points.
Step5. Input the value of co-ordinate according to number of points.
Step6. Using switch statement selects the option to perform
translation, rotation, scaling, reflection and shearing.
Step7. Translation:
a).input the translation vector
b).add the translation vectors with the coordinates
xa1[i]=xa[i]=tx, ya1[i]=ya[i]=ty,
c).using the function line,display the object before and after translation.
Step8. Rotation:
a). input the rotation angle
b). using formula theta=(theta*3.14)/180
c).input the value of reference point
d). calculate new coordinate point using formula
xa1[i]=xf+(xa[i]-xf)*cos(theta)-(ya[i]-yf)*sin(theta),
ya1[i]=yf+(xa[i]-xf)*sin(theta)-(ya[i]-yf)*cos(theta),
14
e). using the function line,display the object before and after rotation.
Step9. Scaling:
a).input the scaling factor and reference point
b).calculate new coordinate point using formula
xa1[i]=(xa[i]*sx+rx*(1-sx),
ya1 [i] = (ya[i]*sy+ry*(1-sy)
c). using the function line, display the object before and after scaling.
Step10. Shearing:
a).input the shearing value and reference point.
b). input the shear direction x or y
i).if direction x
xa1[i]=xa[i]+shx*(ya[i]-yref)
ii).otherwise
ya1[i]=ya[i]+shy*(xa[i]-xref)
iii). using the function line, display the object before and after shearing.
Step11. Reflection:
a).display the object before reflection using the function line
b). display the object after reflection using the function line
Step12. Stop.
EX.NO:4
2D COMPOSITE TRANSFORMATION
15
Aim:
To perform the 2D composite transformation such as translation, rotation,
scaling, shearing and reflection.
FUNCTIONS USED:
Line()
The function line() is used to draw a line from(x1,y1)to (x2,y2)
Syntax:
line (x1,y1,x2,y2)
initgraph()
This function takes thee arguments and they are
i).the video driver to be used (gd).
ii).the graphics mode (gm).
iii).the path name.
Syntax:
Initgraph(gd,gm,path)
Algorithm:
Step 1. Additivity of successive translations
We want to translate a point P to P by T(dx1, dy1) and then to P by
another T(dx2, dy2)
On the other hand, we can define T21= T(dx1, dy1) T(dx2, dy2) first, then
apply T21 to P:
where
T21 = T ( d x 2 , d y 2 )T (d x1 , d y1 )
1 0 d x 2 1 0 d x1
= 0 1 d y 2 0 1 d y1
0 0 1 0 0 1
16
1 0 d x1 + d x 2
= 0 1 d y1 + d y 2
0 0
1
where
S 21 = S ( s x 2 , s y 2 ) S ( s x1 , s y1 )
s x 2
= 0
0
0
sy2
0
s x 2 * s x1
= 0
0
0 s x1
0 0
1 0
0
s y1
0
0
1
s y 2 * s y1
0
0
0
1
where
R21 = R (2 ) R (1 )
cos 2
= sin 2
0
sin 2
cos 2
0
cos(2 + 1 )
= sin(2 + 1 )
0 cos 1
0 sin 1
1 0
sin(2 + 1 )
cos(2 + 1 )
0
sin 1
cos 1
0
0
0
1
0
0
1
Step 4. Stop.
EX.NO:5
COHEN-SUTHERLAND 2D LINE CLIPPING AND WINDOWING
Aim:
To implement Cohen-Sutherland 2D line clipping and windowing algorithm.
Functions used:
17
Line()
The function line() is used to draw a line from(x1,y1)to (x2,y2)
Syntax:
line (x1,y1,x2,y2)
initgraph().
This function takes thee arguments and they are
i).the video driver to be used (gd).
ii).the graphics mode (gm).
iii).the path name.
Syntax:
Initgraph(gd,gm,path)
Setcolor().
This function changes the drawing colour.
Syntax:
Setcolor(value of the color)
Settextstyle().
The function settextstyle() is used to change the style of the text.
Syntax:
Settextstyle(font,direction,charsize)
Where font is the constant value or the font filename, direction is the
number either 0 or 1, which makes the output to display in horizontal, or in
vertical direction, charsize is the character size or magnification factor and it
varies from 1 to 10.
Outtext().
This function display a text message on upper left of the screen
Syntax:
Outtext(message);
Algorithm:
For each line segment
Step 1. compute clip codes
Step 2. if both are 0 0 0 0
accept line segment
else if c1 & c2 != 0
discard line segment
else /* c1 & c2 = 0 */
18
EX.NO:6
SUTHERLAND HODGEMAN POLYGON CLIPPING ALGORITHM
Aim:
To implement Sutherland Hodgeman Polygon clipping Algorithm
Functions used:
Line()
The function line() is used to draw a line from(x1,y1)to (x2,y2)
Syntax:
line (x1,y1,x2,y2)
initgraph().
19
Algorithm:
Step 1. If first point inside add. If outside, dont add
Step 2.Move around polygon from vi to vn and back to v1
Step 3.Check vi,vi+1 wrt the clip edge
Step 4. Need vi,vi+1s inside/outside status
Step 5.Add vertex one at a time.
There are 4 cases:
foreach polygon P P = P
foreach clipping edge (there are 4) {
Clip polygon P to clipping edge
foreach edge in polygon P
20
EX.NO:7
3D- TRANSFORMATION
Aim:
To perform 3D transformations such as translation, rotation and scaling.
Functions used:
Line()
The function line() is used to draw a line from(x1,y1)to (x2,y2)
Syntax:
line (x1,y1,x2,y2)
initgraph().
This function takes thee arguments and they are
i).the video driver to be used (gd).
ii).the graphics mode (gm).
iii).the path name.
Syntax:
Initgraph(gd,gm,path)
Algorithm:
Step 1. Create a class cube with function draw cube.
21
Step 2. Use the function draw cube to draw a cube using eight points by means of
functions line.
Step 3. Declare the variables x1, y1, x2, y2, x3, y3, in array type which of data type
int.
Step 4.Declare the variables theta,op,ch,tx,ty,sx,sy,sz,lz+xy,zf,i,x,y,z.
Step 5.Initialise graphics functions.
Step 6.Input the first point in the cube.
Step 7.Input the size of the edge.
Step 8.Create an object to call the function.
Step 9.Using switch operation select the operation to perform translation,
rotation,scaling.
Step 10.Translation
a).input the translation vectortx,ty,tz.
b).calculate points using formula
x3[i]=x1[i]+tx.
y3[i]=y1[i]+ty
z3[i]=z1[i]+tz.
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.
Step11. Rotation:
a). input the rotation angle
b). using formula theta=(theta*3.14)/180
c).input the direction in x,y,z axis
d). if the direction is along x axis,
x3[i]=x1[i].
y3[i]=y1[i]*cos(theta)-z1[i]*sin(theta),
y3[i]=y1[i]*sin(theta)-z1[i]*cos(theta),
if the direction is along yaxis,
y3[i]=y1[i].
z3[i]=z1[i]*cos(theta)-x1[i]*sin(theta),
x3[i]=z1[i]*sin(theta)-x1[i]*cos(theta),
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),
e).calculate the points using the formula
x4[i]=x3[i]+z3[i]/2
y4[i]=y3[i]+z3[i]/2
f). using the function line,display the object before and after rotation.
Step12. Scaling:
a).input the scaling factor and reference point
b).calculate coordinate 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
22
d). using the function line, display the object before and after scaling.
Step13. Stop.
EX.NO:8
3D- COMPOSITE TRANSFORMATION
Aim:
To perform 3D composite transformations such as translation, rotation and
scaling.
Functions used:
Line()
The function line() is used to draw a line from(x1,y1)to (x2,y2)
Syntax:
line (x1,y1,x2,y2)
initgraph().
This function takes thee arguments and they are
i).the video driver to be used (gd).
ii).the graphics mode (gm).
iii).the path name.
Syntax:
Initgraph(gd,gm,path)
Algorithm:
Step 1.Translate P1 to the origin.
Step 2. Rotate about the y axis such that P1P2 lies in the (y, z) plane.
Step 3. Rotate about the x axis such that P1P2 lies on the z axis.
Step 4. Rotate about the z axis such that P1P3 lies in the (y, z) plane.
23
Step 5. Stop
EX.NO:9
DRAWING THREE DIMENSIONAL OBJECTS AND SCENES
Aim:
To draw a 3 Dimensional objects and scenes
Algorithm:
Step 1. Start
Step 2. Create a World Transformation coordinates.
Step 3. Create a Camera Transformation coordinates.
Step 4. Create a Prespective Transformation coordinates.
Step 5. Stop.
24
EX.NO:10.1
GENERATING FRACTAL IMAGES
Aim:
To generate a Fractal Images.
Algorithm:
The fractals of interest in this chapter can all be generated by the following classes
of algorithms:
Step 1.Linear replacement mapping - Fractal curves such as the Koch and
Sierpinski curves are generated by successive refinement of a given line by some
generator function.
Step 2.Iterated function systems - Many natural objects such as ferns and trees can
be generated by the successive application of a series of contractive affine
transformations.
Step 3.Complex plane mapping - Mathematical objects such as the Julia and
Mandelbrot sets are generated by successive mapping on the complex plane.
Step 4.Stochastic processes - Mountain landscapes and other irregular objects are
generated by applying random processes within a recursive algorithm.
25
EX.NO:10.2
CONVERSION BETWEEN COLOR MODELS (HSV to RGB color model)
HSV to RGB color model
Aim:
To write a program to convert HSV color model to RGB color model.
Algorithm:
Step1: initialize the graphics function
Step2: get the value of h, s and v.
Step3: for a set of TRGB values
HSV point correspond to RGB color values is set for V.
Parameter S is determined as relative distance from V.
Parameter h is determined by calculating
aa=v*(1-s).
bb= v*(s*f).
cc= v*(1-s*(1-f)).
Step4: apply the value to the equation and determined the values r,g,b
26
EX.NO:10.3
CONVERSION BETWEEN COLOR MODELS (RGB to HSV color model)
RGB to HSV color model
Aim:
To write a program to convert RGB color model to HSV color model.
Algorithm:
Algorithm:
Step1: initialize the graphics function
Step2: get the value of r,g and b.
Step3:set pointer v as maximum value *v=max
Step4:set pointer S
*s=delta/max;
Step5:set pointer h as
*h=NO_HUE;
*h=(g-b)/delta;
Step6:a).if r has maximum value
*h=(g-b)/delta;
b). if g has maximum value
*h=2+(b-r)/delta;
c). if b has maximum value
*h=4+(r-g)/delta;
Step7: find the r,s,b and v.
EX.NO:10.4
ANIMATION OF AN OBJECT
27
Aim:
To perform Animation of an object using flash.
Procedure:
Frame by frame:
Step1: Create a new document in flash.
Step2: Determine the frame rate of the animation, if case of fluring ball animation
set frame set fps.
Step3: In frame 1 create a ball using the over tool and place it in a starting position.
draw the flying arm with the initial shapes and post.
Step4: Choose modify/frames/convert to key frames. The content of frame are
copied into new key frames.
Step5: change the fly arms to the second position
Step6: by step 4 and 5 create animation for each frames and play
Step7: Exit out of the program
Result:
Thus the Animation of an object using flash was done and output was
verified.
EX.NO:10.5
BASIC OPERATION ON IMAGES
28
Aim:
To perform basic operation on images using Adobe Photoshop.
Procedure:
Feathering:
Select the elliptical marques tool on duplicate
Right click on area and select feather
Set the feather radius.
Filtering:
Selection Tool:
This is used to select a portion of image and paste it in document.
Lasso Tool:
This is used to select the image in its shape.
Text Tool:
This is used to add text along with image.
Blur Tool:
This is used to change the image by its color, shape and dimension.
Painting Tool:
The powerful Photoshop paint engine lets you to simulate traditional
painting technique include charcoal, pastel and wet or dry brush effects.
Drawing Tool:
Draw resolution independent vector shapes instantly with the line,
rectangle, ellipse, polygon and custom shape tool.
Transformation Tools:
29
Scale, rotate, distor or skew engine easily. Apply the 3D transform filter to
simulate 3D effects such as tables and boxes. Using the liquefy command to
interactively push, pull, bucker or bloat an image
Sponge Tool:
The sponge tool subtly changes the color saturation of an area.in gray scale
mode,the tool increases or decreases contrast by moving gray level away from or
towards the middle gray.
Marques tools:
The marques tools let you to select rectangle, ellipse, rounded rectangular
and 1-pixel rows and columns by default, a selection border is dragged from its
corner.
Pen Tools:
The pen tools used in conjunction with the shape tools to create complex
shapes.
Result:
Thus the basic operations in animation using Flash was studied and
performed.
30