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

gm

The document discusses algorithms for generating circles and ellipses in graphics, emphasizing the importance of symmetry and efficient computation methods. It details the Midpoint Circle Algorithm, which uses incremental calculations to determine pixel positions along a circle's circumference, avoiding complex operations like square roots. Additionally, it introduces ellipse generation by modifying circle algorithms to accommodate different dimensions along major and minor axes.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

gm

The document discusses algorithms for generating circles and ellipses in graphics, emphasizing the importance of symmetry and efficient computation methods. It details the Midpoint Circle Algorithm, which uses incremental calculations to determine pixel positions along a circle's circumference, avoiding complex operations like square roots. Additionally, it introduces ellipse generation by modifying circle algorithms to accommodate different dimensions along major and minor axes.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

CIRCLE-GENERATING ALGORITHMS

a proce
Since the circle is a frequently used component in pictures and graphs, graphics
arcs is included in most
dure for generating either full circles or circular
either
packages. More generally, a single procedure can be provided to display
arrular or elliptical curves.

Properties oi Circles

Acircle is defined as theset of points that are all at a given distance r from a cen
tgr position (1, y) (Fig. 3-12). This distance relationship is expressed by the Figure 3-12)
Pythagoreattheoremin Cartesian coordinates as Circle with center coordinates
(i, y) and radius r.
(x-x + (y -y.)² = (3-24)

We could use this equation to calculate the position of points.on a circle circum
ference by stepping along the x axis in unit steps from x, -r to x, +rand calcu
lating the corresponding yvalues af eachposition as
(3-25)

But this is not the best method for generating a circle. One problem with this ap
pach is that tt involves considerable computation at each step. Moreover, the
cing between plotted pixel positions is not uniform, as demonstrated in Fig Figure 3-13
313. We could adjust the spacing by interchanging xand y (stepping through y Positive half of a circle
values and calculating r values) whenever the absolute value of the slope of the plotted with Eq. 3-25 and
circde is greater than 1. But this simply increases the computation and processing with (. ) = (0,0).
pEquired by the algorifim.
Another way to eliminate the unequal spacing shown in Fig. 3-13 is to cal
culate points along the circular boundary using polar çoordinates_z-and-0 (Fig.
12). Expressing the circe equation in parametric polar form yieldsthe pair of
equations
X=, t r cos.
(3-26)
y=y. trsin h

When adisplay is generated with these equations using a fixed angular step size,
rde is plotted with equally spaced points along the circumference. The step
Ze chosen for depends on the application and the display device. Larger an
Ar separations along the circumference can be connected with straight line
Bents to approximate the circular path. For a more continuous boundary on a
er display, we can set the step size at 1/r. This plots pixel positions
that are
Pproximately one unit apart.
Computaion can be reduced by considering the symmetry of circles. The
ape of the circle is sinmilar in each quadrant. We can generate the circle section
esecond quadrant of the xy plane by noting that the
bymmetric with respect to the y axis. And circle sections intwothecircle sections are
quadrants can be obtained from third and fourth
sections the first and second quadrants by
97
considering symmetry about the x axis. We can take this one step further an
(-y, x) (y, x) note that there is alsó symmetry between octants. Circle sections in adjacent
tants within one quadrant are symmetric with respect to the 45° line divid.ag
(-x, y) two octants. These symmetry conditions are illustrated in Fig,3-14, where apo
1450
at position (x, y) on a one-eighth circle sector is mapped into the seven
points in the other octants of the xv plane. Taking advantage of the circle sym
(-x,-y) x-y) try in this way, we,can generate all pixel positions around a circle by calcula
only the points within the sector from x =0 to x =y.
(-y, -x) W, -x) Determining pixel positions along acircle circumference using either
3-24 Qr Eq. 3-26 still requires a good deal of computation time. The Cartes
equation 3-24 involves multiplications and square-rbot calculations, while
parametric èquations contain multiplications and trigonometric calculatim
More efficient circle algorithms are based on incremental calculation of dedis
Symmetry of a circle. parameters, as in the Bresenham line algorithm, which involves only simple i
Calculation of a circle point ger operations.
(x, y) in one octant yields the
circle points shown for the
Bresenham's line algorithm for raster displays is adapted to circle gene
other seven octants.
tion by setting up decision parameters for finding the closest pixel to the circ
terence at each sampling step. The circle equation 3-24, however, is nonlineat
that square-rootevaluations would be required to compute pixel distances from
circular path. Bresenham s circle algorithm avoids these square-root calculatiu
by comparing the squares of the pixel separation distances.
Amethod for direct distance comparison is to test the halfway position
tween two pixels to determine if this midpoint is inside or outside the id
boundary. This method is more easily applied to other conics; and for an inte
circle radius, the midpoint approach generates the same pixel positions as
Bresenham circle algorithm. Also, the error involved in locating pixel postion
along any conic section using the midpoint test is limited to one-half the pix
separation.

