3
$ßL]ä;  ã               @   s"  d Z ddlZddljZddlmZ G dd„ deƒZdd„ Z	dd	„ Z
d
d„ Zdd„ Zejdddƒd1dd„ƒZG dd„ deƒZejdddƒd2dd„ƒZejdƒejdddƒd3dd„ƒƒZejdddƒd4dd„ƒZdd „ Zd!d"„ Zejdddƒd5d$d%„ƒZd&d'„ Zd(d)„ Zd6d+d,„Zd-d.„ Zd/d0„ ZdS )7zO
A module providing some utility functions regarding bezier path manipulation.
é    N)ÚPathc               @   s   e Zd ZdS )ÚNonIntersectingPathExceptionN)Ú__name__Ú
__module__Ú__qualname__© r   r   ú9/tmp/pip-install-edjban26/matplotlib/matplotlib/bezier.pyr      s   r   c                sÀ   ||  ||  }|| ||  }	||  }
}||  }}|
| ||  ‰ t jˆ ƒdk r^tdƒ‚||  }}| |
 }}‡ fdd„||||gD ƒ\}}}}|| ||	  }|| ||	  }||fS )z‹
    Return the intersection between the line through (*cx1*, *cy1*) at angle
    *t1* and the line through (*cx2, cy2) at angle *t2*.
    gê-�™—q=zcGiven lines do not intersect. Please verify that the angles are not equal or differ by 180 degrees.c                s   g | ]}|ˆ  ‘qS r   r   )Ú.0Úk)Úad_bcr   r   ú
<listcomp>*   s    z$get_intersection.<locals>.<listcomp>)ÚnpÚabsÚ
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_Zc_Zd_ÚxÚyr   )r   r   Úget_intersection   s    
"r   c             C   sl   |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Úy1Zx2Úy2r   r   r   Úget_normal_points2   s    r#   c             C   s(   | d d… d|  | dd … |  }|S )Né   éÿÿÿÿr   )ÚbetaÚtZ	next_betar   r   r   Ú_de_casteljau1K   s    $r(   c             C   s`   t j| ƒ} | g}x&t| |ƒ} |j| ƒ t| ƒdkrP qW dd„ |D ƒ}dd„ t|ƒD ƒ}||fS )z”
    Split a bezier segment defined by its control points *beta* into two
    separate segments 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   ]   s    )r   Úasarrayr(   ÚappendÚlenÚreversed)r&   r'   Z	beta_listZ	left_betaZ
right_betar   r   r   Úsplit_de_casteljauP   s    


r-   z3.1Z	tolerenceÚ	toleranceç        ç      ð?ç{®Gáz„?c             C   s°   | |ƒ}| |ƒ}||ƒ}||ƒ}||kr8||kr8t dƒ‚xrtj|d |d  |d |d  ƒ|k rj||fS d||  }	| |	ƒ}
||
ƒ}||A rœ|	}|
}|}q:|	}|
}|}q:W d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 other doesn't. The search stop
    when |t0-t1| gets smaller than the given tolerance.
    value for

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

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

    z3Both points are on the same side of the closed pathr   r$   g      à?N)r   r   Úhypot)Úbezier_point_at_tÚinside_closedpathÚt0Út1r.   ÚstartÚendZstart_insideZ
end_insideZmiddle_tZmiddleZmiddle_insider   r   r   Ú*find_bezier_t_intersecting_with_closedpathb   s(    (r9   c               @   sP   e Zd ZdZejddgƒejdddgƒejddddgƒdœZdd„ Zdd	„ Zd
S )ÚBezierSegmentz:
    A simple class of a 2-dimensional bezier segment
    g      ð?g       @g      @)r$   é   é   c             C   sJ   t |ƒ}tj|ƒ| _tj|d  }tj|ƒj\}}|| | _|| | _	dS )z³
        *control_points* : location of contol points. It needs have a
         shape of n * 2, where n is the order of the bezier line. 1<=
         n <= 3 is supported.
        r$   N)
r+   r   ZarangeÚ_ordersr:   Ú_binom_coeffr)   ÚTÚ_pxÚ_py)ÚselfZcontrol_pointsZ_oZ_coeffZxxÚyyr   r   r   Ú__init__ž   s    
zBezierSegment.__init__c             C   sF   d| | j  ddd… || j   }tj|| jƒ}tj|| jƒ}||fS )zevaluate a point at tr$   Nr%   )r=   r   Údotr@   rA   )rB   r'   ÚttÚ_xZ_yr   r   r   Ú
point_at_t¬   s    "zBezierSegment.point_at_tN)	r   r   r   Ú__doc__r   Úarrayr>   rD   rH   r   r   r   r   r:   “   s   r:   c       	      C   s>   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:   rH   r9   r-   )	Zbezierr4   r.   Zbzr3   r5   r6   Ú_leftZ_rightr   r   r   Ú)split_bezier_intersecting_with_closedpath´   s    
rL   c                s0   |\‰‰‡ ‡‡‡fdd„}t || |||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>._f)r5   r6   r.   N)r9   )r4   Úxyr   r   ZrminZrmaxr.   rN   r   )r   r   r   r   r   Ú find_r_to_boundary_of_closedpathÈ   s    rP   Fc             C   s  | j ƒ }t|ƒ\}}||d	d… ƒ}|}tj}	d}
d}xZ|D ]J\}}|}
|t|ƒd 7 }||d
d… ƒ|kr‚|	|dd… |gƒ}P |}q<W tdƒ‚|jdƒ}t|||ƒ\}}t|ƒdkrÎtj	g}tj
tj	g}nht|ƒdk� rútjtjg}tj
tjtjg}n<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d… }| jdk�r�t|	| jd|… |gƒƒ}t|	|| j|d… gƒƒ}n\t|	| jd|
… |gƒ|	| jd|
… |gƒƒ}t|	|| j|d… gƒ|	|| j|d… gƒƒ}|�r| �r|| }}||fS )zW divide a path into two segment at the point where inside(x, y)
    becomes False.
    r;   Nr   r$   z*The path does not intersect with the patchr<   é   zThis should never be reachedéþÿÿÿrR   rR   r%   )r%   r;   )Ziter_segmentsÚnextr   Úconcatenater+   r   ZreshaperL   r   ÚLINETOÚMOVETOZCURVE3ZCURVE4ÚAssertionErrorÚcodesÚvertices)ÚpathZinsider.   Zreorder_inoutZ	path_iterZ
