o
    h                     @   sr  d Z ddlmZmZmZ ddlmZ ddlZddlm	Z	 zddl
Z
W n eefy3   ddlm
Z
 Y nw e
jZdZe	dg d	Zg d
ZdddZdd Ze
e
je
je
je
je
je
jde
je
je
je
jddd Ze
e
je
je
je
je
je
jde
je
je
jddddZdZdZe
je
je
e
je
je
je
jddd Ze
je
je
e
je
je
jddd Z d d! Z!e
e
je
je
je
je
je
je
je
je
jd"e
je
je
je
je
je
je
je
jd#d$d% Z"d&d' Z#e
e
je
je
je
je
jd(e
je
je
je
jd)d*d+ Z$d,d- Z%d.d/ Z&e
e
je
je
je
je
je
jde
je
je
je
je
je
jd0d1d2 Z'd3d4 Z(d5d6 Z)d7d8 Z*d9d: Z+d;d< Z,d=d> Z-e
je
je
je
je
je
je
je
je
jd?d@dA Z.e
e
je
je
je
je
je
je
je
je
je
jdBe
je
je
je
je
jdCdDdE Z/dFdG Z0dHdI Z1e
je
je
je
je
je
je
je
je
je
je
je
je
je
jdJdKdL Z2ddMlm3Z3m4Z4m5Z5m6Z6 e3fdNdOZ7dPdQ Z8dRdS Z9dTdU Z:e
je
je
je
je
je
je
je
je
je
jdVdWdX Z;dYdZ Z<d[d\ Z=e
je
je
je
je
je
je
je
je
je
jd]d^d_ Z>d`da Z?dbdc Z@ddde ZAe
e
je
je
je
je
je
je
jdfe
je
je
je
jdgdhdi ZBdjdk ZCdldm ZDdndo ZEdpdq ZFdrds ZGdtdu ZHdvdw ZIdxdy ZJdzd{ ZK	dd}d~ZLdd ZMdd ZNdd ZOdd ZPdd ZQeRdkrddlSZSddlTZTeSUeTV jW dS dS )zNfontTools.misc.bezierTools.py -- tools for working with Bezier path segments.
    )
calcBoundssectRectrectArea)IdentityN)
namedtuple)cythong&.>Intersectionptt1t2)approximateCubicArcLengthapproximateCubicArcLengthCapproximateQuadraticArcLengthapproximateQuadraticArcLengthCcalcCubicArcLengthcalcCubicArcLengthCcalcQuadraticArcLengthcalcQuadraticArcLengthCcalcCubicBoundscalcQuadraticBounds	splitLinesplitQuadratic
splitCubicsplitQuadraticAtTsplitCubicAtTsplitCubicAtTCsplitCubicIntoTwoAtTCsolveQuadratic
solveCubicquadraticPointAtTcubicPointAtTcubicPointAtTClinePointAtTsegmentPointAtTlineLineIntersectionscurveLineIntersectionscurveCurveIntersectionssegmentSegmentIntersections{Gzt?c                 C   s    t t|  t| t| t| |S )a  Calculates the arc length for a cubic Bezier segment.

    Whereas :func:`approximateCubicArcLength` approximates the length, this
    function calculates it by "measuring", recursively dividing the curve
    until the divided segments are shorter than ``tolerance``.

    Args:
        pt1,pt2,pt3,pt4: Control points of the Bezier as 2D tuples.
        tolerance: Controls the precision of the calcuation.

    Returns:
        Arc length value.
    )r   complex)pt1pt2pt3pt4	tolerance r0   n/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/fontTools/misc/bezierTools.pyr   8   s   r   c                 C   s\   | d||   | d }|| | |  d }| | | d || |f||| || d |ffS )N   g      ?      ?r0   )p0p1p2p3midderiv3r0   r0   r1   _split_cubic_into_twoK   s
   r:   )r4   r5   r6   r7   )multarchboxc           	      C   s~   t || }t || t ||  t ||  }||  t |kr&|| d S t||||\}}t| g|R  t| g|R   S Nr3   )absEPSILONr:   _calcCubicArcLengthCRecurse)	r;   r4   r5   r6   r7   r<   r=   onetwor0   r0   r1   rA   T   s   	$
rA   r+   r,   r-   r.   )r/   r;   c                 C   s   dd|  }t || |||S )zCalculates the arc length for a cubic Bezier segment.

    Args:
        pt1,pt2,pt3,pt4: Control points of the Bezier as complex numbers.
        tolerance: Controls the precision of the calcuation.

    Returns:
        Arc length value.
          ?g      ?)rA   )r+   r,   r-   r.   r/   r;   r0   r0   r1   r   h   s   r      g|=v1v2c                 C   s   | |   jS N)	conjugaterealrG   r0   r0   r1   _dot   s   rM   xc                 C   s(   | t | d d  d t | d  S )N      )mathsqrtasinhrN   r0   r0   r1   _intSecAtan   s   (rU   c                 C      t t|  t| t| S )a  Calculates the arc length for a quadratic Bezier segment.

    Args:
        pt1: Start point of the Bezier as 2D tuple.
        pt2: Handle point of the Bezier as 2D tuple.
        pt3: End point of the Bezier as 2D tuple.

    Returns:
        Arc length value.

    Example::

        >>> calcQuadraticArcLength((0, 0), (0, 0), (0, 0)) # empty segment
        0.0
        >>> calcQuadraticArcLength((0, 0), (50, 0), (80, 0)) # collinear points
        80.0
        >>> calcQuadraticArcLength((0, 0), (0, 50), (0, 80)) # collinear points vertical
        80.0
        >>> calcQuadraticArcLength((0, 0), (50, 20), (100, 40)) # collinear points
        107.70329614269008
        >>> calcQuadraticArcLength((0, 0), (0, 100), (100, 0))
        154.02976155645263
        >>> calcQuadraticArcLength((0, 0), (0, 50), (100, 0))
        120.21581243984076
        >>> calcQuadraticArcLength((0, 0), (50, -10), (80, 50))
        102.53273816445825
        >>> calcQuadraticArcLength((0, 0), (40, 0), (-40, 0)) # collinear points, control point outside
        66.66666666666667
        >>> calcQuadraticArcLength((0, 0), (40, 0), (0, 0)) # collinear points, looping back
        40.0
    )r   r*   r+   r,   r-   r0   r0   r1   r      s    r   )r+   r,   r-   d0d1dn)scaleorigDistabx0x1Lenc                 C   s   ||  }|| }|| }|d }t |}|dkrt ||  S t||}t |tk rKt||dkr6t ||  S t |t |}	}
|	|	 |
|
  |	|
  S t||| }t||| }t dt|t|  | |||   }|S )a$  Calculates the arc length for a quadratic Bezier segment.

    Args:
        pt1: Start point of the Bezier as a complex number.
        pt2: Handle point of the Bezier as a complex number.
        pt3: End point of the Bezier as a complex number.

    Returns:
        Arc length value.
    y              ?        r   rP   )r?   rM   epsilonrU   )r+   r,   r-   rX   rY   rZ   r[   r\   r]   r^   r_   r`   ra   rb   r0   r0   r1   r      s"    
