# [metapost] turningnumber revisited

Dan Luecking luecking at uark.edu
Tue May 31 19:19:18 CEST 2011

```At 04:30 AM 5/31/2011, Boguslaw Jackowski wrote:

>Dear colleagues,
>
>I was nearly sure that the problem of turningnumber has
>gone forever... Alas, the simple example below shows that the
>implementation of the turningnumber primitive needs fixing:
>
>%%%% BEGIN turnpath.mp
>path p;
>p=origin .. controls (100,200) and (300,500) ..
>   (0,500) .. controls (-300,500) and (-100,200) ..
>   origin & cycle;
>for i:=0 upto 8: show turningnumber(p rotated 45i); endfor
>end.
>%%%% END turnpath.mp
>
>A 2-node, 4-control path above should have the turningnumber=1.
>And this is the case with MF; with MP, however, the result depends
>on the rotation of the path.
>
>MP 1.211 and MP 1.503 produce the result
>
>   >> -1
>   >> -1
>   >> -1
>   >> 1
>   >> 1
>   >> 1
>   >> 1
>   >> -1
>   >> -1

It is my guess that the cases are not handled properly.

case 1: there is no inflection point in a segment (no zero
of f'xf'') and no cusp in that case
- if f'xf'' is positive, the turning is a positive angle
between 0 and 360.
- If f'xf'' is negative the turning is a negative angle
between 0 and 360
case 2: one or two inflection points. The turning is between
-180 and 180.
case 3: a cusp (f'xf'' has a double 0) there is an ambiguity:
treat it as limiting case of no zeros or a pair of zeros
(latter is most natural, turning is between -180 and 180).
degenerate cases (f'xf'' is zero): The path lies in a line and
there may be an ambiguity (reversing direction on a line
could be either 180 ot -180).

It could be there is some point where where a starting angle is
subtracted from an ending angle, without proper adjustment to the
appropriate range, producing an incorrect sign. Though it is hard
to see how this could always happen to both segments (and not cause
cancellation, producing 0).

Perhaps the corner at the origin is sometimes taken as a small
negative change and in other cases a large positive change (still,
that should sometimes produce 0 if it is the only error).

Perhaps a combination of these conspiring to make two changes of
angle wrong simultaneously.

Some testing (same path, many minute changes in angle) shows that
one gets -1 when the starting direction has angle A in range
0 < A <= 180 and +1 otherwise.

Dan

Daniel H. Luecking
Department of Mathematical Sciences
Fayetteville, Arkansas
http://www-cs-faculty.stanford.edu/~knuth/iaq.html

```