
~9\c           @  s$  d  d l  m Z m Z d  d l m Z m Z m Z m Z m Z m	 Z	 d  d l
 m Z m Z m Z d  d l m Z m Z d  d l m Z d  d l m Z d  d l m Z m Z m Z d  d l m Z d  d	 l m Z d  d
 l m Z d  d l m  Z  d  d l! m" Z" d  d l# m$ Z$ m% Z% m& Z& m' Z' m( Z( d  d l) m* Z* d d l+ m, Z, m- Z- d d l. m/ Z/ d d l0 m1 Z1 d d l2 m3 Z3 m4 Z4 m5 Z5 d  d l6 m7 Z7 d  d l8 Z8 d e- f d     YZ9 d e9 f d     YZ: d e9 f d     YZ; d   Z< d   Z= d   Z> d   Z? d    Z@ d!   ZA d S("   i(   t   divisiont   print_function(   t   Exprt   St   Symbolt   oot   pit   sympify(   t   as_intt   ranget   ordered(   t   _symbolt   Dummy(   t   sign(   t	   Piecewise(   t   cost   sint   tan(   t   GeometryError(   t   And(   t   Matrix(   t   simplify(   t   default_sort_key(   t   has_dupst   has_varietyt   uniqt   rotate_leftt   least_rotation(   t	   func_namei   (   t   GeometryEntityt   GeometrySet(   t   Point(   t   Circle(   t   Linet   Segmentt   Ray(   t   sqrtNt   Polygonc           B  s  e  Z d  Z d   Z e d    Z e d    Z e d    Z e d    Z	 e d    Z
 e d    Z e d    Z d d	  Z e d
    Z e d    Z d   Z d   Z d d  Z d   Z d d  Z d   Z d   Z d   Z d d d  Z d   Z d   Z RS(   sj	  A two-dimensional polygon.

    A simple polygon in space. Can be constructed from a sequence of points
    or from a center, radius, number of sides and rotation angle.

    Parameters
    ==========

    vertices : sequence of Points

    Attributes
    ==========

    area
    angles
    perimeter
    vertices
    centroid
    sides

    Raises
    ======

    GeometryError
        If all parameters are not Points.

    See Also
    ========

    sympy.geometry.point.Point, sympy.geometry.line.Segment, Triangle

    Notes
    =====

    Polygons are treated as closed paths rather than 2D areas so
    some calculations can be be negative or positive (e.g., area)
    based on the orientation of the points.

    Any consecutive identical points are reduced to a single point
    and any points collinear and between two points will be removed
    unless they are needed to define an explicit intersection (see examples).

    A Triangle, Segment or Point will be returned when there are 3 or
    fewer points provided.

    Examples
    ========

    >>> from sympy import Point, Polygon, pi
    >>> p1, p2, p3, p4, p5 = [(0, 0), (1, 0), (5, 1), (0, 1), (3, 0)]
    >>> Polygon(p1, p2, p3, p4)
    Polygon(Point2D(0, 0), Point2D(1, 0), Point2D(5, 1), Point2D(0, 1))
    >>> Polygon(p1, p2)
    Segment2D(Point2D(0, 0), Point2D(1, 0))
    >>> Polygon(p1, p2, p5)
    Segment2D(Point2D(0, 0), Point2D(3, 0))

    The area of a polygon is calculated as positive when vertices are
    traversed in a ccw direction. When the sides of a polygon cross the
    area will have positive and negative contributions. The following
    defines a Z shape where the bottom right connects back to the top
    left.

    >>> Polygon((0, 2), (2, 2), (0, 0), (2, 0)).area
    0

    When the the keyword `n` is used to define the number of sides of the
    Polygon then a RegularPolygon is created and the other arguments are
    interpreted as center, radius and rotation. The unrotated RegularPolygon
    will always have a vertex at Point(r, 0) where `r` is the radius of the
    circle that circumscribes the RegularPolygon. Its method `spin` can be
    used to increment that angle.

    >>> p = Polygon((0,0), 1, n=3)
    >>> p
    RegularPolygon(Point2D(0, 0), 1, 3, 0)
    >>> p.vertices[0]
    Point2D(1, 0)
    >>> p.args[0]
    Point2D(0, 0)
    >>> p.spin(pi/2)
    >>> p.vertices[0]
    Point2D(0, 1)

    c         O  sH  | j  d d  r | j d  } t |  } t |  d k rO | j |  n% t |  d k rt | j d |  n  t | |   Sg  | D] } t | d d | ^ q } g  } x7 | D]/ } | r | | d k r q n  | j |  q Wt |  d k r| d | d k r| j   n  d } x | t |  d k  rt |  d k r| | | | d | | d } }	 }
 t j | |	 |
  r| j | d  | |
 k r| j |  qq%| d 7} q%Wt |  } t |  d k rt	 j
 |  | |  St |  d k rt | |   St |  d k r7t | |   St | |   Sd  S(	   Nt   ni    i   i   t   dimii   i(   t   gett   popt   listt   lent   appendt   insertt   RegularPolygonR   t   is_collinearR   t   __new__t   TriangleR"   (   t   clst   argst   kwargsR&   t   at   verticest   nodupt   pt   it   bt   c(    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyR0   r   s@    (&+(c         C  sz   d } |  j  } xZ t t |   D]F } | | d j  \ } } | | j  \ } } | | | | | 7} q" Wt |  d S(   s  
        The area of the polygon.

        Notes
        =====

        The area calculation can be positive or negative based on the
        orientation of the points. If any side of the polygon crosses
        any other side, there will be areas having opposite signs.

        See Also
        ========

        sympy.geometry.ellipse.Ellipse.area

        Examples
        ========

        >>> from sympy import Point, Polygon
        >>> p1, p2, p3, p4 = map(Point, [(0, 0), (1, 0), (5, 1), (0, 1)])
        >>> poly = Polygon(p1, p2, p3, p4)
        >>> poly.area
        3

        In the Z shaped polygon (with the lower right connecting back
        to the upper left) the areas cancel out:

        >>> Z = Polygon((0, 1), (1, 1), (0, 0), (1, 0))
        >>> Z.area
        0

        In the M shaped polygon, areas do not cancel because no side
        crosses any other (though there is a point of contact).

        >>> M = Polygon((0, 0), (0, 1), (2, 0), (3, 1), (3, 0))
        >>> M.area
        -3/2

        i    i   i   (   R3   R	   R+   R   (   t   selft   areaR3   R9   t   x1t   y1t   x2t   y2(    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyR=      s    )	c         C  s`   | |  } | |  } t  | j | j | j | j  } | j } | d k r\ t d   n  | S(   s-  Return True/False for cw/ccw orientation.

        Examples
        ========

        >>> from sympy import Point, Polygon
        >>> a, b, c = [Point(i) for i in [(0, 0), (1, 1), (1, 0)]]
        >>> Polygon._isright(a, b, c)
        True
        >>> Polygon._isright(a, c, b)
        False
        s   Can't determine orientationN(   R   t   xt   yt   is_nonpositivet   Nonet
   ValueError(   R5   R:   R;   t   bat   cat   t_areat   res(    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyt   _isright   s    

$	c   	      C  s   |  j  } |  j | d | d | d  } i  } x t t |   D] } | | d | | d | | } } } t | |  j t | |   } | |  j | | |  Ar d t j | | | <qC | | | <qC W| S(   s  The internal angle at each vertex.

        Returns
        =======

        angles : dict
            A dictionary where each key is a vertex and each value is the
            internal angle at that vertex. The vertices are represented as
            Points.

        See Also
        ========

        sympy.geometry.point.Point, sympy.geometry.line.LinearEntity.angle_between

        Examples
        ========

        >>> from sympy import Point, Polygon
        >>> p1, p2, p3, p4 = map(Point, [(0, 0), (1, 0), (5, 1), (0, 1)])
        >>> poly = Polygon(p1, p2, p3, p4)
        >>> poly.angles[p1]
        pi/2
        >>> poly.angles[p2]
        acos(-4*sqrt(17)/17)

        ii    i   i   (   R6   RK   R	   R+   R#   t   angle_betweenR   t   Pi(	   R<   R3   t   cwt   retR9   R5   R:   R;   t   ang(    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyt   angles   s    	!(!c         C  s   |  j  d j S(   Ni    (   R6   t   ambient_dimension(   R<   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyRR     s    c         C  sU   d } |  j  } x9 t t |   D]% } | | | d j | |  7} q" Wt |  S(   s  The perimeter of the polygon.

        Returns
        =======

        perimeter : number or Basic instance

        See Also
        ========

        sympy.geometry.line.Segment.length

        Examples
        ========

        >>> from sympy import Point, Polygon
        >>> p1, p2, p3, p4 = map(Point, [(0, 0), (1, 0), (5, 1), (0, 1)])
        >>> poly = Polygon(p1, p2, p3, p4)
        >>> poly.perimeter
        sqrt(17) + 7
        i    i   (   R6   R	   R+   t   distanceR   (   R<   R8   R3   R9   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyt	   perimeter  s
    	#c         C  s   t  |  j  S(   sQ  The vertices of the polygon.

        Returns
        =======

        vertices : list of Points

        Notes
        =====

        When iterating over the vertices, it is more efficient to index self
        rather than to request the vertices and index them. Only use the
        vertices when you want to process all of them at once. This is even
        more important with RegularPolygons that calculate each vertex.

        See Also
        ========

        sympy.geometry.point.Point

        Examples
        ========

        >>> from sympy import Point, Polygon
        >>> p1, p2, p3, p4 = map(Point, [(0, 0), (1, 0), (5, 1), (0, 1)])
        >>> poly = Polygon(p1, p2, p3, p4)
        >>> poly.vertices
        [Point2D(0, 0), Point2D(1, 0), Point2D(5, 1), Point2D(0, 1)]
        >>> poly.vertices[0]
        Point2D(0, 0)

        (   R*   R3   (   R<   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyR6   2  s    "c         C  s   d d |  j  } d \ } } |  j } xz t t |   D]f } | | d j \ } } | | j \ } }	 | |	 | | }
 | |
 | | 7} | |
 | |	 7} q9 Wt t | |  t | |   S(   s  The centroid of the polygon.

        Returns
        =======

        centroid : Point

        See Also
        ========

        sympy.geometry.point.Point, sympy.geometry.util.centroid

        Examples
        ========

        >>> from sympy import Point, Polygon
        >>> p1, p2, p3, p4 = map(Point, [(0, 0), (1, 0), (5, 1), (0, 1)])
        >>> poly = Polygon(p1, p2, p3, p4)
        >>> poly.centroid
        Point2D(31/18, 11/18)

        i   i   i    (   i    i    (   R=   R3   R	   R+   R   R   (   R<   t   At   cxt   cyR3   R9   R>   R?   R@   RA   t   v(    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyt   centroidV  s    	c         C  s  d \ } } } |  j  } x t t |   D] } | | d j  \ } } | | j  \ }	 }
 | |
 |	 | } | | d | |
 |
 d | 7} | | d | |	 |	 d | 7} | | |
 d | | d |	 |
 |	 | | 7} q+ W|  j } |  j d } |  j d } | d | | d } | d | | d } | d | | | } | d k re| | | f S| | | d | d } | | | d | d } | | | d | | d | } | | | f S(   s  Returns the second moment and product moment of area of a two dimensional polygon.

        Parameters
        ==========

        point : Point, two-tuple of sympifiable objects, or None(default=None)
            point is the point about which second moment of area is to be found.
            If "point=None" it will be calculated about the axis passing through the
            centroid of the polygon.

        Returns
        =======

        I_xx, I_yy, I_xy : number or sympy expression
                           I_xx, I_yy are second moment of area of a two dimensional polygon.
                           I_xy is product moment of area of a two dimensional polygon.

        Examples
        ========

        >>> from sympy import Point, Polygon, symbols
        >>> a, b = symbols('a, b')
        >>> p1, p2, p3, p4, p5 = [(0, 0), (a, 0), (a, b), (0, b), (a/3, b/3)]
        >>> rectangle = Polygon(p1, p2, p3, p4)
        >>> rectangle.second_moment_of_area()
        (a*b**3/12, a**3*b/12, 0)
        >>> rectangle.second_moment_of_area(p5)
        (a*b**3/9, a**3*b/9, a**2*b**2/36)

        References
        ==========

        https://en.wikipedia.org/wiki/Second_moment_of_area

        i    i   i   i   i   (   i    i    i    N(   R3   R	   R+   R=   RY   RE   (   R<   t   pointt   I_xxt   I_yyt   I_xyR3   R9   R>   R?   R@   RA   RX   RU   t   c_xt   c_yt   I_xx_ct   I_yy_ct   I_xy_c(    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyt   second_moment_of_areaz  s*    %	""6	"c         C  sV   g  } |  j  } x@ t t |  d  D]( } | j t | | | | d   q& W| S(   s  The directed line segments that form the sides of the polygon.

        Returns
        =======

        sides : list of sides
            Each side is a directed Segment.

        See Also
        ========

        sympy.geometry.point.Point, sympy.geometry.line.Segment

        Examples
        ========

        >>> from sympy import Point, Polygon
        >>> p1, p2, p3, p4 = map(Point, [(0, 0), (1, 0), (5, 1), (0, 1)])
        >>> poly = Polygon(p1, p2, p3, p4)
        >>> poly.sides
        [Segment2D(Point2D(0, 0), Point2D(1, 0)),
        Segment2D(Point2D(1, 0), Point2D(5, 1)),
        Segment2D(Point2D(5, 1), Point2D(0, 1)), Segment2D(Point2D(0, 1), Point2D(0, 0))]

        i    i   (   R6   R	   R+   R,   R"   (   R<   RJ   R3   R9   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyt   sides  s
    	&c         C  si   |  j  } g  | D] } | j ^ q } g  | D] } | j ^ q, } t |  t |  t |  t |  f S(   sw   Return a tuple (xmin, ymin, xmax, ymax) representing the bounding
        rectangle for the geometric figure.

        (   R6   RB   RC   t   mint   max(   R<   t   vertsR8   t   xst   ys(    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyt   bounds  s    	c   
      C  s,  |  j  } |  j | d | d | d  } xN t d t |   D]7 } | |  j | | d | | d | |  Ar@ t Sq@ W|  j } x t |  D] \ } } | j } x{ t | t |  d k r d n d | d  D]J } | | } | j | k r | j	 | k r | j
 |  }	 |	 r t Sq q Wq Wt S(   s_  Is the polygon convex?

        A polygon is convex if all its interior angles are less than 180
        degrees and there are no intersections between sides.

        Returns
        =======

        is_convex : boolean
            True if this polygon is convex, False otherwise.

        See Also
        ========

        sympy.geometry.util.convex_hull

        Examples
        ========

        >>> from sympy import Point, Polygon
        >>> p1, p2, p3, p4 = map(Point, [(0, 0), (1, 0), (5, 1), (0, 1)])
        >>> poly = Polygon(p1, p2, p3, p4)
        >>> poly.is_convex()
        True

        iii    i   i   (   R6   RK   R	   R+   t   FalseRd   t	   enumerateR3   t   p1t   p2t   intersectiont   True(
   R<   R3   RN   R9   Rd   t   sit   ptst   jt   sjt   hit(    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyt	   is_convex  s    	!-		6
c           s,  t    d d     |  j k s@ t   f d   |  j D  rD t Sg  } x3 |  j D]( } | j |    | d j rT d SqT Wt |   } | j	 } t
 t t |  d   } | j   rFd } x| | D]t } | | }	 | | d }
 |	 j |
 j |	 j |	 j |
 j |	 j j } | d k r.| } q | | k	 r t Sq Wt St } | d j	 \ } } x | d D] } | | j	 \ } } d t | |  k rd t | |  k rd t | |  k r| | k r| | | | | | } | | k sd | k r| } qqqqn  | | } } qjW| S(   sT  
        Return True if p is enclosed by (is inside of) self.

        Notes
        =====

        Being on the border of self is considered False.

        Parameters
        ==========

        p : Point

        Returns
        =======

        encloses_point : True, False or None

        See Also
        ========

        sympy.geometry.point.Point, sympy.geometry.ellipse.Ellipse.encloses_point

        Examples
        ========

        >>> from sympy import Polygon, Point
        >>> from sympy.abc import t
        >>> p = Polygon((0, 0), (4, 0), (4, 4))
        >>> p.encloses_point(Point(2, 1))
        True
        >>> p.encloses_point(Point(2, 2))
        False
        >>> p.encloses_point(Point(5, 5))
        False

        References
        ==========

        [1] http://paulbourke.net/geometry/polygonmesh/#insidepoly

        R'   i   c         3  s   |  ] }   | k Vq d  S(   N(    (   t   .0t   s(   R8   (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pys	   <genexpr>@  s    ii   i    N(   R   R6   t   anyRd   Rk   R,   t   free_symbolsRE   R%   R3   R*   R	   R+   Rv   RC   RB   t   is_negativeRp   Re   Rf   (   R<   R8   t   litRX   t   polyR3   t   indicest   orientationR9   R5   R:   t   testt   hit_oddt   p1xt   p1yt   p2xt   p2yt   xinters(    (   R8   s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyt   encloses_point  sF    +.	
1	t   tc   
      C  s   t  | d t } | j d   |  j D k rD t d | j   n  g  } |  j } d } xy |  j D]n } | j | } | | } | j |  j	 | | | |  }	 | j
 |	 t | | k | | k   f  | } qc Wt |   S(   s.  A parameterized point on the polygon.

        The parameter, varying from 0 to 1, assigns points to the position on
        the perimeter that is that fraction of the total perimeter. So the
        point evaluated at t=1/2 would return the point from the first vertex
        that is 1/2 way around the polygon.

        Parameters
        ==========

        parameter : str, optional
            Default value is 't'.

        Returns
        =======

        arbitrary_point : Point

        Raises
        ======

        ValueError
            When `parameter` already appears in the Polygon's definition.

        See Also
        ========

        sympy.geometry.point.Point

        Examples
        ========

        >>> from sympy import Polygon, S, Symbol
        >>> t = Symbol('t', real=True)
        >>> tri = Polygon((0, 0), (1, 0), (1, 1))
        >>> p = tri.arbitrary_point('t')
        >>> perimeter = tri.perimeter
        >>> s1, s2 = [s.length for s in tri.sides[:2]]
        >>> p.subs(t, (s1 + s2/2)/perimeter)
        Point2D(1, 1/2)

        t   realc         s  s   |  ] } | j  Vq d  S(   N(   t   name(   Rw   t   f(    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pys	   <genexpr>  s    sF   Symbol %s already appears in object and cannot be used as a parameter.i    (   R   Rp   R   Rz   RF   RT   Rd   t   lengtht   arbitrary_pointt   subsR,   R   R   (
   R<   t	   parameterR   Rd   RT   t   perim_fraction_startRx   t   side_perim_fractiont   perim_fraction_endt   pt(    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyR   l  s    +	
"
c         C  sI  d d l  m } t | t  s7 t | d |  j } n  t | t  sU t d   n  | j rm t d   n  t	 } t
 d d t } |  j |  } xy | j D]n \ } } | | | | d t }	 |	 s q n  |	 d	 | }
 t | j | |
   t k ri |
 | 6St } q W| r/t d
 t |     n  t d t |     d  S(   Ni(   t   solveR'   s   other must be a points   non-numeric coordinatesR   R   t   dicti    s   Given point may not be on %ss   Given point is not on %s(   t   sympy.solvers.solversR   t
   isinstanceR   R   RR   RF   Rz   t   NotImplementedErrorRk   R   Rp   R   R3   R   R   R   (   R<   t   otherR   R   t   unknownt   TR8   R   t   condt   solt   value(    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyt   parameter_value  s*    	
c         C  s   t  | d t } | d d g S(   s  The plot interval for the default geometric plot of the polygon.

        Parameters
        ==========

        parameter : str, optional
            Default value is 't'.

        Returns
        =======

        plot_interval : list (plot interval)
            [parameter, lower_bound, upper_bound]

        Examples
        ========

        >>> from sympy import Polygon
        >>> p = Polygon((0, 0), (1, 0), (1, 1))
        >>> p.plot_interval()
        [t, 0, 1]

        R   i    i   (   R   Rp   (   R<   R   R   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyt   plot_interval  s    c         C  s]  g  } t  | t  r | j n | g } x8 |  j D]- } x$ | D] } | j | j |   q> Wq1 Wt t |   } g  | D] } t  | t  r{ | ^ q{ } g  | D] } t  | t  r | ^ q } | rI| rIt t g  | D]% }	 | D] }
 |	 |
 k r |	 ^ q q   } | r5x | D] } | j	 |  qWn  t t
 | |   St t
 |   Sd S(   s  The intersection of polygon and geometry entity.

        The intersection may be empty and can contain individual Points and
        complete Line Segments.

        Parameters
        ==========

        other: GeometryEntity

        Returns
        =======

        intersection : list
            The list of Segments and Points

        See Also
        ========

        sympy.geometry.point.Point, sympy.geometry.line.Segment

        Examples
        ========

        >>> from sympy import Point, Polygon, Line
        >>> p1, p2, p3, p4 = map(Point, [(0, 0), (1, 0), (5, 1), (0, 1)])
        >>> poly1 = Polygon(p1, p2, p3, p4)
        >>> p5, p6, p7 = map(Point, [(3, 2), (1, -1), (0, 2)])
        >>> poly2 = Polygon(p5, p6, p7)
        >>> poly1.intersection(poly2)
        [Point2D(1/3, 1), Point2D(2/3, 0), Point2D(9/5, 1/5), Point2D(7/3, 1)]
        >>> poly1.intersection(Line(p1, p2))
        [Segment2D(Point2D(0, 0), Point2D(1, 0))]
        >>> poly1.intersection(p1)
        [Point2D(0, 0)]
        N(   R   R%   Rd   t   extendRo   R*   R   R   R"   t   removeR
   (   R<   t   ot   intersection_resultt   kt   sidet   side1t   entityt   pointst   segmentsRZ   t   segmentt   points_in_segmentsR9   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyRo     s    %!((>c         C  s   t  | t  rd t } xH |  j D]= } | j |  } | d k rG t j S| | k  r | } q q W| St  | t  r |  j   r | j   r |  j	 |  St
    d S(   s  
        Returns the shortest distance between self and o.

        If o is a point, then self does not need to be convex.
        If o is another polygon self and o must be convex.

        Examples
        ========

        >>> from sympy import Point, Polygon, RegularPolygon
        >>> p1, p2 = map(Point, [(0, 0), (7, 5)])
        >>> poly = Polygon(*RegularPolygon(p1, 1, 3).vertices)
        >>> poly.distance(p2)
        sqrt(61)
        i    N(   R   R   R   Rd   RS   R   t   ZeroR%   Rv   t   _do_poly_distanceR   (   R<   R   t   distR   t   current(    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyRS     s    'c          C  s  |  } | j  } | j  } t j } t j } x8 | j D]- } t j | |  } | | k  r4 | } q4 q4 Wx8 | j D]- } t j | |  } | | k  ro | } qo qo Wt j | |  }	 |	 | | k r t j d  n  t d t  }
 t d t  } xP | j D]E } | j	 |
 j	 k s7| j	 |
 j	 k r | j
 |
 j
 k r | }
 q q WxP | j D]E } | j	 | j	 k  s| j	 | j	 k rN| j
 | j
 k  rN| } qNqNWt j |
 |  } i  } i  } x | j D]~ } | j | k r| | j j | j  n | j g | | j <| j | k r*| | j j | j  q| j g | | j <qWx | j D]~ } | j | k rz| | j j | j  n | j g | | j <| j | k r| | j j | j  qK| j g | | j <qKW|
 } | } t t t j t j  t t j t j   } | |
 d } | |
 d } | j t |
 |   } | j t |
 |   } | | k  rg| } nH | | k  r|| } n3 t j |
 |  t j |
 |  k r| } n | } | | d } | | d } | j t | |   } | j t | |   } | | k r| } nH | | k r%| } n3 t j | |  t j | |  k rR| } n | } xt r| j t | |   } t | j t | |   } | | k  t k r7t | |  } t | |  } | j |  } | j   | j   k  r| } n  | | d | k r | } | | d } q| } | | d } n| | k t k rt | |  } t | |  } | j |  } | j   | j   k  r| } n  | | d | k r| } | | d } q| } | | d } n t | |  } t | |  } t | |  } | j |  } | j |  } t | |  } | j   | j   k  rT| } n  | | d | k r| } | | d } n | } | | d } | | d | k r| } | | d } n | } | | d } | |
 k r[| | k r[Pq[q[W| S(   s  
        Calculates the least distance between the exteriors of two
        convex polygons e1 and e2. Does not check for the convexity
        of the polygons as this is checked by Polygon.distance.

        Notes
        =====

            - Prints a warning if the two polygons possibly intersect as the return
              value will not be valid in such a case. For a more through test of
              intersection use intersection().

        See Also
        ========

        sympy.geometry.point.Point.distance

        Examples
        ========

        >>> from sympy.geometry import Point, Polygon
        >>> square = Polygon(Point(0, 0), Point(0, 1), Point(1, 1), Point(1, 0))
        >>> triangle = Polygon(Point(1, 2), Point(2, 2), Point(2, 1))
        >>> square._do_poly_distance(triangle)
        sqrt(2)/2

        Description of method used
        ==========================

        Method:
        [1] http://cgm.cs.mcgill.ca/~orm/mind2p.html
        Uses rotating calipers:
        [2] https://en.wikipedia.org/wiki/Rotating_calipers
        and antipodal points:
        [3] https://en.wikipedia.org/wiki/Antipodal_point
        s1   Polygons may intersect producing erroneous outputi    i   (   RY   R   R   R6   R   RS   t   warningst   warnR   RC   RB   Rd   Rm   R,   Rn   R!   t   OneRL   Rp   R   R"   t   evalfRe   (    R<   t   e2t   e1t	   e1_centert	   e2_centert   e1_max_radiust   e2_max_radiust   vertext   rt   center_distt   e1_ymaxt   e2_ymint   min_distt   e1_connectionst   e2_connectionsR   t
   e1_currentt
   e2_currentt   support_linet   point1t   point2t   angle1t   angle2t   e1_nextt   e2_nextt   e1_anglet   e2_anglet
   e1_segmentt   min_dist_currentt
   e2_segmentt   min1t   min2(    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyR   /  s    %				66-		$			$					g      ?s   #66cc99c         C  s   d d l  m } t | |  j  } g  | D] } d j | j | j  ^ q) } d j | d d j | d   } d j d	 | | |  S(
   s"  Returns SVG path element for the Polygon.

        Parameters
        ==========

        scale_factor : float
            Multiplication factor for the SVG stroke-width.  Default is 1.
        fill_color : str, optional
            Hex string for fill color. Default is "#66cc99".
        i(   t   Ns   {0},{1}s   M {0} L {1} zi    s    L i   sa   <path fill-rule="evenodd" fill="{2}" stroke="#555555" stroke-width="{0}" opacity="0.6" d="{1}" />g       @(   t   sympy.core.evalfR   t   mapR6   t   formatRB   RC   t   join(   R<   t   scale_factort
   fill_colorR   Rg   R8   t   coordst   path(    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyt   _svg  s    +#c   	        s   i      f d   } | |  j   } t | t |   } | t t |  j     } t | t |   } | | k  r~ | } n | } g  | D] }   | ^ q } t |  S(   Nc           s^   i  } x: t  t t |     D]  \ } } | | | <|   | <q Wg  |  D] } | | ^ qJ S(   N(   Rl   R
   t   set(   t
   point_listt   keeR9   R8   (   t   D(    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyt   ref_list  s
    %
(   R3   R   R   R*   t   reversedt   tuple(	   R<   R   t   S1t   r_nort   S2t   r_revR   t   ordert   canonical_args(    (   R   s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyt   _hashable_content  s    	c           s   t    t  r |    k St    t  rE t   f d   |  j D  St    t  r   |  j k rg t Sx$ |  j D] }   | k rq t Sqq Wn  t S(   s  
        Return True if o is contained within the boundary lines of self.altitudes

        Parameters
        ==========

        other : GeometryEntity

        Returns
        =======

        contained in : bool
            The points (and sides, if applicable) are contained in self.

        See Also
        ========

        sympy.geometry.entity.GeometryEntity.encloses

        Examples
        ========

        >>> from sympy import Line, Segment, Point
        >>> p = Point(0, 0)
        >>> q = Point(1, 1)
        >>> s = Segment(p, q*2)
        >>> l = Line(p, q)
        >>> p in q
        False
        >>> p in s
        True
        >>> q*3 in s
        False
        >>> s in l
        True

        c         3  s   |  ] }   | k Vq d  S(   N(    (   Rw   Rx   (   R   (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pys	   <genexpr>K  s    (	   R   R%   R"   Ry   Rd   R   R6   Rp   Rk   (   R<   R   R   (    (   R   s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyt   __contains__!  s    '
N(   t   __name__t
   __module__t   __doc__R0   t   propertyR=   t   staticmethodRK   RQ   RR   RT   R6   RY   RE   Rc   Rd   Rj   Rv   R   R   R   R   Ro   RS   R   R   R   R   (    (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyR%      s.   U	,1,$$?!	.	X;		9			R.   c           B  s  e  Z d  Z d d d d g Z d d  Z e d    Z d   Z d	   Z e d
    Z	 e d    Z
 e d    Z e Z e d    Z e d    Z e d    Z e d    Z e d    Z e d    Z e d    Z e d    Z e d    Z e d    Z e d    Z d   Z d   Z d! d  Z d d d! d  Z d   Z e d    Z d   Z d    Z  RS("   s  
    A regular polygon.

    Such a polygon has all internal angles equal and all sides the same length.

    Parameters
    ==========

    center : Point
    radius : number or Basic instance
        The distance from the center to a vertex
    n : int
        The number of sides

    Attributes
    ==========

    vertices
    center
    radius
    rotation
    apothem
    interior_angle
    exterior_angle
    circumcircle
    incircle
    angles

    Raises
    ======

    GeometryError
        If the `center` is not a Point, or the `radius` is not a number or Basic
        instance, or the number of sides, `n`, is less than three.

    Notes
    =====

    A RegularPolygon can be instantiated with Polygon with the kwarg n.

    Regular polygons are instantiated with a center, radius, number of sides
    and a rotation angle. Whereas the arguments of a Polygon are vertices, the
    vertices of the RegularPolygon must be obtained with the vertices method.

    See Also
    ========

    sympy.geometry.point.Point, Polygon

    Examples
    ========

    >>> from sympy.geometry import RegularPolygon, Point
    >>> r = RegularPolygon(Point(0, 0), 5, 3)
    >>> r
    RegularPolygon(Point2D(0, 0), 5, 3, 0)
    >>> r.vertices[0]
    Point2D(5, 0)

    t   _nt   _centert   _radiust   _roti    c         K  s   t  t | | | f  \ } } } t | d d | } t | t  sX t d |   n  | j r t |  | d k  r t d |   q n  t j	 |  | | | |  } | | _
 | | _ | | _ | j r | d t j | n | | _ | S(   NR'   i   s    r must be an Expr object, not %si   s   n must be a >= 3, not %s(   R   R   R   R   R   R   t	   is_NumberR   R   R0   R   R   R   t	   is_numberR   RM   R   (   R<   R;   R   R&   t   rotR4   t   obj(    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyR0     s    !	
			'c         C  s   |  j  |  j |  j |  j f S(   s-  
        Returns the center point, the radius,
        the number of sides, and the orientation angle.

        Examples
        ========

        >>> from sympy import RegularPolygon, Point
        >>> r = RegularPolygon(Point(0, 0), 5, 3)
        >>> r.args
        (Point2D(0, 0), 5, 3, 0)
        (   R   R   R   R   (   R<   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyR3     s    c         C  s   d t  |  j  S(   Ns   RegularPolygon(%s, %s, %s, %s)(   R   R3   (   R<   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyt   __str__  s    c         C  s   d t  |  j  S(   Ns   RegularPolygon(%s, %s, %s, %s)(   R   R3   (   R<   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyt   __repr__  s    c         C  s@   |  j  \ } } } } t |  | |  j d d t t |  S(   s   Returns the area.

        Examples
        ========

        >>> from sympy.geometry import RegularPolygon
        >>> square = RegularPolygon((0, 0), 1, 4)
        >>> square.area
        2
        >>> _ == square.length**2
        True
        i   i   (   R3   R   R   R   R   (   R<   R;   R   R&   R   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyR=     s    c         C  s   |  j  d t t |  j  S(   s  Returns the length of the sides.

        The half-length of the side and the apothem form two legs
        of a right triangle whose hypotenuse is the radius of the
        regular polygon.

        Examples
        ========

        >>> from sympy.geometry import RegularPolygon
        >>> from sympy import sqrt
        >>> s = square_in_unit_circle = RegularPolygon((0, 0), 1, 4)
        >>> s.length
        sqrt(2)
        >>> sqrt((_/2)**2 + s.apothem**2) == s.radius
        True

        i   (   t   radiusR   R   R   (   R<   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyR     s    c         C  s   |  j  S(   s  The center of the RegularPolygon

        This is also the center of the circumscribing circle.

        Returns
        =======

        center : Point

        See Also
        ========

        sympy.geometry.point.Point, sympy.geometry.ellipse.Ellipse.center

        Examples
        ========

        >>> from sympy.geometry import RegularPolygon, Point
        >>> rp = RegularPolygon(Point(0, 0), 5, 4)
        >>> rp.center
        Point2D(0, 0)
        (   R   (   R<   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyt   center  s    c         C  s   |  j  S(   s   
        Alias for center.

        Examples
        ========

        >>> from sympy.geometry import RegularPolygon, Point
        >>> rp = RegularPolygon(Point(0, 0), 5, 4)
        >>> rp.circumcenter
        Point2D(0, 0)
        (   R   (   R<   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyt   circumcenter   s    c         C  s   |  j  S(   s(  Radius of the RegularPolygon

        This is also the radius of the circumscribing circle.

        Returns
        =======

        radius : number or instance of Basic

        See Also
        ========

        sympy.geometry.line.Segment.length, sympy.geometry.ellipse.Circle.radius

        Examples
        ========

        >>> from sympy import Symbol
        >>> from sympy.geometry import RegularPolygon, Point
        >>> radius = Symbol('r')
        >>> rp = RegularPolygon(Point(0, 0), radius, 4)
        >>> rp.radius
        r

        (   R   (   R<   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyR     s    c         C  s   |  j  S(   s(  
        Alias for radius.

        Examples
        ========

        >>> from sympy import Symbol
        >>> from sympy.geometry import RegularPolygon, Point
        >>> radius = Symbol('r')
        >>> rp = RegularPolygon(Point(0, 0), radius, 4)
        >>> rp.circumradius
        r
        (   R   (   R<   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyt   circumradius,  s    c         C  s   |  j  S(   s<  CCW angle by which the RegularPolygon is rotated

        Returns
        =======

        rotation : number or instance of Basic

        Examples
        ========

        >>> from sympy import pi
        >>> from sympy.abc import a
        >>> from sympy.geometry import RegularPolygon, Point
        >>> RegularPolygon(Point(0, 0), 3, 4, pi/4).rotation
        pi/4

        Numerical rotation angles are made canonical:

        >>> RegularPolygon(Point(0, 0), 3, 4, a).rotation
        a
        >>> RegularPolygon(Point(0, 0), 3, 4, pi).rotation
        0

        (   R   (   R<   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyt   rotation=  s    c         C  s   |  j  t t j |  j  S(   sA  The inradius of the RegularPolygon.

        The apothem/inradius is the radius of the inscribed circle.

        Returns
        =======

        apothem : number or instance of Basic

        See Also
        ========

        sympy.geometry.line.Segment.length, sympy.geometry.ellipse.Circle.radius

        Examples
        ========

        >>> from sympy import Symbol
        >>> from sympy.geometry import RegularPolygon, Point
        >>> radius = Symbol('r')
        >>> rp = RegularPolygon(Point(0, 0), radius, 4)
        >>> rp.apothem
        sqrt(2)*r/2

        (   R   R   R   RM   R   (   R<   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyt   apothemY  s    c         C  s   |  j  S(   s/  
        Alias for apothem.

        Examples
        ========

        >>> from sympy import Symbol
        >>> from sympy.geometry import RegularPolygon, Point
        >>> radius = Symbol('r')
        >>> rp = RegularPolygon(Point(0, 0), radius, 4)
        >>> rp.inradius
        sqrt(2)*r/2
        (   R   (   R<   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyt   inradiusv  s    c         C  s   |  j  d t j |  j  S(   s  Measure of the interior angles.

        Returns
        =======

        interior_angle : number

        See Also
        ========

        sympy.geometry.line.LinearEntity.angle_between

        Examples
        ========

        >>> from sympy.geometry import RegularPolygon, Point
        >>> rp = RegularPolygon(Point(0, 0), 4, 8)
        >>> rp.interior_angle
        3*pi/4

        i   (   R   R   RM   (   R<   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyt   interior_angle  s    c         C  s   d t  j |  j S(   s  Measure of the exterior angles.

        Returns
        =======

        exterior_angle : number

        See Also
        ========

        sympy.geometry.line.LinearEntity.angle_between

        Examples
        ========

        >>> from sympy.geometry import RegularPolygon, Point
        >>> rp = RegularPolygon(Point(0, 0), 4, 8)
        >>> rp.exterior_angle
        pi/4

        i   (   R   RM   R   (   R<   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyt   exterior_angle  s    c         C  s   t  |  j |  j  S(   s  The circumcircle of the RegularPolygon.

        Returns
        =======

        circumcircle : Circle

        See Also
        ========

        circumcenter, sympy.geometry.ellipse.Circle

        Examples
        ========

        >>> from sympy.geometry import RegularPolygon, Point
        >>> rp = RegularPolygon(Point(0, 0), 4, 8)
        >>> rp.circumcircle
        Circle(Point2D(0, 0), 4)

        (   R    R   R   (   R<   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyt   circumcircle  s    c         C  s   t  |  j |  j  S(   s  The incircle of the RegularPolygon.

        Returns
        =======

        incircle : Circle

        See Also
        ========

        inradius, sympy.geometry.ellipse.Circle

        Examples
        ========

        >>> from sympy.geometry import RegularPolygon, Point
        >>> rp = RegularPolygon(Point(0, 0), 4, 7)
        >>> rp.incircle
        Circle(Point2D(0, 0), 4*cos(pi/7))

        (   R    R   R   (   R<   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyt   incircle  s    c         C  s1   i  } |  j  } x |  j D] } | | | <q W| S(   s  
        Returns a dictionary with keys, the vertices of the Polygon,
        and values, the interior angle at each vertex.

        Examples
        ========

        >>> from sympy import RegularPolygon, Point
        >>> r = RegularPolygon(Point(0, 0), 5, 3)
        >>> r.angles
        {Point2D(-5/2, -5*sqrt(3)/2): pi/3,
         Point2D(-5/2, 5*sqrt(3)/2): pi/3,
         Point2D(5, 0): pi/3}
        (   R  R6   (   R<   RO   RP   RX   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyRQ     s
    	c         C  sU   |  j  } t | |  j } | |  j k r. t S| |  j k  rA t St j |  |  Sd S(   sN  
        Return True if p is enclosed by (is inside of) self.

        Notes
        =====

        Being on the border of self is considered False.

        The general Polygon.encloses_point method is called only if
        a point is not within or beyond the incircle or circumcircle,
        respectively.

        Parameters
        ==========

        p : Point

        Returns
        =======

        encloses_point : True, False or None

        See Also
        ========

        sympy.geometry.ellipse.Ellipse.encloses_point

        Examples
        ========

        >>> from sympy import RegularPolygon, S, Point, Symbol
        >>> p = RegularPolygon((0, 0), 3, 4)
        >>> p.encloses_point(Point(0, 0))
        True
        >>> r, R = p.inradius, p.circumradius
        >>> p.encloses_point(Point((r + R)/2, 0))
        True
        >>> p.encloses_point(Point(R/2, R/2 + (R - r)/10))
        False
        >>> t = Symbol('t', real=True)
        >>> p.encloses_point(p.arbitrary_point().subs(t, S.Half))
        False
        >>> p.encloses_point(Point(5, 5))
        False

        N(	   R   R"   R   R   Rk   R  Rp   R%   R   (   R<   R8   R;   t   d(    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyR     s    0	c         C  s   |  j  | 7_  d S(   s  Increment *in place* the virtual Polygon's rotation by ccw angle.

        See also: rotate method which moves the center.

        >>> from sympy import Polygon, Point, pi
        >>> r = Polygon(Point(0,0), 1, n=3)
        >>> r.vertices[0]
        Point2D(1, 0)
        >>> r.spin(pi/6)
        >>> r.vertices[0]
        Point2D(sqrt(3)/2, 1/2)

        See Also
        ========

        rotation
        rotate : Creates a copy of the RegularPolygon rotated about a Point

        N(   R   (   R<   t   angle(    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyt   spin;  s    c         C  s7   t  |   |  j   } | j | 7_ t j | | |  S(   s  Override GeometryEntity.rotate to first rotate the RegularPolygon
        about its center.

        >>> from sympy import Point, RegularPolygon, Polygon, pi
        >>> t = RegularPolygon(Point(1, 0), 1, 3)
        >>> t.vertices[0] # vertex on x-axis
        Point2D(2, 0)
        >>> t.rotate(pi/2).vertices[0] # vertex on y axis now
        Point2D(0, 2)

        See Also
        ========

        rotation
        spin : Rotates a RegularPolygon in place

        (   t   typeR3   R   R   t   rotate(   R<   R  R   R   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyR
  Q  s    i   c         C  s   | rA t  | d d } |  j | j   j | |  j | j   S| | k rf t |  j   j | |  S|  j \ } } } } | | 9} |  j | | | |  S(   s  Override GeometryEntity.scale since it is the radius that must be
        scaled (if x == y) or else a new Polygon must be returned.

        >>> from sympy import RegularPolygon

        Symmetric scaling returns a RegularPolygon:

        >>> RegularPolygon((0, 0), 1, 4).scale(2, 2)
        RegularPolygon(Point2D(0, 0), 2, 4, 0)

        Asymmetric scaling returns a kite as a Polygon:

        >>> RegularPolygon((0, 0), 1, 4).scale(2, 1)
        Polygon(Point2D(2, 0), Point2D(0, 1), Point2D(-2, 0), Point2D(0, -1))

        R'   i   (   R   t	   translateR3   t   scaleR%   R6   t   func(   R<   RB   RC   R   R;   R   R&   R   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyR  h  s    )
c         C  s   |  j  \ } } } } |  j d } | | } | j |  } | j |  }	 |	 | }
 t d |
  } t d |  } | j |  } | | 7} |  j | | | |  S(   s5  Override GeometryEntity.reflect since this is not made of only
        points.

        Examples
        ========

        >>> from sympy import RegularPolygon, Line

        >>> RegularPolygon((0, 0), 1, 4).reflect(Line((0, 1), slope=-2))
        RegularPolygon(Point2D(4/5, 2/5), -1, 4, atan(4/3))

        i    (   i    i    (   i    i    (   R3   R6   t   reflectR#   t   closing_angleR  (   R<   t   lineR;   R   R&   R   RX   R  t   cct   vvt   ddt   l1t   l2RP   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyR    s    


c      	   C  s   |  j  } t |  j  } |  j } d t j |  j } g  t |  j  D]G } t | j	 | t
 | | |  | j | t | | |   ^ qE S(   s  The vertices of the RegularPolygon.

        Returns
        =======

        vertices : list
            Each vertex is a Point.

        See Also
        ========

        sympy.geometry.point.Point

        Examples
        ========

        >>> from sympy.geometry import RegularPolygon, Point
        >>> rp = RegularPolygon(Point(0, 0), 5, 4)
        >>> rp.vertices
        [Point2D(5, 0), Point2D(0, 5), Point2D(-5, 0), Point2D(0, -5)]

        i   (   R   t   absR   R   R   RM   R   R	   R   RB   R   RC   R   (   R<   R;   R   R   RX   R   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyR6     s    		c         C  sB   t  | t  s t St  | t  s2 t j | |   S|  j | j k S(   N(   R   R%   Rk   R.   t   __eq__R3   (   R<   R   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyR    s
    c         C  s   t  t |   j   S(   N(   t   superR.   t   __hash__(   R<   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyR    s    N(!   R   R   R   t	   __slots__R0   R   R3   R   R   R=   R   R   RY   R   R   R   R   R   R  R  R  R  R  RQ   R   R  RE   R
  R  R  R6   R  R  (    (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyR.   V  s:   <			:		 	R1   c           B  s  e  Z d  Z d   Z e d    Z d   Z d   Z d   Z d   Z	 d   Z
 e d    Z e d	    Z e d
    Z e d    Z e d    Z d   Z e d    Z e d    Z e d    Z e d    Z e d    Z e d    Z e d    Z e d    Z RS(   s  
    A polygon with three vertices and three sides.

    Parameters
    ==========

    points : sequence of Points
    keyword: asa, sas, or sss to specify sides/angles of the triangle

    Attributes
    ==========

    vertices
    altitudes
    orthocenter
    circumcenter
    circumradius
    circumcircle
    inradius
    incircle
    exradii
    medians
    medial
    nine_point_circle

    Raises
    ======

    GeometryError
        If the number of vertices is not equal to three, or one of the vertices
        is not a Point, or a valid keyword is not given.

    See Also
    ========

    sympy.geometry.point.Point, Polygon

    Examples
    ========

    >>> from sympy.geometry import Triangle, Point
    >>> Triangle(Point(0, 0), Point(4, 0), Point(4, 3))
    Triangle(Point2D(0, 0), Point2D(4, 0), Point2D(4, 3))

    Keywords sss, sas, or asa can be used to give the desired
    side lengths (in order) and interior angles (in degrees) that
    define the triangle:

    >>> Triangle(sss=(3, 4, 5))
    Triangle(Point2D(0, 0), Point2D(3, 0), Point2D(3, 4))
    >>> Triangle(asa=(30, 1, 30))
    Triangle(Point2D(0, 0), Point2D(1, 0), Point2D(1/2, sqrt(3)/6))
    >>> Triangle(sas=(1, 45, 2))
    Triangle(Point2D(0, 0), Point2D(2, 0), Point2D(sqrt(2)/2, sqrt(2)/2))

    c         O  s  t  |  d k r d | k rE t g  | d D] } t |  ^ q,   Sd | k rx t g  | d D] } t |  ^ q_   Sd | k r t g  | d D] } t |  ^ q   Sd } t |   n  g  | D] } t | d d | ^ q } g  } x7 | D]/ } | r| | d k rq n  | j |  q Wt  |  d	 k r[| d | d
 k r[| j   n  d } x | t  |  d k  rt  |  d k rt	 | | | | d	 | | d g d t
 \ } }	 }
 t j | |	 |
  r| | | <d  | | d	 <| j | d	  n  | d	 7} qdWt t d   |   } t  |  d k rPt j |  | |  St  |  d k rot | |   St | |   Sd  S(   Ni   t   ssst   asat   sass;   Triangle instantiates with three points or a valid keyword.R'   i   ii   i    it   keyc         S  s
   |  d  k	 S(   N(   RE   (   RB   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyt   <lambda>$  t    (   R+   t   _sssR   t   _asat   _sasR   R   R,   R)   t   sortedR   R/   RE   R*   t   filterR   R0   R"   (   R2   R3   R4   R5   t   msgR6   R7   R8   R9   R:   R;   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyR0     s@    '''(&+5
c         C  s   |  j  S(   s  The triangle's vertices

        Returns
        =======

        vertices : tuple
            Each element in the tuple is a Point

        See Also
        ========

        sympy.geometry.point.Point

        Examples
        ========

        >>> from sympy.geometry import Triangle, Point
        >>> t = Triangle(Point(0, 0), Point(4, 0), Point(4, 3))
        >>> t.vertices
        (Point2D(0, 0), Point2D(4, 0), Point2D(4, 3))

        (   R3   (   R<   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyR6   -  s    c         C  s   t  | t  s t Sg  |  j D] } | j ^ q \ } } } g  | j D] } | j ^ qE } d   } | | | | |  p | | | | |  p | | | | |  p | | | | |  p | | | | |  p | | | | |  S(   s  Is another triangle similar to this one.

        Two triangles are similar if one can be uniformly scaled to the other.

        Parameters
        ==========

        other: Triangle

        Returns
        =======

        is_similar : boolean

        See Also
        ========

        sympy.geometry.entity.GeometryEntity.is_similar

        Examples
        ========

        >>> from sympy.geometry import Triangle, Point
        >>> t1 = Triangle(Point(0, 0), Point(4, 0), Point(4, 3))
        >>> t2 = Triangle(Point(0, 0), Point(-4, 0), Point(-4, -3))
        >>> t1.is_similar(t2)
        True

        >>> t2 = Triangle(Point(0, 0), Point(-4, 0), Point(-4, -4))
        >>> t1.is_similar(t2)
        False

        c   	      S  sR   t  |  |  } t  | |  } t  | |  } t | | k  oQ t | | k  S(   N(   R   t   bool(	   t   u1t   u2t   u3t   v1t   v2t   v3R   R   t   e3(    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyt   _are_similaro  s    (   R   R%   Rk   Rd   R   (   t   t1t   t2R   t   s1_1t   s1_2t   s1_3t   s2R/  (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyt
   is_similarG  s    "(	c         C  s   t  d   |  j D  S(   sk  Are all the sides the same length?

        Returns
        =======

        is_equilateral : boolean

        See Also
        ========

        sympy.geometry.entity.GeometryEntity.is_similar, RegularPolygon
        is_isosceles, is_right, is_scalene

        Examples
        ========

        >>> from sympy.geometry import Triangle, Point
        >>> t1 = Triangle(Point(0, 0), Point(4, 0), Point(4, 3))
        >>> t1.is_equilateral()
        False

        >>> from sympy import sqrt
        >>> t2 = Triangle(Point(0, 0), Point(10, 0), Point(5, 5*sqrt(3)))
        >>> t2.is_equilateral()
        True

        c         s  s   |  ] } | j  Vq d  S(   N(   R   (   Rw   Rx   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pys	   <genexpr>  s    (   R   Rd   (   R<   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyt   is_equilateral}  s    c         C  s   t  d   |  j D  S(   s  Are two or more of the sides the same length?

        Returns
        =======

        is_isosceles : boolean

        See Also
        ========

        is_equilateral, is_right, is_scalene

        Examples
        ========

        >>> from sympy.geometry import Triangle, Point
        >>> t1 = Triangle(Point(0, 0), Point(4, 0), Point(2, 4))
        >>> t1.is_isosceles()
        True

        c         s  s   |  ] } | j  Vq d  S(   N(   R   (   Rw   Rx   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pys	   <genexpr>  s    (   R   Rd   (   R<   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyt   is_isosceles  s    c         C  s   t  d   |  j D  S(   s  Are all the sides of the triangle of different lengths?

        Returns
        =======

        is_scalene : boolean

        See Also
        ========

        is_equilateral, is_isosceles, is_right

        Examples
        ========

        >>> from sympy.geometry import Triangle, Point
        >>> t1 = Triangle(Point(0, 0), Point(4, 0), Point(1, 4))
        >>> t1.is_scalene()
        True

        c         s  s   |  ] } | j  Vq d  S(   N(   R   (   Rw   Rx   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pys	   <genexpr>  s    (   R   Rd   (   R<   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyt
   is_scalene  s    c         C  sU   |  j  } t j | d | d  pT t j | d | d  pT t j | d | d  S(   s  Is the triangle right-angled.

        Returns
        =======

        is_right : boolean

        See Also
        ========

        sympy.geometry.line.LinearEntity.is_perpendicular
        is_equilateral, is_isosceles, is_scalene

        Examples
        ========

        >>> from sympy.geometry import Triangle, Point
        >>> t1 = Triangle(Point(0, 0), Point(4, 0), Point(4, 3))
        >>> t1.is_right()
        True

        i    i   i   (   Rd   R"   t   is_perpendicular(   R<   Rx   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyt   is_right  s    	c         C  sj   |  j  } |  j } i | d j | d  | d 6| d j | d  | d 6| d j | d  | d 6S(   s  The altitudes of the triangle.

        An altitude of a triangle is a segment through a vertex,
        perpendicular to the opposite side, with length being the
        height of the vertex measured from the line containing the side.

        Returns
        =======

        altitudes : dict
            The dictionary consists of keys which are vertices and values
            which are Segments.

        See Also
        ========

        sympy.geometry.point.Point, sympy.geometry.line.Segment.length

        Examples
        ========

        >>> from sympy.geometry import Point, Triangle
        >>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, 1)
        >>> t = Triangle(p1, p2, p3)
        >>> t.altitudes[p1]
        Segment2D(Point2D(0, 0), Point2D(1/2, 1/2))

        i   i    i   (   Rd   R6   t   perpendicular_segment(   R<   Rx   RX   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyt	   altitudes  s
    		c         C  s?   |  j  } |  j } t | | d  j t | | d   d S(   s"  The orthocenter of the triangle.

        The orthocenter is the intersection of the altitudes of a triangle.
        It may lie inside, outside or on the triangle.

        Returns
        =======

        orthocenter : Point

        See Also
        ========

        sympy.geometry.point.Point

        Examples
        ========

        >>> from sympy.geometry import Point, Triangle
        >>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, 1)
        >>> t = Triangle(p1, p2, p3)
        >>> t.orthocenter
        Point2D(0, 0)

        i    i   (   R=  R6   R!   Ro   (   R<   R5   RX   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyt   orthocenter  s    		c         C  sg   g  |  j  D] } | j   ^ q
 \ } } } | j |  sV t | | | j |   n  | j |  d S(   s  The circumcenter of the triangle

        The circumcenter is the center of the circumcircle.

        Returns
        =======

        circumcenter : Point

        See Also
        ========

        sympy.geometry.point.Point

        Examples
        ========

        >>> from sympy.geometry import Point, Triangle
        >>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, 1)
        >>> t = Triangle(p1, p2, p3)
        >>> t.circumcenter
        Point2D(1/2, 1/2)
        i    (   Rd   t   perpendicular_bisectorRo   t   print(   R<   RB   R5   R:   R;   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyR   *  s    +c         C  s   t  j |  j |  j d  S(   s  The radius of the circumcircle of the triangle.

        Returns
        =======

        circumradius : number of Basic instance

        See Also
        ========

        sympy.geometry.ellipse.Circle.radius

        Examples
        ========

        >>> from sympy import Symbol
        >>> from sympy.geometry import Point, Triangle
        >>> a = Symbol('a')
        >>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, a)
        >>> t = Triangle(p1, p2, p3)
        >>> t.circumradius
        sqrt(a**2/4 + 1/4)
        i    (   R   RS   R   R6   (   R<   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyR   H  s    c         C  s   t  |  j |  j  S(   s  The circle which passes through the three vertices of the triangle.

        Returns
        =======

        circumcircle : Circle

        See Also
        ========

        sympy.geometry.ellipse.Circle

        Examples
        ========

        >>> from sympy.geometry import Point, Triangle
        >>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, 1)
        >>> t = Triangle(p1, p2, p3)
        >>> t.circumcircle
        Circle(Point2D(1/2, 1/2), sqrt(2)/2)

        (   R    R   R   (   R<   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyR  c  s    c         C  s   |  j  } |  j } |  j } t | d t | d |  j | d  d  } t | d t | d |  j | d  d  } t | d t | d |  j | d  d  } i | | d 6| | d 6| | d 6S(   s   The angle bisectors of the triangle.

        An angle bisector of a triangle is a straight line through a vertex
        which cuts the corresponding angle in half.

        Returns
        =======

        bisectors : dict
            Each key is a vertex (Point) and each value is the corresponding
            bisector (Segment).

        See Also
        ========

        sympy.geometry.point.Point, sympy.geometry.line.Segment

        Examples
        ========

        >>> from sympy.geometry import Point, Triangle, Segment
        >>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, 1)
        >>> t = Triangle(p1, p2, p3)
        >>> from sympy import sqrt
        >>> t.bisectors()[p2] == Segment(Point(1, 0), Point(0, sqrt(2) - 1))
        True

        i    i   i   (   Rd   R6   t   incenterR"   R!   Ro   (   R<   Rx   RX   R;   R  R  t   l3(    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyt	   bisectors}  s    			111c   	      C  s   |  j  } t g  d d d g D] } | | j ^ q  } t |  } |  j } t | j t g  | D] } | j ^ q`   |  } t | j t g  | D] } | j ^ q   |  } t	 | |  S(   s  The center of the incircle.

        The incircle is the circle which lies inside the triangle and touches
        all three sides.

        Returns
        =======

        incenter : Point

        See Also
        ========

        incircle, sympy.geometry.point.Point

        Examples
        ========

        >>> from sympy.geometry import Point, Triangle
        >>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, 1)
        >>> t = Triangle(p1, p2, p3)
        >>> t.incenter
        Point2D(1 - sqrt(2)/2, 1 - sqrt(2)/2)

        i   i   i    (
   Rd   R   R   t   sumR6   R   t   dotRB   RC   R   (	   R<   Rx   R9   t   lR8   RX   t   viRB   RC   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyRA    s    	/	55c         C  s   t  d |  j |  j  S(   s  The radius of the incircle.

        Returns
        =======

        inradius : number of Basic instance

        See Also
        ========

        incircle, sympy.geometry.ellipse.Circle.radius

        Examples
        ========

        >>> from sympy.geometry import Point, Triangle
        >>> p1, p2, p3 = Point(0, 0), Point(4, 0), Point(0, 3)
        >>> t = Triangle(p1, p2, p3)
        >>> t.inradius
        1

        i   (   R   R=   RT   (   R<   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyR    s    c         C  s   t  |  j |  j  S(   s*  The incircle of the triangle.

        The incircle is the circle which lies inside the triangle and touches
        all three sides.

        Returns
        =======

        incircle : Circle

        See Also
        ========

        sympy.geometry.ellipse.Circle

        Examples
        ========

        >>> from sympy.geometry import Point, Triangle
        >>> p1, p2, p3 = Point(0, 0), Point(2, 0), Point(0, 2)
        >>> t = Triangle(p1, p2, p3)
        >>> t.incircle
        Circle(Point2D(2 - sqrt(2), 2 - sqrt(2)), 2 - sqrt(2))

        (   R    RA  R  (   R<   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyR    s    c         C  s   |  j  } | d j } | d j } | d j } | | | d } |  j } i t | | |  |  j  d 6t | | |  |  j  d 6t | | |  |  j  d 6} | S(   s  The radius of excircles of a triangle.

        An excircle of the triangle is a circle lying outside the triangle,
        tangent to one of its sides and tangent to the extensions of the
        other two.

        Returns
        =======

        exradii : dict

        See Also
        ========

        sympy.geometry.polygon.Triangle.inradius

        Examples
        ========

        The exradius touches the side of the triangle to which it is keyed, e.g.
        the exradius touching side 2 is:

        >>> from sympy.geometry import Point, Triangle, Segment2D, Point2D
        >>> p1, p2, p3 = Point(0, 0), Point(6, 0), Point(0, 2)
        >>> t = Triangle(p1, p2, p3)
        >>> t.exradii[t.sides[2]]
        -2 + sqrt(10)

        References
        ==========

        [1] http://mathworld.wolfram.com/Exradius.html
        [2] http://mathworld.wolfram.com/Excircles.html

        i    i   i   (   Rd   R   R=   R   (   R<   R   R5   R:   R;   Rx   R=   t   exradii(    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyRH    s    &		c         C  ss   |  j  } |  j } i t | d | d j  | d 6t | d | d j  | d 6t | d | d j  | d 6S(   s  The medians of the triangle.

        A median of a triangle is a straight line through a vertex and the
        midpoint of the opposite side, and divides the triangle into two
        equal areas.

        Returns
        =======

        medians : dict
            Each key is a vertex (Point) and each value is the median (Segment)
            at that point.

        See Also
        ========

        sympy.geometry.point.Point.midpoint, sympy.geometry.line.Segment.midpoint

        Examples
        ========

        >>> from sympy.geometry import Point, Triangle
        >>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, 1)
        >>> t = Triangle(p1, p2, p3)
        >>> t.medians[p1]
        Segment2D(Point2D(0, 0), Point2D(1/2, 1/2))

        i    i   i   (   Rd   R6   R"   t   midpoint(   R<   Rx   RX   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyt   medians.	  s
    		"c         C  s.   |  j  } t | d j | d j | d j  S(   s"  The medial triangle of the triangle.

        The triangle which is formed from the midpoints of the three sides.

        Returns
        =======

        medial : Triangle

        See Also
        ========

        sympy.geometry.line.Segment.midpoint

        Examples
        ========

        >>> from sympy.geometry import Point, Triangle
        >>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, 1)
        >>> t = Triangle(p1, p2, p3)
        >>> t.medial
        Triangle(Point2D(1/2, 0), Point2D(1/2, 1/2), Point2D(0, 1/2))

        i    i   i   (   Rd   R1   RI  (   R<   Rx   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyt   medialR	  s    	c         C  s   t  |  j j   S(   s  The nine-point circle of the triangle.

        Nine-point circle is the circumcircle of the medial triangle, which
        passes through the feet of altitudes and the middle points of segments
        connecting the vertices and the orthocenter.

        Returns
        =======

        nine_point_circle : Circle

        See also
        ========

        sympy.geometry.line.Segment.midpoint
        sympy.geometry.polygon.Triangle.medial
        sympy.geometry.polygon.Triangle.orthocenter

        Examples
        ========

        >>> from sympy.geometry import Point, Triangle
        >>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, 1)
        >>> t = Triangle(p1, p2, p3)
        >>> t.nine_point_circle
        Circle(Point2D(1/4, 1/4), sqrt(2)/4)

        (   R    RK  R6   (   R<   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyt   nine_point_circleo	  s    c         C  s&   |  j    r |  j St |  j |  j  S(   s  The Euler line of the triangle.

        The line which passes through circumcenter, centroid and orthocenter.

        Returns
        =======

        eulerline : Line (or Point for equilateral triangles in which case all
                    centers coincide)

        Examples
        ========

        >>> from sympy.geometry import Point, Triangle
        >>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, 1)
        >>> t = Triangle(p1, p2, p3)
        >>> t.eulerline
        Line2D(Point2D(0, 0), Point2D(1/2, 1/2))

        (   R7  R>  R!   R   (   R<   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyt	   eulerline	  s    (   R   R   R   R0   R   R6   R6  R7  R8  R9  R;  R=  R>  R   R   R  RC  RA  R  R  RH  RJ  RK  RL  RM  (    (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyR1     s,   8	*	6				$	%#2$ c         C  s   |  t  d S(   sA   Return the radian value for the given degrees (pi = 180 degrees).i   (   R   (   R  (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyt   rad	  s    c         C  s   |  t  d S(   sA   Return the degree value for the given radians (pi = 180 degrees).i   (   R   (   R   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyt   deg	  s    c         C  s   t  t |    } | S(   N(   R   RN  (   R  t   rv(    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyt   _slope	  s    c         C  sW   t  d d t |   j t  | d f d t d |   d } t d | d f |  S(   s8   Return triangle having side with length l on the x-axis.i    t   slopei   (   i    i    (   i    i    (   R!   RQ  Ro   R1   (   t   d1RF  t   d2t   xy(    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyR"  	  s    )c         C  s|   t  d |  } t  |  d f |  } g  | j |  D] } | j j r4 | ^ q4 } | s\ d S| d } t d |  d f |  S(   s7   Return triangle having side of length l1 on the x-axis.i    (   i    i    N(   i    i    (   R    Ro   RC   t   is_nonnegativeRE   R1   (   R  R  RB  t   c1t   c2R5   t   interR   (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyR!  	  s    .
c         C  s]   t  d d  } t  | d  } t  t t |   |  t t |   |   } t | | |  S(   s9   Return triangle having side with length l2 on the x-axis.i    (   R   R   RN  R   R1   (   R  R  R  Rm   Rn   t   p3(    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyR#  	  s    /(B   t
   __future__R    R   t
   sympy.coreR   R   R   R   R   R   t   sympy.core.compatibilityR   R	   R
   t   sympy.core.symbolR   R   t$   sympy.functions.elementary.complexesR   t$   sympy.functions.elementary.piecewiseR   t(   sympy.functions.elementary.trigonometricR   R   R   t   sympy.geometry.exceptionsR   t   sympy.logicR   t   sympy.matricesR   t   sympy.simplifyR   t   sympy.utilitiesR   t   sympy.utilities.iterablesR   R   R   R   R   t   sympy.utilities.miscR   R   R   R   RZ   R   t   ellipseR    R  R!   R"   R#   t   sympyR$   R   R%   R.   R1   RN  RO  RQ  R"  R!  R#  (    (    (    s5   lib/python2.7/site-packages/sympy/geometry/polygon.pyt   <module>   sH   .(    ?  u  					