Midpoint Circle Algorithm


(As in the raster line algorithm, we sample at unit intervals and determine
closest pixel position to the specified circle path at each step. For a given radis
and screen center position (, Y), we can first set up our algorithm to cal
pixel positions around a circle path centered at the coordinate origin (0, 0,
each calculated position (%, y) is moved to its proper screen position by adhn
to xand y, to Along the circle section from x = 0 to x = y in the first quadrg
the slope of the curve varies from 0 to Therefore, we can take unit steg
the positive x direction over this octant afd use a decision parameter to
mine which of the two possible y positions is closer to the circle path at each
Positjons in the other seven octants are then obtained by symmetry.
To apply the midpoint method, we define a circle function:
Siclelx, y)=ty

Any point (x,y) on the boundary of the circle with radius r satisñes the equa
fonelX,y) = 0. If the point is in the interior of the circle, the circle function is
tive. And if the point is outside the circle, the circle function is positive. 1o
marize, the relative position of any point (x, y) can be determined by checking
signof the circle function:

98
(<0, if (x, y) is inside the circle boundary
fdirieli, y) =0, if(r, y) is on the circle boundary
(>0, if lx, y) is outside the circle boundary Y, -1 Midpoint

De inde-function tests in 3-28 are performed for the midposition[ between pix
els near the circle path at each sampling step. Thus, the circle function is the deci x x, +1 x+ 2
sion parameter in the midpoint algorithm, and we can set up incremental calcu
lations for this function as we did in the line algorithm.
Figure 3-15 shows the midpoint between the twa candidate pixels at sam Midpoint between candidate
pling position x, + 1. Assuming we have just plotted the pixel at (x, y), we next pixes at 5ampling poSiton
need to determine whether the pixel at position(x. + 1, v) or the one at position x+1along a circular path.
+ 1,y: -1)is closer to the circle. Our decision parameter is the circle function
327 evaluated at the midpoint between these two pixels:

= (xp + 1 +

fp<, this midpoint is inside the circle and the pixel on scan line y, is closer to
bound
the circle boundary. Otherwise, the midposition is outside or on the circle
ary, and we select the pixel on scanliney, -1.
incremental calculations.
Successive decision parameters are obtained using
We obtain a recursive expression for the next decision parameter by evaluating
the circle function at sampling position x+1 tl=x+ 2:

l(4, +1)+1F+(ya1-

Pi+ =P t 2(x, + 1) + y-y)- (y -y)+1

of p
wiere yi, is either y,or yi-, depending on the sign+1
Increments or obtaining p+jare either 2x+ (ifp, is negative) or 2x+1
incremern
t-2yk+). Evaluation of the terms 2x and 2yk+1 can also be done
tally as
2x;+ = 2x, + 2
2y+ 2y-2

Ahe start position (0, r), these two terms have the values 0and 2r, respectively.
uccessive value is obtained byadding 2 to the previous. value of 2r and
Bubtrasting 2from the previous value of 2y.
he initial decision parameter is obtained by evaluating the circle function
at the start position (x Vo)= (0, r):
99
Chapter 3
Output Primitives

