CG_Class_Notes_Module-2_upto_Ellipse
CG_Class_Notes_Module-2_upto_Ellipse
Module - 2
Output Primitives
CSC305
By Prof. Sunil Katkar
Department of Computer Engineering, VCET
Module -2 Output Primitives
Objective
To emphasize on implementation aspect of Computer Graphics
Algorithms
.
Outcome
At the end of the course student will be able to:
apply scan conversions algorithms to draw point, line, circle, ellipse
and compare flood fill, boundary fill algorithms
Scan conversions of point
(DDA)
Digital Differential
Analyzer
Line Drawing
Algorithm
Bresenham’s Line
Drawing
Vector generation/Digital Differential
Analyzer (DDA)
DDA is a scan conversion line drawing algorithm.
It takes unit step with one coordinate and calculate the corresponding
integer value for the other coordinate.
If y = 1, then we have
yi+1 = yi + 1
and xi+1 = xi + (x2-x1/y2-y1)
DDA Algorithm
1. Read the end point co-ordinates of the line segment such that they
are not equal i.e. A(x1,y1) and B(x2,y2).
If they are equal then plot that point and exit.
2. Calculate
x = | x2 - x1|
y = | y2 - y1|
3. If | x | ≥ | y | , then
step = x
else
step = y
end if
4. x = (x2 - x1) / step
y = (y2 - y1) / step
DDA Algorithm
5. x = x1 + 0.5 * sign(x)
y = y1 + 0.5 * sign(y)
6. i = 1
while (i ≤ step)
{
x = x + x
y = y + y
plot (int x, int y)
i=i+1
}
7. Stop
Ex1- Consider the line from (0,0) to (4,6). Use the simple
DDA algorithm to rasterize this line.
# x1 = 0, y1 = 0
x2 = 4, y2 = 6
# x = | x2 - x1| = | 4 - 0 | = 4
y = | y2 - y1| = | 6 - 0 | = 6
# If | x | ≥ | y | , then
step = x
else
step = y
As | y | ≥ | x | i.e. 6 > 4
step = y = 6
# x = (x2 - x1) / step
= 4/6 = 0.667
y = (y2 - y1) / step
= 6/6 = 1
i.e. i = 1
while (i ≤ 6)
{ x = x + 0.667
y=y+1
plot (int x, int y)
i=i+1
}
Ex2- Consider the line from (0,0) to (- 6, -6). Use the
simple DDA algorithm to rasterize this line.
# x1 = 0, y1 = 0
x2 = - 6 , y2 = - 6
# x = | x2 - x1| = | - 6 - 0 | = 6
y = | y2 - y1| = | - 6 - 0 | = 6
# If | x | ≥ | y | , then
step = x
else
step = y
As | x | = | y | i.e. 6 = 6
step = x = 6
# x = (x2 - x1) / step
=-6/6=-1
y = (y2 - y1) / step
= - 6/6 = - 1
Disadvantages
Floating point arithmetic in DDA is still time-consuming.
The algorithm is orientation dependent and hence end point
accuracy is poor.
Bresenham’s Line Drawing Algorithm
Select the optimum raster locations to represent a straight line.
Algorithm always increments either x or y by one unit depending on
the slope of line.
The increment in the other variable is determined by examining the
distance between the actual line location and the nearest pixel.
This distance is called decision variable/parameter or the error.
Derivation of Bresenham’s Algorithm (m < 1)
If pk < 0, then
xnext = xk+1 = xk
ynext = yk+1 = yk + 1
pk+1 = pk + 2x
If pk ≥ 0, then
xnext = xk+1 = xk + 1
ynext = yk+1 = yk + 1
pk+1 = pk + 2x - 2y
Bresenham’s Algorithm for m ≥ 1
1. Read the end point co-ordinates of the line segment such that they
are not equal i.e. A(x1,y1) and B(x2,y2).
If they are equal then plot that point and exit.
2. Calculate
x = x2 - x1
y = y2 - y1
10 20 (10, 20)
1 6 11 21 2 (11, 21)
2 2 12 22 -2 (12, 22)
3 -2 12 23 14 (12, 23)
4 14 13 24 10 (13, 24)
5 10 14 25 6 (14, 25)
6 6 15 26 2 (15, 26)
7 2 16 27 -2 (16, 27)
8 -2 16 28 14 (16, 28)
9 14 17 29 10 (17, 29)
10 10 18 30 6 (18, 30)
11 > y
Circle Drawing
Any circle generating algorithm
uses Eight-way symmetry of a
circle to plot the eight points by
calculating the coordinates of
any one point.
putpixel (x + xc, y + yc,WHITE)
putpixel (x + xc, - y + yc)
putpixel (- x + xc, y + yc)
putpixel (- x + xc, - y + yc)
putpixel (y + xc, x + yc)
putpixel (y + xc, - x + yc)
putpixel (- y + xc, x + yc)
putpixel (- y + xc, - x + yc)
Midpoint Circle Drawing
It uses the eight-way symmetry of the circle.
It plots 1/8 part of the circle i.e. from 900 to 450, as shown
If pk < 0, then
mid-point is INSIDE the circle and the pixel at yk is closer to
the circle boundary.
Otherwise
mid-point is OUTSIDE or ON the circle boundary and the
pixel at yk-1 is closer to the circle boundary.
If pk < 0, then
xnext = xk+1 = xk + 1
ynext = yk+1 = yk
pk+1 = pk + 2xk + 3 + [yk+12 - yk2] - [yk+1 - yk]
pk+1 = pk + 2xk + 3 + [yk2 - yk2] - [yk - yk]
pk+1 = pk + 2xk + 3
If pk ≥ 0, then
xnext = xk+1 = xk + 1
ynext = yk+1 = yk - 1
pk+1 = pk + 2xk + 3 + [yk+12 - yk2] - [yk+1 - yk]
pk+1 = pk + 2xk + 3 + [(yk - 1)2 - yk2] - [yk - 1 - yk]
pk+1 = pk + 2xk + 3 + [(yk2 - 2 yk + 1 - yk2] - [yk - 1 - yk]
pk+1 = pk + 2xk + 3 - 2 yk + 1 + 1
pk+1 = pk + 2xk - 2 yk + 5
The starting point is at (0, r)
The initial value of decision parameter is obtained by evaluating the
circle function at the start position.
Now we have
pk = fcircle(x, y)
= fcircle(xk+1, yk - 1/2)
= fcircle(xk + 1, yk - 1/2)
p0 = fcircle(x0 + 1, y0 - 1/2)
# Radius of the circle, r = 8 and center of the circle, (xc, yc) = (0, 0).
# Radius of the circle, r = 10 and center of the circle, (xc, yc) = (0, 0).
To check for boundary point between region-1 and region-2, test the
value of the slope of the curve at each step.
pk+1’ = pk’ + 2 ry2 (xk + 1) + ry2 + rx2 [(yk+1 - 1/2)2 - (yk - 1/2)2]
1. Accept the radii of the ellipse ‘rx’ , ‘ry’ and center of the ellipse
(xc, yc)
2. Initialize the starting position as (x, y) = (0, ry)
3. Calculate the initial value of decision parameter for region-1 , p0’
as
p0’ = ry2 - rx2 ry + 1/4 rx2
4. At each xk position in region-1 starting at k = 0 perform the
following test
do
{ putpixel (x, y, COLOR)
If pk’ < 0
{ xnext = xk+1 = xk + 1
ynext = yk+1 = yk
pk+1’ = pk’ + 2 ry2 (xk + 1) + ry2
}
else
{ xnext = xk+1 = xk + 1
ynext = yk+1 = yk - 1
pk+1’ = pk’ + 2 ry2 (xk + 1) + ry2 - 2 rx2 (yk - 1)
}
} while (2 ry2 x < 2 rx2y)
5. Calculate the initial value of decision parameter for region-2 as
p0” = (x0 + 1/2)2 ry2 + (y0 - 1)2 rx2 - rx2. ry2
6. At each xk position in region-2 starting at k = 0 perform the
following test
do { putpixel (x, y, COLOR)
If pk’’ > 0 { xnext = xk+1 = xk
ynext = yk+1 = yk-1 = yk - 1
pk+1” = pk” - 2 rx2 yk + 3 rx2
}
else { xnext = xk+1 = xk + 1
ynext = yk+1 = yk-1 = yk - 1
pk+1” = pk” + 2ry2 [xk + 1] - 2 rx2 yk + 3 rx2
}
} while (y > 0)
7. Determine the symmetry points in the other three quadrant.
8. Translate each pixel position by (xc, yc)
x = xk+1 + xc
y = yk+1 + yc
putpixel (x, y, COLOR)
9. Stop
Ex1- Calculate the pixel positions along the ellipse path
with radii rx= 4, ry = 3 and centered at the origin using mid-
point ellipse drawing algorithm.
# Radii of the ellipse , rx= 4, ry = 3
and center of the circle, (xc, yc) = (0, 0).
# Initialize the starting position as (x, y) = (0, ry) = (0, 3)
# Initial value of decision parameter for region-1
p0’ = ry2 - rx2 ry + 1/4 rx2
= 32 - 42 . 3 + 1/4 42
= 9 - 48 + 4
p0’ = - 35
# At each xk position in region-1 starting at k = 0 perform the following
test
do { putpixel (x, y, COLOR)
If pk’ < 0 { xnext = xk+1 = xk + 1
ynext = yk+1 = yk
pk+1’ = pk’ + 2 (3)2 (xk + 1) + (3)2
pk+1’ = pk’ + 18 (xk + 1) + 9
}
else { xnext = xk+1 = xk + 1
ynext = yk+1 = yk - 1
pk+1’ = pk’ + 2 ry2 (xk + 1) + ry2 - 2 rx2 (yk + 1)
pk+1’ = pk’ + 2 (3)2 (xk + 1) + (3)2 - 2 (4)2 (yk + 1)
pk+1’ = pk’ + 18 (xk + 1) + 9 - 32 (yk - 1)
}
} while (2 (3)2 x < 2 (4)2y) i.e while (18x < 32y)
# If pk’ < 0, xnext = xk+1 = xk + 1
ynext = yk+1 = yk
pk+1’ = pk’ + 18 (xk + 1) + 9
else, xnext = xk+1 = xk + 1
ynext = yk+1 = yk - 1
pk+1’ = pk’ + 18 (xk + 1) + 9 - 32 (yk - 1)
k (xk, yk) pk (xk+1, yk-1) pk+1 Putpixel (int x, int y) 18x < 32y
k (xk, yk) pk (xk+1, yk-1) pk+1 Putpixel (int x, int y) 72x < 128y
0 (0, 6) - 332 (1, 6) -224 (1, 6) 72 < 768