(r   c                 C   rV   )a  Calculates the arc length for a quadratic Bezier segment.

    Uses Gauss-Legendre quadrature for a branch-free approximation.
    See :func:`calcQuadraticArcLength` for a slower but more accurate result.

    Args:
        pt1: Start point of the Bezier as 2D tuple.
        pt2: Handle point of the Bezier as 2D tuple.
        pt3: End point of the Bezier as 2D tuple.

    Returns:
        Approximate arc length value.
    )r   r*   rW   r0   r0   r1   r      s   r   rW   )v0rH   rI   c                 C   sT   t d|  d|  d|  }t ||  d }t d|  d|  d|  }|| | S )a  Calculates the arc length for a quadratic Bezier segment.

    Uses Gauss-Legendre quadrature for a branch-free approximation.
    See :func:`calcQuadraticArcLength` for a slower but more accurate result.

    Args:
        pt1: Start point of the Bezier as a complex number.
        pt2: Handle point of the Bezier as a complex number.
        pt3: End point of the Bezier as a complex number.

    Returns:
        Approximate arc length value.
    g̔xb߿gb?gFVW?gqq?gFVWg̔xb?r?   )r+   r,   r-   re   rH   rI   r0   r0   r1   r      s   !r   c                    s   t | ||\\ \\ d }d }g }|dkr%| |  |dkr1| |   fdd|D | |g }t|S )a  Calculates the bounding rectangle for a quadratic Bezier segment.

    Args:
        pt1: Start point of the Bezier as a 2D tuple.
        pt2: Handle point of the Bezier as a 2D tuple.
        pt3: End point of the Bezier as a 2D tuple.

    Returns:
        A four-item tuple representing the bounding rectangle ``(xMin, yMin, xMax, yMax)``.

    Example::

        >>> calcQuadraticBounds((0, 0), (50, 100), (100, 0))
        (0, 0, 100, 50.0)
        >>> calcQuadraticBounds((0, 0), (100, 0), (100, 100))
        (0.0, 0.0, 100, 100)
           @r   c                    sT   g | ]&}d |  krdk rn n | | |   | | |   fqS r   rQ   r0   .0taxaybxbycxcyr0   r1   
