0% found this document useful (0 votes)
9 views

Study session 2 CSC 433

Done

Uploaded by

projectprinting1
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Study session 2 CSC 433

Done

Uploaded by

projectprinting1
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

CSC 433: Computer Graphic and Visualization

Study Session 2: Line and Circle Generation Algorithm

Introduction
In this study session, you will learn about line and circle algorithm such as Digital Differential
Analyzer (DDA) algorithm, the Bresenham algorithm, mid-point algorithm

In a line generation algorithm, a line connects two points. It is a basic element in graphics. To draw
a line, you need two points between which you can draw a line. In the following three algorithms,
we refer the one point of line as X0, Y0 and the second point of line as X1, Y1.
In a circle generating algorithm, drawing a circle on the screen is a little complex than drawing a
line. There are two popular algorithms for generating a circle: Bresenham’s Algorithm and
Midpoint Circle Algorithm. These algorithms are based on the idea of determining the subsequent
points required to draw the circle.

Learning Outcomes for Study Session 2


On completion of this study session, you should be able to:

1. Discuss briefly line generation algorithm

2. Explain briefly circle generation algorithm

Page 1 of 16
CSC 433: Computer Graphic and Visualization

1. Line Generation Algorithm


Line generating algorithm comprises of the following:

i. Digital Differential Analyzer (DDA) algorithm


ii. Bresenham algorithm
iii. Mid-point algorithm

2.1.1 Digital Differential Analyser (DDA) Algorithm


Digital Differential Analyzer (DDA) algorithm is the simple line generation algorithm which is
explained step by step here.
Step 1: Get the input of two end points (X0, Y0) and (X1, Y1).
Step 2: Calculate the difference between two end points.

Box 2.1: Difference between two end points

dx = X1 - X0

dy = Y1 -

Y0

Step 3: Based on the calculated difference in step-2, you need to identify the number of steps to put
pixel. If dx > dy, then you need more steps in x coordinate; otherwise in y coordinate.

Box 2.2: Step 3

if (dx > dy)

Steps = absolute(dx);

else

Steps = absolute(dy);

Page 2 of 16
CSC 433: Computer Graphic and Visualization

Step 4: Calculate the increment in x coordinate and y coordinate

X increment = dx / (float) steps;

Y increment = dy / (float)

steps;

Step 5: Put the pixel by successfully incrementing x and y coordinates accordingly and complete
the drawing of the line

for(int v=0; v < Steps; v++)

x = x + Xincrement;

y = y +

Yincrement;

putpixel(x,y);

}2.1.2 Bresenham Line Generation


The Bresenham algorithm is another incremental scan conversion algorithm. The big advantage of
this algorithm is that, it uses only integer calculations. Moving across the x axis in unit intervals
and at each step choose between two different y coordinates.

Page 3 of 16
CSC 433: Computer Graphic and Visualization

For example, as shown in the following illustration, from position (2, 3) you need to choose
between (3, 3) and (3, 4). You would like the point that is closer to the original line.

Figure 2.1: Bresenham Graph

At sample position xk+1, the vertical separations from the mathematical line are labelled as d upper
and d lower.

Page 4 of 16
CSC 433: Computer Graphic and Visualization

SOME TEXT ARE MISSING

2.1.3 Mid-Point Algorithm


Mid-point algorithm is due to Bresenham which was modified by Pitteway and Van Aken. Assume
that you have already put the point P at (x, y) coordinate and the slope of the line is 0 ≤ k ≤ 1 as
shown in the following illustration.

Now you need to decide whether to put the next point at E or N. This can be chosen by identifying
the intersection point Q closest to the point N or E. If the intersection point Q is closest to the point
N then N is considered as the next point; otherwise E.

Figure 2.2: Mid-point algorithm

To determine that, first calculate the mid-point M(x+1, y + ½). If the intersection point Q of the line
with the vertical line connecting E and N is below M then take E as the next point; otherwise take N
as the next point.

In order to check this, we need to consider the implicit equation:

F(x,y) = mx + b – y

For positive m at any given X,

 If y is on the line, then F(x, y) = 0

Page 5 of 16
CSC 433: Computer Graphic and Visualization

 If y is above the line, then F(x, y) < 0


 If y is below the line, then F(x, y) > 0

Figure 2.3: Mid-point graph

Pilot Question 2.1


1. What is the major advantage of Bresenham algorithm
2. Enumerate the three line generating algorithm

2.2 Circle Generation Algorithm


Drawing a circle on the screen is a little complex than drawing a line. There are two popular
algorithms for generating a circle: Bresenham’s Algorithm and Midpoint Circle Algorithm.
These algorithms are based on the idea of determining the subsequent points required to draw the
circle. Let us discuss the algorithms in detail:

The equation of circle is X2 + Y2 = r2, where r is radius.

Page 6 of 16
CSC 433: Computer Graphic and Visualization

Figure 2.4: Circle

2.2.1 Bresenham’s Algorithm


We cannot display a continuous arc on the raster display. Instead, we have to choose the nearest
pixel position to complete the arc. From the following illustration, you can see that we have put
the pixel at (X, Y) location and now need to decide where to put the next pixel: at N (X+1, Y) or
at S (X+1, Y-1).

Figure 2.4: Continuous arc

Page 7 of 16
CSC 433: Computer Graphic and Visualization

This can be decided by the decision parameter d.

 If d <= 0, then N(X+1, Y) is to be chosen as next pixel.


 If d > 0, then S(X+1, Y-1) is to be chosen as the next pixel.

Algorithm

