You are on page 1of 46

Computer Graphics: Lab Manual 2011-12

Savitribai phule Shikshan Prasarak Mandals

S.K.N.Sinhgad College of Engineering, Pandharpur

LAB MANUAL

Computer Graphics
Class: SE (CSE) (2011-12)

Prepared By
Prof. Prakash R. Gadekar

1 Dept.of CSE SKN Sinhgad College of Engineering,Pandharpur.

Computer Graphics: Lab Manual 2011-12

Savitribai Phule Shikshan Prasarak Mandals

S.K.N.Sinhgad College of Engineering, Pandharpur

CERTIFICATE
This is to certify that Mr. / Miss _________________________ of Class Second Year - CSE, Roll No. _________, Exam Seat No. _________, has completed all the Term Work / Practical work in the subject Computer Graphics, satisfactorily in the Department of Computer Science and Engineering as prescribed by University of Solapur in the academic year 2011-2012.

Subject In-charge

Head of Department

Principal

2 Dept.of CSE SKN Sinhgad College of Engineering,Pandharpur.

Computer Graphics: Lab Manual 2011-12

Savitribai phule Shikshan Prasarak Mandals

S.K.N.Sinhgad College of Engineering, Pandharpur INDEX


Sr. No. Title of Experiment Study of graphics in built function. Program for: DDA line drawing algorithm Program for: Bresenhams line drawing algorithm Program to implement different 2-D transformations Program for: Bresenhams circle generation algorithm Program to transform object for getting Orthographic projection of the object Program for polygon Filling : 1.Seed Fill Algorithm 2.Edge Fill Algorithm Program for Cohen-Sutherland 2-D Line Clipping Algorithm Program for the creation of Bezier Curve Date Page No. Remarks Sign of Teacher

01. 02. 03. 04. 05. 06. 07. 08. 09.

3 Dept.of CSE SKN Sinhgad College of Engineering,Pandharpur.

Computer Graphics: Lab Manual 2011-12

Lab Innovation
Sr. No. Title Date Page No. Remarks Sign of Teacher

Signature

4 Dept.of CSE SKN Sinhgad College of Engineering,Pandharpur.

Computer Graphics: Lab Manual 2011-12

Experiment No: - 1 Title Name Roll No Batch Date Marks : - Study of Graphics Function :::::Signature :-

5 Dept.of CSE SKN Sinhgad College of Engineering,Pandharpur.

Computer Graphics: Lab Manual 2011-12

Experiment No: - 1

Aim:-Develop a program using a graphics inbuilt function. Objectives:-Learn and understand the functions in graphics Library. Theory:1) Loading computer graphics
When we start with the turbo c editor. We first includes the graphics library to convert the text editor into graphics editor.To include the graphics library the steps are i) ii) iii) iv) v) Select the option menu from the menu bar Select linker option Select libraries from linker option Select graphics library from the libraries window Click on Ok.

2) Initialized the graphics Mode:Before writing the computer graphics program, first we initialize the graphics driver and graphics mode, for that purpose we use detectgraph() function and initgraph() function. Detectgraph():- determines the graphics driver and chooses mode by checking hardware. Detectgraph detects your system graphics adaptor and chooses the mode that provide the highest resolution for that adaptor. If no graphics hardware is detected, detectgraph sets *graphdriver to grNotDetected, and graphresults returns grNotDetected.The main reason to call detectgraph directly is to override the graphics mode that detectgraph recommends to initgraph. Syntax:- detectgraph(&gdriver, &gmode);

Initgraph():It initializes graphics system, to start the graphic system , you must first call initgraph(), initgraph initializes the graphic system by loading a graphics driver from disk, then putting into the graphics mode. Initgraph resets all graphic settings like color,palette, current position, viewport etc. to their defaults. Syntax:- initgraph(&gdriver,&gmode,C:\\tc\\bgi);

6 Dept.of CSE SKN Sinhgad College of Engineering,Pandharpur.

Computer Graphics: Lab Manual 2011-12 Bgi is a file that stores the graphics driver and for each bgi driver there are different graphics mode. In short, it provides graphics interface. Inbuilt functions :Following are inbuilt functions of graphics library:1) Line ():- Line () is used to draw a line. Line(x1,y1,x2,y2); 2)Circle(): is used to draw the circle. Syntax: circle(x1,y1,radius); 3)ellipse():- draws an elliptical arc. Syntax:- ellipse(x,y,stangle,endangle,xradius,yradius);

fillellipse():- draws and fills an ellipse.


Syntax:- fillellipse(x,y,xradius,yradius); 4)drawpoly() :- draws the outline of a polygon, draws a polygon using the current line style and color. Drawpoly(numpoints,*polypoints); Numpoints:- specifies number of points *polypoints :- points to a sequence of (numpoints*2) integers.

fillpoly():-draws and fills a polygon. It draws the outline of a polygon using the current line style
and color. 5)Line style:Line styles for getlinesettings and setlinestyle. SOLID_LINE DOTTED_LINE CENTER_LINE DASHED_LINE 0 Solid line 1 Dotted line 2 Centered line 3 Dashed line