<listcomp>D  s
    .z'calcQuadraticBounds.<locals>.<listcomp>)calcQuadraticParametersappendr   )r+   r,   r-   ax2ay2rootspointsr0   rl   r1   r   *  s   r   c                 C   s   t t|  t| t| t| S )a  Approximates the arc length for a cubic Bezier segment.

    Uses Gauss-Lobatto quadrature with n=5 points to approximate arc length.
    See :func:`calcCubicArcLength` for a slower but more accurate result.

    Args:
        pt1,pt2,pt3,pt4: Control points of the Bezier as 2D tuples.

    Returns:
        Arc length value.

    Example::

        >>> approximateCubicArcLength((0, 0), (25, 100), (75, 100), (100, 0))
        190.04332968932817
        >>> approximateCubicArcLength((0, 0), (50, 0), (100, 50), (100, 100))
        154.8852074945903
        >>> approximateCubicArcLength((0, 0), (50, 0), (100, 0), (150, 0)) # line; exact result should be 150.
        149.99999999999991
        >>> approximateCubicArcLength((0, 0), (50, 0), (100, 0), (-50, 0)) # cusp; exact result should be 150.
        136.9267662156362
        >>> approximateCubicArcLength((0, 0), (50, 0), (100, -50), (-50, 0)) # cusp
        154.80848416537057
    )r   r*   rD   r0   r0   r1   r   L  s   r   )re   rH   rI   v3v4c           	      C   s   t ||  d }t d|  d|  d|  d|  }t ||  | | d }t d|  d|  d|  d|  }t || d }|| | | | S )	zApproximates the arc length for a cubic Bezier segment.

    Args:
        pt1,pt2,pt3,pt4: Control points of the Bezier as complex numbers.

    Returns:
        Arc length value.
    g333333?gc1g85$t?gu|Y?g#$?g?g#$gc1?rf   )	r+   r,   r-   r.   re   rH   rI   rz   r{   r0   r0   r1   r   j  s,   r   c                    s   t | |||\\ \\\ d }d }d }d }dd t||D }dd t||D }	||	 }
 fdd|
D | |g }t|S )aX  Calculates the bounding rectangle for a quadratic Bezier segment.

    Args:
        pt1,pt2,pt3,pt4: Control points of the Bezier as 2D tuples.

    Returns:
        A four-item tuple representing the bounding rectangle ``(xMin, yMin, xMax, yMax)``.

    Example::

        >>> calcCubicBounds((0, 0), (25, 100), (75, 100), (100, 0))
        (0, 0, 100, 75.0)
        >>> calcCubicBounds((0, 0), (50, 0), (100, 50), (100, 100))
        (0.0, 0.0, 100, 100)
        >>> print("%f %f %f %f" % calcCubicBounds((50, 0), (0, 100), (100, 100), (50, 0)))
        35.566243 0.000000 64.433757 75.000000
          @rg   c                 S   (   g | ]}d |  krdk rn n|qS rh   r0   ri   r0   r0   r1   rs        ( z#calcCubicBounds.<locals>.<listcomp>c                 S   r}   rh   r0   ri   r0   r0   r1   rs     r~   c                    s\   g | ]*} | | | | |  |   | | | | |  |   fqS r0   r0   ri   rm   rn   ro   rp   rq   rr   dxdyr0   r1   rs     s    &&)calcCubicParametersr   r   )r+   r,   r-   r.   ax3ay3bx2by2xRootsyRootsrx   ry   r0   r   r1   r     s   &r   c                 C   s   | \}}|\}}|| }|| }	|}
|}||	f| }|dkr#| |fgS ||
|f|  | }d|  kr7dk rMn n|| |
 |	| | f}| |f||fgS | |fgS )a  Split a line at a given coordinate.

    Args:
        pt1: Start point of line as 2D tuple.
        pt2: End point of line as 2D tuple.
        where: Position at which to split the line.
        isHorizontal: Direction of the ray splitting the line. If true,
            ``where`` is interpreted as a Y coordinate; if false, then
            ``where`` is interpreted as an X coordinate.

    Returns:
        A list of two line segments (each line segment being two 2D tuples)
        if the line was successfully split, or a list containing the original
        line.

    Example::

        >>> printSegments(splitLine((0, 0), (100, 100), 50, True))
        ((0, 0), (50, 50))
        ((50, 50), (100, 100))
        >>> printSegments(splitLine((0, 0), (100, 100), 100, True))
        ((0, 0), (100, 100))
        >>> printSegments(splitLine((0, 0), (100, 100), 0, True))
        ((0, 0), (0, 0))
        ((0, 0), (100, 100))
        >>> printSegments(splitLine((0, 0), (100, 100), 0, False))
        ((0, 0), (0, 0))
        ((0, 0), (100, 100))
        >>> printSegments(splitLine((100, 0), (0, 0), 50, False))
        ((100, 0), (50, 0))
        ((50, 0), (0, 0))
        >>> printSegments(splitLine((0, 100), (0, 0), 50, True))
        ((0, 100), (0, 50))
        ((0, 50), (0, 0))
    r   rQ   r0   )r+   r,   whereisHorizontalpt1xpt1ypt2xpt2yrm   rn   ro   rp   r^   rk   midPtr0   r0   r1   r     s   $

