L3 - Computer Graphics

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 34

Scan-Conversion of graphics

primitives
353 ‫عال‬
Lecture three

Dr. Majdi Abdellatief


Assistant Prof of Software Engineering
January 2018
Unit Structure
3.0 Objectives
3.1 Introduction
3.2 Scan-Conversion of a Lines
3.3 Scan- Conversion of Circle and Ellipse
3.3.1 Digital Differential Analyzer Algorithm
3.3.2 Bresenham's Line-Drawing Algorithm
3.4 Drawing Ellipses and Other Conics
3.4.1 Bresenham's Method of Circle Drawing
3.4.2 Midpoint Circle Algorithm
3.5 Drawing Ellipses and Other Conics
3.6 Let us sum up
3.7 References and Suggested Reading
OBJECTIVES

The objective of this chapter is


To understand the basic idea of scan conversion techniques.
To understand algorithms for scan conversion of line,
circle and other conics.

INTRODUCTION
Scan conversion or rasterization is the process of converting
the primitives from its geometric definition into a set of pixels that
make the primitive in image space. This technique is used to draw
shapes like line, circle, ellipse, etc. on the screen. Some of them
are discussed below
The process of representing continuous graphics objects as a
collection of discrete pixels is called scan conversion.
Drawing a Line Segment

The simplest way to represent a line segment is to have the coordinates of its
two end points. One way of drawing a line segment will be simply starting
with one end point and on the way of approaching the other, turning on
pixels.
This approach like our experience of drawing a line segment on paper, but it
poses two questions. First, on what criterion should we select a pixel? And
second, if the criterion is set, how do we efficiently select the required
pixels?
SCAN – CONVERSION OF LINES

A straight line can be represented by a slope intercept equation as


y = mx + b
where
m represents the slope of the line and b as the y intercept.

If two endpoints of the line are specified at positions (x ,y ) and (x ,y ),


1 1 2 2

the values of the slope m and intercept b can be determined as

b = y1 – m.x1
Digital Differential Analyzer (DDA) Algorithm

In any 2-Dimensional plane if we connect two points (X0, Y0)


and (X1, Y1), we get a line segment. But in the case of computer
graphics we can not directly join any two coordinate points, for
that we should calculate intermediate point’s coordinate and
put a pixel for each intermediate point, of the desired color
with help of functions like put pixel( x,y,k) where (x,y) is our co-
ordinate and K denotes some color.
DDAs are used for rasterization of lines, triangles and polygons
Digital Differential Analyzer (DDA) Algorithm
The general form of m =(Yk+1 - Yk )/ (Xk+1 – Xk )

If the slope is less than 1( |m| < 1), the coordinate x is


Case 1 sampled at unit intervals (∆x = 1), this means that Xk+1 =
Xk+1 and each successive values of y is computed as
YN = YO + m . . . Explain How ?
The value of y calculated is rounded off to the nearest integer value.