USERBIT_LINE 4 User-defined line style 6)bar:- Draws a bar, bar draws a filled-in, rectangular, two-dimensional bar. Declaration: void far bar(int left, int top, int right, int bottom); 7 Dept.of CSE SKN Sinhgad College of Engineering,Pandharpur.

Computer Graphics: Lab Manual 2011-12 The bar is filled using the current fill pattern and fill color. bar doesnot outline the bar. To draw an outlined two-dimensional bar, use bar3d with depth = 0. 7) Fill patterns:- for getfillsettings and setfillstyle. EMPTY_FILL SOLID_FILL LINE_FILL 0 Background color 1 Solid fill 2 --3 4 , thick lines 5 , thick lines 6

LTSLASH_FILL SLASH_FILL BKSLASH_FILL LTBKSLASH_FILL HATCH_FILL XHATCH_FILL

7 Light hatch 8 Heavy crosshatch

INTERLEAVE_FILL 9 Interleaving lines WIDE_DOT_FILL 10 Widely spaced dots

CLOSE_DOT_FILL 11 Closely spaced dots USER_FILL 12 User-defined fill pattern

All but EMPTY_FILL fill with the current fill color. EMPTY_FILL uses the current background color. (left, top) the rectangle's upper left corner

(right, bottom) the rectangle's lower right corner The coordinates are in pixels.To draw an outlined two-dimensional bar, use bar3d with depth = 0. (left, top) the rectangle's upper left corner

(right, bottom) the rectangle's lower right corner The coordinates are in pixels.

8)bar3d:=Draws a 3-D bar. Declaration: void far bar3d(int left, int top, int right, int bottom, int depth, int topflag) bar3d draws a three-dimensional rectangular bar, then fills it using the current fill pattern and fill color. 8 Dept.of CSE SKN Sinhgad College of Engineering,Pandharpur.

Computer Graphics: Lab Manual 2011-12 The three-dimensional outline of the bar is drawn in the current line styleand color. depth topflag (left, top) Bar's depth in pixels Governs whether a three-dimensional top is put on the bar Rectangle's upper left corner

(right, bottom) Rectangle's lower right corner If topflag is non-zero, a top is put on the bar. If topflag is 0, no top is put on the bar: This makes it possible to stack several bars on top of oneanother. 9) Getpixel: -get the color of specified pixel Syntax: -usinged far getpixel(int x ,int y); Example: -pcolor = getpixel(100,100)

10) Outtext: -Display a string in the viewport Void far outtext(char far textstring); Example: - outtext(display text in graphics);

11) Putpixel: - plots a pixel at specified point. Syntax: void far putpixel (int x, int y,int pixelcolor); Example:putpixel(100,100,15);

12) Closegraph: shuts down the graphics mode Syntax: -void far close graph( void ); Example: - closegraph( );

Conclusion:-

9 Dept.of CSE SKN Sinhgad College of Engineering,Pandharpur.

Computer Graphics: Lab Manual 2011-12 Frequently Asked Questions:1) What is mean by Computer Graphics?

2) What is the need of Computer graphics?

3) What is interactive Computer graphics?

4) What are the different applications of Computer graphics?

5) What is the declaration for line? Describe each argument?

6) Why is it needed to load graphics library in Computer graphics?

10 Dept.of CSE SKN Sinhgad College of Engineering,Pandharpur.

Computer Graphics: Lab Manual 2011-12

7) What is the job of initgraph() and Detectgraph() functions?

8) What is the job of graphics drivers and modes in computer graphics?

9) How can you fill the color in the ellipse? Are there different methods for filling the color?

10) What are the different ways to fill any picture with pattern?

Design Problem:-

1) Draw a penguin with the graphics function. 2) Write a program to display a flag having more than 3 colors. 3) Write a program to moving ball screen saver.

11 Dept.of CSE SKN Sinhgad College of Engineering,Pandharpur.

Computer Graphics: Lab Manual 2011-12

Experiment No Title

:-2 :- DDA Line drawing algorithm

Name Roll No Batch Date Marks

:::::Signature :-

12 Dept.of CSE SKN Sinhgad College of Engineering,Pandharpur.

Computer Graphics: Lab Manual 2011-12

Experiment No: - 2

Problem Statement: - Write a program to develop a DDA line drawing algorithm Objective: - To draw a rasterized straight line by making use of DDA line drawing algorithm Theory:The process of turning on the pixel for a line segment is called Vector generation . Before discussing line drawing algorithm , it is useful to note the general requirements for such algorithm. This algorithm specify the desired characteristics of line: 1. The line should appear as a straight line and it should start and end accurately. 2. The line should be displayed with constant brightness along its length independent of its length and orientation. 3. The line should be drawn rapidly. Let us see the different lines drawn in following fig:Vertical line line

45

Horizantal Line 13 Dept.of CSE SKN Sinhgad College of Engineering,Pandharpur.

Computer Graphics: Lab Manual 2011-12

a) Vertical & Horizantal Line

b) 45 line

? ? ? ?

c) Line with other orientation