r   c           	      C   sd   t | ||\}}}t|| || || | }tdd |D }|s(| ||fgS t|||g|R  S )a  Split a quadratic Bezier curve at a given coordinate.

    Args:
        pt1,pt2,pt3: Control points of the Bezier as 2D tuples.
        where: Position at which to split the curve.
        isHorizontal: Direction of the ray splitting the curve. If true,
            ``where`` is interpreted as a Y coordinate; if false, then
            ``where`` is interpreted as an X coordinate.

    Returns:
        A list of two curve segments (each curve segment being three 2D tuples)
        if the curve was successfully split, or a list containing the original
        curve.

    Example::

        >>> printSegments(splitQuadratic((0, 0), (50, 100), (100, 0), 150, False))
        ((0, 0), (50, 100), (100, 0))
        >>> printSegments(splitQuadratic((0, 0), (50, 100), (100, 0), 50, False))
        ((0, 0), (25, 50), (50, 50))
        ((50, 50), (75, 50), (100, 0))
        >>> printSegments(splitQuadratic((0, 0), (50, 100), (100, 0), 25, False))
        ((0, 0), (12.5, 25), (25, 37.5))
        ((25, 37.5), (62.5, 75), (100, 0))
        >>> printSegments(splitQuadratic((0, 0), (50, 100), (100, 0), 25, True))
        ((0, 0), (7.32233, 14.6447), (14.6447, 25))
        ((14.6447, 25), (50, 75), (85.3553, 25))
        ((85.3553, 25), (92.6777, 14.6447), (100, -7.10543e-15))
        >>> # XXX I'm not at all sure if the following behavior is desirable:
        >>> printSegments(splitQuadratic((0, 0), (50, 100), (100, 0), 50, True))
        ((0, 0), (25, 50), (50, 50))
        ((50, 50), (50, 50), (50, 50))
        ((50, 50), (75, 50), (100, 0))
    c                 s   ,    | ]}d |  krdk rn n|V  qdS r   rQ   Nr0   ri   r0   r0   r1   	<genexpr>"     * z!splitQuadratic.<locals>.<genexpr>)rt   r   sorted_splitQuadraticAtT)	r+   r,   r-   r   r   r^   r_   c	solutionsr0   r0   r1   r     s   #r   c                 C   sr   t | |||\}}}}	t|| || || |	| | }
tdd |
D }
|
s.| |||fgS t||||	g|
R  S )a  Split a cubic Bezier curve at a given coordinate.

    Args:
        pt1,pt2,pt3,pt4: Control points of the Bezier as 2D tuples.
        where: Position at which to split the curve.
        isHorizontal: Direction of the ray splitting the curve. If true,
            ``where`` is interpreted as a Y coordinate; if false, then
            ``where`` is interpreted as an X coordinate.

    Returns:
        A list of two curve segments (each curve segment being four 2D tuples)
        if the curve was successfully split, or a list containing the original
        curve.

    Example::

        >>> printSegments(splitCubic((0, 0), (25, 100), (75, 100), (100, 0), 150, False))
        ((0, 0), (25, 100), (75, 100), (100, 0))
        >>> printSegments(splitCubic((0, 0), (25, 100), (75, 100), (100, 0), 50, False))
        ((0, 0), (12.5, 50), (31.25, 75), (50, 75))
        ((50, 75), (68.75, 75), (87.5, 50), (100, 0))
        >>> printSegments(splitCubic((0, 0), (25, 100), (75, 100), (100, 0), 25, True))
        ((0, 0), (2.29379, 9.17517), (4.79804, 17.5085), (7.47414, 25))
        ((7.47414, 25), (31.2886, 91.6667), (68.7114, 91.6667), (92.5259, 25))
        ((92.5259, 25), (95.202, 17.5085), (97.7062, 9.17517), (100, 1.77636e-15))
    c                 s   r   r   r0   ri   r0   r0   r1   r   G  r   zsplitCubic.<locals>.<genexpr>)r   r   r   _splitCubicAtT)r+   r,   r-   r.   r   r   r^   r_   r   rZ   r   r0   r0   r1   r   (  s   r   c                 G   s&   t | ||\}}}t|||g|R  S )a  Split a quadratic Bezier curve at one or more values of t.

    Args:
        pt1,pt2,pt3: Control points of the Bezier as 2D tuples.
        *ts: Positions at which to split the curve.

    Returns:
        A list of curve segments (each curve segment being three 2D tuples).

    Examples::

        >>> printSegments(splitQuadraticAtT((0, 0), (50, 100), (100, 0), 0.5))
        ((0, 0), (25, 50), (50, 50))
        ((50, 50), (75, 50), (100, 0))
        >>> printSegments(splitQuadraticAtT((0, 0), (50, 100), (100, 0), 0.5, 0.75))
        ((0, 0), (25, 50), (50, 50))
        ((50, 50), (62.5, 50), (75, 37.5))
        ((75, 37.5), (87.5, 25), (100, 0))
    )rt   r   )r+   r,   r-   tsr^   r_   r   r0   r0   r1   r   M  s   r   c           
      G   sj   t | |||\}}}}t||||g|R  }	| g|	d dd R |	d< g |	d dd |R |	d< |	S )a   Split a cubic Bezier curve at one or more values of t.

    Args:
        pt1,pt2,pt3,pt4: Control points of the Bezier as 2D tuples.
        *ts: Positions at which to split the curve.

    Returns:
        A list of curve segments (each curve segment being four 2D tuples).

    Examples::

        >>> printSegments(splitCubicAtT((0, 0), (25, 100), (75, 100), (100, 0), 0.5))
        ((0, 0), (12.5, 50), (31.25, 75), (50, 75))
        ((50, 75), (68.75, 75), (87.5, 50), (100, 0))
        >>> printSegments(splitCubicAtT((0, 0), (25, 100), (75, 100), (100, 0), 0.5, 0.75))
        ((0, 0), (12.5, 50), (31.25, 75), (50, 75))
        ((50, 75), (59.375, 75), (68.75, 68.75), (77.3438, 56.25))
        ((77.3438, 56.25), (85.9375, 43.75), (93.75, 25), (100, 0))
    r   rQ   N)r   r   )
