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

Hans Hagen j.hagen at xs4all.nl
Thu Feb 11 15:54:10 CET 2021


On 2/11/2021 2:45 PM, Mikael Sundqvist wrote:
> Hi,
> 
> since I already started this thread, I continue here. My metapost code
> is still not always working, and I do not understand what is going
> wrong. In the example below, I draw two curves of constant width (p1
> and p2), one rotated 180 degrees around the origin. Then I draw the
> curve (p3), constructed as follows: for each direction (phi), find the
> point of p1 and p2 which correspond to it, and add those points. This
> should result in a circle, and with the code I paste it does (hooray!
> see the file minkowski-good.pdf). BUT, it seems very unstable. If I
> change u to 0.5cm instead of 1cm, it breaks down (see
> minkowski-bad.pdf). If I loop over more angles phi (say step 2 instead
> of step 30), it gets wrong.
> 
> Any ideas are welcome.
You need help from a metapost-mathematician to answer this (ping ... 
Alan). Here is a variant that shows you what happens (keep in mind that 
".." is not always that useful with that amount of points):

\starttext
\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;

% 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;

% the minkowski sum (darkred) of the reulleaux curves p1 and p2.
% 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;

% for phi=0 step 30 until 360:
%     draw (directionpoint dir(phi) of p1) withpen pencircle scaled 4bp 
withcolor darkgreen;
%     draw (directionpoint dir(phi) of p2) withpen pencircle scaled 4bp 
withcolor darkmagenta;
% endfor ;

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
%     .5[directionpoint dir(phi) of p1, directionpoint dir(phi) of p2] 
-- endfor cycle
withpen pencircle scaled 1bp withcolor darkred;

% 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;

% dotlabel.top("$O$",origin);
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]
     draw image (FOO(1/20)) scaled 5cm withpen pencircle scaled 1bp;
\stopMPpage
\stoptext

You probably end up in the 1bp resolutions ... or something around the 
origin ... so maybe just calculate large and scale the result to what 
you want.

Hans

-----------------------------------------------------------------
                                           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