0% found this document useful (0 votes)
48 views8 pages

Efficient Integer Algorithms For The Generation of Conic Sections

Algorithm for speedy display of conic sections on computer

Uploaded by

Ram Yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views8 pages

Efficient Integer Algorithms For The Generation of Conic Sections

Algorithm for speedy display of conic sections on computer

Uploaded by

Ram Yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Comput. & Graphics, Vol. 22, No. 5, pp.

621±628, 1998
# 1998 Elsevier Science Ltd. All rights reserved
PII: S0097-8493(98)00069-7 Printed in Great Britain
0097-8493/98/$ - see front matter

Technical Section

EFFICIENT INTEGER ALGORITHMS FOR THE


GENERATION OF CONIC SECTIONS
A. AGATHOS, T. THEOHARIS$ and A. BOEHM,

Department of Informatics, University of Athens, Panepistimioupolis, TYPA Buildings, 157 71 Athens,


Greece

AbstractÐEcient integer 8-connected algorithms for the fast generation of Conic Sections whose axes
are aligned to the coordinate axes are described based on a Bresenham-like methodology. Performance
results show that in the case of the ellipse, the algorithm is at least as fast as other known integer algor-
ithms but requires lower integer range and always performs correct region transitions. Antialiasing is
easily incorporated. # 1998 Elsevier Science Ltd. All rights reserved

1. INTRODUCTION gorithm. Section 3 describes the derivation of the


Conic sections (ellipse, hyperbola and parabola) are ellipse generating algorithm. Section 4 brie¯y
important geometric primitives and, after the describes the parabola and hyperbola algorithm
straight line and circle, have received much atten- derivations. Appendix A, Appendix B and
tion from the computer graphics community. A Appendix C give the ellipse, hyperbola and para-
number of algorithms have appeared in the litera- bola Pascal procedures.
ture for the generation of these primitives [1±7].
We have derived ecient integer 8-connected al-
gorithms for the generation of ellipses hyperbolas 2. REFORMATTING THE BRESENHAM CIRCLE
and parabolas whose axes are aligned with the axes ALGORITHM
of the plane, using a Bresenham-like methodology We develop a small variation to Bresenham's cir-
[2] simulating, in e€ect, the midpoint technique [1]. cle generating algorithm which concerns the cri-
Our algorithms use integer arithmetic in a straight- terion for next pixel selection and octant change
forward manner without any scaling and do not detection. We shall later use this small change in a
lack in performance with regard to any previous al- generalisation of the algorithm to the more complex
gorithm. Spacewise, they require a small constant conic sections; the integer algorithms derived in this
number of integer variables. They are also sym- way are correct and ecient.
metric as to the number of arithmetic operations Consider the second octant of a circle of integer
per pixel generated in each octant. Thus they are Radius R (Fig. 1). As in Bresenham's algorithm,
highly suitable for teaching. Erroneous pixels are having chosen pixel A, we de®ne:
not generated at region boundaries due to a better
region transition criterion. In the case of the ellipse d1 ˆ R12 ÿ R2 ˆ … y2i ‡ …x i ‡ 1†2 † ÿ … y2 ‡ …x i ‡ 1†2 †
our algorithm requires lower integer ravage than
Kappel's integer ellipse drawing algorithm. Our al- ˆ y2i ÿ y2 ,
gorithms are very suitable for hardware implemen-
tation especially in view of increasing display d2 ˆ R2 ÿ R22
resolutions and the availability of high-resolution
plotters. Our parabola algorithm in particular, is ˆ … y2 ‡ …x i ‡ 1†2 † ÿ …… yi ÿ 1†2 ‡ …x i ‡ 1†2 †
suitable for very high resolutions due to the elimin-
ation of the calculation of a square factor. We are ˆ y2 ÿ … yi ÿ 1†2
also able to exploit the value of the decision vari-
able for antialiasing. in a manner similar to Fellner [8]. We then take:
The rest of this paper is organised as follows: d ˆ d1 ÿ d2 ˆ … y2i ÿ y2 † ÿ … y2 ÿ … yi ÿ 1†2 †,
Section 2 presents a modi®ed Bresenham circle al-
where d is the decision variable for the selection of
the next pixel between the two candidates B and D.
$Corresponding author. Tel.: +0030-1-7275106; Fax:
+0030-1-7231569; E-mail: [email protected]. At this point we take a diversion from
,We would like to dedicate this paper to the memory Bresenham's algorithm by setting e = yiÿy to get:
of our dear friend Alexandros Boehm who died on May
1st 1998. d…e† ˆ ÿ2 e2 ‡ 4yi e ‡ 1 ÿ 2yi : …1†

