# [NTG-context] Metapost: directionpoint gives unexpected point(?)

Hans Hagen j.hagen at xs4all.nl
Fri Feb 12 10:53:39 CET 2021

```On 2/12/2021 9:35 AM, Taco Hoekwater wrote:
> Hi,
>
>> On 11 Feb 2021, at 17:41, Mikael Sundqvist <mickep at gmail.com> wrote:
>>
>> Thanks for your investigation and extended example!
>>
>> So, if I understand it correctly, the problem occurs where the
>> different circles are glued together with the .. construction.
>
> Took me a while to get it, but the problem is the definition of p0:
>
> p := cs .. cl .. (cs rotated 120) .. (cl rotated 120) .. (cs rotated 240) .. (cl rotated 240) .. cycle;
>
> Here are cs and cl after your earlier definition:
>
> cs := (141.73224999999996,-49.097491614210789)
>        ..(75.312386775380347,111.25424516116959)
>        ..(28.347427842053655,147.2925755432174);
>
> cl := (28.346108531095332,147.29283827977969)
>        ..(0,154.88788322842163)
>        ..(-28.346108531095332,147.29283827977969);
>
> Note how the last point of cs and the first point of cl are nearly the same. When you combine these bits into p0, p0 becomes a cyclic path with 18 points (where you really want/need only 12 points).
>
> The micro-segments between these nearly-identical paths are the problem. At smaller u values the differences between the points become zero, and the directionpoint of a path of length zero is mathematically undefined.
>
> I do not know a quick generic solution off hand, but that is what the issue is.
Brilliant, as usual. So, now I can kick in with the dirty hackery (can
be some proper thing but that's for later):

\starttext

\startluacode

function mp.foo()
local p = mp.scan.path()
local r = math.round
local d = 100000
for i=1,#p do
local pi = p[i]
pi = r(pi * d) / d
pi = r(pi * d) / d
end
local x1 = r(p)
local y1 = r(p)
local n = 1
local t = { p, cycle = p.cycle }
for i=2,#p do
local x2 = r(p[i])
local y2 = r(p[i])
if x1 ~= x2 or y1 ~= y2 then
n = n + 1
t[n] = p[i]
x1 = x2
y1 = y2
end
end
--    inspect(t)
mp.inject.path(t)
end

\stopluacode

\startMPdefinitions{doublefun}

def FOO(expr u) =

path p[];

% This defines the reulleaux curves
% p is a "base" reulleaux curve
path cl,cs,rl ;
z0 = (0,6/sqrt(3)*u);
z1 = z0 rotated 120;
cl := (fullcircle scaled 4u) shifted z0;
cl := cl cutbefore point 1/6 along cl cutafter point 2/6 along cl;
cs := (fullcircle scaled 16u) shifted z1;
cs := cs cutafter point 1/6 along cs;
p := cs .. cl .. (cs rotated 120) .. (cl rotated 120) .. (cs
rotated 240) .. (cl rotated 240) .. cycle;

% p := runscript("mp.foo()") p;

% the first curve (darkyellow)
% p := p rotated 27 shifted (-10u,2u);
p := p rotated 27 shifted (-10u,2u);
draw p1 withpen pencircle scaled 2bp withcolor darkyellow;
% the second curve (darkblue)
p := p rotated 180;
draw p2 withpen pencircle scaled 2bp withcolor darkblue;

if true :

p := runscript("mp.foo()") p;
p := runscript("mp.foo()") p;

p3 := for phi=0 step 30 until 360: ((directionpoint dir(phi) of p1)
shifted (directionpoint dir(phi) of p2)) .. endfor cycle;
draw p3 withpen pencircle scaled 2bp withcolor darkred;

else :

drawarrow for phi=0 step 30 until 360: (directionpoint dir(phi) of
p1) -- endfor cycle withpen pencircle scaled 1bp withcolor darkgreen;
drawarrow for phi=0 step 30 until 360: (directionpoint dir(phi) of
p2) -- endfor cycle withpen pencircle scaled 1bp withcolor darkmagenta;
drawarrow for phi=0 step 30 until 360:
((directionpoint dir(phi) of p1) shifted (directionpoint
dir(phi) of p2)) -- endfor cycle
withpen pencircle scaled 1bp withcolor darkred;

fi ;

% We give one direction as example
% These are merely here to show the construction of the curve
% But they also show what is going wrong

direx:=40;

z11=directionpoint dir(direx) of p1;
z22=directionpoint dir(direx) of p2;

p4 = ((-u,0)--(u,0)) rotated direx;

% These arrows should be tangent
drawarrow p4 shifted z11;
drawarrow p4 shifted z22;
drawarrow p4 shifted (z11 shifted z22);

% Draw the parallelogram.
draw origin -- z11 dashed evenly;
draw origin -- z22 dashed evenly;
draw z11 -- (z11 shifted z22) dashed evenly;
draw z22 -- (z11 shifted z22) dashed evenly;

enddef ;
\stopMPdefinitions

\startMPpage[offset=4bp,instance=doublefun]
FOO(1cm);
\stopMPpage
\startMPpage[offset=4bp,instance=doublefun]
FOO(.8cm);
\stopMPpage
\startMPpage[offset=4bp,instance=doublefun]
FOO(.5cm);
\stopMPpage
\startMPpage[offset=4bp,instance=doublefun]
FOO(.2cm);
\stopMPpage
\stoptext

-----------------------------------------------------------------