5
Po= - r (34
If the radius r is specified as an integer, we can
simply round p, to
Po =1-r (for r an integer)
since all increments are
integers.
As in Bresenham's line algorithm, the
sitions along the circumference of a circle midpoint method calculates pixel p
tions, assuming that the circle parameters using integer additions and subtra
nates. We can summarize the steps in are specified in integer screen
the midpoint circle algorithm coont
as follows.
TKCFW Library
Midpoint Circle Algorithm
1. Input radius rand
001236 the circumference ofcircle center (x, y,
a circle centered on and obtain the first point on
the origin as
(Xo, Yo = (0, r)
2. Calculate the
initial value of the decision
parameter as
Po=4 r

3. At each x posittòn,
p.<0, the next pointstarting at k =0, perform
along the circle centered the following test:
on (0,0) is (x+ y) andI
Pk+ =P+ 2x;t1
Otherwise, the next point along the
circle is (x, + 1, y - 1) and
Pk+1 =Pr t 2x+1t 1 - 2y;-1
where 2x=2x + 2and
4. Determine symmetry points2yk+=2y;
in the
- 2.
other
5. Move each
calculated pixel position (, y)seven
onto
octants.
tered on (x y) and plot the the circular path cen
Coordinate values:
rr+x y=yty.
6. Repeat steps 3through 5until x y.
100
Chapter 3
Output Primitives procedure circleMidpoint (xCenter, yOenter,
Var radius : Integer)
P. Xo y : integer i
procedure plot Points:
begin
setPixel (xCenter + x, yCenter + Y, 1) ;
setPixel (xCenter - x, yCenter +
setPixel (xCenter y, 1) ;
setPixel (xCenter X,X, yCenter - y, 1;
yCenter - y, 1) :
setPixl (xCenter +t y,
setPixl (xCenter - y, yCenter + *, 1):
setPixel (xCenter + y, yCenter t X, 1);
setPixel (xCenter - y, yCenter - x, 1) ;
end; plotPoints yCenter - %, 1)
begin
X := 0:
y := radius;
plotPoints;
p 1-
while x<radius;
y.do
begin
if p <0 then
X := % +
1
else
begin
y. := y - 1
end;
if p < 0 then
p i= p+ 2 * x
else + 1
p = p. + 3 * (x - y)
plotPoints
end;
+ 1;
end: { circleMidpoint )

3-6

ELLIPSE-GENERATING ALGORITHMS
Loosely stated, an ellipse is
generated by modifying an elongated circle.
ferent dimensions of an circle-drawing procedures Therefore,elliptical curves can
ellipse along the major to take into
and minor axes. account the i
P= (x, y)
(Properties oi Ellipses
An ellipse is
F2 defined as the set of points
two fixed positions such that
the two foci from any(foci) is the same for all points the sum of the distances frod
general equation of an point P = (x, y) on the (Fig. 3-17). If the
ellipse can be stated as ellipse are labeled d, and distances
d,, then t
Figure 3-17 d, + d, = constant
Ellipse generated about foci Expressing distances d, and d, in (3
F, and F. F; = (Iy y), we have terms of the focal
coordinates E = (, y) an
102 Vx - x)+ (y - y}+
Vr- x + (y - y)}=
constant
remaining radical, and then squaring
By squaring this equation, isolating theequation in the form
again, we can rewrite the generalellipse
Ax² + By' + Cry + Dx + Ey + F =0
evaluated in terms of the focal coor
where the coefficients A, B, C, D, E, and F are
the ellipse The major
dinates and the dimensions of the major and minor axes of of the ellipse to the
one side
AN0S 0s the straight line segment extending from the shorter dimension of the ellipse,
other through the foci. The minor axis spans
(ellipse center) between the two
biscting the major axis at the halfway position
foci. in an arbitrary orientation is
An interactive method for specifying an ellipse
ellipse boundary. With these three coordi igure 3-IS
to input the two foci and a point on the coefficients in
constant in Eq. 3-33. Then, the Ellipse centered at (a, y) with
nate positions, we can evaluate the along the elliptical path.
to generate pixels semimajor axis r, and
Eq. 3-34 can be evaluated and usedsimplified if the major and minor axes are ori semiminor axis y
Ellipse equations are greatly
Fig. 3-18, we show an ellipse in "stan
ented to align with the coordinate axes. In
oriented parallel to the x and y axes.
dard position" with major and minor axes
semimajor axis, and parameter r, labels
Parameter r, for thi_ example labels the
ellipse shown in Fig.3-18 can be written
the semiminor axisAThe equation of the parameters r, and r,as
in terms of the ellipse center coordinatesand

( standard posi
Using polar coordinates Yand 8, we can
also describe the ellipse in
ion with the parametric equations:
X = X, t r, cost 6-30)

Y=y t r, sin8
reduce computations. An ellipse
Symmetry considerations can be used to further
quadrants, but unlike a circle, it is not
in standard position is symmetric between quadrant. Thus, we must calculate pixel
symmetric between the two octants of a
one quadrant, then we obtain posi
positions along the elliptical arc throughout (Fig 3-19).
tions in the remainingthree quadrants by symmetry

Midpoint Ellipse Algorithm


Our approach here is similar to that used in displaying a
raster circle. Given pa
determine points x, y) for an ellipse
in standard
tameters , ry and (x, y), we
shift the points so the ellipse is cen
Position centered on the origin,and then we position, we
ellipse in nonstandard
ered at (, y). If we wish also to display thecoordinates to reorient the major and
could then rotate the ellipse about its center
only the display of ellipses in standard
uner axes. For the present, we consider transforming object orientations and
Position. We discuss general methods for
positions in Chapter 5. throughout the first quadrant in
The midpoint ellipse method is applied first quadrant according to the
TG parts. Figure 3-20 shows the division of the
Of an ellipse with r, <, Wetheprocess this quadrant by taking unit steps in
e z direction where the slope of curve has a magnitude less than l, and tak
magnitude greater than .
g unit steps in theydirection where the slope has a start
Kegions Iand 2 (Fig.3-20), can be processed in various ways. We can 103
position (0, r)and step clockwise along the ellipticalpath in the first quadrant,
Chapter 3 shifting from unit steps in x to unit steps
Output Primitives in y when the slope becomes less te
-1. Alternatively, we could start at (r, 0) and select points in a counterclock
order, shifting from unit steps in y to unit steps inr when the slope becom
greater than -1. With parallel processors, we could calculate pixel
positions
the two regions simultaneously. As an example of a sequential implernentation
(- X, y) (x, v the midpoint algorithm, we take the start positian at (0, r)
lipse path in clockwise order throughout the first quadrant. and step along the
We define an ellipse function from Eq. 3-35 with (r, y) =(0,0) as
(3
-y
(x, -y)
which has the following properties:

<0, if (x, y) is inside the ellipse boundary


Figure 3-19 felipala, y) =0, if (x, y) is on the ellipse boundary (3
Symmetry of an ellipse. l>0,
Calculation of a point (x, y) if (x, y) is outside the ellipse boundary
in one quadrant yields the
ellipse points shown for the Thus, the ellipse function feli, ) serves as the decision parameter in the m
other three quadrants. point algorithm. At each sampling position, we select the next pixel along the
Iipse path according to the sign of the ellipse function evaluated at the midpoi
between the two carndidate pixels.
Starting at (0, r,), we take unit steps in thex direction until we reach the
boundary between region l and region 2 (Fig. 3-20). Then we switch to unit stes
Regi
1
o Slope =-1 n the y direction over the remainder of the curve in the first quadrant. At ead
Region
step, we need to test the value of the slope of the curve. The ellipse slope is cala
lated from Eq. 3-37 as

dy2r;x
dx 2r:y
At the boundary between region 1and region 2, dy/dx = -1 and

2r;x =2rày
Figure 3-20
Ellipse processing regions. Therefore, we move out of region 1 whenever
Over region 1, the magnitude
of the ellipse slope is less 2r;x >2riy (34
than 1; over region 2, the
magnitude of the slope is Figure 3-21 shows the midpoint between the two candidate pixels at sat
greater than 1.
pling position x, + 1 in the first region. Assuming position (x, y) has been st
lected at the previous step, we determine the next position along the ellipse pa
by evaluating the decision parameter (that is, the ellipse function 3-37) at ths
midpoint:

(3-40

If pl, <0, the midpoint is inside the ellipse and the pixel on scan
to the ellipse boundary. Otherwise, the midposition is outside line y, is cl
104 or on the ellipF
boundary, and we select the pixel on scan line y, - 1.
parameter
decision
+ 2), the
position (I+ t 1= I
sampling
Atthe next
evaluated as
is
r region 1 midpoint
Y, -1
+ 1, yi-1 -

Figure 3-21
Midpoint between candidate
position
(3-42) pixels at sampling
path.
x,+1along an elliptical

depending on the sign


of pl,.
or y, - 1, amounts:
where y., is either y,are incremented by the
following
Decision parameters
if pl,<0
increment =
if p,k > 0
|2r;x,t r; - 2riy
can be calcu
parameters
algorithm, increments for the decision 2r,x and
As in the circle subtraction, since values for the terms
and two
lated using only addition incrementally. At the initial position (0, r), the
2r;ycan also be obtained
terms evaluate to
(3-43)
2r;r = 0
(3-44)
2r3y = 2rir,
values are obtained by adding 2r, to 3-43
As I and y are incremented, updated at each step,
The updated values are compared
and subtracting 2r? from 3-44.region satisfied..
2 when condition 3-40 is
and we move from region 1 to decision parameter is obtained by
evalu
n region 1,the initial value of the (0, r):
ating the ellipse function at the start position (xo yo =

1
(3-45)
pl, =r;-rir, +:
Yer regiôn 2, we sample at unit steps in the negative y direction, and the
t15 now taken between horizontal pixels at each step (Fig. 3-22). For this
On,the decision parameter is evaluated as

(3-46)
105
Chapter 3
Output Primitives x y -rar 0

YE -1 ligure -22
Tmidpoint Midpoint between candidate pixels
at sampling positiony, - 1along an
X x + 1 x, + 2 elliptical path.

If p2, >0, the midposition is outside the ellipse boundary, and we select the pixel
at x. If p2, s 0, the midpoint is inside or on the ellipse boundary, and we select
pixel position x
To determine the relationship between successive decision parameters in
region 2, we evaluate the ellipse function at the next sampling step yke1 -1=
y-2:

or

3-43)

with x 1set either to x or to x + 1,depending on the sign of p2,.


When we enter region 2, the initial position (xo, Yo) is taken as the last posi
tion selected in region 1 and the initial decision parameter in region 2 is then
1

(3-49)

+ riyo -12 -rr


To simplify the calculation of p2o, we could select pixel positions in
wise order starting at (r, 0). Unit steps would then be taken in the counterclock
positive y di
rection up to the last position selected in region 1.
The midpoint algorithm can be adapted to generate an ellipse in
dard position using the ellipse function Eq. 3-34 and calculating nonstan
pixel
over the entire elliptical path. Alternatively, we could reorient the ellipsepositions
standard position, using transformation methods axes to
discussed
the midpoint algorithm to determine curve positions, then in Chapter 5, apply
convert
pixel positions to path positions along the original ellipse orientation. calculated
Assuming r, ryy and the ellipse center are given in integer screen coordh
nates, we only need incremental integer calculations
decisión parameters in the midpoint ellipse algorithm.toThe determine values for the
increments r, r, 2r
and 2r are evaluated once at the beginning of the
procedure. A summary of the
midpoint ellipse algorithm is listed in the following steps:
106
Section 3-6

Midpoint Ellipse Algorithm Ellipse-Generating Algorithms

and obtain the first point on an


1. Input , Ty, and ellipse center (x,y),
ellipse cerntered on the origin as
(xo, yo) = (0, r,)
parameter in region 1 as
2. Calculate the initial value of the decision

k = 0, perform the follow


3. At each x position in region 1, starting atthe ellipse centered on (0, 0)
ing test: lf pl, <0, the next point along
is (x,1, 4) and
pliii =pl, +2rj1 +r
Otherwise, the next point along the circle is (x + 1, y, -
1)and

pli = pl, + 2rix+1 - 2riyk t


with

2r1=2rj +2r, 2riy-1 2riy - 2r?


region 2 using
4. Calculate the initial value of the decision parameter in
the last point (x, Yo) calculated in regionl as

starting at k = 0, perform the follow


3. At each y, position in region 2,
along the ellipse centered on (0, 0) is
Ing test: If p2,>0,the next point
y -1) and

p2k+1 =p2- 2riy+1 +i


Otherwise, the next point along the circle is (x +
1,y - 1) and

p2i1 =p2 +2r1- 2riy+1+ ri


as in region 1.
Using the same incremental calculations for x and y
. Determine symmetry points in the other three quadrants. cen
, Move each calculated pixel position (x, y) onto the elliptical path
ered on (z, V) and plot the coordinate values:
X=Xt

Repeat the steps for region 1until 2rxz 2riy.


107

You might also like