[metapost] Several intersection points

Daniel H. Luecking luecking at uark.edu
Mon Feb 24 17:06:12 CET 2014

On  Monday, February 24, 2014 6:31 AM, José Carlos Santos [jcsantos at fc.up.pt] wrote:

>   for i=1 upto 2:
>    (t[i],whatever) = h1 intersectiontimes h3;
>    q[i] := point t[i] of h1;
>   endfor

This simply applies the intersectiontimes operator to the same pair of paths twice. 
Of course it will produce the same result. Your only hope is to change the paths. 

The intersection of paths is described in "The Metafontbook", 
page 137. The intersectiontimes operator will compare each cubic 
Bezier segment of h1 with each segment of h2. If the intersection 
points are in different segments of one or the other path, then the 
point found will be in the first segment of h1 where they intersect. 
If you want to find a later intersection, you might then split the
path (or reverse it if you are sure there are only 2 intersections):

  (whatever,t1) = h1 intersectiontimes h3;
  (whatever,t2) = (subpath (t1+eps, length h1) of h1) intersectiontimes h3;
  (whatever,t2) = (reverse h1) intersectiontimes h3;

One has to move a little past t1 (say to t1+eps) to get a second 
intersection, since MP often finds an intersection at a starting point. 
Beware that intersections are sometimes not found if they occur (
1) at an endpoint or (2) where the paths are tangent. Also, if paths 
are simply very very close at some point (but not theoretically 
intersecting), intersectiontimes may think they do intersect.

Finding multiple intersection points within a single Bezier
segment can be done the same way, but they are not guaranteed to
be in any particular order, so you have to examine the part 
before the first intersection found as well as the part after.


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

From: metapost [metapost-bounces at tug.org] on behalf of José Carlos Santos [jcsantos at fc.up.pt]
Sent: Monday, February 24, 2014 6:31 AM
To: metapost at tug.org
Subject: [metapost] Several intersection points

Hi all,

Today I tried for the first time to obtain in MetaPost all intersection
points of two lines. And I failed. :-( Here is the code:

----------------------- test.mp -----------------------

prologues := 3;
filenametemplate "%j.mps";
   numeric a,cc,cl,t[];
   pair p[],q[];
   path h[];
   a := 5mm;
   cc := 11a;
   cl := 10a;
   ct := 9.5a;
   p1 := (0,0);
   p2 := (cc,0);
   p3 := (cc,cl);
   p4 := (0,cl);
   h1 := (ct,0){up}
   for i=22.5 step 22.5 until 67.5:
   h2 := (0,0)
   for i=1/16 step 1/16 until 1:
   h3 := h2 reflectedabout ((p1+p2)/2,(p3+p4)/2);
   p5 := h1 intersectionpoint h2;
   for i=1 upto 2:
     (t[i],whatever) = h1 intersectiontimes h3;
     q[i] := point t[i] of h1;
   pickup pencircle scaled .8pt;
   for i=1 upto 3:
     draw h[i];
   pickup pencircle scaled 4pt;


The path h1 is a quarter of a circle and the path h3 is part of a
hyperbola. As you can see, they intersect twice. I tried to define q1
and q2 as these intersection points, but to MetaPost q1 = q2. That is,
if I compile the file above, the line "drawdot(q2)" draws one of the
intersection points, but if I replace it with "drawdot(q1)", then I get
the _same_ point. What am I doing wrong?

Best regards,

Jose Carlos Santos


More information about the metapost mailing list