For slope greater than 1 ( |m| > 1), the roles of y and x are
Case 2 reversed, i.e. , y is sampled at unit intervals ((∆y = 1), this
means that Yk+1 = Yk+1 and corresponding x values are
calculated as XN = XO + 1/m Explain How ?
The value of x calculated is rounded off to the nearest integer value.

Case 3 If the slope is = to ( |m| = 1), ∆x = 1 and ∆y = 1


Digital Differential Analyzer (DDA) Algorithm

Example
Apply the DDL algorithm to find the appropriate coordinate to
draw a line between (2,1) and (5,5)
Example
Apply the DDL algorithm to find the appropriate coordinate to
draw a line between (2,1) and (5,5)
M>1
XN = XO + 1/m
Y= y+1

x R (x) y (x,y)
2 1 (2,1)
2.75 3 2 (3,2)
3.5 4 3 (4,3)
4.25 4 4 (4,4)
5 5 5 (5,5)
What will be happened if we reverse
the position of points ?
Pseudocode for DDA algorithm is as follows

Step 1 − Get the input of two end


points (X0,Y0) and (X1,Y1).
Step 2 − Calculate the slope (m)
Step 3 − if m<1,
Then x changes in unit intervals (Xk + 1)
Y moves with deviation (Yk + m)
Step 4 − if m>1,
Then X moves with deviation (Xk + 1/m)
Y changes in unit intervals (Yk + 1)
Step 5 − if m=1,
X and Y moves in unit intervals (Xk + 1) and (Yk + 1)
Bresenham’s Line Drawing Algorithm

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.
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

8 Xk+1 = Xk+1
7
Yk+1 = ?
6
5
4
d1 d2
3
2
1
y = mx + b
0 1 2 3 4 5 6 6 7 8
How Bresenham’s Line Drawing Algorithm work

Condition |m| <1


P0 = 2∆Y- ∆X
P0>= 0 P0<= 0
Next.point = (x+1, y+1) Next.point = (x+1, y)
PN = PO + 2∆Y - 2∆X PN = PO + 2∆Y

Example
Apply the Bresenham’s Line Drawing Algorithm to find the
appropriate coordinate to draw a line between (20,10) and
(30,18)
Example
Apply the Bresenham’s Line Drawing Algorithm to find the
appropriate coordinate to draw a line between (20,10) and
(30,18)
P0 = 6 >= 0 P0<= 0
Next.point = (x+1, y+1) Next.point = (x+1, y)
PN = PO + 2∆Y - 2∆X PN = PO + 2∆Y

2∆Y 2∆X 2∆Y - ∆X Next.Point

6 6 16-10 = 6 21 11
2 6 16 20 22 12
-2 2 16 20 23 12
14 -2 16 20 24 13
10 14 16 20 25 14
6 10 16 20 26 15
2 6 16 20 27 16
-2 2 16 20 28 16
14 -2 16 20 29 17
10 14 16 20 30 18
Bresenham’s Line Drawing Algorithm

For lines |m| > 1, the Bresenham’s line drawing algorithm


I. Read the end points of the line and store left point in (x0, y0)
II. Plot (x0, y0), the first point.
III. Calculate constants ∆x, ∆y, 2∆y and 2∆y - 2∆x, and obtain a
decision parameter p0
P0 = 2∆Y- ∆X
IV. Perform the following test for each x, starting form P0 if P0 <0, then
next plotting point is (x+1, y) and
PN = PO + 2∆Y
Otherwise, the next point to plot is (x+1, y+1) and
PN = PO + 2∆Y - 2∆X
V. Repeat step 4 ∆x times.
Part Two: SCAN – CONVERSION OF CIRCLE

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. Both of these algorithms uses the
key feature of circle that it is highly symmetric

for whole 360 degree of circle we will divide it in 8-parts each octant of 45
degree. In order to that we will use Bresenham’s Circle Algorithm for
calculation of the locations of the pixels in the first octant of 45 degrees. It
assumes that the circle is centered on the origin. So for every pixel (x, y) it
calculates, we draw a pixel in each of the 8 octants of the circle as shown
below. It can determine where to stop because when y = x, it has reached
45°
We cannot display a continuous arc on the raster display. Instead, we have to
choose the nearest pixel position to complete the arc. At any point (x,y), we
have two choices – to choose the pixel on M3(x+1,y) or the pixel M2(x+1,y-1)
SCAN – CONVERSION OF CIRCLE- Bresenham’s Algorithm

we determine the errors involved with both d1 & d2 which


are f(d1) and f(d2) respectively and whichever gives the
lesser error, we choose that pixel.
Let d = f(d1) + f(d2), where d can be called as "decision
parameter",

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.
SCAN – CONVERSION OF CIRCLE- Bresenham’s Algorithm
now to draw the circle for a given radius ‘r’ and centre (xc, yc) We will
start from (0, r) and move in first quadrant till x=y (i.e. 45 degree). We
should start from listed initial condition:
d = 3 - (2 * r) x=0 y=r
1. Set initial values of (xc, yc) and (x, y)
2. Set decision parameter d to d = 3 – (2 * r).
3. Repeat steps 4 to 8 until x < = y
4. call drawCircle(int xc, int yc, int x, int y) function.
5. Increment value of x.
6. If d < 0, set d = d + (4*x) + 6
7. Else, set d = d + 4 * (x – y) + 10 and decrement y by 1.
8. call drawCircle(int xc, int yc, int x, int y) function

To see the Bresenham’s Algorithm in action lets use it to


draw a circle centered at (0,0) with radius 5

d d + (4*x) + 6 d + 4 * (x – y) + 10
H.W

Using the Bresenham’s Algorithm, draw a circle centered at


(0,0) with radius 10
Mid-Point Circle Algorithm
❖ Similarly to the case with lines, there is an incremental algorithm for
drawing circles – the mid-point circle algorithm.
❖ the midpoint circle algorithm is an algorithm used to determine the
points needed for rasterizing the circle

Assume that we have just plotted


point (xk , yk ) The next point is
a choice between (xk+1, yk ) and
(xk+1, yk -1) We would like to
choose the point that is nearest to
the actual circle So how do we
make this choice?
Mid-Point Circle Algorithm
Mid-Point Circle Algorithm
Mid-Point Circle Algorithm
Mid-Point Circle Algorithm
To see the mid-point circle algorithm in action lets use it
to draw a circle centered at (0,10) with radius 10
K P (x,y) Next Pk equation
K
0 -9 (1,10)
1 -6 (2,10)
2 -1 (3,10)
3 6 (4,9)
4 -3 (5,9)
5 8 (6,8)
6 5 (7,7)
7
8
9
Scan Converting an Ellipses

The ellipse is also a symmetric figure like a


circle, but it has four- way symmetry rather than
eight-way. Ellipse is the set of all sum of the distances from two fixed
points is constant.

Method of defining an Ellipses It is given by the equation:

where (h, k) = ellipse center


a = length of major axis
b = length of minor axis
Polar method of drawing an Ellipses :
The following equations define an ellipse trignometrically:

x = a cos(θ) + h and y = b sin(θ) + k

Where (x, y) = the current coordinates


a = length of major axis
b = length of minor axis
θ = current angle, The value of θ is varied from 0 to π/2 radians.
Check your progress

Describe scan conversion?


8. Explain briefly the DDA line drawing algorithm.
9. Explain the Bresenham’s line drawing algorithm with
example.
10. Discuss scan conversion of circle with Bresenham’s and
midpoint circle algorithms.

You might also like