# [metapost] Re: workaround for turningnumber bug

Larry Siebenmann laurent at math.toronto.edu
Wed Feb 2 17:22:37 CET 2005


Hi All,

Since some of us believe that turningnumber should be defined
only for embedded paths,  I suggest some algorithms
to calculate turningnumber that exploit this assumption.

ALGORITHM (1) [USING EXTERNAL TANGENT]

Suppose one KNOWS that a composite cyclic bezier path bb is
EMBEDDED with (at least on the bounding box of bb):

-- neither cusps
-- nor corners that turn \pm 180 degrees

Then find its turning number N as follows:

Intersect the curve bb with the left edge of its bounding box
getting an intersectiontime T.  That edge is given by the
'ulcorner' and 'llcorner' functions.

-- If T is non-integer, then the direction of bb there is down
for N=+1 and up for N=-1.

-- If T is integer and the turning angle at T is 0 then the
first criterion remains valid.

-- If T is integer and the turning angle of bb at T is
nonzero (this means bb(T) is a corner!), then the turning
angle of bb at time T has the sign of N.

Interestingly, most or all of the excluded behavior can be
dealt with; I'll explain if anyone is interested.

ALGORITHM (2) [USING AREA]

Suppose one KNOWS that a composite cyclic bezier path bb is
EMBEDDED, then then find its turning number N as follows.

Calculate the area of Area(bb).  N is then the sign of Area(bb).
Note that Area(bb) cannot be 0 because bb has a non-empty
interior by the Jordan curve theorem.

I hope some future revision of mp will implement Area as a
primitive for paths.  Since fixed arithmetic is used in mp it
might be necessary to return Area in a form that avoids
overflows, maybe the fraction in mils (positive or negative)
of the bounding box area.  The mp archives of a few years
back give very natural closed formulae for area.

I disapprove of the current use of turning number of
NON-EMBEDDED paths in mp, since it is nonexistent in general
and even when it exists by clever conventions it is a
discontinuous function of the control points. On the other
hand, area is always well defined -- and useful like mass, ink
and money.

Cheers

Laurent Siebenmann