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

CG_Class_Notes_Module-2_upto_Ellipse

The document outlines the second module of a Computer Graphics course focused on output primitives and algorithms, specifically scan conversion and line drawing techniques. It details the Digital Differential Analyzer (DDA) algorithm for line drawing, including its steps, advantages, and limitations, as well as introducing Bresenham's Line Drawing Algorithm. The content aims to equip students with the ability to apply these algorithms for rasterizing graphic objects effectively.

Uploaded by

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

CG_Class_Notes_Module-2_upto_Ellipse

The document outlines the second module of a Computer Graphics course focused on output primitives and algorithms, specifically scan conversion and line drawing techniques. It details the Digital Differential Analyzer (DDA) algorithm for line drawing, including its steps, advantages, and limitations, as well as introducing Bresenham's Line Drawing Algorithm. The content aims to equip students with the ability to apply these algorithms for rasterizing graphic objects effectively.

Uploaded by

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

Computer Graphics

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

Pixel is the smallest addressable


screen element which we can
control by setting the intensity
and colour of the pixel.

The process of determining appropriate pixels for representing


graphic object is known as Rasterization.
The process of representing continuous graphic object as a collection
of discrete pixels is called scan conversion.
Line Drawing Algorithm
Properties of Good Line Drawing -
1. 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.

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

Slope of straight line is


m = y/x = y2-y1/x2-x1 --------------1
y = (y2-y1/x2-x1) * x --------------2
x = (x2-x1/y2-y1) * y --------------3
Now the values for next x and next y on the straight line can be
calculated as
xi+1 = xi + x
= xi + (x2-x1/y2-y1) * y --------------4
yi+1 = yi + y
= yi + (y2-y1/x2-x1) * x --------------5
The equations 4 and 5 represents a recursion relation for successive
values of x and y along the required line.
For DDA either x or y whichever is larger is chosen as one raster
unit.
If | x | ≥ | y | , then
x = 1
else
y = 1
With this simplification
If x = 1, then we have
yi+1 = yi + (y2-y1/x2-x1)
and xi+1 = xi + 1

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

# Now the initial values are


x = x1 + 0.5 * sign(x)
= 0 + 0.5 * sign(0.667)
= 0 + 0.5 * (+1)
= 0.5
y = y1 + 0.5 * sign(y)
= 0 + 0.5 * sign(1)
= 0 + 0.5 * (+1)
= 0.5
# i=1 i x y Putpixel (int x, int y)
while (i ≤ step) 0.5 0.5 (0, 0)
{ 1 1.167 1.5 (1,1)

x = x + x 2 1.833 2.5 (1, 2)

y = y + y 3 2.5 3.5 (2, 3)


4 3.167 4.5 (3, 4)
plot (int x, int y)
5 3.833 5.5 (3, 5)
i=i+1
} 6 4.499 6.5 (4, 6)

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

# Now the initial values are


x = x1 + 0.5 * sign(x)
= 0 + 0.5 * sign(-1)
= 0 + 0.5 * (-1)
= - 0.5
y = y1 + 0.5 * sign(y)
= 0 + 0.5 * sign(-1)
= 0 + 0.5 * (-1)
= - 0.5
i x y Putpixel (int x, int y)
# i=1
while (i ≤ step) - 0.5 - 0.5 (0, 0)
{ 1 - 1.5 - 1.5 (-1, -1)

x = x + x 2 - 2.5 - 2.5 (-2, -2)


3 - 3.5 - 3.5 (-3, -3)
y = y + y
4 - 4.5 - 4.5 (-4, -4)
putpixel(int x, int y, Colour) 5 - 5.5 - 6.5 (-5, -5)
i=i+1 6 - 6.5 - 6.5 (-6, -6)
}
i.e. i = 1
while (i ≤ 6)
{ x=x-1
y=y-1
putpixel (int x, int y, Colour)
i=i+1
}
Limitations of DDA algorithm
Advantages
 Simple algorithm
 Does not required special skills for implementation
 Faster method for calculating pixel positions than direct use of
equation y = mx + c.
 It eliminates the multiplication in the equation by making use of
raster characteristics, so that appropriate increments are applied in
the x and y direction to find the pixel positions along the line path.

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)

Let xk, yk be the selected positions along the line path.


