14

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?

16

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.

  • Great! I Will test it. – 比尔盖子 Oct 28 '12 at 2:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.