Step 1: Get the coordinates of the center of the circle and radius, and store them in x, y, and R
respectively. Set P=0 and Q=R.

Step 2: Set decision parameter D = 3 – 2R. Step 3: Repeat through step-8 while X < Y. Step 4: Call
Draw Circle (X, Y, P, Q).

Step 5: Increment the value of P.

Step 6: If D < 0 then D = D + 4x + 6.

Step 7: Else Set Y = Y + 1, D = D +


4(X-Y) + 10.

Step 8: Call Draw Circle (X, Y, P, Q).


Draw Circle Method(X, Y, P, Q).

Call Putpixel (X + P, Y + Q).

Call Putpixel (X - P, Y + Q).

Call Putpixel (X + P, Y - Q).

Call Putpixel (X - P, Y - Q).

Call Putpixel (X + Q, Y + X).

Call Putpixel (X - Q, Y + X).

Call Putpixel (X + Q, Y - X).

Call Putpixel (X - Q, Y - X).

Page 8 of 16
CSC 433: Computer Graphic and Visualization

2.2.2 Mid-Point Algorithm


Step 1: Input radius r and circle center (xc, yc) and obtain the first point on the circumference of
the circle centred on the origin as

(x0, y0) = (0, r)

Step 2: Calculate the initial value of decision parameter as

P0 = 5/4 – r (See the following description for simplification of this equation.)

f(x, y) = x2 + y2 - r2 = 0 f(xi - 1/2 + e, yi + 1)

= (xi - 1/2 + e)2 + (yi + 1)2 - r2

= (xi- 1/2)2 + (yi + 1)2 - r2 + 2(xi - 1/2)e + e2

= f(xi - 1/2, yi + 1) + 2(xi - 1/2)e + e2 = 0

Page 9 of 16
CSC 433: Computer Graphic and Visualization

di+1 = di +
2xi+1 + 2yi+1 + 1

When point T = (xi, yi + 1) is chosen then

di+1 = di + 2yi+1 + 1

Step 3: At each XK position starting at K=0, perform the following


test:

Page 10 of 16
CSC 433: Computer Graphic and Visualization

Step 4: Determine the symmetry points in other seven octants.

Step 5: Move each calculate pixel position (X, Y) onto the circular path centered on

(XC, YC) and plot the coordinate values

X = X + XC,

Y = Y + YC

Step 6: Repeat step-3 through 5 until X >= Y.

Pilot Question 2.2


1. Circle generating algorithms are based on the idea of determining the points
required to draw the circle.
(a) Subsequent (b) Previous (c) mark (d) none of the above
2. Step 1of mid-point in cycle generating algorithm state that?

Page 11 of 16
CSC 433: Computer Graphic and Visualization

Summary of Study Session 2

In Study Session 2, you have learnt that:

1. There are two popular algorithms for generating a circle: Bresenham’s Algorithm and
Midpoint Circle Algorithm.
2. The full meaning of DDA algorithm is Digital Differential Analyzer
3. The Bresenham algorithm is another incremental scan conversion algorithm.
4. The big advantage of Bresenham algorithm is that, it uses only integer calculations 5.

Page 12 of 16
CSC 433: Computer Graphic and Visualization

Pilot Answer

Pilot Answer 2.1


1. The major advantage of Bresenham algorithm is that, it uses only integer calculations

2. Line generating algorithm comprises of the following:


i. Digital Differential Analyzer (DDA) algorithm
ii. Bresenham algorithm
iii. Mid-point algorithm

Pilot Answer 2.2


1. Circle generating algorithms are based on the idea of determining the
points required to draw the circle. Ans: Subsequent
2. Step 1: Input radius r and circle center (xc, yc) and obtain the first point on the
circumference of the circle centred on the origin as : (x0, y0) = (0, r)

Glossary of Terms

Page 13 of 16
CSC 433: Computer Graphic and Visualization

Self-Assessment Questions (SAQs) for Study Session 2


Now that you have completed this study session, you can assess how well you have achieved its
Learning Outcomes by answering the following questions. Write your answers in your Study Diary
and discuss them with your Tutor at the next Study Support Meeting. You can check your answers
with the Notes on the Self-Assessment Questions at the end of this Module.

SAQ 2.1 (tests Learning Outcome 2.1)


1. What is the major advantage of Bresenham algorithm
2. Enumerate the three line generating algorithm

SAQ 2.2 (tests Learning Outcome 2.2)


1. Circle generating algorithms are based on the idea of determining the points
required to draw the circle.
(b) Subsequent (b) Previous (c) mark (d) none of the above
2. Step 1of mid-point in cycle generating algorithm state that?

Page 14 of 16
CSC 433: Computer Graphic and Visualization

Notes on SAQs for Study Session 2

SAQ2.1
 The major advantage of Bresenham algorithm is that, it uses only integer calculations

 Line generating algorithm comprises of the following:


i. Digital Differential Analyzer (DDA) algorithm
ii. Bresenham algorithm
iii. Mid-point algorithm

SAQ 2.2

1. Circle generating algorithms are based on the idea of determining the


points required to draw the circle. Ans: Subsequent
2. Step 1: Input radius r and circle center (xc, yc) and obtain the first point on the
circumference of the circle centred on the origin as : (x0, y0) = (0, r)

Page 15 of 16
CSC 433: Computer Graphic and Visualization

REFERENCE
1. John F, Hughes, Andries Van Dam, Morgan McGuire(2013). Computer Graphics
principles
and practices. Mexico city: Printed in United State
2. www.tutorialpoint.com

Page 16 of 16

You might also like