The next positions (xnext , ynext) will be
xnext = xk+1 = xk + 1
ynext = yk+1 = yk or yk + 1
The equation of line is
y = mx + b
at x = xk + 1
y = m (xk + 1) + b
If d1 < d2 then yk is closer to the actual line path and yk+1 = yk
However if d2 < d1 then yk+1 is closer to the line path and yk+1 = yk + 1
Now
d2 = yk+1 - y
= yk + 1 - y
= yk + 1 - [m (xk + 1) + b]
= yk + 1 - m (xk + 1) - b
and
d1 = y - yk
= [m (xk + 1) + b] - yk
= m (xk + 1) + b - yk
Now d1 - d2 = m (xk + 1) + b - yk - [yk + 1 - m (xk + 1) - b]
= m (xk + 1) + b - yk - yk - 1 + m (xk + 1) + b
= 2m (xk + 1) - 2yk + 2b - 1
= 2 y/x (xk + 1) - 2yk + 2b - 1
x (d1 - d2) = 2 y (xk + 1) - 2 x yk + 2 x b - x
= 2 y xk + 2y - 2 x yk + 2 x b - x
Let the decision parameter is pk
pk = x (d1 - d2)
pk = 2 y xk + 2y - 2 x yk + 2 x b - x
 pk+1 = 2 y xk+1 + 2y - 2 x yk+1 + 2 x b - x
pk+1 - pk = 2 y xk+1 + 2y - 2 x yk+1 + 2 x b - x
- [2 y xk + 2y - 2 x yk + 2 x b - x]
= 2 y xk+1 + 2y - 2 x yk+1 + 2 x b - x
- 2 y xk - 2y + 2 x yk - 2 x b + x
= 2y [xk+1 - xk] - 2 x [yk+1 - yk]
 pk+1 = pk + 2y [xk+1 - xk] - 2 x [yk+1 - yk]
Now initial value of decision parameter (p0)
The equation of line is
y =mx+b
at k = 0, (xk, yk) = (x0, y0)
y0 = m x0 + b
b = y0 - m x0
b = y0 - (y/x) x0
Now we have decision parameter pk as
pk = 2 y xk + 2y - 2 x yk + 2 x b - x
 p0 = 2 y x0 + 2y - 2 x y0 + 2 x b - x
Substituting the value of b
p0 = 2 y x0 + 2y - 2 x y0 + 2 x [y0 - (y/x) x0 ] - x
p0 = 2 y x0 + 2y - 2 x y0 + 2 [y0 x - y x0 ] - x
p0 = 2 y x0 + 2y - 2 x y0 + 2 y0 x - 2 y x0 - x
p0 = 2 y x0 + 2y - 2 x y0 + 2 y0 x - 2 y x0 - x
p0 = 2y - x
If pk < 0, then
xnext = xk+1 = xk + 1
ynext = yk+1 = yk
pk+1 = pk + 2y [xk+1 - xk] - 2 x [yk+1 - yk]
pk+1 = pk + 2y [xk + 1 - xk] - 2 x [yk - yk]
pk+1 = pk + 2y
If pk ≥ 0, then
xnext = xk+1 = xk + 1
ynext = yk+1 = yk + 1
pk+1 = pk + 2y [xk+1 - xk] - 2 x [yk+1 - yk]
pk+1 = pk + 2y [xk + 1 - xk] - 2 x [yk + 1 - yk]
pk+1 = pk + 2y - 2 x
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
3. Initialize starting point
xnext = xk+1 = x1
ynext = yk+1 = y1
4. Calculate the initial value of decision parameter, p0 as
p0 = 2y - x
5. Initialize counter, k = 0
6. Plot (xnext , ynext) = (xk+1, yk+1)
5. At each xk position starting at k = 0 perform the following test -
If pk < 0, then
xnext = xk+1 = xk + 1
ynext = yk+1 = yk
pk+1 = pk + 2y
If pk ≥ 0, then
xnext = xk+1 = xk + 1
ynext = yk+1 = yk + 1
pk+1 = pk + 2y - 2 x
6. k = k + 1
7. Plot (xnext , ynext) = (xk+1, yk+1)
8. Repeat the steps 5 and 7 until
[ xk+1 = x2 or/and yk+1 = y2 ]
9. Stop
Derivation of Bresenham’s Algorithm (m ≥ 1)
Let xk, yk be the selected positions along the line path.
The next positions (xnext , ynext) will be
xnext = xk+1 = xk or xk + 1
ynext = yk+1 = yk + 1

If d1 < d2 then xk is closer to the actual line path and xk+1 = xk


However if d2 < d1 then xk+1 is closer to the line path & xk+1 = xk + 1
Now
d2 = xk+1 - x
= xk + 1 - x
and
d1 = x - xk
Initial value of decision parameter (p0) is
p0 = 2x - y

If pk < 0, then
xnext = xk+1 = xk
ynext = yk+1 = yk + 1
pk+1 = pk + 2x

If pk ≥ 0, then
xnext = xk+1 = xk + 1
ynext = yk+1 = yk + 1
pk+1 = pk + 2x - 2y
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

3. Calculate the initial value of decision parameter, p0 as


