Curve-Curve Intersection¶
The problem of intersecting two curves is a difficult one
in computational geometry. The Curve.intersect()
method uses a combination of curve subdivision, bounding
box intersection, and curve approximation (by lines) to
find intersections.
Curve-Line Intersection¶
>>> curve1 = bezier.Curve(np.array([
... [0.0, 0.0],
... [0.5, 1.0],
... [1.0, 0.0],
... ]))
>>> curve2 = bezier.Curve(np.array([
... [0.0, 0.375],
... [1.0, 0.375],
... ]))
>>> curve1.intersect(curve2)
array([[ 0.25 , 0.375],
[ 0.75 , 0.375]])

>>> curve1 = bezier.Curve(np.array([
... [0.0, 0.0],
... [0.5, 1.0],
... [1.0, 0.0],
... ]))
>>> curve2 = bezier.Curve(np.array([
... [0.5, 0.0 ],
... [0.5, 0.75],
... ]))
>>> curve1.intersect(curve2)
array([[ 0.5, 0.5]])

>>> curve1 = bezier.Curve(np.array([
... [0.0, 0.0],
... [4.5, 9.0],
... [9.0, 0.0],
... ]))
>>> curve2 = bezier.Curve(np.array([
... [0.0, 8.0],
... [6.0, 0.0],
... ]))
>>> curve1.intersect(curve2)
array([[ 3., 4.]])

>>> curve1 = bezier.Curve(np.array([
... [0.0, 0.375],
... [1.0, 0.375],
... ]))
>>> curve2 = bezier.Curve(np.array([
... [0.5, 0.0 ],
... [0.5, 0.75],
... ]))
>>> curve1.intersect(curve2)
array([[ 0.5 , 0.375]])

Curved Intersections¶
For curves which intersect at exact floating point numbers, we can typically compute the intersection with zero error:
>>> curve1 = bezier.Curve(np.array([
... [0.0, 0.0],
... [0.5, 1.0],
... [1.0, 0.0],
... ]))
>>> curve2 = bezier.Curve(np.array([
... [0.0, 0.75],
... [0.5, -0.25],
... [1.0, 0.75],
... ]))
>>> curve1.intersect(curve2)
array([[ 0.25 , 0.375],
[ 0.75 , 0.375]])

>>> curve1 = bezier.Curve(np.array([
... [0.0, 0.0],
... [1.5, 3.0],
... [3.0, 0.0],
... ]))
>>> curve2 = bezier.Curve(np.array([
... [ 3.0 , 1.5 ],
... [ 2.625, -0.90625],
... [-0.75 , 2.4375 ],
... ]))
>>> curve1.intersect(curve2)
array([[ 0.75 , 1.125 ],
[ 2.625 , 0.65625]])

>>> curve1 = bezier.Curve(np.array([
... [0.0 , 0.0 ],
... [0.375, 0.75 ],
... [0.75 , 0.375],
... ]))
>>> curve2 = bezier.Curve(np.array([
... [0.25 , 0.5625],
... [0.625, 0.1875],
... [1.0 , 0.9375],
... ]))
>>> curve1.intersect(curve2)
array([[ 0.375 , 0.46875],
[ 0.625 , 0.46875]])

Even for curves which don’t intersect at exact floating point numbers, we can compute the intersection to machine precision:
>>> curve1 = bezier.Curve(np.array([
... [0.0, 0.0],
... [0.5, 1.0],
... [1.0, 0.0],
... ]))
>>> curve2 = bezier.Curve(np.array([
... [1.125, 0.5],
... [0.625, -0.5],
... [0.125, 0.5],
... ]))
>>> intersections = curve1.intersect(curve2)
>>> sq31 = np.sqrt(31.0)
>>> expected = np.array([
... [36 - 4 * sq31, 16 + sq31],
... [36 + 4 * sq31, 16 - sq31],
... ]) / 64.0
>>> max_err = np.max(np.abs(intersections - expected))
>>> np.log2(max_err)
-54.0

>>> curve1 = bezier.Curve(np.array([
... [0.0, 0.0],
... [0.5, 1.0],
... [1.0, 0.0],
... ]))
>>> curve2 = bezier.Curve(np.array([
... [0.0, 0.265625],
... [0.5, 0.234375],
... [1.0, 0.265625],
... ]))
>>> intersections = curve1.intersect(curve2)
>>> sq33 = np.sqrt(33.0)
>>> expected = np.array([
... [33 - 4 * sq33, 17],
... [33 + 4 * sq33, 17],
... ]) / 66.0
>>> max_err = np.max(np.abs(intersections - expected))
>>> np.log2(max_err)
-54.0

>>> curve1 = bezier.Curve(np.array([
... [0.0, 0.0],
... [0.5, 1.0],
... [1.0, 0.0],
... ]))
>>> curve2 = bezier.Curve(np.array([
... [0.0 , 0.0],
... [0.25, 2.0],
... [0.5 , -2.0],
... [0.75, 2.0],
... [1.0 , 0.0],
... ]))
>>> intersections = curve1.intersect(curve2)
>>> sq7 = np.sqrt(7.0)
>>> expected = np.array([
... [7 - sq7, 6],
... [7 + sq7, 6],
... [ 0, 0],
... [ 14, 0],
... ]) / 14.0
>>> max_err = np.max(np.abs(intersections - expected))
>>> np.log2(max_err)
-54.0

