U
    Pe                     @   s(   d Z dZddlmZ dd Zdd ZdS )	zo
Geometry utilities
==================

This module contains some helper functions for geometric calculations.
)circumcircleminimum_bounding_circle    Vectorc                 C   sJ  t | d | d }t |d |d }t |d |d }|| d }|| d }|j |j |j|j  |j|j  |j|j  |j|j  |j|j  |j|j  |j|j   }|j |j |j|j  |j|j  |j|j  |j|j  |j|j  }	||	 }
|
 |j|j  |j }|
|j|j  |j }||f|||f  fS )a  
    Computes the circumcircle of a triangle defined by a, b, c.
    See: http://en.wikipedia.org/wiki/Circumscribed_circle

    :Parameters:
        `a`: iterable containing at least 2 values (for x and y)
            The 1st point of the triangle.
        `b`: iterable containing at least 2 values (for x and y)
            The 2nd point of the triangle.
        `c`: iterable containing at least 2 values (for x and y)
            The 3rd point of the triangle.

    :Return:
        A tuple that defines the circle :
         * The first element in the returned tuple is the center as (x, y)
         * The second is the radius (float)
    r            ?)r   yxlength)abcPQRZmPQZmQRZnumerZdenomtZcxcy r   1/tmp/pip-unpacked-wheel-xzebddm3/kivy/geometry.pyr      s2    0



$


r   c                    s.  dd | D } t | dkr2| d j| d jfdfS t | dkrb| \}}|| d || d  fS t| dd	 d
  fdd}t| |d
| D ]} fdd}t| |d
}||dkr؈  d   d  f  S t|    dkr| qt  | dkr|q q"qt |S )a  
    Returns the minimum bounding circle for a set of points.

    For a description of the problem being solved, see the `Smallest Circle
    Problem <http://en.wikipedia.org/wiki/Smallest_circle_problem>`_.

    The function uses Applet's Algorithm, the runtime is ``O(h^3, *n)``,
    where h is the number of points in the convex hull of the set of points.
    **But** it runs in linear time in almost all real world cases.
    See: http://tinyurl.com/6e4n5yb

    :Parameters:
        `points`: iterable
            A list of points (2 tuple with x,y coordinates)

    :Return:
        A tuple that defines the circle:
            * The first element in the returned tuple is the center (x, y)
            * The second the radius (float)

    c                 S   s   g | ]}t |d  |d qS )r   r   r   ).0pr   r   r   
<listcomp>I   s     z+minimum_bounding_circle.<locals>.<listcomp>r   r   g           r   c                 S   s   | j S )N)r   )r   r   r   r   <lambda>S       z)minimum_bounding_circle.<locals>.<lambda>)keyc                    s   |  krdS t |   dS )N    _B)r   r   absangle)q)r   r   r   x_axis_angleW   s    z-minimum_bounding_circle.<locals>.x_axis_anglec                    s&   |  fkrdS t |   |  S )Nr   r   )rr   r   r   r   angle_pq_   s    z)minimum_bounding_circle.<locals>.angle_pqg     V@Z   )lenr	   r   r
   minr   r   r   )Zpointsp1p2r!   r   r$   r   r   r#   r   r   3   s,     r   N)__doc____all__Zkivy.vectorr   r   r   r   r   r   r   <module>   s   &