p0 = 2x - y
4. At each xk position starting at k = 0 perform the following test -
If pk < 0, then
xnext = xk+1 = xk
ynext = yk+1 = yk + 1
pk+1 = pk + 2x
If pk ≥ 0, then
xnext = xk+1 = xk + 1
ynext = yk+1 = yk + 1
pk+1 = pk + 2x - 2y
5. Display (xnext , ynext) = (xk+1, yk+1)
6. Repeat the steps 4 and 5 until
xk+1 = x2
yk+1 = y2
7. Stop
Ex1- Consider the line from (5,5) to (13,9).
Use the Bresenham’s algorithm to rasterize this line.
# x1 = 5, y1 = 5
x2 = 13, y2 = 9
# x = | x2 - x1| = | 13 - 5 | = 8
y = | y2 - y1| = | 9 - 5 | = 4
# Slope, m = y/x = 4/8 = 0.5 < 1
# Initialize starting point
xnext = xk+1 = x1 = 5
ynext = yk+1 = y1 = 5
# The initial value of decision parameter, p0 is
p0 = 2y - x
= 2*4 - 8
=8-8
p0 = 0
# At each xk position starting at k = 0 perform the following test -
If pk < 0, then
xnext = xk+1 = xk + 1
ynext = yk+1 = yk
pk+1 = pk + 2y
pk+1 = pk + 2y
pk+1 = pk + 2(4)
pk+1 = pk + 8
If pk ≥ 0, then
xnext = xk+1 = xk + 1
ynext = yk+1 = yk + 1
pk+1 = pk + 2y - 2 x
pk+1 = pk + 2(4) - 2 (8)
pk+1 = pk + 8 - 16
pk+1 = pk - 8
p0 = 0 If pk ≥ 0, then
If pk < 0, then xnext = xk+1 = xk + 1
xnext = xk+1 = xk + 1 ynext = yk+1 = yk + 1
ynext = yk+1 = yk pk+1 = pk - 8
pk+1 = pk + 8

k pk xnext = xk+1 ynext = yk+1 pk+1 Putpixel (int x, int y)


0 5 5 -- (5, 5)
1 0 6 6 -8 (6, 6)
2 -8 7 6 0 (7, 6)
3 0 8 7 -8 (8, 7)
4 -8 9 7 0 (9, 7)
5 0 10 8 -8 (10, 8)
6 -8 11 8 0 (11, 8)
7 0 12 9 -8 (12, 9)
8 -8 13 9 0 (13, 9)
k pk xnext = xk+1 ynext = yk+1 pk+1 Putpixel (int x, int y)
0 5 5 -- (5, 5)
1 0 6 6 -8 (6, 6)
2 -8 7 6 0 (7, 6)
3 0 8 7 -8 (8, 7)
4 -8 9 7 0 (9, 7)
5 0 10 8 -8 (10, 8)
6 -8 11 8 0 (11, 8)
7 0 12 9 -8 (12, 9)
8 -8 13 9 0 (13, 9)
Ex2- Consider the line from (10,20) to (18,30).
Use the Bresenham’s algorithm to rasterize this line.
# x1 = 10, y1 = 20
x2 = 18, y2 = 30
# x = | x2 - x1| = | 18 - 10 | = 8
y = | y2 - y1| = | 30 - 20 | = 10
# Slope, m = y/x = 10/8 = 1.25 > 1
# Initialize starting point
xnext = xk+1 = x1 = 10
ynext = yk+1 = y1 = 20
# The initial value of decision parameter, p0 is
p0 = 2x - y
= 2*8 - 10
= 16 - 10
p0 = 6
# At each xk position starting at k = 0 perform the following test -
If pk < 0, then
xnext = xk+1 = xk
ynext = yk+1 = yk + 1
pk+1 = pk + 2x
pk+1 = pk + 2(8)
pk+1 = pk + 16
If pk ≥ 0, then
xnext = xk+1 = xk + 1
ynext = yk+1 = yk + 1
pk+1 = pk + 2x - 2 y
pk+1 = pk + 2(8) - 2 (10)
pk+1 = pk + 16 - 20
pk+1 = pk - 4
k pk xnext = xk+1 ynext = yk+1 pk+1 Putpixel (int x, int y)

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

x moves in the positive direction


y moves in the negative direction
To draw 1/8 part of the circle we take unit step in the positive x-
direction and make use of decision parameter to determine which of
the two possible y-positions is closer to the circle path at each step.
fcircle(x, y) = x2 + y2 - r2 = 0
fcircle(x, y) = < 0
=0
=>0

Consider the pixel at (xk, yk).