As shown in fig a) horizontal and vertical lines are straight and have same width. The 45 line is straight but its width is not constant. On the other hand , the line with any other orientation is neither straight nor has same width. Such a cases are due to the finite resolution of display and we have to accept approximate pixels in such situations in fig. c) The brightness of the line is dependent on the orientation of the line.we can observe that the effective spaceing between pixels for the 45 line is grater than for the vertical and horizontal lines. This will make the vertical and horizontal lines appear brighter than the 45 line. Complex calculations are required to provide equal brightness along lines of varying length and orientation. Therefore , to draw line rapidly some compromises are made such as, Calculate only an approximate line length Reduce the calculations using simple integer arithmetic Implement the result in hardware or firmware.

Considering the assumptions made most line drawing algorithms use incremental methods. In these methods line starts with the starting point. Then a fix increment is added to the current point to get the next point on the line. This continued till the end of line. DDA ( Digital Differential Analyzer)/ Vector generation algorithm:The vector generation algorithms which step along the line to determine the pixel which should be turned on are sometimes called Digital Differential Analyzer. As we know the slope of the straight line is given as
M = Y X = Y2-Y1 X2-X1 14 Dept.of CSE SKN Sinhgad College of Engineering,Pandharpur.

------------------- (1)

Computer Graphics: Lab Manual 2011-12

The above differential equation can be used to obtain a rasterized straight line. For any given X interval X along a line, we can compute the corresponding y interval Y from euation (1) as
Y2-Y1 Y = X2-X1 X ----------------------(2)

Similarly, we can obtain X interval X corresponding to a specified Y as,


X2-X1 X = Y2-Y1 Y

Once the intervals are known the values for next X and next Y on the straight line can be obtained as follows: Xi+1 = Xi + X -------(4) and Yi+1 = Yi + Y ------(5)

Put the values of X and Y in the equations (4) and (5) , then these equation represent a recursion for successive values of X and Y along the required line. Such a way of rasterizing a line is called Digital Differential Analyzer (DDA). For simple DDA either X or Y , whichever is larger, is chosen as one raster unit i.e, If |X| |Y| then X = 1 Else Y = 1. Let us see the algorithm for rasterizing a line.

Algorithm: 1) Read the line end points (x1,y1) and (x2,y2) such hat they are not equal.[If equal then plot that point and exit]
2) 3) X = |x2-x1| and Y = |y2-y1| If (X Y) then Length = X Else Length = Y End if X = (x2-x1) / length 15 Dept.of CSE SKN Sinhgad College of Engineering,Pandharpur.

4)

Computer Graphics: Lab Manual 2011-12 Y = (y2-y1) / length This makes either X or Y equal to 1 because length is either | x2-x1| or |y2-y1|. Therefore , the incremental value for eithr X or Y is one.

5)

X= X1+0.5*sign(X) Y=Y1+0.5*sign(Y) [Here , sign function makes the algorithm work in all quadrant. It returns -1,0,1 depending on whether its argument is <0, =0, >0 respectively. The factor 0.5 makes it possible to round the values in the integer functions rather than truncating them.] 6) i=1[begins the loop , in this loop points are plotted] while (i length) { Plot(Integer(x), Integer(y)) X=X+X;
Y=Y+Y; i=i+1

} 7)stop

Conclusion:-

Frequently Asked Question:1) Why the name DDA is given for line drawing algorithm?

2)Which type of line you will get with DDA line drawing algorithm?

16 Dept.of CSE SKN Sinhgad College of Engineering,Pandharpur.

Computer Graphics: Lab Manual 2011-12

3) What is one complete raster unit?

4) What is the use of Sign() function?

5) What is the use of floor() function?

6) What are the disadvantages of DDA Algorithm?

7) Define 1) Line

2) Line segment 3) Vector

17 Dept.of CSE SKN Sinhgad College of Engineering,Pandharpur.

Computer Graphics: Lab Manual 2011-12

Experiment No Title

:- 3 :- Bresenhams Line drawing algorithm

Name Roll No Batch Date Marks

:::::Signature :-

18 Dept.of CSE SKN Sinhgad College of Engineering,Pandharpur.

Computer Graphics: Lab Manual 2011-12

Experiment No: - 3

Problem Statement:- Write a program to develop a Bresenhams line drawing algorithm Objective:- To draw a rasterized straight line by making use of Bresenhams line drawing algorithm Theory: Bresenhams Line drawing algorithm:Bresenhams line algorithm uses only integer addition and substraction and multiplication by 2, and we know that the computer can perform the operations of integer addition and substraction very rapidly. The computer is also time-effiecient when performing integer multiplication by powers of 2. Therefore , it is an efficient metyhod for scan converting straight lines. The basic principle of bresenhams line algorithm is to select the optimum raster locations to represent a straight line. To accomplish this the algorithm always increments either x or y by one unit depending upon the slope of line . the increment I the othr variable is determined by examing the distance between the actual line location and the nearest pixel. This distance is called Decision variable or the error. This is as shown in the fig. as below:
D A 1 0 19 Dept.of CSE SKN Sinhgad College of Engineering,Pandharpur. X= 1

Computer Graphics: Lab Manual 2011-12

