# [metapost] METATYPE1 pen-stroking and tight curves

Daniel Henry Luecking luecking at uark.edu
Wed Sep 19 21:23:08 CEST 2012

```Matthew Skala <mskala at ansuz.sooke.bc.ca> wrote:

> But I can't just look at the magnitude of that dot product because it
> scales with the vector lengths.  Scale the coordinates up by a factor of 2
> and the dot product will increase by a factor of 4.  So I want to divide
> by another factor that will scale the same way, but will not depend on
> angles.  The dot product of a vector with itself, equal to the length of
> the vector squared, behaves that way.  So I divide by
>   (z2-z1) dotprod (z2-z1)
> to cancel out the length scaling.

Metapost has the unitvector command. The combination:
(unitvector(z1-z0)) dotprod (unitvector (z3-z2))
will give the cosine of the angle difference. This is equivalent
to (( z1-z0)/abs(z1-z0)) dotprod ((z3-z2)/abs(z3-z2))

Metapost also has the angle command. The combination
angle ((z3-z2) rotated -angle(z1-z0))
will give the actual angle difference. This one will not suffer

If neither of these is suitable, and one is worried about division
by 0, one might consider dividing by
(z1-z0) dotprod (z1-z0) + (z2-z1) dotprod (z2-z1) +(z3-z2) dotprod (z3-z2)
which will only be 0 for trivial paths.

One may still have to worry about extreme cases. The angle
difference at the start and end of a path doesn't necessarily
reflect the amount of turning in between.

For example, it is a theoretically possible for a path segment
to turn arbitrarily close to 360 degrees, which would be seen
as a small angle difference between the directions at the ends.
It is also theoretically possible to have a nearly 180 degree
turn, followed by an inflection point and a nearly -180 degree
turn.

If this is a possibility, one might look instead at _two_ angle
differences: from z1-z0 to z2-z1 and from z2-z1  to z3-z2. If
the path itself turns a lot, then one or both of those two
angles will be large.

Finally, a large angle difference in a short space is bad. But
on a long path, the start and end can have very different
directions without a problem for the envelope.

Dan
--
Daniel H. Luecking
Department of Mathematical Sciences
University of Arkansas
Fayetteville, Arkansas
```