The next pixel (xnext, ynext) along the circumference of a circle will be
either (xk+1, yk) OR (xk+1, yk-1) whichever is closer to the circle path
at each step.
Let the decision parameter pk is equal to the circle function evaluated
at the mid-point between two pixels.
pk = fcircle(x, y)
= fcircle(xk+1, yk - 1/2)
pk = fcircle(x, y)
= fcircle(xk+1, yk - 1/2)
= fcircle(xk + 1, yk - 1/2)
= (xk+1)2 + (yk - 1/2)2 - r2
= (xk + 1)2 + (yk - 1/2)2 - r2
pk = (xk )2 + (2 xk) + 1 + (yk )2 - (yk) +(1/4) - r2
Now
pk = fcircle(xk + 1, yk - 1/2)
 pk+1 = fcircle(xk+1 + 1, yk+1 - 1/2)
 pk+1 = (xk+1 + 1)2 + (yk+1 - 1/2)2 - r2
= (xk + 1 + 1)2 + (yk+1 - 1/2)2 - r2
= (xk + 2)2 + (yk+1 - 1/2)2 - r2
= xk2 + 4xk + 4 + yk+12 - yk+1 + 1/4 - r2
Now, pk+1 - pk = xk2 + 4xk + 4 + yk+12 - yk+1 + 1/4 - r2
- [xk2 + 2xk + 1 + yk2 - yk + 1/4 - r2 ]
Now, pk+1 - pk = xk2 + 4xk + 4 + yk+12 - yk+1 + 1/4 - r2
- [xk2 + 2xk + 1 + yk2 - yk + 1/4 - r2 ]
pk+1 - pk = xk2 + 4xk + 4 + yk+12 - yk+1 + 1/4 - r2
- xk2 - 2xk - 1 - yk2 + yk - 1/4 + r2 ]
pk+1 - pk = 2xk + 3 + [yk+12 - yk2] - [yk+1 - yk]

pk+1 = pk + 2xk + 3 + [yk+12 - yk2] - [yk+1 - yk]

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)

Let (x0, y0) = (0, r)


 p0 = fcircle(1, r - 1/2)
p0 = (1)2 + (r - 1/2)2 - r2
p0 = 12 + r2 - r + 1/4 - r2
p0 = 5/4 - r
Midpoint Circle Drawing Algorithm
1. Accept the radius ‘r’ and center of the circle (xc, yc)

2. Initialize the starting position as (x0, y0) = (0, r)

3. Calculate the initial value of decision parameter, p0 as


p0 = 5/4 - r = 1.25 - r
4. At each xk position 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 + 2xk + 3
}
else
{ xnext = xk+1 = xk + 1
ynext = yk+1 = yk - 1
pk+1 = pk + 2xk - 2 yk + 5
}
}
while (x < y)
5. Determine the symmetry points in the other seven octants.
6. Translate each calculated pixel position in eight octant by (xc, yc)
and plot the pixels.
x = xk+1 + xc
y = yk+1 + yc
putpixel (x, y, COLOUR)
7. Repeat the steps 4 to 6 until x ≥ y.
8. Stop
Ex1- Calculate the pixel positions along the circle path with
radius r = 8 centered at the origin using mid-point circle
drawing algorithm.

# Radius of the circle, r = 8 and center of the circle, (xc, yc) = (0, 0).

# Starting position is (x0, y0) = (0, 8)

# Initial value of decision parameter, p0 is


p0 = 5/4 - 8 = 1.25 - 8 = - 6.75
# At each xk position 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 + 2xk + 3
}
else
{ xnext = xk+1 = xk + 1
ynext = yk+1 = yk - 1
pk+1 = pk + 2xk - 2 yk + 5
}
}
while (x < y)
(x0, y0) = (0, 8)
p0 = 5/4 - 8 = 1.25 - 8 = - 6.75
pk = (xk )2 + (2 xk) + 1 + (yk )2 - (yk) - 63.75
pk+1 = pk + 2xk + 3 + [yk+12 - yk2] - [yk+1 - yk]
k (xk, yk) pk (xk+1, yk-1) pk+1 Putpixel (int x, int y)
0 (0, 8) - 6.75 (1, 8) - 3.75 (1, 8)

1 (1, 8) - 3.75 (2, 8) 1.25 (2, 8)

2 (2, 8) 1.25 (3, 7) - 5.75 (3, 7)


3 (3, 7) - 5.75 (4, 7) 3.25 (4, 7)

4 (4, 7) 3.25 (5, 6) 2.25 (5, 6)


5 (5, 6) 2.25 (6, 5) 5.25 (6, 5)
6 (6, 5) 5.25 (7, 4) 12.25 (7, 4)
7 (7,4) 12.25 (8,3) 37.25 (8, 3)
Ex2- Calculate the pixel positions along the circle path with
radius r = 10 centered at the origin using mid-point circle
drawing algorithm up to x = y.

# Radius of the circle, r = 10 and center of the circle, (xc, yc) = (0, 0).

# Starting position is (x0, y0) = (0, 10)

# Initial value of decision parameter, p0 is


