Checking if two cubic Bézier curves intersect

前端 未结 8 525
星月不相逢
星月不相逢 2020-12-04 14:42

For a personal project, I\'d need to find out if two cubic Bézier curves intersect. I don\'t need to know where: I just need to know if they do. However, I\'d need to do it

相关标签:
8条回答
  • 2020-12-04 15:24

    I don't know how fast it will be, but if you have two curves C1(t) and C2(k) they intersect if C1(t) == C2(k). So you have two equations (per x and per y) for two variables (t, k). You can solve the system using numerical methods with enough for you accuracy. When you've found t,k parameters you should check if there is a parameter on [0, 1]. If it is they intersects on [0, 1].

    0 讨论(0)
  • 2020-12-04 15:26

    Intersection of Bezier curves is done by the (very cool) Asymptote vector graphics language: look for intersect() here.

    Although they don't explain the algorithm they actually use there, except to say that it's from p. 137 of "The Metafont Book", it appears that the key to it is two important properties of Bezier curves (which are explained elsewhere on that site though I can't find the page right now):

    • A Bezier curve is always contained within the bounding box defined by its 4 control points
    • A Bezier curve can always be subdivided at an arbitrary t value into 2 sub-Bezier curves

    With these two properties and an algorithm for intersecting polygons, you can recurse to arbitrary precision:

    bezInt(B1, B2):

    1. Does bbox(B1) intersect bbox(B2)?
      • No: Return false.
      • Yes: Continue.
    2. Is area(bbox(B1)) + area(bbox(B2)) < threshold?
      • Yes: Return true.
      • No: Continue.
    3. Split B1 into B1a and B1b at t = 0.5
    4. Split B2 into B2a and B2b at t = 0.5
    5. Return bezInt(B1a, B2a) || bezInt(B1a, B2b) || bezInt(B1b, B2a) || bezInt(B1b, B2b).

    This will be fast if the curves don't intersect -- is that the usual case?

    [EDIT] It looks like the algorithm for splitting a Bezier curve in two is called de Casteljau's algorithm.

    0 讨论(0)
提交回复
热议问题