D D B B

DA Distance Above
DB Distance Below

As shown in gig, the line does not pass though all the raster points( pixels). It passes through raster point(0,0) and subsequently crosses three pixels. It is seen that the intercept of line with the line x = 1 is closer to the line y=0 i.e pixel (1,0) than to line y =1 i.e pixel(1,1). The intercept of the line with line x = 2 is close to the line y = 1 i.e pixel (2,1) than to the line y=0 i.e pixel(2,0). Hence , the raster point at (2,1) better represents the path of the line shown in fig. e = Db Da or Da - Db

Let us define e = Db-Da, now if e>0, then it implies that Db>Da i.e pixel above the line is closet to the true line if Db<Da (i.e e<0) then we can say that the pixel below the line is closet to the true line. Thus by checking only the sign of error term it is possible to determine the better pixel to represent the line path. The error term is initially set as e = 2y-x where, y = y2-y1 and x = x2-x1 Then according to the value of e following actions are taken. While(e>=0) { y = y+1 e = e-2 * x } x=x+1; e =e + 2*y where, e>=0 , error is initialized with e = e-2* x. this is continued till error is negative. In each iteration y is incremented by 1 . when e<0, error is initialized to e =e+2*y. in both the cases x is incremented by 1. Let us see the algorithm.

Algorithm:1. Read the line end points (x1,y1) and (x2,y2) such that they are not equal. 2. x = |x2-x1| and y = |y2-y1| 3. Initialize starting point
20 Dept.of CSE SKN Sinhgad College of Engineering,Pandharpur.

Computer Graphics: Lab Manual 2011-12

4. 5. 6. 7.

x = x1 y = y1 e = 2* y-x [initialize value of decision variable or error to compensate for nonzero intercepts] i=1 [initialize counter] plot(x,y) while(e>=0) { y = y+1; e = e-2 * x } x=x+1; e =e + 2*y

8. i =i+1 9. if(i<=x) then go to step 6 10. stop Conclusion:-

Frequently Asked Question:1) Define 1) Line 2) Line segment 3) Vector

2) What is the need of Bresenhams line drawing algorithm?

3) What is the difference between DDA Line generation algorithm & Bresenhams line generation algorithm?

21 Dept.of CSE SKN Sinhgad College of Engineering,Pandharpur.

Computer Graphics: Lab Manual 2011-12

4) justify why Bresenhams line Drawing algorithm is superior than DDA line Drawing algorithm?

Experiment No :- 4 Title Name Roll No Batch Date Marks :- Program to implement different 2D Transformations. :::::Signature :-

22 Dept.of CSE SKN Sinhgad College of Engineering,Pandharpur.

Computer Graphics: Lab Manual 2011-12

Experiment No: - 4 Problem Statement: - Program to implement different 2D Transformations Objective: - To study the basic need of 2Dtransformation with matrices by using the basic scaling, rotation , translation , x shear and y shear operations. Theory:A graphics system must allow the user to change the way object appears. In the real world , we frequently rearrange objects or look at them from different angles. The computer should do the same with images on the screen, or its orientation. It is much more sensible to make changes later to the objects original basic description. Implementing such a change is call a Transformation. Basically transformation means making changes or allowing modifications to the picture. we know that once the objects are created, the different applications may require the variations in these. In some cases, the object size may be large, which needs to be stored in compressed form. All these requirements suggest us that we need to transform these objects. This process is carried out by transformation. For performing the different 2D transformations we need some mathematical operations, out of that , we are considering matrices. Basically, transformation means change in image. We can modify the image by performing some basic transformations, such as: Scaling Rotation Translation Reflection Shear Transformation Now , we will see the details of all the transformations.

23 Dept.of CSE SKN Sinhgad College of Engineering,Pandharpur.

Computer Graphics: Lab Manual 2011-12

1. Scaling Transformation:Scaling is the process of changing the size of the object or image. Suppose we are having point p1 whose co-ordinates are (x1,y1) , then we can represent this point p1 in matrix form as, P1 = (x1,y1) = [x1 y1] Now suppose we want to multiply this point i.e one P1s matrix by some matix, say matrix T of dimension (2*2), then we will get resultant point P2 with dimension as (1*2) i.e P1 * T = P2 [ x1 y1] * T = [ x2 y2] Thus we can say, the matrix T gives the relation between original point P1 and the new point P2. But if the matrix T is identity matrix then point p2 will be the same as P1. i.e P1.* T = P2 1 0 P1 * = P2 0 1 1 [x1 y1] * 0 P2 = P1 But if the matrix T is not identity matrix , but it is 2 0 T= Then P2 will become 0 1 P1 * T = P2 2 [x1 y1] * 0 P2 = | 2x1 y1| 1
Before After

0 = [ x1 y1] 1

0 = | 2x1 y1|

i.e all x-co-ordinates will get double as compared to original one. But the y-co-ordinates remains same as original. See fig, it means the width of the object becomes double but height is not getting changed. If matrix T is i.e if all x co-ordinates becomes half 24 of the original and y co-ordinates Dept.of CSE SKN Sinhgad College of Engineering,Pandharpur. remains same as original