p0 = 5/4 - 10 = 1.25 - 10 = - 8.75
# At each xk position 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 + 2xk + 3
}
else
{ xnext = xk+1 = xk + 1
ynext = yk+1 = yk - 1
pk+1 = pk + 2xk - 2 yk + 5
}
}
while (x < y)
(x0, y0) = (0, 10)
p0 = 5/4 - 8 = 1.25 - 8 = - 8.75
pk = (xk )2 + (2 xk) + 1 + (yk )2 - (yk) +(1/4) - (10)2
pk+1 = pk + 2xk + 3 + [yk+12 - yk2] - [yk+1 - yk]
If pk < 0, pk+1 = pk + 2xk + 3
If pk ≥ 0, pk+1 = pk + 2xk - 2 yk + 5

k (xk, yk) pk (xk+1, yk-1) pk+1 Putpixel (int x, int y)


0 (0, 10) - 8.75 (1, 10) - 5.75 (1, 10)
1 (1, 10) - 5.75 (2, 10) - 0.75 (2, 10)
2 (2, 10) - 0.75 (3, 10) 6.25 (3, 10)
3 (3, 10) 6.25 (4, 9) - 2.75 (4, 9)
4 (4, 9) - 2.75 (5, 9) 8.25 (5, 9)
5 (5, 9) 8.25 (6, 8) 5.25 (6, 8)
6 (6, 8) 5.25 (7, 7) 6.25 (7, 7)
7 (7,7) 6.25 (8,6) -- --
Midpoint Ellipse Drawing
It uses the four-way symmetry of the ellipse.
It plots 1/4th part of the ellipse i.e. from 900 to 00, as shown

x moves in the positive direction


y moves in the negative direction and it passes through two regions.
While processing first quadrant, take steps in the x-direction where
the slope of the curve has a magnitude less than -1(for region 1).
and take steps in the y-direction where the slope has a magnitude
greater than -1 (for region 2).
The equation of ellipse with center at
origin is
(x2/rx2) + (y2/ry2) = 1
x2 ry2 + y2 rx2 = rx2. ry2
x2 ry2 + y2 rx2 - rx2. ry2 = 0
fellipse(x, y) = x2 ry2 + y2 rx2 - rx2. ry2

The ellipse function serves as the decision parameter.


At each sampling position, the next pixel (xnext, ynext) along the ellipse
path is selected according to the sign of the ellipse function evaluated
at mid-point between the pixels (xk+1, yk) OR (xk+1, yk-1) for region-1
and (xk, yk-1) OR (xk+1, yk-1) for region-2.
Starting at (0, r) take unit steps in the x-direction until the boundary
between region-1 and region-2 reach.
Then take unit steps in the y-direction over the remainder of the
curve in the first quadrant.

To check for boundary point between region-1 and region-2, test the
value of the slope of the curve at each step.

The slope of the ellipse at each step is given as


dy/dx = - (2 ry2 x / 2 rx2y)

At the boundary point between region-1


and region-2, slope is -1
 2 ry2 x = 2 rx2y

 when 2 ry2 x ≥ 2 rx2y, take unit steps in the y-direction.


For Region-1 (Slope < 1)
Consider the pixel at (xk, yk).

The next pixel (xnext, ynext) along the


ellipse path will be either
(xk+1, yk) OR (xk+1, yk-1) whichever is
closer to the ellipse path at each step.

Let the decision parameter pk’ is equal to the ellipse function


evaluated at the mid-point between two pixels.
 pk’ = fellipse(xk+1, yk - 1/2)
= fellipse(xk + 1, yk - 1/2)
= (xk + 1)2 ry2 + (yk - 1/2)2 rx2 - rx2. ry2
pk’ = fellipse(xk+1, yk - 1/2)
= fcircle(xk + 1, yk - 1/2)
pk’ = (xk + 1)2 ry2 + (yk - 1/2)2 rx2 - rx2. ry2
Now
pk+1’ = (xk+1 + 1)2 ry2 + (yk+1 - 1/2)2 rx2 - rx2. ry2
= (xk + 1 + 1)2 ry2 + (yk+1 - 1/2)2 rx2 - rx2. ry2
= (xk + 2)2 ry2 + (yk+1 - 1/2)2 rx2 - rx2. ry2
Now
pk+1’ - pk’ = (xk + 2)2 ry2 + (yk+1 - 1/2)2 rx2 - rx2. ry2
- [(xk + 1)2 ry2 + (yk - 1/2)2 rx2 - rx2. ry2 ]
= (xk + 2)2 ry2 + (yk+1 - 1/2)2 rx2 - rx2. ry2
- (xk + 1)2 ry2 - (yk - 1/2)2 rx2 + rx2. ry2
= ry2 (xk2 + 4xk + 4 - xk2 - 2xk - 1 ) + rx2 [(yk+1 - 1/2)2 - (yk - 1/2)2]
= ry2 (2xk + 2 + 1 ) + rx2 [(yk+1 - 1/2)2 - (yk - 1/2)2]
pk+1’ - pk’ = ry2 (2xk + 2 + 1) + rx2 [(yk+1 - 1/2)2 - (yk - 1/2)2]

