15

On MetaPost, I can use p intersectionpoint q to get a intersection point. But It will only return one point. How can I get all intersection points?

1 Answer 1

18

Taken from http://www.tug.org/pipermail/metapost/2008-October/001469.html with a number of changes to make the code more robust and general:

beginfig(1)

  path p,q,r;
  p := fullcircle xscaled 72 yscaled 36 shifted (10cm, 10cm);
  q := fullcircle xscaled 36 yscaled 72 shifted (10cm, 10cm);
  r := p;
  draw p withcolor green;
  draw q withcolor red;
  n = 0;
  forever:
    r := r cutbefore q;
    exitif length cuttings = 0;
    r := subpath(epsilon, length r) of r;
    z[n] = point 0 of r;
    fill fullcircle scaled 2 shifted z[n] withcolor blue;
    n := n + 1;
  endfor;

endfig;

end

In practice one finds the first intersection, then cuts one of the two paths just after the intersection, and go on finding the next intersections.

1
  • 1
    Afaik, you don't find the 'first one' on a path, you find one according to some algorithm but you may end up with more intersections before and after the first you have found.
    – gctwnl
    Mar 20, 2020 at 23:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .