8
C0MPUTF~R GRAPHICS iNI) IMAGE PROCESSING 8, 286--293 (1978) An NOTE Efficient Algorithm for the Piecewise Linear Approximation of Planar Curves CHA.RLES 1V[. WILLIAAIS Information Systems Department, GeorgiaStale University, Atlanta, Georgia 30803 Received h~ay 2, 1977; revised July 13, 1977 A simple, highiy efficient, and computationallystable algorithmis developedfor the approximation of arbitrary planar curves by linear segments. The approximation yields excellent results in economizingthe number of line vectors required to represent curves on display within precisions specified by an error toleranceparameter. 1. INTRODUCTION The display of planar curves defined by sequences of accurately determined data points is a frequent problem in computer graphics. The source values may originate from the evaluation of mathematical functions or be produced by such devices as scanning digitizers tracking geometrical designs. In this paper we assume that the display is produced by means of line vectors and that the data are sufficiently precise and densely spaced that reasonable results may be ob- tained by using the line segments connecting the given unsmoothed points. Improving upon the efficiency of this representation while retaining its visual acceptability involves economizing the number of line vectors required subject to constraints imposed by the visual considerations of precision and repeata- bility ['1]. Precision in this context implies that the shape of an approximated display must not deviate from the original beyond a tolerance which is often prescribed by the resolution and width of lines used by the display device. It should be noted that maximum deviations from the original are more important in this instance than are average errors. Second, the quality of a display is strongly influenced by the repeatability of line closure points ['2], for the human visual system accentuates the effect of distortions which result in gaps or overshoots of intersecting lines. Indeed, the repeatability of line junctures is probably of more importance in effective displays than is precision, provided distortions are locally continuous. The economization of line vectors is, of course, of critical concern in decreasing computer memory and transmission time requirements, which in turn can help alleviate flicker problems on refresh displays. It should be remembered, however, 286 0146-664X/78/0082-0286502.00/0 Copyright~ 1978 by Academic Press, Ino. All rightsofreproduction in ~nyform reserved.

An efficient algorithm for the piecewise linear approximation of planar curves

Embed Size (px)

Citation preview

Page 1: An efficient algorithm for the piecewise linear approximation of planar curves

C0MPUTF~R GRAPHICS iNI) IMAGE PROCESSING 8, 286--293 (1978)

An

NOTE Efficient Algorithm for the Piecewise Linear Approximation

of Planar Curves