= 2 ry2 (xk + 1) + ry2 + rx2 [(yk+1 - 1/2)2 - (yk - 1/2)2]

pk+1’ = pk’ + 2 ry2 (xk + 1) + ry2 + rx2 [(yk+1 - 1/2)2 - (yk - 1/2)2]

If pk’ < 0, then


mid-point is INSIDE the ellipse boundary and the pixel at yk is
closer to the ellipse boundary.
Otherwise
mid-point is OUTSIDE or ON the ellipse boundary and the
pixel at yk-1 is closer to the ellipse boundary.
If pk’ < 0, then
xnext = xk+1 = xk + 1
ynext = yk+1 = (yk-1) = yk
pk+1’ = pk’ + 2 ry2 (xk + 1) + ry2 + rx2 [(yk+1 - 1/2)2 - (yk - 1/2)2]
pk+1’ = pk’ + 2 ry2 (xk + 1) + ry2 + rx2 [(yk - 1/2)2 - (yk - 1/2)2]
pk+1’ = pk’ + 2 ry2 (xk + 1) + ry2
If pk’ ≥ 0, then
xnext = xk+1 = xk + 1
ynext = yk+1 = (yk-1) = yk - 1
pk+1’ = pk’ + 2 ry2 (xk + 1) + ry2 + rx2 [(yk+1 - 1/2)2 - (yk - 1/2)2]
pk+1’ = pk’+ 2 ry2 (xk + 1) + ry2 + rx2 [(yk+12 - yk+1 + 1/4) - (yk2 - yk + 1/4)]
pk+1’ = pk’ + 2 ry2 (xk + 1) + ry2 + rx2 [yk+12 - yk+1 + 1/4 - yk2 + yk - 1/4]
pk+1’ = pk’ + 2 ry2 (xk + 1) + ry2 + rx2 [(yk+12 - yk2 ) - (yk+1 - yk)]
pk+1’ = pk’ + 2 ry2 (xk + 1) + ry2 + rx2 [(yk - 1)2 - yk2 ) - (yk - 1 - yk)]
pk+1’ = pk’ + 2 ry2 (xk + 1) + ry2 + rx2 [yk2 - 2yk + 1 - yk2 + 1]
pk+1’ = pk’ + 2 ry2 (xk + 1) + ry2 + rx2 [(yk+12 - yk2 ) - (yk+1 - yk)]
pk+1’ = pk’ + 2 ry2 (xk + 1) + ry2 + rx2 [(yk - 1)2 - yk2 ) - (yk - 1 - yk)]
pk+1’ = pk’ + 2 ry2 (xk + 1) + ry2 + rx2 [yk2 - 2yk + 1 - yk2 + 1]
pk+1’ = pk’ + 2 ry2 (xk + 1) + ry2 + rx2 [- 2yk + 1 + 1]
pk+1’ = pk’ + 2 ry2 (xk + 1) + ry2 - 2 rx2 (yk - 1)
Now, Starting point in region-1 is (x0, y0) = (0, ry)
Initial value of decision parameter is
pk’ = (xk + 1)2 ry2 + (yk - 1/2)2 rx2 - rx2. ry2
p0’ = (x0 + 1)2 ry2 + (y0 - 1/2)2 rx2 - rx2. ry2
p0’ = (0 + 1)2 ry2 + (ry - 1/2)2 rx2 - rx2. ry2
p0’ = ry2 + (ry2 - ry + 1/4) rx2 - rx2. ry2
p0’ = ry2 + rx2 (ry2 - ry + 1/4 - ry2)
p0’ = ry2 + rx2 (- ry + 1/4)
 p0’ = ry2 - rx2 ry + 1/4 rx2
For Region-2 (Slope > 1)
Consider the pixel at (xk, yk).

The next pixel (xnext, ynext) along the


ellipse path will be either
(xk, yk-1) OR (xk+1, yk-1) whichever is
closer to the ellipse path at each step.

Let the decision parameter pk” is equal to the ellipse function


evaluated at the mid-point between two pixels.
 pk” = fellipse(xk+1, yk-1)
= fellipse(xk + 1/2, yk - 1 )
= (xk + 1/2)2 ry2 + (yk - 1)2 rx2 - rx2. ry2
 pk” = fellipse(xk+1, yk-1)
= fellipse(xk + 1/2, yk - 1 )
 pk” = (xk + 1/2)2 ry2 + (yk - 1)2 rx2 - rx2. ry2
Now
 pk+1”= fellipse(xk+1 + 1/2, yk+1 - 1 )