>>> curve1 = bezier.Curve(np.array([
... [-0.125, -0.28125],
... [ 0.5 , 1.28125],
... [ 1.125, -0.28125],
... ]))
>>> curve2 = bezier.Curve(np.array([
... [ 1.5625, -0.0625],
... [-1.5625, 0.25 ],
... [ 1.5625, 0.5625],
... ]))
>>> intersections = curve1.intersect(curve2)
>>> sq5 = np.sqrt(5.0)
>>> expected = np.array([
... [6 - 2 * sq5, 5 - sq5],
... [ 4, 6 ],
... [ 16, 0 ],
... [6 + 2 * sq5, 5 + sq5],
... ]) / 16.0
>>> max_err = np.max(np.abs(intersections - expected))
>>> np.log2(max_err)
-50.415...

Intersections at Endpoints¶
>>> curve1 = bezier.Curve(np.array([
... [0.0, 0.0],
... [0.5, 1.0],
... [1.0, 0.0],
... ]))
>>> curve2 = bezier.Curve(np.array([
... [1.0, 0.0],
... [1.5, -1.0],
... [2.0, 0.0],
... ]))
>>> curve1.intersect(curve2)
array([[ 1., 0.]])

>>> curve1 = bezier.Curve(np.array([
... [0.0, 0.0],
... [0.5, 1.0],
... [1.0, 0.0],
... ]))
>>> curve2 = bezier.Curve(np.array([
... [2.0, 0.0],
... [1.5, 1.0],
... [1.0, 0.0],
... ]))
>>> curve1.intersect(curve2)
array([[ 1., 0.]])

>>> curve1 = bezier.Curve(np.array([
... [0.0, 0.0],
... [4.5, 9.0],
... [9.0, 0.0],
... ]))
>>> curve2 = bezier.Curve(np.array([
... [11.0, 8.0],
... [ 7.0, 10.0],
... [ 3.0, 4.0],
... ]))
>>> curve1.intersect(curve2)
array([[ 3., 4.]])

Detecting Self-Intersections¶
>>> curve1 = bezier.Curve(np.array([
... [ 0.0 , 0.0 ],
... [-1.0 , 0.0 ],
... [ 1.0 , 1.0 ],
... [-0.75, 1.625],
... ]))
>>> left, right = curve1.subdivide()
>>> left.intersect(right)
array([[-0.09375 , 0.578125]])

Limitations¶
Intersections that occur at points of tangency are in general problematic. For example, consider
The first curve is the zero set of \(y - 2x(1 - x)\), so plugging in the second curve gives
This shows that a point of tangency is equivalent to a repeated root of a polynomial. For this example, the intersection process successfully terminates
>>> curve1 = bezier.Curve(np.array([
... [0.0, 0.0],
... [0.5, 1.0],
... [1.0, 0.0],
... ]))
>>> curve2 = bezier.Curve(np.array([
... [0.0, 1.0],
... [0.5, 0.0],
... [1.0, 1.0],
... ]))
>>> curve1.intersect(curve2)
array([[ 0.5, 0.5]])

However this library mostly avoids (for now) computing tangent intersections. For example, the curves

have a tangent intersection that this library fails to compute:
>>> curve1 = bezier.Curve(np.array([
... [0.0 , 0.0 ],
... [0.375, 0.75 ],
... [0.75 , 0.375],
... ]))
>>> curve2 = bezier.Curve(np.array([
... [0.25 , 0.625],
... [0.625, 0.25 ],
... [1.0 , 1.0 ],
... ]))
>>> curve1.intersect(curve2)
Traceback (most recent call last):
...
NotImplementedError: Delta_0 x Delta_1 = 0 not supported
This failure comes from the fact that the linear approximations of the curves near the point of intersection are parallel.
As above, we can find some cases where tangent intersections are resolved:
>>> curve1 = bezier.Curve(np.array([
... [0.0, 0.0],
... [4.5, 9.0],
... [9.0, 0.0],
... ]))
>>> curve2 = bezier.Curve(np.array([
... [3.0, 4.5],
... [8.0, 4.5],
... ]))
>>> curve1.intersect(curve2)
array([[ 4.5, 4.5]])

In addition to points of tangency, coincident curve segments are (for now) not supported. For the curves

the library fails as well
>>> curve1 = bezier.Curve(np.array([
... [0.0, 0.0],
... [0.5, 1.0],
... [1.0, 0.0],
... ]))
>>> curve2 = bezier.Curve(np.array([
... [0.25, 0.375],
... [0.75, 0.875],
... [1.25, -0.625],
... ]))
>>> curve1.intersect(curve2)
Traceback (most recent call last):
...
NotImplementedError: The number of candidate intersections is too high.