Page 75 - DCAP313_LAB_ON_COMPUTER_GRAPHICS
P. 75
Unit 5: Implementing Ellipse Algorithm
x 0 and y 0 will be the accumulative results from region I at the boundary. It is not necessary to Notes
calculate them from values of a and b.
P 0 = P k –¼[a (4y 0 – 3) + b (4x 0 + 3)] where P k is the accumulative result from region I at the
I
2
I
2
boundary.
∆P 0 = b (2x 0 + 3)
E
2
2
SE
∆P 0 = 2a + 3b 2
Implementation of the Algorithm
The algorithm explained shows how to get 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:
PlotEllipse (xC, yC, x, y)
putpixel(xC+x, yC+y)
putpixel(xC+x, yC–y)
putpixel(xC–x, yC+y)
putpixel(xC–x, yC–y)
end PlotEllipse
The function to draw an ellipse is described in the following pseudo-codes:
DrawEllipse (xC, yC, a, b)
Declare integers x, y, P, ∆PE, ∆PS, ∆PSE, ∆2PE, ∆2PS and ∆2PSE
// Set initial values in region I
Set x = 0 and y = b
P = b + (a (1 – 4b) – 2)/4 //Intentionally – 2 to round off the value
2
2
∆P = 3b 2
E
∆ 2 P = 2b 2
E
∆P = ∆P – 2a (b – 1)
2
E
SE
SE
2
E
∆ 2 P = ∆ P + 2a 2
//Plot the pixel in region I
PlotEllipse(x c , y c , x, y)
While ∆P < 2a + 3b 2
2
SE
if P < 0 then // Select E
P = P + ∆P E
∆P = ∆P + ∆ P E
E
2
E
SE
SE
∆P = ∆P + ∆2P E
else // Select SE
P = P + ∆P SE
∆P = ∆P + ∆ P E
E
2
E
SE
2
SE
∆P = ∆P + ∆ P SE
decrement by
end if
LOVELY PROFESSIONAL UNIVERSITY 69