7/30/2019 Scan Converting Ellipse
1/42
Scan Converting Ellipse
General equation of an ellipse:
d1+ d2= constant
Or,
However, we will only consider standard ellipse:
(x, y)
F1
F2
d1
d2
constant222
2
2
1
2
1 yyxxyyxx
1
22
b
yy
a
xx cc
a
b
7/30/2019 Scan Converting Ellipse
2/42
Scan Converting Ellipse
Formally An ellipse is defined as the locus of points
satisfying following equation: r from a fixed point (h,k).
This distance is described In the Pythagoras theorem as :
where (h,k) is the centre, a and b are the length of major
and minor axis respectively
The standard equation for the ellipse at centre (0,0) is:
1)(
2
2
2
2
b
ky
a
h)(x
12
2
2
2
b
y
a
x
7/30/2019 Scan Converting Ellipse
3/42
Scan Converting Ellipse
So, we can write a simple circle drawing algorithm by
solving the equation fory at unitx intervals using:
Which is not a good solution. (you know that)
))/(1.( 22 axby
7/30/2019 Scan Converting Ellipse
4/42
Scan Converting Ellipse
Four-Way Symmetry Like circle, ellipse centred at (0, 0) follows symmetry. But
now it isfour symmetry.
Thus ellipse points are computed
in the first quadrant,
rest three of the ellipse points
are found by symmetry.
Centre can be shifted while
plotting
(x,y)(-x,y)
(-x,-y)(x,-y)
X-axis
Y-axis
o
7/30/2019 Scan Converting Ellipse
5/42
Scan Converting Ellipse
So we define a routine that plots ellipse pixels with centre
(h,k) in all the four quadrants
put_ellipse_pixel(x,y,h,k)
{
Put_pixel(h+x, k+y)
Put_pixel(h-x, k+y)
Put_pixel(h+x, k-y)
Put_pixel(h-x, k-y)}
7/30/2019 Scan Converting Ellipse
6/42
Ellipse Drawing Algorithms
1. Polar Domain Algorithms
2. Midpoint Ellipse Algorithm3. Bresenham Ellipse Algorithm
7/30/2019 Scan Converting Ellipse
7/42
Polar Domain Algorithm
We could use polar coordinates r and ,
x =h + a cos y = k + b sin
where (h,k) is the centre, a and b are the length of major and minor axis
respectively
A fixed angular step size can be used to plot equally spaced
points along the circumference
A step size of 1/a or 1/b, whichever is smaller can be used to
set pixel positions to approximately 1 unit apart for a
continuous boundary.
7/30/2019 Scan Converting Ellipse
8/42
Polar Domain Algorithm
We could also use incremental form. Let P(xk,yk) be currentlyplotted point,
xk = a cos()
yk= b sin()
At next iteration k = k+1 ; = +Dxk+1 = a cos(+ D)
= acos() cos(D) - a sin() sin(D)
= xkcos(D) - yk(a/b)sin(D)
yk+1 = b sin(+D)
= b sin() cos(D) - b cos() sin(D)
= ykcos(D) + xk(b/a)sin(D)
7/30/2019 Scan Converting Ellipse
9/42
Polar Domain Algorithm
STEPS
1. Input centre (h,k),anda,b
2. If (a>b) D = 1/a else D = 1/b ;
3. C = cos(D);S = sin(D); t = a/b;
4. Initialize x = 0;y = b;= 0;
5. WHILE ( < /2)
{
xtemp = x
x = x*C-t*y*S;
y = y*C+(1/t)*xtemp*S;put_ellipse_pixel(x,y,h,k);
= + D;
}
This method is still computationally expensive
7/30/2019 Scan Converting Ellipse
10/42
Ellipse Drawing Algorithms
1. Polar Domain Algorithms
2. Midpoint Ellipse Algorithm
3. Bresenham Ellipse Algorithm
7/30/2019 Scan Converting Ellipse
11/42
Midpoint Ellipse Algorithm
Reconsider an ellipse centered at the origin:
The discriminator function?
and its properties:
fe(x,y) < 0 for a point inside the ellipse
fe(x,y) > 0 for a point outside the ellipse
fe(x,y) = 0 for a point on the ellipse
222222, bayaxbyxfe
1
22
b
y
a
x
7/30/2019 Scan Converting Ellipse
12/42
Midpoint Ellipse Algorithm
7/30/2019 Scan Converting Ellipse
13/42
Midpoint Ellipse Algorithm
7/30/2019 Scan Converting Ellipse
14/42
Midpoint Ellipse Algorithm
Ellipse is different from circle.
Similar approach with circle, different
is sampling direction.
Region 1: Sampling is atx direction
Choose between (xk+1, yk), or (xk+1, yk-1)
Midpoint: (xk+1, yk-0.5)
Region 2:
Sampling is aty direction
Choose between (xk, yk-1), or (xk+1, yk-1)
Midpoint: (xk+0.5, yk-1)
Slope =
-1
Region 1
Region 2
7/30/2019 Scan Converting Ellipse
15/42
Midpoint Ellipse Algorithm
Region 1:
p1kve:
midpoint is inside
choose pixel (xk+1
, yk)
p1k
+ve:
midpoint is outside
choose pixel (xk+1
, yk-1)
21,11 kkek yxfp
Region 2:
p2kve:
midpoint is inside
choose pixel (xk+1, y
k-1)
p2k+ve:
midpoint is outside
choose pixel (xk, y
k-1)
1,2 21 kkek yxfp
Decision Parameters
7/30/2019 Scan Converting Ellipse
16/42
Midpoint Ellipse Algorithm
Derivation for Region 1:
Let us assume that P(xk, yk )
is the currently plotted pixel.
Q(xk+1, yk+1 ) (xk+1, y ) is
the next point along the actualcircle path. We need to decide
next pixel to be plotted from
among candidate positions
Q1(xk+1, yk) orQ2(xk+1, yk-1)
7/30/2019 Scan Converting Ellipse
17/42
Midpoint Ellipse Algorithm
Our decision parameter is the circle function evaluated at the
midpoint between these two pixels
p1k= fe (xk+1, yk-1/2) = b2(xk+1)
2 + a2(yk-1/2)2a2 b2
Ifp1k< 0 ,
this midpoint is inside the ellipse and
the pixel on the scan lineykis closer to the ellipse boundary.
Otherwise, the mid position is outside or on the ellipse boundary,
and we select the pixel on the scan lineyk-1
7/30/2019 Scan Converting Ellipse
18/42
Midpoint Ellipse Algorithm
Successive decision parameters are obtained using incrementalcalculations
p1k = b2 (xk+1)
2 + a2 (yk )2a2b2
Put k = k+1p1k+1 = b
2 [(xk+1)+1]2 + a2(yk+1 )
2a2b2
= b2 (xk+2)2 + a2(yk+1 )
2a2b2
subtracting pk from pk+1
p1k+1p1k= b2(xk+2)2 + a2(yk+1 )2[b2(xk+1)2 + a2(yk )2]or
p1k+1 = p1k+2 b2(xk+1) + a
2 [(yk+1)2(yk)
2]+b2
Whereyk+1is eitherykoryk-1, depending on the sign of p1k
7/30/2019 Scan Converting Ellipse
19/42
Midpoint Ellipse Algorithm
Ifp1k< 0
Q1(xk+1, yk) was the next choice
yk+1 = yk
(yk+1
)
2
(yk
)
2
= 0p1k+1 = p1k+b
2(2.xk+3)
else
Q2(xk+1, yk-1) was the next choice
yk+1 = yk1(yk+1)
2(yk)2=2.yk+2
p1k+1 = p1k+b2(2.xk+3) + a
2(2.yk+ 2)
7/30/2019 Scan Converting Ellipse
20/42
Midpoint Ellipse algorithm
Initial decision parameter is obtained by evaluating the circle
function at the start position (x0,y0) = (0,b)
p10 = fe(1, b1/2)
= b2a2b + a2
7/30/2019 Scan Converting Ellipse
21/42
Midpoint Ellipse Algorithm
Derivation for Region 2:
Let us assume that P(xk, yk )
is the currently plotted pixel.
Q(xk+1, yk+1 ) (xk+1, y ) is
the next point along the actualcircle path. We need to decide
next pixel to be plotted from
among candidate positions
Q1(xk, yk-1) orQ2(xk+1, yk-1)
7/30/2019 Scan Converting Ellipse
22/42
Midpoint Ellipse Algorithm
Our decision parameter is the circle function evaluated at the
midpoint between these two pixels
p2k= fe (xk+1/2, yk-1) = b2(xk+1/2)
2 + a2(yk1)2a2 b2
Ifp2k
7/30/2019 Scan Converting Ellipse
23/42
Midpoint Ellipse Algorithm
Successive decision parameters are obtained using incrementalcalculations
p2k = b2 (xk+)
2 + a2 (yk1 )2a2b2
Put k = k+1
p2k+1 = b2 [(xk+1)+]
2 + a2(yk+11 )2a2b2
= b2 [(xk+1)+]2 + a2(yk2 )
2a2b2
subtracting pk from pk+1
p2k+1
p2k= b2(x
k+1+)2 + a2(y
k2 )2[b2(x
k+)2 + a2(y
k1 )2]
or
p2k+1 = p2k+b2[(xk+1+)
2(xk+)2 ]a2 (2.yk
3)
Wherexk+1is eitherxkorxk+1, depending on the sign of p2k
7/30/2019 Scan Converting Ellipse
24/42
Midpoint Ellipse Algorithm
Ifp2k< 0
Q2(xk+1, yk-1) was the next choice
xk+1 = xk+1
(xk+1+)2
(xk+)2
= 2.xk+2p2k+1 = p2k+b
2(2.xk+2)a2(2.yk3)
else
Q1(xk, yk1) was the next choice
xk+1 = xk(xk+1+)
2(xk+)2= 0
p2k+1 = p2ka2(2.yk3)
7/30/2019 Scan Converting Ellipse
25/42
Midpoint Ellipse algorithm
Initial decision parameter is obtained by evaluating the circlefunction at the start position (x0,y0) the last point plotted afterdrawing region 1
p20 = fe(x0+, y01)
= b2(x0+)2 + a2(y01)
2a2b2
7/30/2019 Scan Converting Ellipse
26/42
Midpoint Ellipse algorithm
Condition to move from one region to another
Starting at point (0,b) in the region 1 where slope is 1. At each
step we need to check slope.
The ellipse slope can be calculated as At the boundary of region 1 and 2
dy/dx = -1
2b2x = 2a2y
We move out of region 1 if 2b2x 2a2y
ya
xb
dx
dy2
2
2
2
7/30/2019 Scan Converting Ellipse
27/42
Midpoint Ellipse Algorithm
1. Input a, b and ellipse center (xc, y
c). First point on
the similar ellipse centered at the origin is (0, b).
2. Initial value for decision parameter at region 1:
3. At each xk in region 1, starting from k = 0, testp1k :Ifp1
k< 0, next point (x
k+1, y
k) and
else, next point (xk+1, y
k-1) and
4. Determine symmetry points in the other 3 octants.
5. Get the actual point for ellipse centered at (xc, y
c)
that is (x+ xc, y+ y
c).
6. Repeat step 3 - 6 until 2b2x 2a2y.
2
4
122
01 ababp
,3211 221 bxbpp kkk
.2322112222
1 abyaxbpp kkkk
7/30/2019 Scan Converting Ellipse
28/42
Midpoint Ellipse Algorithm
7. Initial value for decision parameter in region 2:
8. At each ykin region 2, starting from k = 0, testp2
k:
Ifp2k> 0, next point is (x
k, y
k-1) and
else, next point is (xk+1, y
k-1) and
9. Determine symmetry points in the other 3 octants.
10.Get the actual point for ellipse centered at (xc, y
c)
that is (x+ xc, y+ y
c).
11.Repeat step 8 - 10 until y< 0.
22
1 3222 ayapp kkk
222022
21
02
0 12 bayaxbp
.2322222222
1bayaxbpp kkkk
7/30/2019 Scan Converting Ellipse
29/42
Ellipse Drawing Algorithms
1. Polar Domain Algorithms
2. Midpoint Ellipse Algorithm
3. Bresenham Ellipse Algorithm
7/30/2019 Scan Converting Ellipse
30/42
Bresenhams Ellipse Algorithm
Reconsider an ellipse centered at the origin:
or
We will define the distance from the actual value to two
candidate positions and proceed as in circle algorithm.
1
22
b
y
a
x
)2)/(1.(22 axby
7/30/2019 Scan Converting Ellipse
31/42
Bresenhams Ellipse Algorithm
Derivation for Region 1
Let us assume that P(xk, yk ) is
the currently plotted pixel.
Q(xk+1, yk+1 ) (xk+1, y ) is the
next point along the actual
circle path. We need to decide
next pixel to be plotted from
two candidate positions
Q1(xk+1, yk) or Q2(xk+1, yk-1)
Q1
Q2
P
7/30/2019 Scan Converting Ellipse
32/42
Bresenhams Ellipse AlgorithmThus actual value ofy atx = x
k+1is given by
y2 = b2 (1((xk+1) /a)
2 )
We define the distance measure in squares to simplify calculations
Let d1 =yk2y
2
=yk2
[b
2
(1((xk+1) /a)2
)]d2 =y2
(yk-1)2
= [b2 (1((xk+1) /a)2 )](yk-1)
2
The difference in d1 and d2 determine the next pixel to be plottedif(d1-d2)
7/30/2019 Scan Converting Ellipse
33/42
Bresenhams Ellipse AlgorithmWe can define a decision parameterp
kfor the kth step as follows:
p1k= ( d1-d2)
=2. b2(1(xk+1)2/a2 )+yk
2 + (yk1)2
To putpk in recurrence relation replace k = k+1
p1k+1 =
2. b
2
(1
(xk+1 +1)2
/a
2
)+yk+12
(yk+1
1)2
=2. b2(1(xk+2)2/a2 )+yk+1
2(yk+11)2
From above two equations
p1k+1p1k= ?
p1k+1 = p1k+ (b2/a2)(4xk+6) +[y
2k+1y
2k][(yk+1-1)
2(yk-1)2]
7/30/2019 Scan Converting Ellipse
34/42
Bresenhams Ellipse Algorithm
Ifp1k< 0
Q1(xk+1, yk) was the next choice
yk+1 = yk[y
k+1
2yk
2][(yk+1
-1)
2(yk
-1)
2 ] = 0
p1k+1 = p1k+ (b2/a2)(4.xk+6)
else
Q2(xk+1, yk1) was the next choice
yk+1 = yk1[yk+1
2yk2][(yk+1 -1)
2(yk-1)2 ] =4.yk+4
p1k+1 = p1k+ (b2/a2)(4.xk+6)(4.yk4)
7/30/2019 Scan Converting Ellipse
35/42
Bresenhams Ellipse Algorithm
The first parameterp0 is directly computed from:
Pk=2. b2(1(xk+1)
2/a2 )+yk2 + (yk1)
2
By putting k = 0,xk= 0 andyk= bp0 =2. b
2(1(0+1)2/a2 )+b2 + (b1)
2
= 2.b2/a22.b + 1
Let us derive it for region 2
7/30/2019 Scan Converting Ellipse
36/42
Bresenhams Ellipse Algorithm
Derivation for Region 2
Let us assume that P(xk, yk ) is
the currently plotted pixel.
Q(xk+1, yk+1 ) (x, yk+1 ) is the
next point along the actual
circle path. We need to decide
next pixel to be plotted from
two candidate positions
Q1(xk, yk-1 ) or Q2(xk+1, yk-1)
7/30/2019 Scan Converting Ellipse
37/42
Bresenhams Ellipse AlgorithmThus actual value ofx aty = y
k+1= y
k1is given by
x2 = a2 (1((yk1) /b)
2 )
We define the distance measure in squares to simplify calculations
Let d1 =x2
xk2
= [a2 (1((yk1) /b)
2 )]x
k
2
d2 = (xk+ 1)
2x2= (xk+1)
2[a2 (1((yk1) /b)2 )]
The difference in d1
and d2
determine the next pixel to be plotted
if(d1-d2)>0 Q1(xk, yk1 ) is plotted
else Q2(xk+1, yk1) is plotted.
7/30/2019 Scan Converting Ellipse
38/42
Bresenhams Ellipse AlgorithmWe can define a decision parameterp2
kfor the kth step as follows:
p2k= ( d1-d2)
= 2. a2(1(yk-1)2/b2 )xk
2(xk+ 1)2
To putp2k in recurrence relation replace k = k+1p2
k+1=2. a2(1(y
k+11)
2/b2 )xk+1
2(xk+1
+ 1)
2
= 2. a2(1(yk2)2/b2 )xk+1
2(xk+1 + 1)2
From above two equations
p2k+1p2k= ?
p2k+1 = p2k(a2/b2)(4yk6)[x
2k+1 +x
2k][(xk+1+1)
2+ (xk+1)2]
7/30/2019 Scan Converting Ellipse
39/42
Bresenhams Ellipse Algorithm
Ifp2k> 0
Q1(xk, yk1) was the next choice
xk+1 = xk[x
k+1
2xk
2][(xk+1
+1)
2(xk
+1)
2 ] = 0
p2k+1 = p2k(a2/b2)(4.yk6)
else
Q2(xk+1, yk1) was the next choice
xk+1 = xk+1[xk+1
2xk2][(xk+1+1)
2(xk+1)2 ] = 4.xk+4
p2k+1 = p2k(a2/b2)(4.yk6) + (4.xk + 4)
7/30/2019 Scan Converting Ellipse
40/42
Bresenhams Ellipse Algorithm
The first parameterp20 is directly computed from:
p2k=2. a2(1(yk-1)
2/b2 )xk2(xk+ 1)
2
Where (xk,yk) is the last point plotted in region .
Write the algorithm, implement in lab and report me how the
output of this algorithm is different from Midpoint Algorithm
7/30/2019 Scan Converting Ellipse
41/42
Brsenhams Ellipse Algorithm
1. Input a, b and ellipse center (xc, yc). First point onthe similar ellipse centered at the origin is (0, b).
2. Initial value for decision parameter at region 1:
3. At each xk in region 1, starting from k = 0, testp1k :Ifp1
k< 0, next point (x
k+1, y
k) and
else, next point (xk+1, y
k-1) and
4. Determine symmetry points in the other 3 octants.
5. Get the actual point for ellipse centered at (xc, y
c)
that is (x+ xc, y+ y
c).
6. Repeat step 3 - 6 until 2b2x 2a2y.
.21.2122
0babp
).3.2(211 221 kkk xabpp
).1(4)3.2(21122
1 yxabpp
kkk
7/30/2019 Scan Converting Ellipse
42/42
Bresenhams Ellipse Algorithm
7. Initial value for decision parameter in region 2:
8. At each ykin region 2, starting from k = 0, testp2
k:
Ifp2k> 0, next point is (x
k, y
k-1) and
else, next point is (xk+1, y
k-1) and
9. Determine symmetry points in the other 3 octants.
10.Get the actual point for ellipse centered at (xc, y
c)
that is (x+ xc, y+ y
c).
11.Repeat step 8 - 10 until y< 0.
).6.4)((22 221 kkk ybapp
222220 )1()1(1.22 xxbyap
).4.4()6.4(22 221 kkkk xyabpp