[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[0] := 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[1] = r(pi[1] * d) / d
pi[2] = r(pi[2] * d) / d
end
local x1 = r(p[1][1])
local y1 = r(p[1][2])
local n = 1
local t = { p[1], cycle = p.cycle }
for i=2,#p do
local x2 = r(p[i][1])
local y2 = r(p[i][2])
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[0] 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[0] := cs .. cl .. (cs rotated 120) .. (cl rotated 120) .. (cs
rotated 240) .. (cl rotated 240) .. cycle;
% p[0] := runscript("mp.foo()") p[0];
% the first curve (darkyellow)
% p[1] := p[0] rotated 27 shifted (-10u,2u);
p[1] := p[0] rotated 27 shifted (-10u,2u);
draw p1 withpen pencircle scaled 2bp withcolor darkyellow;
% the second curve (darkblue)
p[2] := p[1] rotated 180;
draw p2 withpen pencircle scaled 2bp withcolor darkblue;
if true :
p[1] := runscript("mp.foo()") p[1];
p[2] := runscript("mp.foo()") p[2];
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
-----------------------------------------------------------------
Hans Hagen | PRAGMA ADE
Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------
More information about the ntg-context
mailing list