621
622 A. Agathos et al.

Fig. 1. Candidate Pixels B or D

The above expression is monotonically increasing in each step in the generation the X value is therefore
the interval e $ (ÿ1,yi]. We can therefore use the always incremented. It must be determined whether
value of d(e) at e = 1/2 i.e. d(1/2) = 1/2 as the de- the Y value should be decremented or not. This
cision value: region corresponds to the 2nd octant of the circle
(Fig. 1) and we de®ne:
if d R 1/2 then pixel B is chosen
else pixel D is chosen. d1 ˆ y2i ÿ y2

d2 ˆ y2 ÿ … yi ÿ 1†2 ,

Note that since d is the integer, we can replace as in the case of the circle. Taking as decision vari-
the 1/2 by 0, without a€ecting the semantics. Note able:
that what we really accomplish here is to simulate a d ˆ a2 …d1 ÿ d2†,
midpoint-type technique [1].
Due to the 8-way symmetry of the circle, we need the following will hold (Fig. 1):
not consider another octant; in the case of the
if dRa2 =2 then pixel B…x i ‡ 1, yi † is chosen
ellipse, which has 4-way symmetry, we need to con-
sider 2 regions which make up one-quarter of the else pixel D…x i ‡ 1, yi ÿ 1† is chosen: …2†
ellipse.
The only reason for multiplying by a2 is to facilitate
its incremental derivation (see below). Of course the
3. DERIVATION OF THE ELLIPSE GENERATING division of the integer value a2 by 2 can be replaced
ALGORITHM by one Right Shift of a2; since d is an integer the
Consider an ellipse centered at the origin of the result is semantically equivalent.
2D cartesian space de®ned by: We next derive the incremental computation of
the decision variable; its value for the ith step of
x 2 =a2 ‡ y2 =b2 ˆ 1
the algorithm in Region 1 is:
The ellipse has a 4-way symmetry and it is therefore d1,i ˆ a2 …d1 ÿ d2†
only necessary to generate its arc in the ®rst quad-
rant. Here we distinguish two regions separated by ˆ a2 y2i ‡ a2 … yi ÿ 1†2 ÿ 2a2 y2
the point on the ellipse where dy/dx = ÿ 1. In the
®rst region the axis of major movement is X and in
the second Y (Fig. 2).
We must derive an incremental expression for the
decision variables in Regions 1 and 2, the initial
values of the decision variables and a condition to
detect the transition from Region 1 to Region 2.

3.1. Decision variable for Region 1


Let us begin by considering the 1st region of
Fig. 2, where the X-axis is the major axis of move-
ment. Assume that the ellipse is generated in a
clockwise manner starting from the point (0,b). At Fig. 2. Ellipse
Ecient integer algorithms 623

Given that a2y2=a2b2ÿb2(xi+1)2 [equation of 3.2. Transition from Region 1 to Region 2


ellipse], Van Aken [3] proposed a transition criterion
based on midpoints which gives correct results but
d1,i ˆ ÿ2a2 b2 ‡ 2b2 …x i ‡ 1†2 is computationally expensive. Kappel's method [4]
is ecient but there exist cases where an erroneous
‡ a2 y2i ‡ a2 … yi ÿ 1†2 : …3† pixel can arise at region boundaries. This is
because:
We shall now de®ne d1,i + 1 in terms of d1,i:
i. Region change is detected by taking the tangent on
d1,i‡1 ˆ ÿ 2a2 b2 ‡ 2b2 …x i‡1 ‡ 1†2 ‡ a2 y2i‡1 integer coordinates rather than the true ellipse as
acknowledged in Kappel's paper [4] and
‡ a2 … yi‡1 ÿ 1†2
ii.A drastic change of curvature can take place within
ˆ ÿ 2a2 b2 ‡ 2b2 ……x i ‡ 1† ‡ 1†2 ‡ a2 y2i‡1
a single pixel.
‡ a2 … yi‡1 ÿ 1†2 fx i‡1 ˆ x i ‡ 1g