Computer Graphics: Lab Manual 2011-12

0.5 T= 0

0 Then P2 will become 1

P1 * T = P2 0.5 [x1 y1] * 0 1 0 = | 0.5x1 y1|

Before After P2 = | 0.5x1 y1| Similarly, we can change the height of image also by keeping width constant. In that case our transformation matrix will be, 1 0 T= 0 2

Like this we can get both width and height double also by using matrix T as 2 T= 0 2 In general term, we can say matrix S is a scaling matrix and Sx is scale factor for xcordinate and Sy will be the scale factor for y coordinate. Sx 0 S= 0 Sy When we scale image, all points will get changed except origin. 0

2. Rotation Transformation:For rotation we need some trigonometry. Suppose we have a point P1 = ( x1,y1) and we rotate it about the origin by an angle to get a new position P2 = (x2,y2). We wish to find the transformation which will change (x1,y1) into (x2,y2).
P2(x2,y2)

P1 * T = P2

P1(x1,y1)

Dept.of CSE

But before we can check any transformation to see if it is correct, we have to first know what (x2,y2) should be in terms of (x1,y1) and angle . To determine this we need trigonometric functions 25 line sin and cos. SKN Sinhgad College of Engineering,Pandharpur.

Computer Graphics: Lab Manual 2011-12 x

Let us say we have drawn a line from origin to point(x,y) of length L at an angle anticlockwise from x-axis , as shown in fig.
Y (X ,Y) L (0,0) X

The line segment L will have (0,0) and (x,y) as its two end points. We can say, L = (x+y)

Sin = Opposite side/hypotenuse = y/L = y/(x+y) Similarly, cos = adjacent side / hypotenuse = x/L = x/(x+y) If we consider length as exactly 1 then, Sin = y/1 = y Cos= x/1 = x Now we will determine the transformation matrix for rotation.
Y

Consider a point p1 as (1,0) ref to fig. after rotating this point p1 in anticlockwise by an angle , we will get point P2. Now coordinates of P2 will be
P1(x1,y1) = (1,0) X

(0,0) P2(x2,y2)

X2 = Cos Similarly, if current point P1 is (0,1). Ref to fig and if we rotate P1 in Y2= Sin then we will get P2 as ( - Cos , Sin). Here anticlockwise by angle distance of P1 from origin is 1 unit. L=1

P1(x1,y1) = (0,1)

As

Sin = opposite side / hypotenuse = -x/L = -x Cos = adjacent side / hypotenuse = y/L

as L = 1

as L = 1
26

Dept.of CSE

SKN Sinhgad College of= Engineering,Pandharpur. y

X = - Sin

and

y = cos

Computer Graphics: Lab Manual 2011-12

(0,0)

Coming back to original concept P1 * T = P2 Where, P1 is original point P2 is new point T is transformation matrix. For case 1 i.e when p1 = (1,0) P1* T = P2 1 0 * T = cos Sin Now we know that original point i.e P1 , we also know what will be new point i.e P2. Here we have to find what will be the transformation matrix , which will change P1 to P2. For simplicity let us assume that transformation matrix (T) as a b T= c d a b Now , 1 0* = a b c d cos = a and Sin = b

Similarly for case 2 i.e when P1 = ( 0,1) P1 * T = P2 0 1 * T = -Sin cos a b 0 1 * = c d c d c = -Sin d = cos Now , place the values of a ,b ,c d in transformation matrix.

27 Dept.of CSE SKN Sinhgad College of Engineering,Pandharpur.

Computer Graphics: Lab Manual 2011-12

a b c d =

cos Sin -Sin cos

For rotation in clockwise direction, the matrix will be cos Sin R = -Sin cos The sign of angle determines the direction of rotation. When angle is positive i.e when we are following the direction exactly reverse as that of the direction of rotating hands of a normal clock, then it is anticlockwise or sometimes it is called Counter Clockwise see in fig. And when angle is negative I.e when we are following the same path as that of hands of clock then it is called Clockwise, as shown in fig. Anticlockwise Clockwise

So rotate a point in clockwise direction our matrix will become, Cos(-) Sin(-) R= -Sin(-) cos(-) Here, as we are rotating in clockwise direction, so we are replaceing by . But cos(-)=cos() and sin(-) = -sin() The transformation matrix for rotation in clockwise direction will be Cos -Sin R= Sin cos The transformation matrix for roatation in anticlockwise direction will be as. Cos Sin R = -Sin cos

3. Translation :Moving the whole image is called Translation. We can easily shift the image by adding same value to all the points of image by which we want to shift the image.

Before

After
28

Dept.of CSE

SKN Sinhgad College of Engineering,Pandharpur.

Computer Graphics: Lab Manual 2011-12

Before

After

If we want to shift the image down by 5 unit then we have to add -5 to all the y values of that image Similarly, if want to shift image up by 5 units as shown in fig, then add 5 to all y values of the image. Same thing for x values. In general, to translate the image we have to add some value to old(x,y) values so that we will get new ( x,y) values. X2 = x1 + tx and y2 = y1 + ty Where, tx is translation factor for x-axis ty is translation factor for y axis Depending on values of tx and ty we are shifting image to new position. But we cannot represent translation by using matrix.

