Lecture 5
Lecture 5
Algorithms
Assoc. Prof. Mohamed AbdelNasser
Circle Drawing
Since the circle is a frequently used component in
pictures and graphs, a procedure for generating
either full circles or circular arc is included in most
graphics packages.
Direct Algorithm
The equation for a circle is:
x2 + y2 = r2
Where r is the radius of the circle. So, we can write
a direct circle drawing algorithm by solving the
equation for y at unit x intervals using:
y = (r2 – x2)1/2 2
Direct Algorithm
To draw a circle with radius=20
y(0) = (202 – 02)1/2 20
y(1) = (202 – 12)1/2 20
y(2) = (202 – 22)1/2 20
…..
y(19) = (202 – 192)1/2 6
y(20) = (202 – 202)1/2 = 0
the resulting circle has large
gaps where the slope
approaches the vertical. And
the calculations are not very
efficient:
✓ The square (multiply) operations
✓ The square root operation
3
Eight-Way Symmetry
The first thing we can notice to make our circle
drawing algorithm more efficient is that circles
centred at (0, 0) have eight-way symmetry
4
Mid-Point Circle Algorithm
As in the raster line algorithm, we sample at unit
intervals and determine the closest pixel position to
the specified circle path at each step.
• For a given radius r and screen center position
(xc, yc), we can first set up our algorithm to
calculate pixel positions around a circle path
centered at the coordinate (0, 0).
• Then each calculated position (x, y) is moved to
its proper screen position by adding xc to x and yc
to y.
5
Mid-Point Circle Algorithm
• Along the circle section from x = 0 to x = y in the
first quadrant, the slope of the curve varies from 0
to –1. Therefore, we can take unit steps in the
positive x direction over this octant and use a
decision parameter to determine which of the two
possible y positions is closer to the circle path at
each step positions in the other seven octants are
then obtained by symmetry.
• To apply the midpoint algorithm, we define a circle
function:
fcircle(x, y) = x2 + y2 – r2
6
Mid-Point Circle Algorithm
• The relative position of any point (x, y) can be
determined by checking the sign of the on the
boundary of the circle function:
<0 If (x, y) is inside the circle boundary
8
Mid-Point Circle Algorithm
pk = fcircle(xk + 1,yk – 1/2)
=(xk + 1)2 + (yk – 1/2)2 – r2
• If pk < 0, this midpoint is inside the circle and the pixel at
yk is closer to the circle boundary.
• Otherwise the midpoint is outside or on the circle
boundary, and we select the pixel at yk–1.
✓ Successive decision parameters are obtained using
incremental calculations. We obtain a recursive expression
for the next decision parameter by evaluating the circle
function at sampling position xk+1 + 1 = xk + 2
pk+1 = fcircle(xk+1 + 1,yk+1 – 1/2)
= ((xk + 1) + 1)2 + (yk – 1/2)2 – r2
9
Mid-Point Circle Algorithm
pk+1 = pk + 2(xk+1) + (yk +12 – yk 2 ) – (yk +1 – yk) + 1
11
Mid-Point Circle Algorithm
1. Input radius r and circle centre (xc, yc), and obtain the
first point on the circumference of a circle centred on the
origin as:
(x0, y0) = (0, r)
2. Calculate the initial value of the decision parameter as:
P0 = 5/4 – r
3. At each position xk Starting with k=0, perform the
following test.
If pk < 0, the next point along the circle centred on (0, 0)
is (xk+1 , yk) and pk+1 = pk + 2xk+1 + 1
Otherwise the next point along the circle is (xk+1 , yk– 1)
and pk+1 = pk + 2xk +1 +1 – 2yk +1
where 2xk+1 = 2xk+1 + 2 and 2yk+1= 2yk+1
12– 2
Mid-Point Circle Algorithm
4. Determine symmetry points in the other seven octants
5. Move each calculated pixel position (x, y) onto the circular
path centred at (xc, yc) and plot the coordinate values:
X= x+ xc y = y+ yc
6. Repeat steps 3 to 5 until x >= y
13
Mid-Point Circle Algorithm Example
Given a circle radius=10, we demonstrate the midpoint
circle algorithm by determining positions along the circle
octant in the first quadrant from x = 0 to x = y. the initial
value of the decision parameter is P0 = 1 – r = – 9
For the circle centred on the origin, the initial point is (x0,
y0) = (0, 10), and the initial increment term for calculating
the decision parameters are
2x0 = 0 and 2y0= 2r
2x0 = 0 and 2y0= 20
Successive decision parameters values and positions along
the circle path are calculated using midpoint algorithm as:
14
Mid-Point Circle Algorithm
xk+1 ,
k Pk 2xk+1 2yk+1
yk– 1
0 -9 (1, 10) 2 20
1 -6 (2, 10) 4 20
2 -1 (3, 10) 6 20
3 6 (4, 9) 8 18
4 -3 (5, 9) 10 18
5 8 (6, 8) 12 16
6 5 (7, 7) 14 14
10/1.4
A plot of the generated pixel positions in the first quadrant is
shown in the figure
15
Midpoint Ellipse Algorithm
General procedure (same like circle):
1. Determine curve positions for an ellipse with center at
(0, 0) (origin)
2. Move to proper position, i.e. add xc and yc
3. If required perform rotation
4. Use decision parameter to determine closest pixel
5. For other 3 quadrants use symmetry
3 Quadrants? In circle 7 octants!
So what is wrong?
▪ Ellipse function:
f ellipse ( x , y ) = r x + r y − r r
y
2 2
x
2 2 2 2
x y
19
Over region 1, the magnitude of the ellipse slope is less than 1.0; over region 2, the
magnitude of the slope is greater than 1.0.
20
Midpoint Ellipse Algorithm: Decision
Parameter {Region 1}
21
Between candidate pixels at sampling position xk + 1 along an
elliptical path.
22
Between candidate pixels at sampling position yk − 1 along an
elliptical path. 23
Midpoint Ellipse Algorithm Decision
Parameter {Region 2}
We can find a recursive expression for the next
decision parameter as:
24
Midpoint Ellipse Algorithm Decision
Parameter
25
Midpoint Ellipse Algorithm Region 1-
Region 2?
27
Example
29
30
Pixel positions along an elliptical path centered on the origin with rx = 8 and
ry = 6, using the midpoint algorithm to calculate locations within the first
quadrant
31