
UW=                 @   s  d  Z  d d l m Z m Z m Z m Z d d l m Z d d l Z	 d d l
 m Z d d l m Z d d l Z Gd d   d e  Z d	 d
   Z d d   Z d d   Z d d   Z d d d d d  Z Gd d   d e  Z d d d  Z d d d d d  Z d d d d  Z d d    Z d! d"   Z d# d$ d%  Z d& d'   Z d( d)   Z d d* d d+ d,  Z  d- d.   Z! d/ d0   Z" d S)1zO
A module providing some utility functions regarding bezier path manipulation.
    )absolute_importdivisionprint_functionunicode_literals)sixN)Path)xorc               @   s   e  Z d  Z d S)NonIntersectingPathExceptionN)__name__
__module____qualname__ r   r   L/s/python-3.5.2/amd64_ubu14/lib/python3.5/site-packages/matplotlib/bezier.pyr	      s   r	   c                s   | |  | | } | | | | }	 | | }
 } | | } } |
 | | |     d k rj t  d   | | } } | |
 } }   f d d   | | | | g D \ } } } } | | | |	 } | | | |	 } | | f S)z return a intersecting point between a line through (cx1, cy1)
    and having angle t1 and a line through (cx2, cy2) and angle t2.
    g        zGiven lines do not intersectc                s   g  |  ] } |    q Sr   r   ).0k)ad_bcr   r   