4. Reflection:a.Reflection at y-axis:Basically reflection at y-axis is similar to placing a mirror at y-axis and taking the mirror image of an object. If ww have appoint (x.y) then reflection at y-axis will become (-x,y) which is shown in fig. (y)
(y) (x,y) (-x) (0,0) (x) (-x) (-x,y) (x) (0,0)

(-y) Before

(-y) After

29 Dept.of CSE SKN Sinhgad College of Engineering,Pandharpur.

Computer Graphics: Lab Manual 2011-12

Here the magnitude of both x and y remains same. Only the sign of x co-ordinates gets changed. Now let us derive what will be the transformation matrix which gives us the desired result. Our basic transformation rule will be P1 * T = P2 Now , P1 = [x y] and we need P2 as [-x,y] (x y) * T = (- x y) Now what should be the transformation matrix T, to get the desired result. So the T will be -1 0 0 1 Let us check it out P1 * T = P2 (x y) * -1 0 = (- x y) 0 1 After multiplying the transformation matrix with the point P1 we will get the desired point P2. b.Reflection at x-axis:Similarly, the same rule you have to apply for 1) Reflection at X-axis and you will get the new point p2 as (x y) * 1 0 = [x -y] 0 -1 c. Reflection in the origin:We have seen reflection at x-axis and reflection at y-axis, if we want both these transformation simultaneously then, we have to apply reflection in the origin. It is a combination of both reflection at x-axis and reflection at y-axis. 1 0 -1 0 = -1 0 0 -1 * 0 1 0 -1 (x y) * -1 0 = (-x -y) 0 -1 Thus , in this we can perform the reflection transformation.

5. Shear transformation:The shear transformation means slanting the image. This shear transformation is of two types: Y-shear X-shear 1. Y-shear:In y-shear we are keeping x-co-ordinate values as it is and shifting the ycordinate values only, which result in tilting of horizontal lines see fig.
30 Dept.of CSE SKN Sinhgad College of Engineering,Pandharpur.

Computer Graphics: Lab Manual 2011-12

Before

After

If P is (x,y) , then X-co-ordinate of P2 must be same as x co-ordinate of P1 . But y co-ordinate of P2 should change. Transformation matrix for y-shear will be 1 a where a is constant factor, which will 0 1 Decide , how much to tilt. Now , P1 * T = P2 x y * 1 a = x x.a+y 0 1 So, x-co-ordinate of P2 are not changed but y-cordinate of P2 will be combination of constant value a and x and u values of P1. P2 = ( x x.a+y) So here we say that value of y will depend on value of x. 2. X-shear:X-shear preserves y co-ordinate values and shifts x co-ordinate values causing vertical line to tilt. See in fig. means vice versa we are doing here.

Before So , you will get, x y *

After

1 0 b 1

= x+yb

here, we have to say, the value of x will depend on value of y. Conclusion:-

Frequently Asked Question:1) Describe with respect to 2D transformation:- 1) scaling 2) Rotation 3) Translation?

31 Dept.of CSE SKN Sinhgad College of Engineering,Pandharpur.

Computer Graphics: Lab Manual 2011-12

2)What is mean by Transformation?

3) Compare homogeneous co-ordinate system and normalized co-ordinate system?

4) What is the need of homogeneous co-ordinates system?

5) What is composite transformation?

6) Explain the inverse transformation?

7) How can you perform homogeneous co-ordinates for scaling?

32 Dept.of CSE SKN Sinhgad College of Engineering,Pandharpur.

Computer Graphics: Lab Manual 2011-12

8) What are the different steps for rotation about an arbitrary axis?

9) What is mean by shear and reflection?

10) Explain reflection about line?

Design Problems: 1. Implement rotation about all co-ordinate axes transformation on square in 3D. 2. Implement rotation about any arbitrary axis transformation on square in 3D. 3. Implement scaling and translation transformation on square in 3D.

33 Dept.of CSE SKN Sinhgad College of Engineering,Pandharpur.

Computer Graphics: Lab Manual 2011-12

Experiment No :- 5 Title :- Bresenhams Circle Generation Algorithm

Name Roll No Batch Date Marks

:::::Signature :-

34 Dept.of CSE SKN Sinhgad College of Engineering,Pandharpur.

Computer Graphics: Lab Manual 2011-12

Experiment No:-5

Problem Statement:- Write a program to develop a Bresenhams Circle Generation Algorithm Objective:- To draw a rasterized straight line by making use of Bresenhams Circle Generation Algorithm Theory: Bresenhams Circle Generation Algorithm:The Bresenhams circle drawing algorithm considers the eight-way symmetry of the y circle to generate it. It plots th part of the circle, i.e from 90 to 45, as shown in fig. To achieve best approximation to the true circle we have to select those pixels in the raster that fall the least distance from the true circle. Ref to the fig, let us observe the 90 to 45 portion of the circle. It can be noticed that if points are generated from 90 to 45, each new point closet to the true circle can be found by applying either of the two options: Increment in positive x direction by one unit or Increment in positive x direction and negative y direction both by one unit. -y 90 0 45
+ x x