CHA.RLES 1V[. WILLIAAIS

Information Systems Department, Georgia Stale University, Atlanta, Georgia 30803

Received h~ay 2, 1977; revised July 13, 1977

A simple, highiy efficient, and computationally stable algorithm is developed for the approximation of arbitrary planar curves by linear segments. The approximation yields excellent results in economizing the number of line vectors required to represent curves on display within precisions specified by an error tolerance parameter.

1. INTRODUCTION

The display of planar curves defined by sequences of accurately determined data points is a frequent problem in computer graphics. The source values may originate from the evaluation of mathematical functions or be produced by such devices as scanning digitizers tracking geometrical designs. In this paper we assume that the display is produced by means of line vectors and tha t the data are sufficiently precise and densely spaced that reasonable results may be ob- tained by using the line segments connecting the given unsmoothed points.

Improving upon the efficiency of this representation while retaining its visual acceptability involves economizing the number of line vectors required subject to constraints imposed by the visual considerations of precision and repeata- bility ['1]. Precision in this context implies that the shape of an approximated display must not deviate from the original beyond a tolerance which is often prescribed by the resolution and width of lines used by the display device. I t should be noted that maximum deviations from the original are more important in this instance than are average errors.

Second, the quality of a display is strongly influenced by the repeatability of line closure points ['2], for the human visual system accentuates the effect of distortions which result in gaps or overshoots of intersecting lines. Indeed, the repeatability of line junctures is probably of more importance in effective displays than is precision, provided distortions are locally continuous.

The economization of line vectors is, of course, of critical concern in decreasing computer memory and transmission time requirements, which in turn can help alleviate flicker problems on refresh displays. I t should be remembered, however,

286

0146-664X/78/0082-0286502.00/0 Copyright ~ 1978 by Academic Press, Ino. All rights of reproduction in ~ny form reserved.

Page 2: An efficient algorithm for the piecewise linear approximation of planar curves

LINEAR APPROXIMATION OF CUR, VIeS 287

that absolute minimization of vectors, while desirable, is not necessarily manda- tory, especially if it must be bought with the expenditure of critical computer resources.

2. APPROXIMATION METHODS

The approximation of curves by straight line sequences can be accomplished by complicated least squares [3-6], Chebycheff E7], or nonlinear minimization ~8, 9~ procedures, which unfortunately may require considerable amounts of computation, especially in those cases where the analytic properties of the source data are unknown. These methods are noticeably cumbersome because they directly yield neither the maximum deviation nor the error at each point.

A considerable improvement over these classical approaches was obtained by an algorithm of Reumann and Witkam [-10] which is based on geometrical considerations and guarantees a precision less than a given tolerance. Unfortu- nately, the economization performance of their algorithm depends heavily upon an accurate determination of the initial curve slope, which is difficult to compnte in practice if data points are closely spaced or are derived from devices with limited precision. Furthermore, they make the unnecessarily conservative assumption that an acceptable line segment must lie within a band which is parallel to this initial direction and has a half-width equal to the tolerance.

3. LINEAI~ SEGMENTATION OF CUII,VES

The algorithm presented in this paper is derived from considerations similar to those of I~eumann and ~Vitkam but results in computations which are numeri- cally stable and geometrically concise.

Let P be an ordered set of points which may be connected by a sequence of straight line segments to form an acceptable curve :

P = {p, = (x~, v,)li = I, ~}.

We wish to find a subset P ' of P

P ' = {p '~ l i = 1, m ; p q = p l ; p '~ = p~} ~ P

in such a manner that the curve formed by the sequence of line segments joining the points of P ' does not deviate by more than a distance d from the original points in P.

I t is convenient to express the points in P in polar coordinates with the origin at p~ :

r = { (o, o), p, = (r~, 0~)li = % ~}, where

r , = ( ( x , - x l ) 2 + ( y i - y l ) 2 ) ~ ,

0i = arctan ((yi -- y l ) / ( x ~ - - x l )) .

Then the set S~ of all lines through point Pl which pass within a distance d of another point p~ may be expressed in terms of an angular distance ~ :

S , = {0 = ¢ l e ~ - &_< ¢ _< 0, + ~,}

Page 3: An efficient algorithm for the piecewise linear approximation of planar curves

2S8 CHARLES !VI. WILLIAMS

with ~ = arcsin (d/r~); i ~ 1. Moreover, the set of all lines through p~ which pass within a distance d of tlle first i points is given by the set intersection

T , = ,i=2

Provided it is not empty, T, may also be expressed in terms of the individual limiting angular values of Sj:

T ~ = { 8 = ¢ [ e , , l ~ _ < ¢ _ < e ..... } where

8nl~n = max (0~. - ~.),

Om.x = rain (0j + ~j); 2 _< j _< i.

Figure 1 gives a geometric interpretation of these considerations. The calculations of 0~n and em~x can be simplified computationally by replacing

angle evaluations with those of the tangent of half the angle. These substitutions lead to :

d x ~ = x l - - x l ;

dy~ -- Yi - - y l ;

r~ -- (dx~ ~ Jr dy~ 2)~;

tan ( ~ / 2 ) = dy~/ (r~ ~- dxi ) ;

tan ((0~ ~ ~i) /2) -- (dy~ ± d ) / ( ( r~ ~ - - d2) ~ --}- dx~) ;

k_

FIG. 1. The circles abou~ each point pl define sectors S~ which contain all lines through pl passing within distance d of p~. Sector T~ contains all lines through pl which pass within distance d of both p~ and p3.

Page 4: An efficient algorithm for the piecewise linear approximation of planar curves

LINEAR APPROXIMATION OF CUfCVES 289

with the assumptions that r~> d,

r~ -t- dx~ > O.

Then 0=i, and 0 . . . . can be determined by successive evaluations:

(1) Initially tan (0ml,/2) = tan ((02 -- a2)/2),

and tan (0 ..... /2) ---- tan((O2 + 62)/2).

(2) Then for i = 3, t . . . and provided that

tan (0mt~/2) _ tan (0J2) < tan (0 ..... /2) we have

and tan (0~.~./2) +--max (tan (Omit/2), tan((Oi -- &)/2))

tan (0,~x/2) *--min (tan (0m,.~/2), tan ((0i -{- 80/2)).

If the provision in (2) does not hold, the line segment from pl to p;-1 is of maximal length, and a new line segment must be started with origin at p~-l.

The provision r~ > d is not confining in practice, for points closer than d to Pl are also close to lines passing through Pl. The second provision that r~ + dx~ > 0 will be guaranteed for points within the angular bounds 0mi~, 0 . . . . provided tha t the curve points are computationally reflected about the y axis in those cases where the initial curve angle lies in the second or third quadrants. I t is assumed tha t the curve does not reverse direction (the points in P are nondecreasing or noninereasing in either abscissa or ordinate values).

Figure 2 gives an ALGOL embodiment of this approximation procedure. Still further simplifications can be obtained by substituting arctan (d/rO

for aresin (d/r0 in the expression for & and using tangents of the angles in place of the angles themselves. This substitution results in:

tan (&) = d/r~;

tan (00 = dy~/dx~ if [dxll > ]dyaI

= dx~/dy~ otherwise;

tan (0~. :t: &) = (tan (0,) 4- tan (&))/(1 ~ tan (00 tan (~i)).

The search procedure for Omi. and 0~.~ then becomes :

(1) Initially tan(Omm) = tan (02 -- ~2), tan (0~,) = tan (02 -{- 6~).

(2) Then for i ~ 3, 4 . . . and provided that

tan (0,~t,) _~ tan (0~) _~ tan (0 ..... ), we have

tan (O,~i~) e- max (tan (0,,~.), tan (Oi -- 6i)) if 1 + tan (00 tan (6i) > O,

Page 5: An efficient algorithm for the piecewise linear approximation of planar curves

290 CH A R L E S ~I. W I L L I A M S

procedure segment (n, m, d, x, y) ; value n, d; real d; integer n, m; array x, y; begin comment

real comment

Boolean comment

Economize on the number of line segments needed to approximate the curve repre- sented by the n points (x, y) subject to the restrictions :

1. The resul tant m points define line segments which are no fur ther than distance d from the originals.

2. The original terminal points are preserved.

n nmaber of original p o i n t s . . , n > 1 m number of approximating p o i n t s . . , m _< n d error tolerance x, y Cartesian coordinates of points ; dx, dy, dsq, rsq, theta, the tamin, thetamax, de]x, s;

dx, dy Cartesian components of polar distance r measured from s ta r t of line segment

dsq square of error tolerance rsq square of polar distance r the ta tangent of one-half the polar angle the tamin min imum allowable value of the ta the tamax max immn allowable value of the ta ; testing, reflect ;

testing true if t he ta bounds are to be tested reflec~ true if init ial angle in 2nd or 3rd quadrants implying curve is to be re-

flected about y axis; integer i ; m : = 2; test ing : = false; dsq: = d ~ 2; for i : ---- 2 step 1 until n do begin

comment Calculate square of polar dis tance; dx: = x[ i ] -- x [m -- 1]; dy: = y [ i ] -- y [ m -- 1] ; rsq:~- d x l ' 2 + d y l " 2 ; i f tes t ing and (rsq :> dsq) then begin

comment Check whether point within angle bounds ; delx: = if reflect then - d x else dx ; s: -~ de]x + sqrt (rsq) ; i f s <7 0.0 then testing: = falsa else begin

t h e t a : = dy/s ; if ( thetamin ~ theta) and ( thet~ < thetamax) then begin

commenf. Point within b o u n d s . . . u p d a t e hounds; s : = delx + sqr t (rsq -- dsq) ; ,if s > 0.0 then begin

FIG. 2. ALGOL representat ion of the curve approximation algorittun.

Page 6: An efficient algorithm for the piecewise linear approximation of planar curves

LINEAII APPROXIMATION OF CUf~VES 291

thetamin: = max(thetamin, (dy - d)/s) ; thetamax: = min (thetamax, (dy + d)/s) ;

end ; end else testing: = :false;

end; i f not ~esting then begin

comment Point out of angle bounds... end old line segment and begin new one;

x[m]:= x[i - I]; y[m]: = y[i - I]; dx: = x[-i] - x[m]; dy: = yEi] - y[m]; rsq:= dxJ~2q-dyT2; m: = m + I;

end; end; i f not tes/ing and (rsq > dsq) then begin

comment Initialize angle bounds...begin testing; testing : = true ; reflect:= dx < 0.0; s := sqr~(rsq -- dsq) -b (if reflect then - d x els~ dx); tJletamin: = (dy - d)/s ; thetamax: = (dy + d)/s;

end; end ;

comment End points of approximation are to match originals; xFm'] : = x[n]; YOn] : = y[n] ; end ;

F1o. 2--Continued

and

tan (#~,.) *--rain (tan (~ ..... ), tan (8; q- &)) if i -- tan (~) tan (8~-) > O.

This simplification results in significantly shorter calculations bu t does suffer f rom inaccuracies in the allowable deviation &. This discrepancy has the effect of reducing the value of the allowable tolerance d by a factor f which is a funct ion of the distance r~:

f = 1 - - d/r~ -~ sin (arctan (cl /r l)) .

As the value of f at tains its minimum of 0.707 when r~ = d and is greater t h an 0.949 when r~ > 3d, this problem only affects points close to the initial point p~.

4. DISCUSSION

This algorithm is highly economical in both computer time and storage require- ments . The routine operates in a fixed and small amount of memory, and operat ing t imes are l inearly proportional to the number of data points processed. Moreover, the algori thm is computat ional]y simple and short, wi th the evaluation of the square root contributing ~ ~ a i o r fraction of the time. Even this deficiency m a y

Page 7: An efficient algorithm for the piecewise linear approximation of planar curves

292 CHARLES ~. WILLIAMS

i O = 0.005 - - -~ 32 VEC, TOR8

/ \~ o.o.oso \ 10 VECTORS

I ~', o- o~oo l . . . . . f~ , VECTO,S

Fro. 3. Plotted results obtained by approximating 8000 points defining ~he unit circle using various settings of the allowable tolerance parameter d.

be partially corrected by employing a Chebycheff approximation ['6~

(x2_b y2)~ ~_. 0.96 max (Ixl, lyl) ~-0.398 rain (Ixl, lyl)

which yields a maximum relative error of 4~o. In addition the algorithm is numerically stable, as the computations involve

quantities which are easily defined and calculable. The use of angular distances, rather than Euclidean distances measured with respect to the coordinate axes, implies that the algorithm does not suffer greatly from direction dependencies imposed by the Cartesian coordinate system.

The disadvantage of the system is that it does not necessarily yield optimum results either in accuracy or in economization of line vectors. That is, the resultant sequence of line segments may not be a "best fit" approximation to the original curve, and an absolute minimization of vectors may not have been achieved. However, in practice the author has obbained highly acceptable results in approxi- mating drawings obtailled from a scanning digitizer Fll-]. I t is doubtful that significantly better results would have been achieved through further analyses.

Figure 3 shows results obtained from approximating a table of 8000 points defining the unit circle, using various settings of the ~oleranee parameter d. A comparison of processing rates was made by coding procedures in Mgol W ['12] and running them on a Univac System 70/7. The algorithm given in Fig. 2 processed points at a rate of 870 points per second. The simplified procedure involving the substitution of arctan (d/ri) for arcsin (d/r~) was 25% faster at 1060 points per second and was speeded up 30% more to 1390 points per second b y incorporating the Chebyeheff square root approximation.

A considerably more important shortcoming of the process concerns the special role tha t curve endpoints play. The fact that the approximation curve passes through these points does guarantee the repeatability of line closures, but can have disagreeable consequences in the case of isolated line terminal points. A bet ter approach in these instances would be to include starting and ending procedures for selecting the most appropriate curve terminal points according to some optimization criterion.

Page 8: An efficient algorithm for the piecewise linear approximation of planar curves

LINEAR APPROXI.~IATION OF CUItVES 293

REFERENCES

1. C. Machover, Lecture notes...CLOT graphic terminals, in Graphics for Designer~, Univ. of Michigan Engineering Summer Conference, June 17-28, 1968.

2. S. Sherr, Fundamentals of Display System Design, p. 47, Wiley, New York, 1970. 3. H. Stone, Approximation of curves by line segments, Math. Comp. 18, 1961, 4047. 4. B. Gluss, A line segment curve-fitting algorithm related to optimal encoding o~ information,

Inform. Contr. 5, 1962, 261-267. 5. A. Cantoni, Optimal curve-fitting with piecewise linear functions, IEEE Trans. Computers

C-20, 1971, 59-67. 6. S. D. Conte and C. d~ Boor, F-,lemsntary Numerical Analysis, pp. 191-273, MeGraw-Itill, New

York, 1972. 7. g. R. Rice, The Approximation of Functions, ¥ol. 1, Addison-Wesley, Raading, Mass., t964. 8. H. Freeman and J. M. Glass, On quantization of line-drawing data, IEEE Trans. Systems

Science and Cybernetics SSC-8, 1969, 70-79. 9. U. Montanari, A note on minimal length polygonal approximation to a digitized contour,

Comm. AC~I 13, 1970, 41-47. 10. K. Reumann and A. P. hi. Witkam, Optimizing curve segmentation in computer graphics, in

International Computer Symposium (A. Gunther, B. Levrat and H. Lipps, Eds.), pp. 467-472, American Elsevier, New York, 1974.

11. BroomalI Industries, Inc., 700 Abbott Drive, Broomall, Pennsylvania 19008, USA. 12. R. L. Sites, Algol W Reference Manual STAN-CS-71-230, Computer Science Department,

Stanford University, 1972.