<listcomp>.   s   	 z$get_intersection.<locals>.<listcomp>)
ValueError)Zcx1Zcy1cos_t1sin_t1Zcx2Zcy2cos_t2sin_t2Z	line1_rhsZ	line2_rhsabcdZa_Zb_c_Zd_xyr   )r   r   get_intersection   s    	1r   c             C   s   | d k r |  | |  | f S| | } } | | } } | | |  | | | }	 }
 | | |  | | | } } |	 |
 | | f S)z
    For a line passing through (*cx*, *cy*) and having a angle *t*, return
    locations of the two points located along its perpendicular line at the
    distance of *length*.
    g        r   )cxcycos_tsin_tlengthr   r   r   r   x1y1x2y2r   r   r   get_normal_points6   s    r)   c             C   s.   |  d  d  d | |  d d   | } | S)N   r   )betatZ	next_betar   r   r   _de_casteljau1O   s    *r.   c             C   s   t  j |   }  |  g } x3 t |  |  }  | j |   t |   d k r Pq Wd d   | D } d d   t |  D } | | f S)zsplit a bezier segment defined by its controlpoints *beta*
    into two separate segment divided at *t* and return their control points.

    r*   c             S   s   g  |  ] } | d   q S)r   r   )r   r,   r   r   r   r   `   s   	 z&split_de_casteljau.<locals>.<listcomp>c             S   s   g  |  ] } | d  q S)r*   r+   r   )r   r,   r   r   r   r   a   s   	 )npasarrayr.   appendlenreversed)r,   r-   Z	beta_listZ	left_betaZ
right_betar   r   r   split_de_casteljauT   s    	r4   g        g      ?g{Gz?c             C   s   |  |  } |  |  } | |  } | |  } | | k rT | | k rT t  d   x | d | d d | d | d d | d k  r | | f Sd | | }	 |  |	  }
 | |
  } t | |  r |	 } |
 } | } qW |	 } |
 } | } qW Wd S)a$   Find a parameter t0 and t1 of the given bezier path which
    bounds the intersecting points with a provided closed
    path(*inside_closedpath*). Search starts from *t0* and *t1* and it
    uses a simple bisecting algorithm therefore one of the end point
    must be inside the path while the orther doesn't. The search stop
    when |t0-t1| gets smaller than the given tolerence.
    value for

    - bezier_point_at_t : a function which returns x, y coordinates at *t*

    - inside_closedpath : return True if the point is insed the path

    z4the segment does not seem to intersect with the pathr      r*   g      ?N)r	   r   )bezier_point_at_tinside_closedpatht0t1	tolerencestartendZstart_insideZ
end_insideZmiddle_tZmiddleZmiddle_insider   r   r   *find_bezier_t_intersecting_with_closedpathg   s,    	!
	r=   c            
   @   s|   e  Z d  Z d Z d e j d d g  d e j d d d g  d e j d d d d g  i Z d d	   Z d
 d   Z d S)BezierSegmentz:
    A simple class of a 2-dimensional bezier segment
    r*   g      ?r5   g       @   g      @c             C   s   t  |  } t j |  |  _ t j | d } t j |  } | d d  d f } | d d  d f } | | |  _ | | |  _ d S)z
        *control_points* : location of contol points. It needs have a
         shpae of n * 2, where n is the order of the bezier line. 1<=
         n <= 3 is supported.
        r*   Nr   )	r2   r/   arange_ordersr>   _binom_coeffr0   _px_py)selfZcontrol_points_oZ_coeffZ_control_pointsZxxyyr   r   r   __init__   s    zBezierSegment.__init__c             C   su   t  j d | |  j  d d d  } t  j | |  j  } | | } t | |  j  } t | |  j  } | | f S)zevaluate a point at tg      ?Nr*   r+   )r/   powerrA   sumrC   rD   )rE   r-   Zone_minus_t_powersZt_powerstt_xZ_yr   r   r   
point_at_t   s    &
zBezierSegment.point_at_tN)	r
   r   r   __doc__r/   arrayrB   rH   rM   r   r   r   r   r>      s   !r>   c       	      C   sW   t  |   } | j } t | | d | \ } } t |  | | d  \ } } | | f S)z
    bezier : control points of the bezier segment
    inside_closedpath : a function which returns true if the point is inside
                        the path
    r:   g       @)r>   rM   r=   r4   )	Zbezierr7   r:   Zbzr6   r8   r9   _left_rightr   r   r   )split_bezier_intersecting_with_closedpath   s    
	rR   c          	      sJ   | \        f d d   } t  | |  d | d | d | d S)z
    Find a radius r (centered at *xy*) between *rmin* and *rmax* at
    which it intersect with the path.

    inside_closedpath : function
    cx, cy : center
    cos_t, sin_t : cosine and sine for the angle
    rmin, rmax :
    c                s     |    |   f S)Nr   )r)r"   r    r!   r#   r   r   _f   s    z,find_r_to_boundary_of_closedpath.<locals>._fr8   r9   r:   N)r=   )r7   xyr"   r#   rminrmaxr:   rT   r   )r"   r    r!   r#   r    find_r_to_boundary_of_closedpath   s
    rX   Fc             C   s  |  j    } t |  \ } } | | d	 d   } d } | }	 t j }
 d } d } xm | D]e \ } } | } | t |  d 7} | | d
 d   | k r |
 |	 d d  | g  } P| }	 q\ W| d k r t d   t t | d d d  | d d d    } t | | |  \ } } t |  d k rWt	 j
 g } t	 j t	 j
 g } n t |  d k rt	 j t	 j g } t	 j t	 j t	 j g } nT t |  d k rt	 j t	 j t	 j g } t	 j t	 j t	 j t	 j g } n	 t    | d d  } | d d  } |  j d k rft	 |
 |  j d |  | g   } t	 |
 | |  j | d  g   } n t	 |
 |  j d |  | g  |
 |  j d |  | g   } t	 |
 | |  j | d  g  |
 | |  j | d  g   } | r| d k r| | } } | | f S)zW divide a path into two segment at the point where inside(x, y)
    becomes False.
    r5   Nr   r*   z2The path does not seem to intersect with the patchr?      FrZ   rZ   )Ziter_segmentsnextr/   concatenater2   r   listziprR   r   LINETOMOVETOZCURVE3ZCURVE4codesvertices)pathZinsider:   Zreorder_inoutZ	path_iterZ
ctl_pointscommandZbegin_insideZbezier_pathZctl_points_oldconcatZioldibpleftrightZ
codes_leftZcodes_rightZ
verts_leftZverts_rightZpath_inZpath_outr   r   r   split_path_inout   sV    	
/!	%(""rj   c                s&   | d      f d d   } | S)Nr5   c                s*   |  \ } } |   d |  d  k  S)Nr5   r   )rU   r   r   )r    r!   r2r   r   rT   6  s    zinside_circle.<locals>._fr   )r    r!   rS   rT   r   )r    r!   rk   r   inside_circle3  s    
rl   c             C   sM   | |  | | } } | | | | d } | d k r; d S| | | | f S)Ng      ?r           )rm   rm   r   )Zx0Zy0r%   r&   dxdyr   r   r   r   get_cos_sin>  s
    rp   gh㈵>c             C   so   t  j |  |  } t  j | |  } t  j | |  } | | k  rG d St  j | t  j  | k  rg d Sd Sd S)z returns
       * 1 if two lines are parralel in same direction
       * -1 if two lines are parralel in opposite direction
       * 0 otherwise
    r*   FNr+   )r/   arctan2abspi)Zdx1Zdy1Zdx2Zdy2r:   Ztheta1Ztheta2Zdthetar   r   r   check_if_parallelG  s    rt   c          	   C   s  |  d \ } } |  d \ } } |  d \ } } t  | | | | | | | |  } | d k r t j d  t | | | |  \ }	 }
 |	 |
 } } n6 t | | | |  \ }	 }
 t | | | |  \ } } t | | |	 |
 |  \ } } } } t | | | | |  \ } } } } | d k r`d | | d | | } } d | | d | | } } nN t | | |	 |
 | | | |  \ } } t | | |	 |
 | | | |  \ } } | | f | | f | | f g } | | f | | f | | f g } | | f S)z
    Given the quadratic bezier control points *bezier2*, returns
    control points of quadratic bezier lines roughly parallel to given
    one separated by *width*.
    r   r*   r5   z8Lines do not intersect. A straight line is used instead.g      ?r+   )rt   warningswarnrp   r)   r   )bezier2widthc1xc1ycmxcmyc2xc2yZparallel_testr   r   r   r   c1x_leftc1y_left	c1x_right	c1y_rightZc2x_leftZc2y_leftZ	c2x_rightZ	c2y_rightZcmx_leftZcmy_leftZ	cmx_rightZ	cmy_right	path_left
