Midpoint Algorithm For Ellipse
Midpoint Algorithm For Ellipse
Let us consider one quarter of an ellipse. The curve is divided into two regions. In
region I, the slope on the curve is greater than –1 while in region II less than –1.
y
Region I
b
dy/dx = –1
Region II
a x
m = –1
SE
Prediction
(xk+1, yk–½)
Region I
Page 1 of 5
Mid-point Ellipse Algorithm
Pk = f(xk+1, yk–½)
= b2(xk+1)2 + a2(yk–½)2 – a2b2
= b2(xk2 + 2xk + 1) + a2(yk2 – yk + ¼) – a2b2
If Pk < 0, select E :
If Pk > 0, select SE :
If E is selected,
∆Pk+1E = b2(2xk + 5)
∆2PkE = ∆Pk+1E – ∆PkE = 2b2
If SE is selected,
∆Pk+1E = b2(2xk + 5)
∆2PkE = ∆Pk+1E – ∆PkE = 2b2
Initial values:
x0 = 0, y0 = b, P0 = b2 + ¼a2(1 – 4b)
∆P0E = 3b2, ∆P0SE = 3b2 – 2a2(b – 1)
In region II (dy/dx < –1), all calculations are similar to that in region I except that y is
decremented in each step.
Page 2 of 5
Mid-point Ellipse Algorithm
(xk, yk)
SE
Prediction
(xk+½, yk–1)
Region II
Pk = f(xk+½, yk–1)
= b2(xk+½)2 + a2(yk–1)2 – a2b2
= b2(xk2 + xk + ¼) + a2(yk2 – 2yk + 1) – a2b2
If Pk > 0, select S :
If Pk < 0, select SE :
If S is selected,
∆Pk+1S = a2(5 – 2yk)
∆2PkS = ∆Pk+1S – ∆PkS = 2a2
If SE is selected,
∆Pk+1S = a2(5 – 2yk)
∆2PkS = ∆Pk+1S – ∆PkS = 2a2
Page 3 of 5
Mid-point Ellipse Algorithm
dy − bx
Set f(x, y) = 0, = .
dx a 1 − x 2 a 2
2
a2 b2
When dy/dx = –1, x = and y = .
a 2 + b2 a 2 + b2
a2 b2
At region I, dy/dx > –1, x < and y > , therefore
a 2 + b2 a 2 + b2
2a 2 b2
∆PkSE < b2 + 3 − 2a 2 − 1 = 2a 2 + 3b2 .
a 2 + b2 a +b
2 2
The algorithm described above shows how to obtain the pixel coordinates in the first
quarter only. The ellipse centre is assumed to be at the origin. In actual
implementation, the pixel coordinates in other quarters can be simply obtained by use
of the symmetric characteristics of an ellipse. For a pixel (x, y) in the first quarter, the
corresponding pixels in other three quarters are (x, –y), (–x, y) and (–x, –y)
respectively. If the centre is at (xC, yC), all calculated coordinate (x, y) should be
adjusted by adding the offset (xC, yC). For easy implementation, a function
PlotEllipse() is defined as follows:
Page 4 of 5
Mid-point Ellipse Algorithm
Set x = 0 and y = b
P = b2 + (a2(1 – 4b) – 2)/4 // Intentionally –2 to round off the value
∆PE = 3b2
∆2PE = 2b2
∆PSE = ∆PE – 2a2(b – 1)
∆2PSE = ∆2PE + 2a2
// Plot the pixels in region I
PlotEllipse(xC, yC, x, y)
while ∆PSE < 2a2 + 3b2
if P < 0 then // Select E
P = P + ∆PE
∆PE = ∆PE + ∆2PE
∆PSE = ∆PSE + ∆2PE
else // Select SE
P = P + ∆PSE
∆PE = ∆PE + ∆2PE
∆PSE = ∆PSE + ∆2PSE
decrement y
end if
increment x
PlotEllipse(xC, yC, x, y)
end while
// Set initial values in region II
P = P – (a2(4y – 3) + b2(4x + 3) + 2) / 4 // Intentionally +2 to round off the value
∆PS = a2(3 – 2y)
∆PSE = 2b2 + 3a2
∆2PS = 2a2
// Plot the pixels in region II
while y > 0
if P > 0 then // Select S
P = P + ∆P E
Page 5 of 5