r+   r,   r-   r.   r   r^   r_   r   rZ   splitr0   r0   r1   r   e  s
   r   )r+   r,   r-   r.   r^   r_   r   rZ   c           	      g   s8    t | |||\}}}}t||||g|R  E dH  dS )a  Split a cubic Bezier curve at one or more values of t.

    Args:
        pt1,pt2,pt3,pt4: Control points of the Bezier as complex numbers..
        *ts: Positions at which to split the curve.

    Yields:
        Curve segments (each curve segment being four complex numbers).
    N)calcCubicParametersC_splitCubicAtTC)	r+   r,   r-   r.   r   r^   r_   r   rZ   r0   r0   r1   r     s    r   )rk   r+   r,   r-   r.   pointAtToff1off2)r   _1_t_1_t_2_2_t_1_tc                 C   s   || }d| }|| }d| | }|| |  d|| | || |    || |  }	||  ||  ||  }
|| ||  ||  }| ||  |  }||| |  }| ||
|	f|	|||ffS )a  Split a cubic Bezier curve at t.

    Args:
        pt1,pt2,pt3,pt4: Control points of the Bezier as complex numbers.
        t: Position at which to split the curve.

    Returns:
        A tuple of two curve segments (each curve segment being four complex numbers).
    rQ   rP   r2   r0   )r+   r,   r-   r.   rk   r   r   r   r   r   r   r   r0   r0   r1   r     s   2r   c                 G   s  t |}g }|dd |d | \}}|\}}|\}	}
tt|d D ]_}|| }||d  }|| }|| }|| }|| }d| | | | }d| | | | }|| }|| ||  |	 }|| ||  |
 }t||f||f||f\}}}||||f q%|S )Nr   rc   rE   rQ   rP   )listinsertru   rangelencalcQuadraticPoints)r^   r_   r   r   segmentsrm   rn   ro   rp   rq   rr   ir   r   deltadelta_2a1xa1yb1xb1yt1_2c1xc1yr+   r,   r-   r0   r0   r1   r     s,   
r   c           "      G   s  t |}|dd |d g }| \}}|\}}	|\}
}|\}}tt|d D ]}|| }||d  }|| }|| }|| }|| }|| }|| }|| }d| | | | }d| | |	 | }d| | |
 d| |  | }d|	 | | d| |  | }|| ||  |
|  | }|| |	|  ||  | }t||f||f||f||f\}}} }!|||| |!f q)|S Nr   rc   rE   rQ   r2   rP   )r   r   ru   r   r   calcCubicPoints)"r^   r_   r   rZ   r   r   rm   rn   ro   rp   rq   rr   r   r   r   r   r   r   r   delta_3r   t1_3r   r   r   r   r   r   d1xd1yr+   r,   r-   r.   r0   r0   r1   r     s:   
  r   )r^   r_   r   rZ   r   r   r   r   r   a1b1c1rY   c                 g   s    t |}|dd |d tt|d D ]^}|| }||d  }|| }|| }	||	 }
|| }|| }| |
 }d|  | | |	 }d| | | d|  |  | }| | ||  ||  | }t||||\}}}}||||fV  qd S r   )r   r   ru   r   r   calcCubicPointsC)r^   r_   r   rZ   r   r   r   r   r   r   r   r   r   r   r   r   rY   r+   r,   r-   r.   r0   r0   r1   r     s&   
 r   )rS   acoscospic                 C   s   t | tk rt |tk rg }|S | | g}|S || d|  |  }|dkr>||}| | d |  | | d |  g}|S g }|S )uK  Solve a quadratic equation.

    Solves *a*x*x + b*x + c = 0* where a, b and c are real.

    Args:
        a: coefficient of *x²*
        b: coefficient of *x*
        c: constant term

    Returns:
        A list of roots. Note that the returned list is neither guaranteed to
        be sorted nor to contain unique values!
          @rc   rg   r?   rd   )r^   r_   r   rS   rx   DDrDDr0   r0   r1   r   /  s   
$r   c                 C   s  t | tk rt|||S t| } ||  }||  }||  }|| d|  d }d| | | d| |  d|  d }|| }	|| | }
|	tk rJdn|	}	t |
tk rTdn|
}
|	|
 }|	dkro|