path_rightr   r   r   get_parallelsX  s<    $$ 						r   c             C   sK   d d | |  | } d d | | | } |  | f | | f | | f g S)z Find control points of the bezier line throught c1, mm, c2. We
    simply assume that c1, mm, c2 which have parametric value 0, 0.5, and 1.
    g      ?rY   r   )ry   rz   ZmmxZmmyr}   r~   r{   r|   r   r   r   find_control_points  s    r   g      ?c       %      C   s  |  d \ } } |  d \ } } |  d \ }	 }
 t  | | | |  \ } } t  | | |	 |
  \ } } t | | | | | |  \ } } } } t |	 |
 | | | |  \ } } } } | | d | | d } } | |	 d | |
 d } } | | d | | d } } t  | | | |  \ } } t | | | | | |  \ } }  }! }" t | | | |  | |  }# t | | |! |" | |  }$ |# |$ f S)z
    Being similar to get_parallels, returns control points of two quadrativ
    bezier lines having a width roughly parralel to given one separated by
    *width*.
    r   r*   r5   g      ?)rp   r)   r   )%rw   rx   Zw1ZwmZw2ry   rz   r{   r|   Zc3xZc3yr   r   r   r   r   r   r   r   Zc3x_leftZc3y_leftZ	c3x_rightZ	c3y_rightZc12xZc12yZc23xZc23yZc123xZc123yZcos_t123Zsin_t123Z
c123x_leftZ
c123y_leftZc123x_rightZc123y_rightr   r   r   r   r   make_wedged_bezier2  s&    (((		r   c             C   sl   |  j  } | d k rd t j |  j j d d  d  } | j t j  t j | d <t |  j |  S|  Sd S)z$
    fill in the codes if None.
    Nr*   rf   r   )	ra   r/   emptyrb   shapefillr   r_   r`   )pr   r   r   r   make_path_regular  s    	"r   c             C   sn   g  } g  } x: |  D]2 } t  |  } | j | j  | j | j  q Wt t j |  t j |   } | S)z7
    concatenate list of paths into a single path.
    )r   r1   rb   ra   r   r/   r\   )pathsrb   ra   r   _pathr   r   r   concatenate_paths  s    r   )#rN   
__future__r   r   r   r   Zmatplotlib.externalsr   numpyr/   Zmatplotlib.pathr   operatorr   ru   r   r	   r   r)   r.   r4   r=   objectr>   rR   rX   rj   rl   rp   rt   r   r   r   r   r   r   r   r   r   <module>   s0   "2*B	J3