ctl_pointsÚcommandZbegin_insideZctl_points_oldÚconcatZioldÚiZbezier_pathZbpÚleftÚrightZ
codes_leftZcodes_rightZ
verts_leftZverts_rightZpath_inZpath_outr   r   r   Úsplit_path_inoutá   sP    

r`   c                s   |d ‰‡ ‡‡fdd„}|S )Nr;   c                s$   | \}}|ˆ  d |ˆ d  ˆk S )Nr;   r   )rO   r   r   )r   r   Úr2r   r   rN   #  s    zinside_circle.<locals>._fr   )r   r   rM   rN   r   )r   r   ra   r   Úinside_circle   s    rb   c             C   sB   ||  ||  }}|| ||  d }|dkr2dS || || fS )Ng      à?r   ç        )rc   rc   r   )Zx0Zy0r    r!   ZdxZdyr   r   r   r   Úget_cos_sin+  s
    rd   çñhãˆµøä>c             C   sR   t j| |ƒ}t j||ƒ}t j|| ƒ}||k r2dS t j|t j ƒ|k rJdS dS dS )z• returns
       * 1 if two lines are parallel in same direction
       * -1 if two lines are parallel in opposite direction
       * 0 otherwise
    r$   FNr%   )r   Zarctan2r   Úpi)Zdx1Zdy1Zdx2Zdy2r.   Ztheta1Ztheta2Zdthetar   r   r   Úcheck_if_parallel4  s    rg   c          	   C   sn  | d \}}| d \}}| d \}}t || || || || ƒ}|dkrrtjdƒ t||||ƒ\}	}
|	|
 }}n$t||||ƒ\}	}
t||||ƒ\}}t|||	|
|ƒ\}}}}t|||||ƒ\}}}}|dk�rd||  d||   }}d||  d||   }}n4t|||	|
||||ƒ\}}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$   r;   z8Lines do not intersect. A straight line is used instead.g      à?r%   )rg   ÚcbookZ_warn_externalrd   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_parallelsF  s<    


rw   c             C   s>   dd| | |   }dd| ||   }| |f||f||fgS )z�
    Find control points of the Bezier curve passing through (*c1x*, *c1y*),
    (*mmx*, *mmy*), and (*c2x*, *c2y*), at parametric values 0, 0.5, and 1.
    g      à?rQ   r   )rk   rl   ZmmxZmmyro   rp   rm   rn   r   r   r   Úfind_control_points�  s    rx   ç      à?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 quadratic
    bezier lines having a width roughly parallel to given one separated by
    *width*.
    r   r$   r;   g      à?)rd   r#   rx   )%ri   rj   Zw1ZwmZw2rk   rl   rm   rn   Zc3xZc3yr   r   r   r   rq   rr   rs   rt   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_rightru   rv   r   r   r   Úmake_wedged_bezier2š  s&    rz   c             C   sF   | j }|dkr>tjt| jƒtjtjd�}tj|d< t| j|ƒS | S dS )z¾
    If the :attr:`codes` attribute of `Path` *p* is None, return a copy of *p*
    with the :attr:`codes` set to (MOVETO, LINETO, LINETO, ..., LINETO);
    otherwise return *p* itself.
    N)Zdtyper   )	rX   r   Úfullr+   rY   r   rU   Z	code_typerV   )Úpr   r   r   r   Úmake_path_regularÍ  s    
r}   c             C   s2   t jdd„ | D ƒƒ}t jdd„ | D ƒƒ}t||ƒS )z/Concatenate a list of paths into a single path.c             S   s   g | ]
}|j ‘qS r   )rY   )r	   r|   r   r   r   r   Þ  s    z%concatenate_paths.<locals>.<listcomp>c             S   s   g | ]}t |ƒj‘qS r   )r}   rX   )r	   r|   r   r   r   r   ß  s    )r   rT   r   )ÚpathsrY   rX   r   r   r   Úconcatenate_pathsÜ  s    r   )r/   r0   r1   )r1   )r/   r0   r1   )r1   F)re   )r0   ry   r/   )rI   Znumpyr   Zmatplotlib.cbookrh   Zmatplotlib.pathr   r   r   r   r#   r(   r-   Z_rename_parameterr9   Úobjectr:   rL   Ú
deprecatedrP   r`   rb   rd   rg   rw   rx   rz   r}   r   r   r   r   r   Ú<module>   s6   
!/!>	J

3