= (xk+1 + 1/2)2 ry2 + (yk - 1 - 1)2 rx2 - rx2. ry2
= (xk+1 + 1/2)2 ry2 + (yk - 2)2 rx2 - rx2. ry2
Now
pk+1” - pk” = (xk+1 + 1/2)2 ry2 + (yk - 2)2 rx2 - rx2. ry2
- [(xk + 1/2)2 ry2 + (yk - 1)2 rx2 - rx2. ry2]
pk+1” - pk” = (xk+1 + 1/2)2 ry2 + (yk - 2)2 rx2 - rx2. ry2
- (xk + 1/2)2 ry2 - (yk - 1)2 rx2 + rx2. ry2]
pk+1” - pk” = ry2 [(xk+1 + 1/2)2 - (xk + 1/2)2 ] + rx2[(yk - 2)2 - (yk - 1)2 ]
pk+1” = pk” + ry2 [(xk+1 + 1/2)2 - (xk + 1/2)2 ] + rx2[(yk - 2)2 - (yk - 1)2 ]
pk+1” = pk” + ry2 [(xk+1 + 1/2)2 - (xk + 1/2)2 ] + rx2[(yk - 2)2 - (yk - 1)2 ]
Now
If pk” > 0, then
xnext = xk+1 = xk
ynext = yk+1 = yk-1 = yk - 1
pk+1” = pk” + ry2 [(xk+1 + 1/2)2 - (xk + 1/2)2 ] + rx2[(yk - 2)2 - (yk - 1)2 ]
pk+1” = pk” + ry2 [(xk + 1/2)2 - (xk + 1/2)2] + rx2[(yk - 2)2 - (yk - 1)2]
pk+1” = pk” + rx2[(yk - 2)2 - (yk - 1)2]
= pk” + rx2[(yk2 - 4yk + 4) - (yk2 - 2yk + 1)]
= pk” + rx2[yk2 - 4yk + 4 - yk2 + 2yk - 1]
= pk” + rx2[- 4yk + 4 + 2yk - 1]
= pk” + rx2[- 2yk + 3]
pk+1” = pk” - 2 rx2 yk + 3 rx2
Now
If pk’ ≤ 0, then
xnext = xk+1 = xk + 1
ynext = yk+1 = yk-1 = yk - 1
pk+1” = pk” + ry2 [(xk+1 + 1/2)2 - (xk + 1/2)2 ] + rx2[(yk - 2)2 - (yk - 1)2]
pk+1” = pk” + ry2 [(xk + 1 + 1/2)2 - (xk + 1/2)2] + rx2[(yk - 2)2 - (yk - 1)2]
= pk” + ry2 [(xk + 3/2)2 - (xk + 1/2)2] + rx2[(yk - 2)2 - (yk - 1)2]
= pk” + ry2 [(xk2 + 3xk + 9/4) - (xk2 + xk + 1/4)] + rx2[- 2yk + 3]
= pk” + ry2 [xk2 + 3xk + 9/4 - xk2 - xk - 1/4] + rx2[- 2yk + 3]
= pk” + ry2 [2xk + 8/4] + rx2[- 2yk + 3]
= pk” + ry2 [2xk + 2] + rx2[- 2yk + 3]
pk+1” = pk” + 2ry2 [xk + 1] - 2 rx2 yk + 3 rx2
Now,
Starting point in region-2 is (x0, y0) = last position in region-1
Initial value of decision parameter for region-2 is
pk” = fellipse(xk+1, yk-1)
= fellipse(xk + 1/2, yk - 1 )
p0” = fellipse(x0 + 1/2, y0 - 1 )
p0” = (x0 + 1/2)2 ry2 + (y0 - 1)2 rx2 - rx2. ry2
Midpoint Ellipse Drawing Algorithm

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

0 (0, 3) - 35 (1, 3) -8 (1, 3) 18 < 96

1 (1, 3) -8 (2, 3) 37 (2, 3) 36 < 96

2 (2, 3) 37 (3, 2) 36 (3, 2) 54 < 64

3 (3, 2) 36 (4, 1) 85 (4, 1) 72 < 32


# Now
Starting point in region-2 is (x0, y0) = last position in region-1
 (x0, y0) = (4, 1)
# Initial value of decision parameter for region-2 is
p0” = (x0 + 1/2)2 ry2 + (y0 - 1)2 rx2 - rx2. ry2
p0” = (4 + 1/2)2 (3)2 + (1 - 1)2 (4)2 - (4)2. (3)2
p0” = 38.25
# 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
= pk” - 2 (4)2 yk + 3 (4)2
= pk” - 32yk + 48
}
else { xnext = xk+1 = xk + 1
ynext = yk+1 = yk-1 = yk - 1
pk+1” = pk” + 2ry2 [xk + 1] - 2 rx2 yk + 3 rx2
pk+1” = pk” + 2(3)2 [xk + 1] - 2 (4)2 yk + 3(4)2
pk+1” = pk” + 18(xk + 1) - 32yk + 48
}
} while (y > 0)
If pk’’ > 0, xnext = xk+1 = xk
ynext = yk+1 = yk-1 = yk - 1
pk+1” = pk” - 32yk + 48
else xnext = xk+1 = xk + 1
ynext = yk+1 = yk-1 = yk - 1
pk+1” = pk+1” = pk” + 18(xk + 1) - 32yk + 48

