Piecewise linear approximations of fewest line segments
by D. G. WILSON
Virginia Commonwealth University
Richmond, Virginia
INTRODUCTION
at discrete abscissa values within a variable tolerance.
Our algorithms can also be used to approximate continuous functions. However, in this case a partition of
the interval of interest must be chosen first, the values
of the function at these points computed, and an initial
continuous piecewise linear approximation obtained by
linear interpolation. It is evident that our algorithms
cannot give a "good" fit if this initial approximation is
not "good." It is also evident how to make this initial
approximation "good." But it is not at all obvious how
to make it how "good." We have not solved this
problem in general. Perhaps a combination of Phillip's
algorithm and ours could be used to solve the problem
for continuous functions whose domains are separable
into intervals over which the second derivative is of
one sign.
In the second section we give a precise statement of
the problem we have solved. Section three contains the
basic algorithm and the modification. Section four
contains the proofs which validate the algorithm.
Section five contains some sample results. Section six
acknowledges the assistance of others. A listing of a
FORTRAN program which implements our algorithms
is appended.
Schwerdtfeger4.5 has considered the problem of interpolation and curve fitting over a given partition by a
sum of continuous sectionally linear functions. He
showed how to construct an orthogonal system of these
functions over a given partition and how to use this
system to obtain an approximation of a given function
which is best in the least square sense.
Stone,6 Bellman,! and Gluss 2 have analyzed various
aspects of the problem of obtaining a piecewise linear
approximation, not necessarily continuous, with a given
number of line segments which is best in the least
square sense.
IVlore recently, Phillips3 has given an algorithm for
obtaining a continuous piecewise linear approximation
to a function whose second derivative is of one sign.
This algorithm gives an approximation which is of
fewest segments among those with a given deviation.
The output from this algorithm is a partition of the
given interval and the parameters of a straight line
over each subinterval of the partition.
We consider the problem of approximating a continuous sectionally linear function by another piecewise
linear function, which mayor may not be continuous,
satisfying:
STATEMENT OF THE PROBLEM
(a) the approximation is within a given deviation,
which need not be constant, of the given function
and
(b) the approximation is extremal in the sense that
there is no other piecewise linear approximation
which is within the same or lesser deviation and
which has fewer line segments.
Let P denote a partition of the finite interval [a, b].
Let f (x) be a continuous function defined on [a, bJ
which is linear on each subinterval [Xi-I, xiJi = 1, ... , n.
Let 0(x) be the continuous sectionally linear extension
to [a, bJ, obtained by linear interpolation, or 0 (x) = OJ
when x=xjEP, where 0i>O, j=O, ... , n, are the
permissible deviations at the points of P.
Then the problem is, given Uo = a, determine m, pi, qi,
and Ui, i= 1, ... , m such that F(x) given by:
We give an algorithm which solves the problem as
stated and a modification which gives a continuous
piecewise linear, approximation which is within a given
deviation of the given function and which is extremal
among such approximations in the sense given above.
We thus solve the problem of fitting data known only
187
From the collection of the Computer History Museum (www.computerhistory.org)
188
Spring Joint Computer Conference, 1972
when xE [Ui-I, Ui) for i = 1, ... ,m and
F(um) =Pmx+qm,
satisfies If(x)-F(x)l:::;o(x) for xE[a,bJ and m,
the number of linear segments of F, is a minimum.
Xk(x, A, B)
THE ALGORITHM
rmax[x, {Xi I xiEP, Xi<Y.(X, A, B),
We consider eachf(xi) as the center of a vertical line
segment of length 20i over the corresponding Xi. We
denote by Lt(x) and Lb(X) the continuous piecewise
linear functions f +0 and f - 0 respectively.
Briefly our algorithm successively determines the
longest "line of sight" into the two dimensional "tunnel"
bounded by Lb and Lt. The process terminates since
there are only a finite number of data points.
We index the approximating line segments by a
parameter k initially set to 1. We define a parameter
"A (k) taking values + 1 and -1 which indicates whether
the previous line segment, the (k-l)th, terminated on
L t, "A(k) = +1, or on L b, "A(k) = -1. It is evident that
any line segment which at each point X of its domain,
is within 0(x) of f (x) must either extend to x = b or
terminate either on L t or on Lb. Initially we arbitrarily
take "A(I) = +1.
Weare interested in the maximum and minimum
permissible slopes for an approximating line segment
through a given point. Accordingly we define:
= [Lt(y) -f(x) - "A(k)o(x) J/ (y-x),
(2.1)
Sk(X, y) = [Lb(Y) -f(x) -"A(k) o(x) J/ (y-x),
(2.2)
Sk(X, y)
and
=
1
S.(x, Xi)
~ 2:. (X, Xi,
A) I]
if
A(k)
~ +1
max[x, {xiE P, Xi< Y.(x, A, B),
Sk(X,Xi)=Uk(X,Xi,B)}J
if
}"(k) =-1,
(the sets defined in braces in the previous expression
may be empty) ,
Pk(w, x, y, z)
min{min[sk(Y, Xi) I xiE P, W:::;Xi<yJ,
if "A(k) =
Sk(X, y)
+1,
if "A(k) = -1,
Qk(W, x, y, z)
Sk(X,y)
if "A(k)=+I,
if "A(k)=-1.
CTk(X, y, B) = supz<z:::;y{Sk(X, z), B}.
~k (x,
y, A) is the minimum of the maximum permissible slopes of approximating segments over [x, y J
through (x,f(x) +"A(k)o(x)) which are not greater than
some given slope A. Similarly CTk(X, y, B) is the maximum of the minimum permissible slopes of approximating segments over [x, yJ through (x,f(x) +"A(k)o(x))
which are not less than some given slope B.
If ~k(X, y, A) ~Uk(X, y, B), then an approximating
segment can be drawn through (x,f(x) +"A(k)o(x))
which extends at least from x to y. We are naturally
interested in the largest y for which this is true. We
define
Yk(x, A, B)
= sup{y I y:::;b,
Hk(x, A, B)
= max {i I xiE P, Xi:::; Y k(x, A, B) }.
~k(X, y, A) ~Uk(X, y, B)},
Yk(x, A, B) is the right endpoint of the domain
of
(x,f(x) +"A(k)o(x)) with slope between Band A.
The value of Yk(x, A, B) may not be in P, but this
value can be determined since f is a continuous piecewise linear function. We will return to this point.
Before giving the algorithm we define three more
quantities of interest. Let
the
longest
approximating
segment
through
Xk(X, A, B) determines a "pivot point" in our two
dimensional tunnel to the right of x. This will be taken
as a new "eyepoint" for determining a new "line of
sight" extending farther to the right. P k and Qk determine respectively a maximum and a minimum slope for
a new segment through the pivot point. These will
determine the parameters A and B for subsequent
evaluations of Uk, ~k, Y k, X k. This is another point to
which we shall return.
We are now ready to give our algorithm:
Initialization:
Set: Zo = z* = Uo = a, "A ( 1) =
+ 1, j =
1, k = 1,
Ao= [f(XI) +O(Xl) -f(xo) -o(xo) J/ (Xl-XO) ,
Bo= [f(Xl) -O(Xl) -f(xo) -o(xo) J/ (Xl-XO).
Set an indicator if the approximation is to be continuous.
From the collection of the Computer History Museum (www.computerhistory.org)
Piecewise Linear Approximations of Fewest Line Segments
Iteration:
1. Compute: Yi= Yk(Zi-l, Ai-I, B i- I) ,
189
does generate an extremal continuous piecewise linear
approximation as asserted.
h=Hk(Zi-l, Ai-I, B i- I) +1,
Lemma 1:
Zi=Xk(Zi-l, Ai-I, B i - I).
For a fixed x the functions Sk (x, y) and Sk (x, y) are
continuous piecewise linear functions of (Y-X)-I for
Y E (x, b], and the partition of the functions into
segments linear in (Y-X)-I is exactly P.
2. If Yi = b, then go to Termination step 4.
3. If X(k)=+1 and Sk(Zi-I,Xh) <Uk(Zi-I,Xh,Bi- I),
or if X(k) = -1 and Sk (Zi-I, Xh) > ~k (Zi-I, Xh, Ai-I),
then go to Termination step 1.
4. If Zi = Zi-I, then go to Termination step 2.
5. Compute: Ai=Pk(Z*, Zi-I Zh Yi),
Bi=Qk(Z*, Zi-I, Zh Yi)'
6. Increment j by
+ 1 and go to Iteration step 1.
Proof:
The functions are rational and hence continuous on
(x, b]. Let Xi and Xi+! be successive points of P n [x, b].
That Sk and Sk are linear in (Y-X)-I for yE (Xi, Xi+!]
follows from substituting
LJ.I(Y) =LJ.I(Xi) + [LJ.I(Xi+l) -LJ.I(Xi) ](Y-Xi) / (Xi+I-Xi)
Termination:
1. If X(k)=+1, then set Pk=Sk(ZhYi)' If
X(k) = -1, then set Pk= Sk(Zh Yi)' Set qk= f(zi) +
"A(k)O(Zi) -PkZi' Set X(k+l) =X(k). Go to
Termination step 3.
2. If X(k) = -1, then set Pk=Sk(Zi, Yi)' If
"A(k) = 1, then set Pk = Sk (Zh Yi)' Set qk = f(zi) +
"A(k)o(z) -PkZi' Set X(k+1) = -X(k).
3. Set: Uk=Yh
+
for p,=t into the definition of Sk equation (2.1) and
for p,=b into the definition of Sk equation 2.2. Writing
(Y-Xi) = (y-x) + (X-Xi) and dividing out (y-x)
gives an expression linear in (Y-X)-I.
Corollary 1:
For a fixed XE [a, b], if Xi and Xi+! are successive
points of Pn[x,b],then Sk(X,y) and Sk(X,Y) are
monotone functions of Y for Y E (Xi, Xi+l].
Corollary 2:
Ao= Sk(ZO, Xh),
Bo= Sk (zo, Xh).
If the approximation is to be continuous, then
set z* = Zh otherwise set z* = Yi' If the approximation is to be continuous and k> 1, then
recompute Uk-I, the abscissa value at the intersection of the current and previous segments.
Set: j = 1, increment k by
1 and go to
Iteration step 1.
4. Set: Pk = {~k (Zi-I, b, Ai-I) +Uk (Zi-I, b, B i- I) }/2,
+
For fixed xE [a, b], Sk(X, y) and Sk(X, y) take their
minimum and maximum values respectively at points
y' and y" which belong to pn (x, b].
Lemma 2:
For a fixed XE [a, b], A and B, if Xi and Xi+! are
successive points of pn (x, b] with ~k(X, Xi, A) ~
Uk(X, Xi, B) and ~k(X, Xi+!, A) <Uk (X, Xi+!, B), then
either Sk (x, Xi+l) > ~k (x, Xi, A)
or
Sk (x, Xi+l) <
Uk(X, Xi, B) and not both.
qk=f(Zi-l) +"A(k) o(Zi-I) -Pkzi-I,
Proof:
uk=b.
We show first that under the hypothesis not both
inequalities may hold. Since Sk(X, Xi+!) ~Sk(X, Xi+!) ,
both inequalities would imply Uk (X, Xi, B) > ~k(X, Xi, A)
which contradicts the hypothesis.
We now show that under the hypothesis not both
inequalities may fail. From the definition of ~k and Uk,
and lemma 1: If Sk (x, Xi+!) ~ ~k (X, Xi, A) and
Uk(X, Xi, B) ~~k(X, Xi, A),
then
Uk(X, Xi+l, B) ~
~k(X, Xi, A); similarly if Sk(X, Xi+!) ~Uk(X, Xi, B) and
~k(X, Xi, A) ~Uk(X, Xi, B),
then
~k(X, Xi+l A) ~
Uk (X Xi, B). Thus, if the hypothesis held but both
inequalities failed we would have: ~k (x, Xi, A) ~
Uk(X, Xi+l, B) > ~k(X, Xi+l, A) ~Uk(X, Xi, B). But this
implies Sk (x, Xi+l) > Sk (x, Xi+!) which is impossible.
If the approximation is to be continuous and
k> 1, then recompute Uk-I, the abscissa value at
the intersection of the current and previous
segments. End.
VALIDATION
In this section we show that Yk(x, A, B) can be
computed, that the algorithm just given does generate
a piecewise linear approximation which is extremal in
. the sense already defined and finally that the modification to be implemented for a continuous approximation
From the collection of the Computer History Museum (www.computerhistory.org)
190
Spring Joint Computer Conference, 1972
Proof:
Lemma 3:
For a fixed xE [a, b], A and B, let Xi and Xi+! be
successive points of P(](x,b] with ~k(X,Xi,A)~
Uk (X, Xi, B) and Uk (X, Xi+l, B) > ~k (x, Xi+l, A) . If
Sk(X, Xi+l) > ~k(X, Xi, A), then Yk(x, A, B) is the z
value for which
Lb(Z) = (z-x)
~k(X,
Xi, A) +f(x) +A(k)o(x);
while if Sk(X, Xi+!) <Uk(X,Xi,B), then Yk(x,A,B)
the Z value for which
IS
Lt(z) = (Z-X)Uk(X, Xi, B) +f(x) +A(k)o(x).
Proof:
Lemma 2 shows that one of these formulae can be
selected. The formulae follow from the definitions of
Sk and Sk, Equations (2.1) and (2.2) and the observations that if Sk (x, Xi+!) > ~k (x, Xi, A), then Sk (x, z) is
an increasing function of z for Z E (Xi, Xi+l] and we are
interested in the Z for which Sk(X, z) = ~k(X, Xi, A),
while if Sk (x, Xi+!) < Uk (X, Xi, B), then Sk (x, z) is a
decreasing function of z for zE (Xi, Xi+!] and we are
interested in the z for which Sk(X, z) =Uk(X, Xi, B).
This lemma shows constructively that Yk(x, A, B)
can be computed. This is the only function we have
defined about which there was potential doubt.
It is evident that our algorithm generates a piecewise
linear approximation which is within 0 of f. We now
show that this approximation is extremal.
For convenience we identify the last Zj associated
with the kth segment as ek, the abscissa value of the
eyepoint of this segment. This value is available at the
termination step of each segment. Note that
Since each segment, except possibly the right most,
terminates on either L t or Lb we have shown that each
approximating segment, again except possibly the
right most, has at least two points on the boundary of
our two dimensional tunnel. The extremal property of
our approximation follows from each approximating
segment, except possibly the right most, having at
least three points on the boundary which alternate
between the boundary lines.
We will prove this assertion in the next two lemmas.
The first of these merely establishes a useful geometric
fact.
Lemma 4:
Let asx<y<z, and let R stand for any of: >, <,
S, or =. If A(k) = +1 and Sk(X, y)Rsk(x, z), then
Sk(X, y)Rsk(y, z). If A(k) = -1 and Sk(X, y)RSk(x, z),
thensk(x, y)RSk(y, z).
~,
If A(k) = +1, then the hypotheses imply:
[Lt(y) -Lt(x) ](z-x)R[Lb(z) -Lt(x)] (y-x).
In this relation we substitute (z-y+y-x) for (z-x)
on the left and [Lb(Z) -Lt(y) +Lt(y) -Lt(x)] for
[Lb(z) -Lt(x)] on the right. The result is:
[Lt(y) -Lt(x) ](z-y)R[Lb(z) -Lt(y) ](y-x).
The conclusion, Sk(X, y)Rsk(y, z), follows from dividing
this relation by (z-y)(y-x) >0.
The argument is similar in the case A(k) = - 1.
Lemma 5:
If the kth approximating segment is not the right
most and terminates on Lp., where,." = b or t, then there
exist x'E [Uk-I, Uk) and x"E (x', Uk) such that Lp.(x') =
X'Pk+A(k)o(x') and Lv(x") =X"Pk+A(k)o(x") where
if ,." = b, then v = t and vice versa.
Proof:
The idea of the proof is simple although the notation
is complicated. The outline of the proof is as follows.
If the kth segment is not the right most, then the slope
of this segment is both the maximum of the minimum
permissible slopes for approximating segments through,
(ek' Lp.(ek» which intersect the vertical segment over
Uk-I, and the minimum of the maximum permissible
slopes for approximating segments through this eyepoint which intersect this vertical segment. Furthermore each of these slopes is determined by a point on
the boundary. One of these points is the terminal point
of the segment, the other is the point whose existence
this lemma asserts.
There are four possible cases determined by:
Lt(Uk) =UkPk+qk with A(k) = ±1, and Lb(Uk) =UkPk+qk
with A(k) = ± 1. We shall consider the two cases with
A(k) = + 1. The other two are similar to these.
We consider first A(k) = +1 and Lt(Uk) =UkPk+qk.
Then Lt(ek) =ekPk+qk, x'=ek and we need to find
x" E (x', Uk). If ek = Uk-I, then the slope of the line
through (Uk-I, Lt(Uk-I» and (Uk+l, Lt(Uk+!» is the
minimum permissible slope of an approximating
segment over [Uk-I, Uk]. By lemma 3 and corollary 2 of
lemma 1 this minimum slope is the slope of a line from
(Uk-I, Lt(Uk-I»
through (Xi, Lb(Xi»
for some
Xi E P(] (Uk-I, Uk]. Furthermore, this Xi is not Uk since
Lt(Uk) =UkPk+qk. In this case x" is this Xi.
If ek¢.uk-I, then to determine Uk, Pk and qk more than
one pass through the iteration phase of our algorithm
was required. Let ek* and Yk* be respectively Zj-2 and
Yi-I, where ek = Zi from the determination of Uk, Pk and
From the collection of the Computer History Museum (www.computerhistory.org)
Piecewise Linear Approximations of Fewest Line Segments
qk and let Xk * = min {x I x E P, x ~ Yk * }. Then
Pk= ~k(ek, Uk, A) =uk(ek, Uk, B),
where
and
191
left intersects the (k - 1) th segment between the x"
and Uk of lemma 5. That this approximation is extremal
follows from the observation made in the proof of
corollary 2 of lemma 5.
SAMPLE RESULTS
If there were no x" E (ek' Uk) such that Lb (x") =
X"Pk+qk, then Pk would equal Band sk(ek, Xi) would be
less than B for each Xi E pn (ek' Uk]. But this is impossible. For the algorithm which gives the value of Yi
insures that Sk(ek*, ek) :::;sk(ek*,xk*); and lemma 4 then
implies Sk (ek*' ek) :::;Sk (ek' Xk*). Finally, since A= + 1, we
have B=Sk(ek*, ek). Hence sk(ek, Xk*) ~B, and the
existence of an x" is established.
We suppose now that A(k) = +1 and Lb(Uk) =
UkPk+qk. Then Lt(ek) =ekPk+qk and we will show that
there exists x' E [Uk-I, ek) such that Lb(x') = X'Pk+qk.
This follows from Sk(ek, Uk) =Pk(Uk-I, ek*, ek, Yk*). For
Sk (ek' Uk) = Uk (ek' Uk, B) = ~k (ek' Uk, A), and if this is
not A, namely Pk(Uk-I, ek*, ek, Yk*), then there exists
some xiEPn(ek, Uk) such that Sk(ek, Xi) <A. But this
contradicts the definition of ek as the last Zi from the
iteration phase in the determination of Uk, Pk and qk.
Corollary 1:
There is no approximating linear segment whose
domain includes Uk-I which extends farther to the right
than Uk.
Proof:
Such a segment would have to cross our kth approximating segment twice.
Corollary 2:
Our approximation is extremal in the sense stated.
Proof:
Any other linear approximation of fewer line segments must have at least one segment whose domain
begins to the left of and ends to the right of the domain
of one of our approximating segments.
Lemma 6:
The modification of our algorithm for a continuous
approximation does give a continuous piecewise linear
approximation which is extremal in the sense we have
defined.
The appended FORTRAN program, with a suitable
driver, was run a number of times. An input parameter
determined if the approximation was to be continuous
and whether or not the deviation was to vary. If the
deviation was not to vary it was taken as the first
input tolerance.
One set of test data was the following:
INPUT DATA, NUMBER OF POINTS=20
x
Y
TOLERANCE
1.0000
2.0000
3.0000
4.0000
5.0000
6.0000
7.0000
8.0000
9.0000
10.0000
11.0000
12.0000
13.0000
14.0000
15.0000
16.0000
17.0000
18.0000
19.0000
20.0000
0.7175
0.6250
0.6250
0.6250
0.6563
0.7500
0.6563
0.6406
0.7500
0.5000
0.7500
0.6250
0.6250
0.6250
0.6563
0.7500
0.6563
0.6406
0.7500
1.0000
0.1250
0.0625
0.0625
0.1250
0.1250
0.2500
0.1250
0.0313
0.0312
0.0156
0.1250
0.0625
0.0625
0.1250
0.1250
0.2500
0.1250
0.0313
0.0312
0.0156
The results, when the deviation was taken as constant,
namely 0.125, were:
2 LINE SEGMENTS WERE REQUIRED TO
FIT THESE DATA EQUATION OF
APPROXIMATION IS Y=PX+Q
FOR U.LT.X.LT.V
P
Q
U
v
Y(U)
Y(V)
0.0
0.6250 1.0000 19.0000 0.6250 0.6250
0.3750 -6.5000 19.0000 20.0000 0.6250 1.0000
Proof:
The modification is to one of the argument values
where P k and Qk are to be evaluated. The result is to
insure that for k> 1 the kth segment extended to the
whether or not the approximation was required to be
continuous. When the deviation was permitted to vary,
From the collection of the Computer History Museum (www.computerhistory.org)
192
Spring Joint Computer Conference, 1972
the results were:
The results were:
6 LINE SEGMENTS WERE REQUIRED TO
FIT THESE DATA EQUATION OF
APPROXIMATION IS Y=PX+Q
FOR U.LT.X.LT.V
P
Q
v
U
0.0113 0.5812 1.0000 8.6372
-0.0627 1.2828 8.6372 9.3075
-0.1893 2.4086 9.3075 10.0947
0.0832 -0.2904 10.0947 11.9235
0.0172 0.3621 11.9235 18.6778
0.2393 - 3. 7867 18.6778 20.0000
Y(U)
Y(V)
0.5925
0.7415
0.6467
0.5497
0.5673
0.6835
0.6791
0.6995
0.4977
0.7018
0.6835
1.0000
when the approximation was not required to be continuous, and:
6 LINE SEGMENTS WERE REQUIRED TO
FIT THESE DATA EQUATION OF
APPROXIMATION IS Y=PX+Q
FOR U.LT.X.LT.V
P
Q
v
u
0.0113 0.5812 1.0000 8.0000
0.0469 0.2965 8.0000 9.0000
-0.2032 2.5474 9.0000 10.0000
0.1094 -0.5781 10.0000 11.0000
0.0067 0.5513 11.0000 18.6084
0.2329 -3.6572 18.6084 20.0000
Y(U)
Y(V)
0.5925
0.6719
0.7188
0.5156
0.6250
0.6759
0.6719
0.7188
0.5156
0.6250
0.6759
1.0000
when the approximation was required to be continuous.
These results show that it may not require any
additional segments to have the approximation be
continuous for a given set of tolerances.
Another set of test data was:
5 LINE SEG:\IENTS WERE REQUIRED TO
FIT THESE DATA EQUATIOX OF
APPROXLUATION IS Y =PX+Q
FOR U.LT.X.LT.V
P
Q
U
v
-0.7500 4.8000 0.0
2.5333
0.2727 2.2091 2.5333 4.0000
0.8333 -0.0333 4.0000 7.4800
-0.2632 8.1684 7.4800 9.0360
- 1 . 6500 20. 7000 9.0360 12.0000
Y(U)
Y(V)
4.8000
2.9000
3.3000
6.2000
5.7905
2.9000
3.3000
6.2000
5.7905
0.9000
when the approximation was required to be continuous
and the deviation was taken as 0.2; and:
5 LINE SEGl\1ENTS WERE REQUIRED TO
FIT THESE DATA EQUATION OF
APPROXIlVIATION IS Y=PX+Q
FOR U.LT.X.LT.V
P
Q
U
v
-0.7000 4.8000 0.0
2.4530
0.0130 3.0510 2.4530 4.0000
0.7990 -0.0930 4.0000 7.9099
-0.3000 8.6000 7.9099 9.1905
-1.7237 21.6847 9.1905 12.0000
Y(U)
Y(V)
4.8000
3.0829
3.1030
6.2270
5.8428
3.0829
3.1030
6.2270
5.8429
1.0000
when the approximation was required to be continuous
and the deviation was permitted to vary.
The program will accept any non-negative tolerance
including zero. If all tolerances are zero the program
gives the parameters for linear interpolation.
INPUT DATA, NUMBER OF POINTS = 13
X
Y
TOLERANCE
0.0
1.0000
2.0000
3.0000
4.0000
5.0000
6.0000
7.0000
8.0000
9.0000
10.0000
11.0000
12.0000
5.0000
4.0000
3.1000
3.1000
3.1000
4.0000
5.0000
6.0000
6.0000
6.0000
4.0000
2.5000
1.0000
0.2000
0.1000
0.3000
0.0100
0.0030
0.2000
0.3000
0.5000
0.2000
0.1000
0.7500
1.0000
0.2000
ACKNOWLEDGMENTS
This problem was originally given to me by lVlr. Fred
Stahl of the John D. Kettelle Corporation. I am indebted to Dr. W. A. Thedford of Virginia Commonwealth University for a suggestion which led to the
modification for a continuous approximation.
Mr. W. T. Lipscomb III, a student at V.C.U.,
programmed a preliminary version of the algorithm and
in numerous discussions contributed some of the
descriptive terminology. His name appears as a coauthor of the appended listing.
Sincere appreciation goes to my wife Gwendolyn
who typed this manuscript.
From the collection of the Computer History Museum (www.computerhistory.org)
Piecewise Linear Approximations of Fewest Line Segments
193
1 R BELLMAN
3 G M PHILLIPS
Algorithms for piecewise straight line approximations
The Computer J 11 2 August 1968 pp 211-212
4 H SCHWERDTFEGER
Interpolation and curve fitting by sectionally linear functions
On the approximation of curves by line segments using
dynamic programming
Comm ACM 4 6 June 1961 p 284
2 B GLUSS
Further remarks on line segment curve-fitting using
dynamic programming
Comm ACM 5 8 August 1962 pp 441-443
Canad Math Bull 3 1 January 1960 pp 41-57
5-Further remarks on sectionally linear functions
Canad Math Bull 4 1 January 1961 pp 53-55
6 H STONE
Approximation of curves by line segments
Math of Comp 15 73 January 1961 pp 40-47
REFERENCES
From the collection of the Computer History Museum (www.computerhistory.org)
194
Spring Joint Computer Conference, 1972
APPENDIX
FORTRAN IV G LEVEL
C
C
C
C
MAIN
19
PIECWISE LINEAR APPROXIMATION OF FEWEST
LINE SEGMENTS WITHIN A GIVEN TOLERANCE.
BY D G WILSON AND W T LIPSCOMB III
VIRGINIA COMMONWEALTH UNIVERSITY.
C
SUBROUTINE STL(X,Y,E,N,U,P,Q,K,ITCH)
DIMENSION X(9),Y(9),E(9),U(9),P(9),Q(9)
X AND Y ARE INPUT DATA ARRAYS OF N ELEMENTS
Y = P(I)*X + Q(I) IS THE EQUATION OF THE
ITH SEGMENT OF THE PIECEWISE LINEAR APPROXIMATION. THE ITH SEGMENT EXTENDS FROM
X = UeI) to X = U(I+l1. K IS THE NUMBER
OF SEGMENTS.
IF THE INDICATOR ITCH IS
EITHER 1 OR 3, THE APPROXIMATION MUST BE
CONTINUOUS.
IF ITCH IS 2 OR 3, E IS A
TABLE OF N PERMISSIBLE DEVIATIONS..
IF ITCH
IS LESS THAN OR EQUAL TO 1, THEN E(l) IS
THE SINGLE PERMISSIBLE DEVIATION.
0001
0002
r'
C
C
C
C
C
C
C
C
C
C
C
C
C
* * * * * * * *
INITIALIZATION
* * * * *
* *
* *
* *
* *
*
c
C
FROM SCRATCH
C
.J = 1
000:3
C
C
C
C
0004
0005
C
C
0001;.
0007
000::::
0009
0010
0011
0012
001:3
0014
0015
r'
r'
C:
IS THE INDEX INTO THE U ARRAY.
ILLEGITIMATE DATA CAUSES ERROR RETURN WITH
K, THE NUMBER OF LINEAR SEGMENTS, EQUAL O.
THE MINIMUM NUMBER OF DATA POINTS IS TWO.
IF (N.LE.l) GO TO 777
IF (E(l).LT.O.O) GO TO 777
DEVIATIONS MUST BE NONNEGATIVE
CHECK FOR DATA OUT OF ORDER.
DO 29 L =. 2, N
IF (X(L-l).GE.X(L» GO TO 777
IF (ITCH.LE.l) GO TO 29
IF (E(L).LT.O.O) GO TO 777
29 CONTINUE
EP:3LN = E( 1 )
YINIT = Y(l) - EPSLN
:::;dN = 1.0
KEEP = 1
U(l) = X(l)
U(M) IS THE LEFTMOST ABCISSA VALUE FOR THE
MTH SEGMENT AND THE RIGHTMOST ABCISSA VALUE
FOR THE (M-l)TH SEGMENT.
J
I
0011:..
= 1
I IS THE INDEX INTO THE X AND Y ARRAYS.
YEYE = Y(l) + EPSLN
XEYE, YEYE ARE THE COORDINATES OF THE
CURRENT EYEPOINT.
C
0017
C
C
,-.
'-'
FOR EACH LINE SEGMENT
C
c
3!:i
0018
0019
0020
c
CONT I NUE
IF (U(J).GE.X(N» GO TO 777
TEST FOR END OF DATA
XEYE = U(.J)
From the collection of the Computer History Museum (www.computerhistory.org)
Piecewise Linear Approximations of Fewest Line Segments
FORTRAN IV G LEVEL
C
rC:
0021
C
C
C
C
0022
c
002::::
0024
0025
C
r-
C
C
(l021::..
0027
C
r-
C
C
C
0029
c
c
c
STL
19
U(J-l), YINIT ARE THE COORDINATES OF
THE POINT OPPOSITE THE ORIGONAL EYEPOINT
OF THE CURRENT SEGMENT.
INIT :: 1
I NIT U:: THE I NDE X OF THE F I'R::::T 1 NPUT
X VALUE IN THE CURRENT SUBDIVISION.
THIS MAY NOT BE THE INITIAL X-VALUE
OF THE CURRENT SEGMENT.
IF (XEYE .GE. XCI») I:: 1+1
TEST FOR SEGMENT ENDING AT AN INPUT X VALUE
IF (ITCH.NE.l.AND.ITCH.LT.3) KEEP:: IN IT
IF (ITCH.GE.2) EPSLN :: SGN*E(I)
DX:: X(I) - XEYE
DX :: DISTANCE FROM CURRENT EYEPOINT TO XCOORDINATE OF POINT CURRENTLY BEING CONSIDERED. TO BE USED IN COMPUTING MAX AND MIN
SLOPES SMAX AND SMIN RESPECTIVELY
SMAX :: (Y(I) + EPSLN - YEYE)/DX
SMIN:: (YCI) - EPSLN - YEYE)/DX
IPIV :: I
IPIV IS THE INDEX OF THE RIGHTMOST DATA
POINT FOR WHICH THE CANDIDATE FOR SMAX IS
LESS THAN OR EQUAL TO THE CURRENT SMAX.
THIS LOCATES THE CANDIDATE FOR NEW EYEPOINT
IF PIVOTING IS NECE~SARY.
IGRAZE :: I
IGRAIE IS THE INDEX OF THE RIGHTMOST DATA
POINT FOR WHICH THE CANDIDATE FOR SMIN IS
GREATER THAN OR EQUAL TO THE CURRENT SMIN
.J :: .J + 1
00::::0
C
C
r-
C
C
INCREMENT INDEX INTO OUTPUT ARRAYS
END OF INITIALIZATION
*
* * * * * * * * *
* *
*
* *
*
* * *
* * *
DETERMINE MAX AND MIN SLOPES FOR SEGMENT
C
0031
0032
55 CONTINUE
c
IF (I .EI). N) GO TO 705
TEST FOR END OF DATA WITHIN CURRENT SEGMENT
I :: 1+1
00:::::::
C
00:34
0035
C
0031:..
00:37
C
003::::
00::::9
C
C
C
C
r-
0040
C
C
C
INCREMENT INDEX INTO DATA ARRAYS
57 CONTINUE
DX:: XCI) - XEYE
DX AS BEFORE
IF (ITCH.GE.Z) EPSLN :: SGN*E(I)
TEMP1 :: (Y(I) + EPSLN - YEYE)/DX
TEMP1 IS A CANDIDATE FOR SMAX
TEST :: TEMP1 - SMAX
IF (SGN.LE.O.O) TEST:: -TEST
SGN WILL BE POS IF PREVIOUS SEGMENT
ENDS AT TOP OF TUNNEL, NEG IF PREVIOUS
SEGMENT ENDS AT BOTTOM OF TUNNEL
IF SGN IS NEG CONSIDER TUNNEL TO BE UPSIDE
DOWN FOR TESTS WITH TEMPl AND TEMP2.
IF (TEST) 75, 91, 9~:;
IF CANDIDATE FOR SMAX IS GE OLD SMAX,
THEN KEEP OLD SMAX. OTHERWISE CANDIDATE
BECOMES THE NEW SMAX. EPSLN IS NEG IF
From the collection of the Computer History Museum (www.computerhistory.org)
195
196
Spring Joint Computer Conference, 1972
FORTRAN IV G LEVEL
C
19
STL
INITIAL EYEPOINT IS AT BOTTOM OF TUNNEL.
75 CONTINUE
0041
C
r
r
0042
004:3
0044
004~;
004t.
0047
0048
0049
r
C
0050
0051
0052
005::::
0054
0055
C
C
r
C
C
005l:;..
0057
005::::
0059
OOt.O
CANDIDATE FOR SMAX IS LESS THAN OLD SMAX.
TEST IF CANDIDATE IS ALSO LESS THAN OLD
SMIN.
IF SO END SEGMENT AT TOP OF TUNNEL.
TEST = TEMPl - SMIN
IF (SGN.LE.O.O) TEST = -TEST
IF (TE!::;T .LT. 0.0) GO TO lZ1
!::;MAX = TENPl
91 CONTINUE
IPIV = I
95 CONTINUE
TEMP2 = (Y(I) - EPSLN - YEYE)/DX
COMPUTE CANDIDATE FOR NEW SMIN AND CONPARE
WITH OLD MINIMUM SLOPE.
TEST = SMIN - TEMPZ
IF (SGN.LE.O.O) TEST = -TEST
IF <. TE~::;T) 9}, 99, 55
97 CONTINUE
TEST = TEMPZ - SNAX
IF (SGN.lE.O.O) TEST = -TEST
COMPARE NEW CANDIDATE FOR MIN SLOPE
WITH OLD NAX SLOPE.
IF NEW MIN IS
GREATER THAN OLD MAX CHECK IF PIVOT
IS POSSIBLE. OTHERWISE SET MIN SLOPE
TO NEW VALUE AND CONSIDER NEXT DATUM.
IF (TEST.GT.O.O) GO TO 101
st'll N = TEMP2
9'~ CONT I NUE
IGRAZE = I
GO TCI 55
C
C
C
*CHECK
* * *IF *PIVOT
* * * * * * * * * * * * * * * * *
POSSIBLE
C
0061
0062
C
C
C
C
006:::
0064
0065
OOt.,~.
00/:;..7
0068
OOt.9
0070
.-.
1_.
C
C
C
007 1
0072
0073
0074
0075
101 CONTINUE
IF (XEYE.EG!.X(IPIV}) GO TO 125
X(IPIV) IS THE X-COORD OF THE LAST DATUM
FOR WHICH THE CANDIDATE FOR SMAX WAS LESS
THAN OR EQUAL TO THE OLD SMAX.
IF XEYE IS
EQUAL TO X(IPIV) NO PIVOT IS POSSIBLE.
IF (ITCH.GE.2) EPSLN = SGN*E(IPIV)
XEYE = X(IPIV)
Y( IPIV) + EP~=;LN
YEYE
Sl"lIN =
Sl"lAX = (YINIT - YEYE)/(U(J-1) - XEYE)
IF (KEEP.GE.IPIV) GO TO 105
IT = IPIV - 1
TEMP2 = YEYE + EPSLN
COMPUTE THE NIN OF THE SLOPES FROM ALL
PRECEEDING DATA POINTS OF CURRENT SEGMENT
TO (X(IPIV),Y(IPIV) + 2*EPSLN). THIS WILL
BE A FIRST APPROXIMATION TO THE NEW SMAX.
DO 103 L = KEEP, IT
IF (ITCH.GE.2) TEMPZ
= YEYE + SGN*E(L)
TEMP1 = (Y<'L) - TEMPZ)/(X(L) - XEYE)
TEST = TEMP1 - S~~X
IF (SGN.LE.O.O) TEST = -TEST
From the collection of the Computer History Museum (www.computerhistory.org)
Piecewise Linear Approximations of Fewest Line Segments
FORTRAN IV G LEVEL
0076
0077
0078
0079
0080
19
STL
IF (TEST.LT.O.O) SMAX :: TEMPl
103 CONTINUE
105 CONTINUE
IF (IPIV.GE.I-1) GO TO 57
IT = I - 2
C
C
COMPUTE THE MIN OF THE SLOPES FROM ALL
SUCCEEDING DATA POINTS OF THE CURRENT
C
SEGMENT TO (X(IPIV),Y(IPIV»
IF THIS IS
C
LESS THAN THE PREVIOUSLY COMPUTED VALUE,
C
THEN IT BECOMES THE NEW SMAX.
TEMP2 = YEYE - EPSLN
DO 111 L = IPIV,IT
DX = X(L+l) - XEYE
IF (ITCH.GE.2) TEMP2 = YEYE - SGN*E(L+l)
TEMPl ::: (Y(L+l) - TEMP2)/DX
TEST ::: TEMP1 - SMAX
IF (SGN.LE.O.O) TEST = -TEST
IF (TEST) 107, 109, 111
107 CONTINUE
:::;MAX ::: TENP1
109 CONTINUE
IPIV ::: L + 1
111 CONTINUE
GO TO 57
C
0081
00:32
008:3
0084
0085
0081:.,
00::::7
0089
0090
0091
0092
009:3
0094
c
* * * * * * * * * * * * * * * * * ~ * * * *
END CURRENT SEGMENT
121 CONTINUE
TEMP2 ::: SNIN
KEEP ::: IGRAZE
c
EQ OF THIS SEGMENT IS
c
Y :: SMIN * (X - XEYE) + YEYE
NEW EYEPOINT WILL BE AT THE INTERSECTION
C
ro
OF THIS LINE WITH THE LINE BETWEEN
( X( 1-1 ) , Y( 1-1 ) +EP:::;;LN) AND (X <. I ) ,y <. I) +EP::;:LN )
C
IF (ITCH.LE.l) GO TO 135
GO TO 129
C
r
0095
0097
0098
0099
C
0100
0101
0102
0103
0104
C
ro
C
C
C
0105
C
C
0106
0107
0108
o 10':t
0110
0111
125 CONTINUE
SGN ::: -::;GN
EP:::;LN ::: -EPSLN
TENP2 ::: SMAX
KEEP:: IPIV
EQUATION OF THIS SEGNENT IS
Y :: SMAX * (X - XEYE) + YEYE
NEW EYEPOINT WILL BE AT THE INTERSECTION
OF THIS LINE WITH THE LINE BETWEEN
(X( 1--1) ,y( 1-1 )-EPSLN) AND (X( I) ,Y( I )-·EPSLN)
IF (ITCH.LE.l) GO TO 135
IF ITCH.LE.l, THEN EPSLN IS A CONSTANT
NAMELY E(l).
129 CONTINUE
TEMPl ::: EPSLN - SGN*E(I-l)
GO TO 1:37
13~i CONT I NUE
TEMPl ::: 0.0
137 CONTINUE
From the collection of the Computer History Museum (www.computerhistory.org)
197
198
Spring Joint Computer Conference, 1972
FORTf~~AN
IV
13
LE',jEL
01 1'-'.::..
1 '1
TEMP1 ::: (Y(I) - Y(I-l) + TEMP1)/
(X(I) - X(I-I»)
U(J) ::: (Y(I) + EPSLN - YEYE - TEMPl *
XCI) + TEMP2*XEYE)/(TEMP2 - TEMP1)
1
P( ,J-1) ::: TE\':1P2
PCM) IS THE SLOPE OF THE MTH SEGMENT
Q(J-l) ::: YEYE - TEMP2 * XEYE
Q(M) IS THE INTERCEPT OF THE MTH SEGMENT
YEYE::: TEMP2*U(J) + Q(J-l)
(XEYE,YEYE) WILL BE THE COORDINATES OF THE
NEW EYEPOINT.
XEYE WILL BE SET TO U(J).
IF SEGMENT ENDS AT BOTTOM OF TUNNEL, THEN
START NEXT SEGMENT WITH EYE POINT AT BOTTOM
OF TUNNEL.
IF SEGMENT ENDS AT TOP, START
NEXT WITH EYEPOINT AT TOP OF TUNNEL.
TEMP 1 ::: EP!:::LN
IF (ITCH.LE.l) GO TO 141
TEMP1 ::: EPSLN + (SGN*E(I-1) - EPSLN)*
1
(XCI) - U(.J»/(X(I) -, XCI-l»
1.41 CONTINUE
YINIT ::: YEYE - TEMP1 - TEMP1
IF APPRO X MUST BE CONTINUOUS, THEN U(J-l)
MAY HAVE TO BE RECOMPUTEDo
IN THIS CASE
ITCH WILL BE 1 OR :3.
IF (ITCH.NE.l.AND.ITCH.LT.3) GO TO 35
145 CONTINUE
IF (INIT.EQ.l) GO TO 35
IF INIT IS 1 THE CURRENT SEGMENT IS THE
INITIAL SEGt1ENT.
IF (XEYE.EQ.U(J-1» GO TO 35
IF XEYE IS STILL U(J-1), THEN NO PIVOT WAS
MADE AND U(J-1) NEED NOT BE RECOMPUTED.
U(J-1) :::(Q(J-2) - Q(J-1»/
(P(J-1) - P(J-2»
1
GO TO :::5
* * * * * * * * * * * * * * * * * * * * * *
END OF DATA ENCOUNTERED
1
01 1 ,-',
.~
01 14
C
01 15
C
01 1/;.
c·-'
C
r'-'
C
C
r'
01 17
01 1'-'1='
01 19
0120
0121
C
C
C
0122
012:3
0124
C
0125
c
c
0126
0127
C
C
C'
0128
0129
01::::0
01:31
01:32
01:3:3
01::::4
01:35
01 :36
705 CONTINUE
U(.J) ::: X(N)
P(J-l) ::: O.5*(SMAX + SMIN)
Q(J-l) ::: YEYE - XEYE * P(J-l)
IF (ITCH.EQ.l.0R.ITCH.GE.3) GO TO 145
777 CONTINUE
K:::.J-l
RETURN
END
From the collection of the Computer History Museum (www.computerhistory.org)
© Copyright 2025 Paperzz