dkrot| d t}|||gS |td krttt|t	|
 d	d
}dt	| }|d }|t
|d  | }|t
|dt  d  | }|t
|dt  d  | }t|||g\}}}|| tk r|| tk rt|| | d t } }}n>|| tk rt|| d t }}t|t}n'|| tk r	t|t}t|| d t }}nt|t}t|t}t|t}|||gS tt	|t | d}|||  }|dkr6| }t||d  t}|gS )ut  Solve a cubic equation.

    Solves *a*x*x*x + b*x*x + c*x + d = 0* where a, b, c and d are real.

    Args:
        a: coefficient of *x³*
        b: coefficient of *x²*
        c: coefficient of *x*
        d: constant term

    Returns:
        A list of roots. Note that the returned list is neither guaranteed to
        be sorted nor to contain unique values!

    Examples::

        >>> solveCubic(1, 1, -6, 0)
        [-3.0, -0.0, 2.0]
        >>> solveCubic(-10.0, -9.0, 48.0, -29.0)
        [-2.9, 1.0, 1.0]
        >>> solveCubic(-9.875, -9.0, 47.625, -28.75)
        [-2.911392, 1.0, 1.0]
        >>> solveCubic(1.0, -4.5, 6.75, -3.375)
        [1.5, 1.5, 1.5]
        >>> solveCubic(-12.0, 18.0, -9.0, 1.50023651123)
        [0.5, 0.5, 0.5]
        >>> solveCubic(
        ...     9.0, 0.0, 0.0, -7.62939453125e-05
        ... ) == [-0.0, -0.0, -0.0]
        True
    r|   g      "@rg   g      ;@g      K@r   rc   r3   rE   g      g       r   UUUUUU?)r?   rd   r   floatroundepsilonDigitsr   maxminrS   r   r   r   pow)r^   r_   r   rZ   r   a2a3QRR2Q3R2_Q3rO   thetarQ2a1_3r`   ra   x2r0   r0   r1   r   P  sT   &(
 





r   c                 C   s^   |\}}|\}}| \}}|| d }	|| d }
|| |	 }|| |
 }||f|	|
f||ffS )Nrg   r0   )r+   r,   r-   r   y2x3y3rq   rr   ro   rp   rm   rn   r0   r0   r1   rt     s   rt   c                 C   s   |\}}|\}}|\}}	| \}
}||
 d }|| d }|| d | }|| d | }||
 | | }|	| | | }||f||f||f|
|ffS Nr|   r0   )r+   r,   r-   r.   r   r   r   r   x4y4r   r   rq   rr   ro   rp   rm   rn   r0   r0   r1   r     s   r   )r+   r,   r-   r.   r^   r_   r   c                 C   s8   ||  d }|| d | }||  | | }|||| fS r   r0   )r+   r,   r-   r.   r   r_   r^   r0   r0   r1   r        r   c                 C   sf   | \}}|\}}|\}}|}	|}
|d | }|d | }|| | }|| | }|	|
f||f||ffS r>   r0   )r^   r_   r   rm   rn   ro   rp   rq   rr   ra   y1r   r   r   r   r0   r0   r1   r     s   r   c                 C   s   | \}}|\}}|\}}	|\}
}|
}|}|d |
 }|	d | }|| d | }||	 d | }||
 | | }|| |	 | }||f||f||f||ffS r   r0   )r^   r_   r   rZ   rm   rn   ro   rp   rq   rr   r   r   ra   r   r   r   r   r   r   r   r0   r0   r1   r     s   r   r^   r_   r   rZ   r6   r7   p4c                 C   s8   |d | }|| d | }| | | | }||||fS )Nr   r0   r   r0   r0   r1   r     r   r   c                 C   s8   | d d|  |d |  | d d|  |d |  fS )zFinds the point at time `t` on a line.

    Args:
        pt1, pt2: Coordinates of the line as 2D tuples.
        t: The time along the line.

    Returns:
        A 2D tuple with the coordinates of the point.
    r   rQ   r0   )r+   r,   rk   r0   r0   r1   r#     s   8
r#   c                 C   s   d| d|  | d  dd|  | |d   || |d   }d| d|  | d  dd|  | |d   || |d   }||fS )zFinds the point at time `t` on a quadratic curve.

    Args:
        pt1, pt2, pt3: Coordinates of the curve as 2D tuples.
        t: The time along the curve.

    Returns:
        A 2D tuple with the coordinates of the point.
    rQ   r   rP   r0   )r+   r,   r-   rk   rO   yr0   r0   r1   r      s   @
@r    c           
      C   s   || }d| }|| }|| | d  d|| |d  || |d     || |d   }|| | d  d|| |d  || |d     || |d   }	||	fS )zFinds the point at time `t` on a cubic curve.

    Args:
        pt1, pt2, pt3, pt4: Coordinates of the curve as 2D tuples.
        t: The time along the curve.

    Returns:
        A 2D tuple with the coordinates of the point.
    rQ   r   r2   r0   )
r+   r,   r-   r.   rk   r   r   r   rO   r   r0   r0   r1   r!   ,  s    
""r!   )rk   r+   r,   r-   r.   )r   r   r   c                 C   sL   || }d| }|| }|| |  d|| | || |    || |  S )zFinds the point at time `t` on a cubic curve.

    Args:
        pt1, pt2, pt3, pt4: Coordinates of the curve as complex numbers.
        t: The time along the curve.

    Returns:
        A complex number with the coordinates of the point.
    rQ   r2   r0   )r+   r,   r-   r.   rk   r   r   r   r0   r0   r1   r"   F  s   4r"   c                 C   sb   t | dkrtg | |R  S t | dkrtg | |R  S t | dkr-tg | |R  S tdNrP   r2      Unknown curve degree)r   r#   r    r!   