35 Dept.of CSE SKN Sinhgad College of Engineering,Pandharpur.

Computer Graphics: Lab Manual 2011-12

Let us assume that point P in fig as a last scan converted pixel. Now we have two options either to choose pixel A or Pixel B. The closer pixel amongst these two can be determined as follows: The distances of pixels A and B from the origin are given as, Da = (xi+1)+(yi) and Db = (xi+1)+(yi-1) Now the distances of pixels A and B from the true circle are given as, a = Da r and b = Db r

However, to avoid square root term in derivation of decision variable, i.e to simplify the computation and to make algorithm more efficient the a and b are defined as, a = Da - r b =Db-r.

36 Dept.of CSE SKN Sinhgad College of Engineering,Pandharpur.

Computer Graphics: Lab Manual 2011-12

from fig, we can observe that ais always positive and b always negative . Therefore, we can define Decision variable di as, di = a + b and we can say that , if di<0 i.e a< b then only x is incremented; otherwise x is incremented in positive direction and y is incremented in negative direction. In other words we can write, for di<0, for di >= 0 Xi+1 = Xi + 1 and Xi+1 = Xi + 1 and Yi+1 = Yi-1

The equation for di at starting point , i.e at x =0 and y=r can be simplified as follows, di = a + b = (Xi + 1) + (Yi) - r + ( Xi + 1 )+ (Yi-1) - r = ( 0+1) + (r) - r + (0+1) + (r-1) - r = 1+ r - r+ 1 + r - 2r + 1 - r = 3 2r Similarly, the equations for di+1 for both the cases are given as, For di<0 , di+1 = di + 4xi + 6 and For di<=0, di+1 = di + 4( xi-yi) + 10

Algorithm to plot 1/8 symmetry of circle :1. Read the radius r of the circle
2. d = 3- 2r 3. x = 0, y = r ( initialize starting point) 4. do { Putpixel(x,y,10); If(d<0) then { d=d + 4x + 6 } Else { D =d + 4(x-y) +10 y= y-1 } x = x+1 }while(x<y) 5. stop
37 Dept.of CSE SKN Sinhgad College of Engineering,Pandharpur.

Computer Graphics: Lab Manual 2011-12

The remaining part of circle can be drawn by reflecting point about y-axis , x-axis and about origin as shown in fig. Therefore, by adding seven more plot commands after the plot command in step 4 of the algorithm, the circle can be plotted . the remaining seven plot commands are: Plot(y,x) Plot(y,-x) Plot(x,-y) Plot(-x,-y) Plot(-y,-x) Plot(-y,x) and Plot(-x,y)

Conclusion:-

Frequently Asked Question:1) What is one complete raster unit?

2) What is the use of Sign() function?

3) What is the use of floor() function?

38 Dept.of CSE SKN Sinhgad College of Engineering,Pandharpur.

Computer Graphics: Lab Manual 2011-12

4) What is the need of Bresenhams Circle drawing algorithm?

9) What is the difference between DDA Circle generation algorithm & Bresenhams circle generation algorithm?

Design Problems:1) Design Isometric Cube by using complete straight line. 2) Write a program for animated circles.

Experiment No :- 6 Title Name Roll No Batch Date Marks :- Program for filling a polygon using scan-fill method :::::Signature :39 Dept.of CSE SKN Sinhgad College of Engineering,Pandharpur.

Computer Graphics: Lab Manual 2011-12

Experiment No: - 6 Problem Statement:- Program for filling a polygon using scan-fill method Objective:- To fill the polygoin using scan fill method of raster display. Theory:Recursive Algorithm for seed fill methods have got two difficulties: 1) The first difficulty is that if some inside pixels are already displayed in fill color then recursive branch terminates, leaving further internal pixels that are initially set to the fill color before applying the seed fill procedures. 2) Another difficulty with recursive seed fill method is that it cannot be used for large polygons .This is because recursive seed fill procedures require stacking of neighboring points and in case of large polygons stack space may be insufficient for stacking of neighboring points. To avoid this problem more efficient method can be used. Such method fills horizontal pixel spans across scan lines , instead of proceeding to 4-connected or 8-connected neighboring points. This is achieved by identifying the rightmost and leftmost pixels of the seed pixel and then drawing a horizontal line between these two boundary pixels. This procedure is repeated with changing the seed pixel above and below the line just drawn until complete polygon is filled. With this efficient method we have to stack only a beginning position for each horizontal

40 Dept.of CSE SKN Sinhgad College of Engineering,Pandharpur.

Computer Graphics: Lab Manual 2011-12

pixel span, instead of stacking all unprocessed neighboring positions around the current position. The fig.1. illustrates the scan line algorithm for filling of polygon. For each scan line crossing a polygon, this algorithm locates the intersection points of the scan line with the polygon edges. These intersections points are then sorted from left to right and the corresponding positions between each intersection pair are set to the specified fill color. In fig.1, we can see that there are two stretches of interior pixels from x = 6 to x = 9 and x = 12 to x = 15. The scan line algorithm first finds the largest and smallest y values of the polygon. It then starts with the largest y value and works its way down scanning from left to right, in the manner of a raster display.

