rL iiJdZddlmZddlZddlZddlZddlmZejeddZ Gdd e Z d Z d Zd Zd Z ddZGddZ ddZddZdZdZddZdZdZddZy)uP A module providing some utility functions regarding Bézier path manipulation. ) lru_cacheN)_api)maxsizec||kDryt|||z }tjd|dz}tj|dz|z |z j t S)Nr)minnparangeprodastypeint)nkis W/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/matplotlib/bezier.py_combrsW 1u Aq1u A !QUA 77AEAIq= ! ( ( --c eZdZy)NonIntersectingPathExceptionN)__name__ __module__ __qualname__rrrrsrrc||z||zz }||z||zz } || } } || } } | | z| | zz tdkr td| | }}| | }}fd||||fD\}}}}||z|| zz}||z|| zz}||fS)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.c3(K|] }|z  yw)Nr).0rad_bcs r z#get_intersection..9s:Aa%i:s)abs ValueError)cx1cy1cos_t1sin_t1cx2cy2cos_t2sin_t2 line1_rhs line2_rhsabcda_b_c_d_xyrs @rget_intersectionr6 s v|+I v|+I F7qA F7qA EAEME 5zENO OBRB:"b"b)9:NBB Yi'A Yi'A a4Krcx|dk(r||||fS|| }}| |}}||z|z||z|z} } ||z|z||z|z} } | | | | fS)z For a line passing through (*cx*, *cy*) and having an angle *t*, return locations of the two points located along its perpendicular line at the distance of *length*. r) cxcycos_tsin_tlengthr$r%r(r)x1y1x2y2s rget_normal_pointsrBAss|2r2~UFFFVUFF f_r !6F?R#7B f_r !6F?R#7B r2r>rc.|ddd|z z|dd|zz}|S)Nrr)betat next_betas r_de_casteljau1rHZs+Sb QU#d12hl2I rctj|}|g} t||}|j|t |dk(rn-|Dcgc]}|d }}t |Dcgc]}|d }}||fScc}wcc}w)u Split a Bézier segment defined by its control points *beta* into two separate segments divided at *t* and return their control points. rrrD)r asarrayrHappendlenreversed)rErF beta_list left_beta right_betas rsplit_de_casteljaurQ_s ::d DI dA& t9>   &//Ta/I/'/ ':;t$r(;J; j  0;s A8& A=cB||}||}||}||}||k(r||k7r td tj|d|dz |d|dz |kr||fSd||zz} || } || } || z r| }|| k(r||fS| }n| }|| k(r||fS| }| }j)u Find the intersection of the Bézier curve with a closed path. The intersection point *t* is approximated by two parameters *t0*, *t1* such that *t0* <= *t* <= *t1*. Search starts from *t0* and *t1* and uses a simple bisecting algorithm therefore one of the end points must be inside the path while the other doesn't. The search stops when the distance of the points parametrized by *t0* and *t1* gets smaller than the given *tolerance*. Parameters ---------- bezier_point_at_t : callable A function returning x, y coordinates of the Bézier at parameter *t*. It must have the signature:: bezier_point_at_t(t: float) -> tuple[float, float] inside_closedpath : callable A function returning True if a given point (x, y) is inside the closed path. It must have the signature:: inside_closedpath(point: tuple[float, float]) -> bool t0, t1 : float Start parameters for the search. tolerance : float Maximal allowed distance between the final points. Returns ------- t0, t1 : float The Bézier path parameters. z3Both points are on the same side of the closed pathrr?)rr hypot) bezier_point_at_tinside_closedpatht0t1 tolerancestartend start_inside end_insidemiddle_tmiddle middle_insides r*find_bezier_t_intersecting_with_closedpathraqsL b !E B C$U+L"3'Jz!esl* AC C  88E!Hs1v%uQx#a&'8 9I Er6M"r'?"8,)&1 - 'Bf}2v CB2v E(L3 rcheZdZdZdZdZdZedZedZ edZ edZ d Z y ) BezierSegmentu A d-dimensional Bézier segment. Parameters ---------- control_points : (N, d) array Location of the *N* control points. c tj||_|jj\|_|_tj |j|_t|jDcgc]`}tj|jdz tj|tj|jdz |z zzb}}|jj|zj|_ ycc}w)Nr) r rJ_cpointsshape_N_dr _ordersrangemath factorialT_px)selfcontrol_pointsrcoeffs r__init__zBezierSegment.__init__s >2 ==..yy)  .*! ,^^A&! a)HHJ**MMOOe+..*s9A%D ctj|}tjjd|z |jdddtjj||jz|j zS)u) Evaluate the Bézier curve at point(s) *t* in [0, 1]. Parameters ---------- t : (k,) array-like Points at which to evaluate the curve. Returns ------- (k, d) array Value of the curve for each point in *t*. rNrD)r rJpowerouterrirnrorFs r__call__zBezierSegment.__call__s` JJqMq1udll4R4&89((..DLL1259XX> >rc$t||S)zX Evaluate the curve at a single point, returning a tuple of *d* floats. )tuplervs r point_at_tzBezierSegment.point_at_tsT!W~rc|jS)z The control points of the curve.)reros rrpzBezierSegment.control_pointss}}rc|jS)zThe dimension of the curve.)rhr|s r dimensionzBezierSegment.dimensions wwrc |jdz S)z@Degree of the polynomial. One less the number of control points.r)rgr|s rdegreezBezierSegment.degreesww{rc:|j}|dkDrtjdt|j}t j |dzdddf}t j |dzdddf}d||zzt||z}t|||z|zS)u The polynomial coefficients of the Bézier curve. .. warning:: Follows opposite convention from `numpy.polyval`. Returns ------- (n+1, d) array Coefficients after expanding in polynomial basis, where :math:`n` is the degree of the Bézier curve and :math:`d` its dimension. These are the numbers (:math:`C_j`) such that the curve can be written :math:`\sum_{j=0}^n C_j t^j`. Notes ----- The coefficients are calculated as .. math:: {n \choose j} \sum_{i=0}^j (-1)^{i+j} {j \choose i} P_i where :math:`P_i` are the control points of the curve. zFPolynomial coefficients formula unstable for high order Bezier curves!rNrD)rwarningswarnRuntimeWarningrpr r r)rorPjr prefactors rpolynomial_coefficientsz%BezierSegment.polynomial_coefficientss2 KK r6 MM12@ B    IIacN1d7 # IIacN47 #1q5ME!QK/ Q{Y&**rc|j}|dkr*tjgtjgfS|j}tjd|dzdddf|ddz}g}g}t |j D]V\}}tj|ddd}|j||jtj||Xtj|}tj|}tj||dk\z|dkz} || tj|| fS)a Return the dimension and location of the curve's interior extrema. The extrema are the points along the curve where one of its partial derivatives is zero. Returns ------- dims : array of int Index :math:`i` of the partial derivative which is zero at each interior extrema. dzeros : array of float Of same size as dims. The :math:`t` such that :math:`d/dx_i B(t) = 0` rNrDr) rr arrayrr enumeratermrootsrK full_like concatenateisrealreal) rorCjdCjdimsrrpirin_ranges raxis_aligned_extremaz"BezierSegment.axis_aligned_extremas KK 688B<"- -  ) )ii1Q34(2ab61suu% ,EArDbD"A LLO KK Q* + ,u%~~d#99U#uz2eqjAH~rwwu~h777rN) rrr__doc__rrrwrzpropertyrpr~rrrrrrrcrcsl/>$ !+!+F8rrcct|}|j}t|||\}}t|||zdz \}}||fS)ur Split a Bézier curve into two at the intersection with a closed path. Parameters ---------- bezier : (N, 2) array-like Control points of the Bézier segment. See `.BezierSegment`. inside_closedpath : callable A function returning True if a given point (x, y) is inside the closed path. See also `.find_bezier_t_intersecting_with_closedpath`. tolerance : float The tolerance for the intersection. See also `.find_bezier_t_intersecting_with_closedpath`. Returns ------- left, right Lists of control points for the two Bézier segments. )rYg@)rcrzrarQ) bezierrVrYbzrUrWrX_left_rights r)split_bezier_intersecting_with_closedpathr<sS, v B  7, CFB'vR2~>ME6 &=rc Jddlm}|j}t|\}}||dd}|} d} d} |D]D\}}| } | t |dzz } ||dd|k7rt j | dd|g} n|} Ftd| jd} t| ||\}}t |dk(r&|jg}|j|jg}nt |d k(r<|j|jg}|j|j|jg}nkt |d k(rR|j|j|jg}|j|j|j|jg}n td |dd}|dd}|jU|t j |j d| |g}|t j ||j | dg}n|t j |j d| |gt j |jd| |g}|t j ||j | dgt j ||j| dg}|r|s||}}||fS) z` Divide a path into two segments at the point where ``inside(x, y)`` becomes False. r)PathNrz*The path does not intersect with the patch)rDrzThis should never be reached)pathr iter_segmentsnextrLr rr!reshaperLINETOMOVETOCURVE3CURVE4AssertionErrorcodesvertices)rinsiderY reorder_inoutr path_iter ctl_pointscommand begin_insidectl_points_oldioldr bezier_pathbpleftright codes_left codes_right verts_left verts_rightpath_inpath_outs rsplit_path_inoutr_s ""$Iy/J*RS/*LN D A(G G S_ !! *RS/ "l 2...*=z)JKK # GEFF   W %B; FIKD% 4yA~kk] {{DKK0 Takk4;;/ {{DKK= Takk4;; < {{DKKdkkJ ;<<abJ(K zzr~~t}}Ra'8*&EFG T]]125F'GHIr~~t}}Ud';Z&HI~~tzz%4'8*&EFH T]]125F'GH TZZ^'DEG\$g H rc$|dzfd}|S)z Return a function that checks whether a point is in a circle with center (*cx*, *cy*) and radius *r*. The returned function has the signature:: f(xy: tuple[float, float]) -> bool rc6|\}}|z dz|z dzzkS)Nrr)xyr4r5r9r:r2s r_fzinside_circle.._fs,1B1}B1},r11rr)r9r:rrrs`` @r inside_circlers aB2 IrcR||z ||z }}||z||zzdz}|dk(ry||z ||z fS)NrSr)r8r8r)x0y0r>r?dxdyr/s r get_cos_sinrsF "Wb2gB b27 r!AAv 626>rctj||}tj||}t||z }||kryt|tjz |kryy)a Check if two lines are parallel. Parameters ---------- dx1, dy1, dx2, dy2 : float The gradients *dy*/*dx* of the two lines. tolerance : float The angular tolerance in radians up to which the lines are considered parallel. Returns ------- is_parallel - 1 if two lines are parallel in same direction. - -1 if two lines are parallel in opposite direction. - False otherwise. rrDF)r arctan2r r)dx1dy1dx2dy2rYtheta1theta2dthetas rcheck_if_parallelrsX&ZZS !F ZZS !F & !F  Vbee^ y (rc B|d\}}|d\}}|d\}}t||z ||z ||z ||z }|dk(r+tjdt||||\} } | | } } n"t||||\} } t||||\} } t ||| | |\} }}}t ||| | |\}}}} t | || | ||| | \}}t ||| | ||| | \}}| |f||f||fg}||f||f||fg}||fS#t $r#d| |zzd||zz}}d||zzd||zz}}YEwxYw)u Given the quadratic Bézier control points *bezier2*, returns control points of quadratic Bézier lines roughly parallel to given one separated by *width*. rrrrDz8Lines do not intersect. A straight line is used instead.rS)rr warn_externalrrBr6r!)bezier2widthc1xc1ycmxcmyc2xc2y parallel_testr$r%r(r)c1x_leftc1y_left c1x_right c1y_rightc2x_leftc2y_left c2x_right c2y_rightcmx_leftcmy_left cmx_right cmy_right path_left path_rights r get_parallelsrsqzHCqzHCqzHC%cCis&)Cis( 0 9f06 906 @ 9 H%H%H%'Ii(i(i(*J j  )   8h& '80C)D 9y( )3)i2G+H   s.*C22)DDcPdd|z||zz z}dd|z||zz z}||f||f||fgS)u Find control points of the Bézier curve passing through (*c1x*, *c1y*), (*mmx*, *mmy*), and (*c2x*, *c2y*), at parametric values 0, 0.5, and 1. rSrr)rrmmxmmyrrrrs rfind_control_pointsr sK C39% &C C39% &C #Jc S#J //rc|d\}}|d\}}|d\} } t||||\} } t||| | \} }t||| | ||z\}}}}t| | | |||z\}}}}||zdz||zdz}}|| zdz|| zdz}}||zdz||zdz}}t||||\}}t||||||z\}} }!}"t|||| ||}#t|||!|"||}$|#|$fS)u Being similar to `get_parallels`, returns control points of two quadratic Bézier lines having a width roughly parallel to given one separated by *width*. rrrrS)rrBr)%rrw1wmw2rrrrc3xc3yr$r%r(r)rrrrc3x_leftc3y_left c3x_right c3y_rightc12xc12yc23xc23yc123xc123ycos_t123sin_t123 c123x_left c123y_left c123x_right c123y_rightrrs% rmake_wedged_bezier2r*siqzHCqzHCqzHC!c34NFF c34NFF #sFFEBJ?-Hh 9 #sFFEBJ?-Hh 9 )r!C#I#3$D)r!C#I#3$D4K2%t r'95E%T4t<Hh %(EBJG5J K$Hh$. $,h8I%Y %0+%. ;J j  r)r8?{Gz?)r)rF)gh㈵>)rrSr8)r functoolsrrkrnumpyr matplotlibr vectorizerr!rr6rBrHrQrarcrrrrrrrrrrrrs  3.. : B2 !&GKI)X|8|8@.2F:z&