ValueError)segrk   r0   r0   r1   r$   _  s   r$   c           	      C   st   | \}}|\}}|\}}t || tk rt || tk rdS t || t || kr2|| ||  S || ||  S )Nr   r   )	ser
   sxsyexeypxpyr0   r0   r1   _line_t_of_ptn  s    r   c                 C   sR   | d |d  |d |d   }| d |d  |d |d   }|dko'|dk S )Nr   rQ   rc   r0   )r^   r_   originxDiffyDiffr0   r0   r1   '_both_points_are_on_same_side_of_origin|  s     r   c                 C   s  | \}}|\}}|\}}	|\}
}t ||
r$t ||r$t ||s$g S t |	|r8t ||r8t ||	s8g S t ||
rFt |	|rFg S t ||rTt ||rTg S t ||r|}||	 |
|  }|||  |	 }||f}t|t| ||t|||dgS t ||
r|}|| ||  }|||  | }||f}t|t| ||t|||dgS || ||  }||	 |
|  }t ||rg S || | ||  |	 ||  }|||  | }||f}t||| rt|||rt|t| ||t|||dgS g S )a  Finds intersections between two line segments.

    Args:
        s1, e1: Coordinates of the first line as 2D tuples.
        s2, e2: Coordinates of the second line as 2D tuples.

    Returns:
        A list of ``Intersection`` objects, each object having ``pt``, ``t1``
        and ``t2`` attributes containing the intersection point, time on first
        segment and time on second segment respectively.

    Examples::

        >>> a = lineLineIntersections( (310,389), (453, 222), (289, 251), (447, 367))
        >>> len(a)
        1
        >>> intersection = a[0]
        >>> intersection.pt
        (374.44882952482897, 313.73458370177315)
        >>> (intersection.t1, intersection.t2)
        (0.45069111555824465, 0.5408153767394238)
    r	   )rR   iscloser   r   r   )s1e1s2e2s1xs1ye1xe1ys2xs2ye2xe2yrO   slope34r   r
   slope12r0   r0   r1   r%     sr   





 
r%   c                 C   sT   | d }| d }t |d |d  |d |d  }t| |d  |d  S )Nr   r   rQ   )rR   atan2r   rotate	translate)segmentstartendangler0   r0   r1   _alignment_transformation  s   $ r
  c                 C   s   t || }t| dkr!t| \}}}t|d |d |d }n"t| dkr?t| \}}}}t|d |d |d |d }ntdtdd |D S )Nr2   rQ   r   r   c                 s   s,    | ]}d |  krdkrn n|V  qdS )rc   rQ   Nr0   rj   r   r0   r0   r1   r     r   z._curve_line_intersections_t.<locals>.<genexpr>)	r
  transformPointsr   rt   r   r   r   r   r   )curvelinealigned_curver^   r_   r   intersectionsrZ   r0   r0   r1   _curve_line_intersections_t  s    r  c                 C   s   t | dkr	t}nt | dkrt}ntdg }t| |D ]'}|g | |R  }tg ||R  }tg ||R  }|t|||d q|S )a  Finds intersections between a curve and a line.

    Args:
        curve: List of coordinates of the curve segment as 2D tuples.
        line: List of coordinates of the line segment as 2D tuples.

    Returns:
        A list of ``Intersection`` objects, each object having ``pt``, ``t1``
        and ``t2`` attributes containing the intersection point, time on first
        segment and time on second segment respectively.

    Examples::
        >>> curve = [ (100, 240), (30, 60), (210, 230), (160, 30) ]
        >>> line  = [ (25, 260), (230, 20) ]
        >>> intersections = curveLineIntersections(curve, line)
        >>> len(intersections)
        3
        >>> intersections[0].pt
        (84.9000930760723, 189.87306176459828)
    r2   r   r   r	   )	r   r    r!   r   r  r   r#   ru   r   )r  r  pointFinderr  rk   r
   line_tr0   r0   r1   r&     s   r&   c                 C   s0   t | dkr
t|  S t | dkrt|  S td)Nr2   r   r   )r   r   r   r   )r   r0   r0   r1   _curve_bounds  s
   r  c                 C   st   t | dkr| \}}t|||}||f||fgS t | dkr'tg | |R  S t | dkr6tg | |R  S tdr   )r   r#   r   r   r   )r   rk   r   r   midpointr0   r0   r1   _split_segment_at_t  s   r  MbP?c              	      sr  t | }t |}|sd}|sd}t||\}}|sg S dd }	t| k r4t| k r4|	||	|fgS t| d\}
}|d |	|f}|	||d f}t|d\}}|d |	|f}|	||d f}g }|t|
| ||d |t|| ||d |t|
| ||d |t|| ||d  fdd	}t }g }|D ]}||}||v rq|| || q|S )
N)rc   rE   c                 S   s   d| d | d   S )Nr3   r   rQ   r0   )rr0   r0   r1   r  1  s   z._curve_curve_intersections_t.<locals>.midpointr3   r   rQ   )range1range2c                    s    t | d   t | d   fS )Nr   rQ   )int)r   	precisionr0   r1   <lambda>V       z._curve_curve_intersections_t.<locals>.<lambda>)	r  r   r   r  extend_curve_curve_intersections_tsetaddru   )curve1curve2r  r  r  bounds1bounds2
intersects_r  c11c12	c11_range	c12_rangec21c22	c21_range	c22_rangefound
unique_keyseenunique_valuesr   keyr0   r  r1   r!  !  sb   




