intersection_sphere_line

compas.geometry.intersection_sphere_line(sphere, line)[source]

Computes the intersection of a sphere and a line.

Parameters
  • sphere ([point, radius] | Sphere) – A sphere defined by a point and a radius.

  • line ([point, point] | Line) – A line defined by two points.

Returns

tuple[[float, float, float], [float, float, float]] | [float, float, float] | None – Two points (if the line goes through the sphere), one point (if the line is tangent to the sphere), or None (otherwise).

Notes

There are 3 cases of sphere-line intersection:

  1. they intersect in 2 points

  2. they intersect in 1 point (line tangent to sphere), or

  3. they do not intersect.

Examples

>>> from compas.geometry import allclose
>>> sphere = (3.0, 7.0, 4.0), 10.0
>>> line = (1.0, 0, 0.5), (2.0, 1.0, 0.5)
>>> x1, x2 = intersection_sphere_line(sphere, line)
>>> allclose(x1, [11.634, 10.634, 0.500], 1e-3)
True
>>> allclose(x2, [-0.634, -1.634, 0.50], 1e-3)
True