ˆ ÿ 2a2 b2 ‡ 2b2 …x i ‡ 1†2 ‡ 2b2 We propose a transition criterion which combines


2 2 the advantages of the above two methods (see
‡ 4b …x i ‡ 1† ‡ a2 y2i‡1 2
‡ a … yi‡1 ÿ 1† …4†
Fig. 3).
Our integer algorithm uses a correct criterion
But ÿ 2a2b2 + 2b2(xi + 1)2=d1,i ÿ a2y2i ÿ a2(yi ÿ 1)2,
which is based on the value of the error function in
therefore the next column of pixels. In particular, the value
of the error function d at the point (xi + 1, yi ÿ 3/
d1,i‡1 ˆ d1,i ‡ a2 y2i‡1 ‡ a2 … yi‡1 ÿ 1†2
2) is considered; note that if the ellipse `passes
under' this point then an octant transition is
ÿ a2 y2i ÿ a2 … yi ÿ 1†2 ‡ 2b2 ‡ 4b2 …x i ‡ 1†
required.
We can express the error function d given in
If d1,i>a2/2 then yi+1=yi ÿ 1 by Equation (2), thus
Equation (3) in terms of e(=yi ÿ y) in a manner
d1,i‡1 ˆ d1,i ‡ 2b2 ‡ 4b2 …x i ‡ 1† ÿ 4a2 … yi ÿ 1†: similar to the circle (1):
d…e† ˆ ÿ2 a2 e2 ‡ 4 a2 yi e ‡ a2 ÿ 2 a2 yi
if d1,i Ra2/2 then yi+1=yi by Equation (2), thus
setting e = 3/2 we get:
d1,i‡1 ˆ d1,i ‡ 2b2 ‡ 4b2 …x i ‡ 1†:
d…3=2† ˆ ÿ2 a2 …3=2†2 ‡ 4 a2 yi …3=2† ‡ a2 ÿ 2 a2 yi
The initial value d1,0 is determined by substituting
the coordinates of the ®rst pixel of Region 1 (0,b) ˆ 4 a2 … yi ÿ 1† ‡ a2 =2:
for (xi, yi) in the expression for d1,i: The transition criterion is as follows:
2 2
d1,0 ˆ 2b ‡ a …1 ÿ 2b†
if dR 4a2(yiÿ1) + a2/2 then we remain in the same
region

Fig. 3. Transition from Region 1 to Region 2


624 A. Agathos et al.

else we change region. d2,i ˆ ÿ 2a2 b2 ‡ b2 x 2i ‡ 2a2 … yi ÿ 1†2

As we have already mentioned above the division ‡ b2 …x i ‡ 1†2 …5†


a2/2 can be replaced by a semantically equivalent
An incremental expression for d2,i can be derived in
Right Shift. The above criterion is optimised and
a similar manner to d1,i to be:
used in the code fragment in Appendix A. We have
to note that the above criterion works correctly for d2,i‡1 ˆ d2,i ‡ b2 x 2i‡1 ‡ b2 …x i‡1 ‡ 1†2 ÿ b2 x 2i
yi r 1. Square corners, as mentioned by McIlroy [6],
can be predicted easily in our algorithm using one ÿ b2 …x i ‡ 1†2 ‡ 2a2 ÿ 4a2 … yi ÿ 1†
copy of the pixel last printed in the ®rst region.
which can be simpli®ed, depending on the value of
Other degenerate cases, such as McIlroy's long thin
xi + 1, as follows:
ellipses, have been tested. It is heuristically believed
that no degenerate cases will cause the algorithm to if d2,i>b2/2 then xi+1 = xi, thus
fail.
The initial value of the decision variable d2,i for
d2,i‡1 ˆ d2,i ‡ 2a2 ÿ 4a2 … yi ÿ 1†,
Region 2 (see Equation (5) below) can be calculated
by adding to the ®nal value of d1,i the di€erence
d2,i ÿ d1,i. From Equation (4) and Equation (5): if d2,i < b2/2 then xi+1=xi + 1 thus
d2,i ˆ d1,i ÿ a2 …2yi ÿ 1† ÿ b2 …2x i ‡ 1†:
d2,i‡1 ˆ d2,i ‡ 2a2 ‡ 4b2 …x i ‡ 1† ÿ 4a2 … yi ÿ 1†:

3.3. Decision variable for Region 2


In Region 2 the expressions for d1 and d2, as can
be seen from Fig. 4, are:
d1 ˆ …xi ‡ 1†2 ÿ x2 3.4. Antialiasing and results
In the past a linear antialiasing function for conic
d2 ˆ x2 ÿ x2i sections has been proposed [5]. A similar function
could be applied to our algorithm (speci®cally the
function (d(e) ÿ d(0))/(d(1) ÿ d(0))), but unfortu-
Having chosen pixel A(xi, yi), the di€erence nately such linear approximation only works cor-
d = d1 ÿ d2 determines which of the 2 pixels in the rectly for very few bits of colour ( [9], page 971).
next row of pixels (y = yi ÿ 1) is closer to the real We have incorporated a very fast version of the
ellipse: box-®ltering antialising technique [10] in the ellipse
if d R b2/2 then pixel D(xi + 1, yi ÿ 1) is drawing algorithm, achieving satisfactory results,
selected see Fig. 5.
else pixel C(xi, yi ÿ 1) is selected. Since it is computationally expensive to compute
the reverse of d(e) function, we perform a binary
The decision variable (scaling again by b2) for
search of the given value of d in the space of values
Region 2 step i is de®ned to be:
d(t/n, yi), t = 0 . . .n where n is the number of grey
d2,i ˆ b2 …d1 ÿ d2† levels available to determine the required grey level
(in the second region we would use d(t/n, xi)). The
ˆ b2 …x i ‡ 1†2 ‡ b2 x 2i ÿ 2b2 x 2 …4†

Given that b2x2 = a2b2 ÿ a2(yi ÿ 1)2 [equation of


ellipse],

Fig. 4. Ellipse Construction (Region 2) Fig. 5. Midpoint and Antialiased ellipse (4 Bits per pixel)
Ecient integer algorithms 625

values of the function d(t/n, yi) can be precomputed.


Note that in the case of antialiasing we do not fol-
low the midpoint philosophy as it is always necess-
ary to keep the 2 pixels above and below the true y-
intercept for Region 1. The y value of these 2 pixels
is decremented when dr d(1,yi) and an octant
change occurs when d r d(2,yi).
Our 8-connected algorithm produces equally
good antialiased results as 4-connected algorithms
[5], but an 8-connected algorithm is advantageous
in the case of a single bit per pixel since it provides
regions of constant thickness.
The time performance of the new algorithm was
compared against the algorithm described by
Kappel [4] as well as an integer version of Kappel's
algorithm which we derived by suitably scaling by 4 Fig. 6. Hyperbola.
its variables in order to achieve the best possible
performance. The integer Kappel algorithm exhibits
4.1. Hyperbola
similar performance to our ellipse algorithm; this
Figure 6 shows a hyperbola centered at (0,0),
should be expected because the integer version of
symmetric about the X and Y-axes, de®ned by the
Kappel we derived is very similar in structure to
equation
our algorithm. However, the integer Kappel pro-
duces arithmetic over¯ow quicker than ours. It also x 2 =a2 ÿ y2 =b2 ˆ 1
requires a greater integer range as can be seen in
Scheme 1 which compares the two algorithms in We consider here only the case a>b in which the
terms of the maximum integer value required, as hyperbola has 2 regions, one in which the major
ellipse size increases. The maximum integer arises in axis of movement is Y (Region 1) and another in
the calculation of y_slope in both of the algorithms. which the major axis of movement is X (Region 2).
It must be restated here that Kappel's algorithm If a < =b there is no Region 2. The two regions
can give rise to erroneous pixels at the 4 region are separated by the point where the tangent to the
boundaries as pointed out by Kappel (see Fig. 3). hyperbola has slope dy/dx = 1.
Our integer algorithm does not exhibit this pro- In Region 1 (see Fig. 7), the expressions for a
blem. measure of the distance of the true hyperbola to the
2 nearest pixels are:
d1 ˆ b2 x 2 ÿ b2 x 2i

b2 ˆ b2 …x i ‡ 1†2 ÿ b2 x 2
4. THE HYPERBOLA AND PARABOLA ALGORITHMS
Setting e = x ÿ xi we get:
In a similar manner to the ellipse, one can derive
incremental error expressions for the construction d…e† ˆ d1 ÿ d2
of our hyperbola and parabola generating algor-
ithms. ˆ 2b2 e2 ‡ 4b2 x i e ÿ b2 ÿ 2b2 x i : …5†

Scheme 1. Maximum integer graph.


626 A. Agathos et al.

Fig. 7. Hyperbola construction (Region 1).

The above expression is monotonically increasing in


the interval e $ [ÿ xi, +1). Thus by noting that
d(1/2) = ÿ b2/2, the following will hold (Fig. 7): Fig. 8. Parabola.
2
if dr ÿ b =2then pixel D…x i ‡ 1,yi ‡ 1† is chosen
else pixel C…x i ,yi ‡ 1† is chosen: …6†
d2,i‡1 ˆ d2,i ÿ 2b2 ÿ 4b2 …x i ‡ 1† ‡ 4a2 … yi ‡ 1†,
We next derive the incremental computation of the
decision variable whose value for the ith step of the
if d2,i>a2/2 then yi+1=yi, thus
algorithm in Region 1 is:
d1,i ˆ d1 ÿ d2
d2,i‡1 ˆ d2,i ÿ 2b2 ÿ 4b2 …x i ‡ 1†
2 2 2 2 2 2
ˆ 2a b ‡ 2a … yi ‡ 1† ÿ b x 2i 2
ÿ b …x i ‡ 1†

which can be incrementally derived to be: In a manner similar to the ellipse, the transition cri-
2
terion from Region 1 to Region 2 is as follows:
if d1,i rÿ b /2 then xi+1=xi + 1 by
Equation (6), thus if d < 4 b2(xi+1) ÿ b2/2 then we remain in the
d1,i‡1 ˆ d1,i ‡ 2a2 ‡ 4a2 … yi ‡ 1† ÿ 4b2 …x i ‡ 1†, same region else we
if d1,i<ÿ b2/2 then xi+1=xi by Equation (6), change region.
thus

d1,i‡1 ˆ d1,i ‡ 2a2 ‡ 4a2 … yi ‡ 1†:


The expression for the initial value of the error
term in Region 2 can then be derived:
The initial value d1,0 is determined by substituting d2,i ˆ d1,i ÿ b2 …1 ‡ 2x i † ÿ a2 …1 ‡ 2yi †
the coordinates of the ®rst pixel of Region 1 (a,0)
for (xi, yi) in the expression for d1,i in Equation (7):
d1,0 ˆ 2a2 ÿ b2 …1 ‡ 2a†: 4.2. Parabola
Figure 8 shows a parabola centered at (0,0) sym-
In Region 2, expressions for a measure of the dis-
metric about the X-axis de®ned by the equation
tance of the true parabola to the 2 nearest pixel
centers are: y2 ˆ 2px

d1 ˆ a2 … yi ‡ 1†2 ÿ a2 y2 In Region 1 the axis of major movement is Y


while in Region 2 it is X. The two regions meet at
d2 ˆ a2 y2 ÿ a2 y2i x = p/2, y = p where the tangent to the parabola
has slope dy/dx = 1.
The error term is: In Region 1 the expressions for a measure of the
d2,i ˆ d1 ÿ d2 distance of the true parabola to the 2 nearest pixels
are:
ˆ 2a2 b2 ÿ 2b2 …x i ‡ 1†2 ‡ a2 … yi ‡ 1†2 ‡ a2 y2i
d1 ˆ px ÿ px i
which can be incrementally derived to be: d2 ˆ p…x i ‡ 1† ÿ px

if d2,i R a2/2 then yi+1=yi+1, thus The error term is:


Ecient integer algorithms 627

d1,i ˆ d1 ÿ d2 derivations, are at least as fast as previous integer


2 algorithms, require lower integer arithmetic pre-
ˆ … yi ‡ 1† ÿ px i ÿ p…x i ‡ 1†
cision and do not set erroneous pixels at region
which can be incrementally derived to be: boundaries, thus incorporating the advantages of
well-known previous algorithms. They are very suit-
if d1,i r 0 then xi+1=xi+1, thus
able for high performance applications and teach-
ing. Fast antialiasing can also be incorporated.
d1,i‡1 ˆ d1,i ‡ 2… yi ‡ 1† ‡ 1 ÿ 2p,

REFERENCES
if d1,i<0 then xi+1=xi, thus
1. Pitteway, M. L. V., Algorithms for drawing ellipses
or hyperbolae with a digital plotter. Computer J.,
d1,i‡1 ˆ d1,i ‡ 2… yi ‡ 1† ‡ 1, 1967, 10(3), 282±289.
2. Bresenham, J. E., A linear algorithm for incremental
digital display of circular arcs. CACM, 1977, 20(2),
100±106.
In Region 2, expressions for a measure of the dis- 3. Van Aken, J. R., An ecient ellipse-drawing algor-
tance of the true parabola to the 2 nearest pixel ithm. CG&A, 1984, 4(9), 24±35.
centers are: 4. Kappel M. R., An Ellipse-Drawing Algorithm for
Raster Displays. In Earnshaw R. (ed) Fundamental
d1 ˆ … yi ‡ 1†2 ÿ y2 Algorithms for Computer Graphics, NATO ASI Series,
Springer-Verlag, Berlin, 1985, pp. 257±280.
d2 ˆ y2 ÿ y2i 5. Pitteway M. L. V. and Ebadollah Banissi, Soft
Edging Fonts. Computer Graphics Technology and
Systems. In Proceedings of the conference held at
The error term is:
Computer Graphics `87, London, October 1987.
d2,i ˆ d1 ÿ d2 6. McIlroy, M. D., Getting Raster ellipses right. ACM
TOG, 1992, 11(3), 259±275.
7. Da Silva D., Raster Algorithms for 2D Primitives.
ˆ … yi ‡ 1†2 ‡ y2i 4p…x i ‡ 1† Master's Thesis, Computer Science Department,
Brown University, Providence, R.I., 1989.
which can be incrementally derived to be: 8. Fellner W. D., Computer Gra®k. Bibliogra®sches
if d2,i<0 then yi+1=yi+1, thus Institut, Zuerich, 1992.
9. Foley J. D. et al., Computer Graphics, Principles and
Practice, 2nd Edn. Addison-Wesley, 1990.
d2,i‡1 ˆ d2,i ‡ 4… yi ‡ 1† ÿ 4p, 10. Wu, X., An ecient antialiasing technique. Computer
Graphics, 1991, 25(4), 143±152.

if d2,i>0 then yi+1=yi, thus


APPENDIX A
d2,i‡1 ˆ d2,i ÿ 4p, Ellipse Pascal Code

The expression for the error, when making the tran-


sition from Region 1 to Region 2 can be derived to
be:
d2,i ˆ d1,i ‡ y2i ÿ p…2x i ‡ 3†,
The square in the calculation of d2,i gives rise to
large integers and is unsuitable for hardware im-
plementation. We have proved and veri®ed exper-
imentally that the ®nal value of d1,i will be 1 or
p+1 and:
if d1,i=1 then d2,i= ÿ 4p + 1,
if d1,i=p + 1 then d2,i= ÿ 2p + 1,

5. CONCLUSIONS

Despite years of research into basic graphics al-


gorithms, new algorithms still emerge. The integer
algorithms for conic sections described in this paper
have straightforward Bresenham-like symmetric
628 A. Agathos et al.

APPENDIX C
Parabola Pascal Code

APPENDIX B
Hyperbola Pascal Code

You might also like