import math


def cartesian_to_polar(x, y, xorigin=0.0, yorigin=0.0):
    """

    Helper function to convert Cartesian coordinates to polar coordinates
    (centred at a defined origin). In the polar coordinates, theta is an
    angle measured clockwise from the Y axis.

    :Parameters:

    x: float
        X coordinate of point
    y: float
        Y coordinate of point
    xorigin: float (optional)
        X coordinate of origin (if not zero)
    yorigin: float (optional)
        Y coordinate of origin (if not zero)

    :Returns:

    (r, theta): tuple of 2 floats
        Polar coordinates of point. NOTE: theta is in radians.

    """
    PI2 = 2.0 * math.pi
    PIBY2 = math.pi / 2.0
    xdiff = float(x) - float(xorigin)
    ydiff = float(y) - float(yorigin)
    distsq = (xdiff * xdiff) + (ydiff * ydiff)
    r = math.sqrt(distsq)
    theta = PIBY2 - math.atan2(ydiff, xdiff)
    # Adjust theta to be in the range 0 - 2*PI
    while theta < 0.0:
        theta += PI2
    while theta > PI2:
        theta -= PI2
    return (r, theta)