Y Scan line

The important task in scan line algorithm is to find the intersection points of scan line X points are even, they are sorted from left to right, with the polygon boundary. When intersection paired and pixels between paired points are set to the fill colour. But in some cases intersection Fig .1 point is a vertex. When scan line intersects polygon vertex special handling is required to find the exact intersection points. To handle such cases, we must look at the other endpoints of the two line segments of the polygon which meet at this vertex. If these points lie on the same side of the scan line, then the point in question counts as an even number of intersections. If they lie on opposite sides of the scan line, then the point is counted as single intersection. We have seen that it is necessary to calculate x intersection points for scan line with every polygon side. We can simplify these calculations by using Coherence properties. A coherence property of a scene by which we can relate one part of the scene with the other parts of a scene. Here, we can use a slope of an edge as a coherence property. By using this property we can determine the x intersection value on the lower scan line if the x intersection value for current scan line is known. This is given as, Xi+1 = Xi - 1/m where, m is the slope of the edge.

As we scan from top to bottom value of y-co-ordinates between the two scan line changes by 1.
41 Dept.of CSE SKN Sinhgad College of Engineering,Pandharpur.

Computer Graphics: Lab Manual 2011-12

Yi+1 = Yi 1 Many times it is not necessary to compute the x intersections for scan line with every polygon side. We need to consider only the polygon sides with endpoints straddling the current scan line. See in fig:
Consider only theses sides

Scan line

Fig. consiser only the sides which intersect the scan line

It will be easier to identify which polygon sides should be tested for x-interception, if we first sort the sides in order of their maximum y value. Once the sides are sorted we can process the scan lines from the top of the polygon to its bottom producing an active edge list for each scan line crossing the polygon boundaries. The active edge list for scan line contains all edges crossed by that scan line. The fig show sorted edges of the polygon with active edges.

B C A

D G E F I Scan line H

BC BA DC DE AJ GF
Top Active edges

GH EF

Fig. sorted edges of the polygon with active edges

HI JI

Bottom

Dept.of CSE

Sorted List SKN Sinhgad College of Engineering,Pandharpur.

42

Computer Graphics: Lab Manual 2011-12

In summary, a scan line algorithm for filling a polygon begins by ordering the polygon sides on the largest y value. It beginsWith the largest y value and scans down the polygon. For each y , it determines which sides can be intersected and finds the x values of these intersection points. It then sorts, pairs and passes these x values to a line drawing routine. Algorithm for polygon filling using scan line conversion:Read n, the number of vertices of polygon Read x and y co-ordinates of all vertices in array X[n] and Y[n] Find Ymin and Ymax Store the initial x value (x1) y values y1 and y2 for two endpoints and x increment x 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 its minimum y co-ordinate. Sort the rows of array, edges[n][4] in descending order of y1 , descending order of y2 and ascending orer of x2. Set Y = Ymax Find the active edges and update active edge list: If( y>y2 and y<= y1) { Edge is active } Else { Edge is not active
43 Dept.of CSE SKN Sinhgad College of Engineering,Pandharpur.

1. 2. 3. 4.

5. 6. 7.

Computer Graphics: Lab Manual 2011-12

} 8. Compute the s intersects for all active edges for current y value [initially x-intersect is x1 and x intersects for successive y values can be given as Xi+1 = Xi +X Where, X = -1/m and m = y2-y1 / x2-x1 i.e slope of a line segment. 9. If x intersect is vertes 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 intersects in the xintersect[] array. 10. Sort x-intersct[] array in the ascending order 11. Extract pairs of intersects from the sorted x-intersect [] array 12. Pass pairs of x values to line drawing routine to draw corresponding line segments 13. Set y = y 1 14. Repeat steps 7 through 13 unitl y>= ymin 15. Stop In step 7, we have checked for y<=y1 and not simply y<y1. Hence step 9 a becomes redundant. Conclusion:-

Frequently Asked Question:1) What is polygon and Polyline?

2)what are the different types of polygon?

3) Which are the various approaches used to represent polygon?

44 Dept.of CSE SKN Sinhgad College of Engineering,Pandharpur.

Computer Graphics: Lab Manual 2011-12

3) Represent the polygon using display file structure?

5) Write down the different methods for testing a pixel inside of polygon?

6) Which are the different methods used for filling polygon?

7) Explain boundary fill method for polygon?

8) What is mean by Seed pixel and what is mean by four connected region and eight connected region?

9) Compare Seed fill , edge fill method and flood fill ?

10) What is the need of scan line algorithm?


45 Dept.of CSE SKN Sinhgad College of Engineering,Pandharpur.

Computer Graphics: Lab Manual 2011-12

Design Problems:1) Implement flood fill algorithm for polygon having 8 vertex. 2) Implement Boundary fill algorithm for polygon using seed pixel and 8-connected region. 3) Implement scan line algorithm for hexagon

46 Dept.of CSE SKN Sinhgad College of Engineering,Pandharpur.

You might also like