r!  c                 C   s    t | | }tdd |D S )Nc                 s   s     | ]}t |d  dV  qdS )rQ   rc   N)rR   r   )rj   pr0   r0   r1   r   f  s    z_is_linelike.<locals>.<genexpr>)r
  r  all)r  	maybeliner0   r0   r1   _is_lineliked  s   r:  c                    s   t  r& d  d f}t |r!|d |d f}tg ||R  S t||S t |r7|d |d f}t |S t |} fdd|D S )a  Finds intersections between a curve and a curve.

    Args:
        curve1: List of coordinates of the first curve segment as 2D tuples.
        curve2: List of coordinates of the second curve segment as 2D tuples.

    Returns:
        A list of ``Intersection`` objects, each object having ``pt``, ``t1``
        and ``t2`` attributes containing the intersection point, time on first
        segment and time on second segment respectively.

    Examples::
        >>> curve1 = [ (10,100), (90,30), (40,140), (220,220) ]
        >>> curve2 = [ (5,150), (180,20), (80,250), (210,190) ]
        >>> intersections = curveCurveIntersections(curve1, curve2)
        >>> len(intersections)
        3
        >>> intersections[0].pt
        (81.7831487395506, 109.88904552375288)
    r   r   c                    s,   g | ]}t t |d  |d  |d dqS )r   rQ   r	   )r   r$   )rj   r   r$  r0   r1   rs     s    z+curveCurveIntersections.<locals>.<listcomp>)r:  r%   r&   r!  )r$  r%  line1line2intersection_tsr0   r;  r1   r'   i  s   



r'   c                 C   s   d}t |t | kr| |}} d}t | dkr)t |dkr#t| |}n t| |}nt | dkr?t |dkr?tg | |R  }ntd|sG|S dd |D S )a)  Finds intersections between two segments.

    Args:
        seg1: List of coordinates of the first segment as 2D tuples.
        seg2: List of coordinates of the second segment as 2D tuples.

    Returns:
        A list of ``Intersection`` objects, each object having ``pt``, ``t1``
        and ``t2`` attributes containing the intersection point, time on first
        segment and time on second segment respectively.

    Examples::
        >>> curve1 = [ (10,100), (90,30), (40,140), (220,220) ]
        >>> curve2 = [ (5,150), (180,20), (80,250), (210,190) ]
        >>> intersections = segmentSegmentIntersections(curve1, curve2)
        >>> len(intersections)
        3
        >>> intersections[0].pt
        (81.7831487395506, 109.88904552375288)
        >>> curve3 = [ (100, 240), (30, 60), (210, 230), (160, 30) ]
        >>> line  = [ (25, 260), (230, 20) ]
        >>> intersections = segmentSegmentIntersections(curve3, line)
        >>> len(intersections)
        3
        >>> intersections[0].pt
        (84.9000930760723, 189.87306176459828)

    FTrP   z4Couldn't work out which intersection function to usec                 S   s    g | ]}t |j|j|jd qS )r	   )r   r
   r   r   r  r0   r0   r1   rs     r  z/segmentSegmentIntersections.<locals>.<listcomp>)r   r'   r&   r%   r   )seg1seg2swappedr  r0   r0   r1   r(     s   
r(   c                 C   s@   zt | }W n ty   d|   Y S w dddd |D  S )zw
    >>> _segmentrepr([1, [2, 3], [], [[2, [3, 4], [0.1, 2.2]]]])
    '(1, (2, 3), (), ((2, (3, 4), (0.1, 2.2))))'
    z%gz(%s)z, c                 s   s    | ]}t |V  qd S rJ   )_segmentrepr)rj   rO   r0   r0   r1   r     s    z_segmentrepr.<locals>.<genexpr>)iter	TypeErrorjoin)objitr0   r0   r1   rB    s   rB  c                 C   s   | D ]}t t| qdS )zlHelper for the doctests, displaying each segment in a list of
    segments on a single line as a tuple.
    N)printrB  )r   r  r0   r0   r1   printSegments  s   rI  __main__)r)   )r  NN)X__doc__fontTools.misc.arrayToolsr   r   r   fontTools.misc.transformr   rR   collectionsr   r   AttributeErrorImportErrorfontTools.misccompiledCOMPILEDr@   r   __all__r   r:   returnsdoublelocalsr*   rA   r   r   rd   cfuncinlinerM   rU   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rS   r   r   r   r   r   rt   r   r   r   r   r   r#   r    r!   r"   r$   r   r   r%   r
  r  r&   r  r  r!  r:  r'   r(   rB  rI  __name__sysdoctestexittestmodfailedr0   r0   r0   r1   <module>   s  
 
	



#		 
!"
$&9-%



#
!a		
N
&
C'0