k (xk, yk) pk (xk+1, yk-1) pk+1 Putpixel (int x, int y) y>0

0 (4, 1) 38.25 (4, 0) (4, 0) 0>0


k (xk, yk) pk (xk+1, yk-1) pk+1 Putpixel (int x, int y) 18x < 32y
0 (0, 3) - 35 (1, 3) -8 (1, 3) 18 < 96
1 (1, 3) -8 (2, 3) 37 (2, 3) 36 < 96
2 (2, 3) 37 (3, 2) 36 (3, 2) 54 < 64
3 (3, 2) 36 (4, 1) 85 (4, 1) 72 < 32

k (xk, yk) pk (xk+1, yk-1) pk+1 Putpixel (int x, int y) y>0


0 (4, 1) 38.25 (4, 0) (4, 0) 0>0
Ex2- Calculate the pixel positions along the ellipse path
with radii rx= 8, ry = 6 and centered at the origin using mid-
point ellipse drawing algorithm.
# Radii of the ellipse , rx= 8, ry = 6
and center of the circle, (xc, yc) = (0, 0).
# Initialize the starting position as (x, y) = (0, ry) = (0, 6)
# Initial value of decision parameter for region-1
p0’ = ry2 - rx2 ry + 1/4 rx2
= 62 - 82 . 6 + 1/4 82
= 36 - 384 + 16
p0’ = - 332
# 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
pk+1’ = pk’ + 2 (6)2 (xk + 1) + (6)2
pk+1’ = pk’ + 72 (xk + 1) + 36
}
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 (6)2 (xk + 1) + (6)2 - 2 (8)2 (yk + 1)
pk+1’ = pk’ + 72 (xk + 1) + 36 - 128 (yk - 1)
}
} while (2 (6)2 x < 2 (8)2y) i.e while (72x < 128y)
# If pk’ < 0, xnext = xk+1 = xk + 1
ynext = yk+1 = yk
pk+1’ = pk’ + 72 (xk + 1) + 36
Else, xnext = xk+1 = xk + 1
ynext = yk+1 = yk - 1
pk+1’ = pk’ + 72 (xk + 1) + 36 - 128 (yk - 1)

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

1 (1, 6) -224 (2, 6) -44 (2, 6) 144 < 768

2 (2, 6) -44 (3, 6) 208 (3, 6) 216 < 768


3 (3, 6) 208 (4, 5) -108 (4, 5) 288 < 640
4 (4, 5) -108 (5, 5) 288 (5, 5) 360 < 640

5 (5, 5) 288 (6, 4) 244 (6, 4) 432 < 512


6 (6, 4) 244 (7, 3) 400 (7, 3) 504 < 384
# Now
Starting point in region-2 is (x0, y0) = last position in region-1
 (x0, y0) = (7, 3)
# Initial value of decision parameter for region-2 is
p0” = (x0 + 1/2)2 ry2 + (y0 - 1)2 rx2 - rx2. ry2
p0” = (7 + 1/2)2 (6)2 + (3 - 1)2 (8)2 - (8)2. (6)2
p0” = - 23
# 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
= pk” - 2 (8)2 yk + 3 (8)2
= pk” - 128yk + 192
}
else { xnext = xk+1 = xk + 1
ynext = yk+1 = yk-1 = yk - 1
pk+1” = pk” + 2ry2 [xk + 1] - 2 rx2 yk + 3 rx2
pk+1” = pk” + 2(6)2 [xk + 1] - 2 (8)2 yk + 3(8)2
pk+1” = pk” + 72(xk + 1) - 128yk + 192
}
} while (y > 0)
If pk’’ > 0, xnext = xk+1 = xk
ynext = yk+1 = yk-1 = yk - 1
pk+1” = pk” - 128yk + 192
else xnext = xk+1 = xk + 1
ynext = yk+1 = yk-1 = yk - 1
pk+1” = pk” + 72(xk + 1) - 128yk + 192

k (xk, yk) pk (xk+1, yk-1) pk+1 Putpixel (int x, int y) y>0

0 (7, 3) -23 (8, 2) 361 (8, 2) 2>0

1 (8, 2) 361 (8, 1) 297 (8, 1) 1>0

2 (8, 1) 297 (8, 0) 361 (8, 0) 0>0

You might also like