# Path: skyfield/functions.py
# def length_of(xyz):
#     """Given a 3-element array |xyz|, return its length.
# 
#     The three elements can be simple scalars, or the array can be two
#     dimensions and offer three whole series of x, y, and z coordinates.
# 
#     """
#     return sqrt((xyz * xyz).sum(axis=0))


from numpy import nan, where
from .functions import length_of
"""Routines solving basic geometric problems in astronomy."""


def intersect_line_and_sphere(endpoint, center, radius):
    """Compute distance to intersections of a line and a sphere.

    Given a line through the origin (0,0,0) and an |xyz| ``endpoint``,
    and a sphere with the |xyz| ``center`` and scalar ``radius``,
    return the distance from the origin to their two intersections.

    If the line is tangent to the sphere, the two intersections will be
    at the same distance.  If the line does not intersect the sphere,
    two ``nan`` values will be returned.

    """
    # See http://paulbourke.net/geometry/circlesphere/index.html#linesphere
    # Names "b" and "c" designate the familiar values from the quadratic
    # formula; happily, a = 1 because we use a unit vector for the line.


# Path: skyfield/functions.py
# def length_of(xyz):
#     """Given a 3-element array |xyz|, return its length.
# 
#     The three elements can be simple scalars, or the array can be two
#     dimensions and offer three whole series of x, y, and z coordinates.
# 
#     """
#     return sqrt((xyz * xyz).sum(axis=0))


# Path: skyfield/geometry.py
from numpy import nan, where
from .functions import length_of

"""Routines solving basic geometric problems in astronomy."""


def intersect_line_and_sphere(endpoint, center, radius):
    """Compute distance to intersections of a line and a sphere.

    Given a line through the origin (0,0,0) and an |xyz| ``endpoint``,
    and a sphere with the |xyz| ``center`` and scalar ``radius``,
    return the distance from the origin to their two intersections.

    If the line is tangent to the sphere, the two intersections will be
    at the same distance.  If the line does not intersect the sphere,
    two ``nan`` values will be returned.

    """
    # See http://paulbourke.net/geometry/circlesphere/index.html#linesphere
    # Names "b" and "c" designate the familiar values from the quadratic
    # formula; happily, a = 1 because we use a unit vector for the line.


    minus_b = 2.0 * (endpoint / length_of(endpoint) * center).sum(axis=0)