
    lj!                        d dl mZ d dlmZ d dlmZ d dlZerd dlm	Z	 d dl
mZmZmZmZmZ  G d d          Z G d	 d
          ZdddZdS )    )annotations)PriorityQueue)TYPE_CHECKINGN)Sequence)Point2DPoint2D_ArrayPoint2DLikePoint2DLike_ArrayPoint3DLike_Arrayc                  :    e Zd ZdZddZdd
ZddZddZddZdS )Polygonz
    Initializes the Polygon with the given rings.

    Parameters
    ----------
    rings
        A sequence of points, where each sequence represents the rings of the polygon.
        Typically, multiple rings indicate holes in the polygon.
    ringsSequence[Point2DLike_Array]returnNonec                   d |D             }t          j        d |D                       }t          j        |d          | _        t          j        | j        |dz
  d          | _        t          j        | j        ||d         z  d          | _        t          j        t          j        | j        d          |d d         dz
  d          | _        | j        t          j        d| j        | j                  	                    dd          z  | _
        | j        d d df         | j        d d df         }}| j        d d df         | j        d d df         }}dt          j        ||          t          j        ||          z
  z  | _        | j        rr||z  ||z  z
  }t          j        ||z   |z            d	| j        z  z  }	t          j        ||z   |z            d	| j        z  z  }
t          j        |	|
g          | _        d S d S )
Nc                6    g | ]}t          j        |          S  npasarray.0rings     P/home/agentuser/manim-venv/lib/python3.11/site-packages/manim/utils/polylabel.py
<listcomp>z$Polygon.__init__.<locals>.<listcomp>!   s"    (L(L(LdD)9)9(L(L(L    c                (    g | ]}|j         d          S )r   )shaper   s     r   r   z$Polygon.__init__.<locals>.<listcomp>#   s    ===D$*Q-===r   r   axis   ij,ij->ig      ?g      @)r   cumsumconcatenatearraydeletestartstopdiffeinsumreshapenormdotareasumcentroid)selfr   np_ringscsumxyxryrfactorcxcys              r   __init__zPolygon.__init__    s   (L(Le(L(L(Ly==H===>>$&N8!$D$D$D
 %'Idj$($K$K$K
#%9TZRq#Q#Q#Q	#%9GDJQ'''crcQQ$
 $
 $
	 $(9ry	490
 0

'"a..$	
 z!!!Q$AAAqD!11111a4$)AAAqD/B"&B--"&Q--"?@	9 	/Vb1f_FR6)**cDIo>BR6)**cDIo>BHb"X..DMMM		/ 	/r   pointr	   floatc                \   t          j        d| j        || j        z
            }t          j        |dd                              dd          }t          j        t           j                            | j        | j        |z  z   |z
  d                    }| 	                    |          r|n| S )z9Compute the minimum distance from a point to the polygon.r$   r   r"   r#   r    )
r   r,   r.   r)   clipr-   minlinalgr+   inside)r3   r>   scalarsclipsds        r   compute_distancezPolygon.compute_distance:   s    )J	54:3EFFA&&..r1556INN4:	E(99EANJJ
 
 KK&&.qqQB.r   x_pointy_pointx0y0x1y1boolc                0   t          ||          |cxk    rt          ||          k    rln nit          ||          |cxk    rt          ||          k    r@n n=||z
  }||z
  }|||z
  z  |||z
  z  z
  }	t          t          j        |	d                    S dS )zn
        Check if a point is on the segment.

        The segment is defined by (x0, y0) to (x1, y1).
        g        F)rB   maxrO   r   isclose)
r3   rI   rJ   rK   rL   rM   rN   dxdycrosss
             r   _is_point_on_segmentzPolygon._is_point_on_segmentC   s     r2;;'0000SR[[00000SR[[G 6
 6
 6
 6
sP
 P
 6
 6
 6
 6
 6
 bBbB'B,'""*==E
5#..///ur   c                t    ||k    ||k    k    r)||z
  ||z
  z  }|||z
  z  |z   }t          ||k               S dS )z
        Check if a horizontal ray to the right from point (x_point, y_point) crosses the segment.

        The segment is defined by (x0, y0) to (x1, y1).
        F)rO   )	r3   rI   rJ   rK   rL   rM   rN   slopex_intersects	            r   _ray_crosses_segmentzPolygon._ray_crosses_segmentZ   sT     Lb7l++"Wb)E7R<025K+-...ur   c           
        |\  }}| j         dddf         | j         dddf         }}| j        dddf         | j        dddf         }}t          |          }t          |          D ]7}	|                     ||||	         ||	         ||	         ||	                   r dS 8d}
t          |          D ]9}	|                     ||||	         ||	         ||	         ||	                   r|
dz  }
:|
dz  dk    S )z
        Check if a point is inside the polygon.

        Uses ray casting algorithm and checks boundary points consistently.
        Nr   r"   T   )r)   r*   lenrangerV   rZ   )r3   r>   point_xpoint_ystart_xstart_ystop_xstop_ysegment_counti	crossingss              r   rD   zPolygon.insiden   s7    !:aaad+TZ1-=111a4$)AAAqD/G}%% 		 		A((

q	q	   tt 	}%% 		 		A((

q	q	   Q	1}!!r   N)r   r   r   r   )r>   r	   r   r?   )rI   r?   rJ   r?   rK   r?   rL   r?   rM   r?   rN   r?   r   rO   )r>   r	   r   rO   )	__name__
__module____qualname____doc__r=   rH   rV   rZ   rD   r   r   r   r   r      s         / / / /4/ / / /   .   ("" "" "" "" "" ""r   r   c                  :    e Zd ZdZdd
ZddZddZddZddZdS )Cella  
    A square in a mesh covering the :class:`~.Polygon` passed as an argument.

    Parameters
    ----------
    c
        Center coordinates of the Cell.
    h
        Half-Size of the Cell.
    polygon
        :class:`~.Polygon` object for which the distance is computed.
    cr	   hr?   polygonr   r   r   c                    t          j        |          | _        || _        |                    | j                  | _        | j        | j        t          j        d          z  z   | _        d S Nr\   )r   r   rn   ro   rH   rG   sqrtp)r3   rn   ro   rp   s       r   r=   zCell.__init__   sP    *Q--))$&11$&271::--r   otherrO   c                "    | j         |j         k     S NrG   r3   ru   s     r   __lt__zCell.__lt__       vr   c                "    | j         |j         k    S rw   rx   ry   s     r   __gt__zCell.__gt__   r{   r   c                "    | j         |j         k    S rw   rx   ry   s     r   __le__zCell.__le__       v  r   c                "    | j         |j         k    S rw   rx   ry   s     r   __ge__zCell.__ge__   r   r   N)rn   r	   ro   r?   rp   r   r   r   )ru   rm   r   rO   )	rh   ri   rj   rk   r=   rz   r}   r   r   r   r   r   rm   rm      s         . . . .              ! ! ! !! ! ! ! ! !r   rm   {Gz?r   Sequence[Point3DLike_Array]	precisionr?   r   c                p   d | D             }t          |          }t          j        |j        d          }t          j        |j        d          }||z
  }t          j        |          }|dz  }t                      }	t          j        t          j        |d         |d         |          t          j        |d         |d         |                    \  }
}t          j        |
	                                |	                                g          j
        D ])}|	                    t          ||z   ||                     *t          |j        d|          }t          ||dz  z   d|          }|j        |j        k    r|}t          j        ddgddgddgddgg          }|	                                s|	                                }||k    r|}|j        |j        z
  |k    r|j        dz  }|j        ||z  z   }|	                    t          |d         ||                     |	                    t          |d         ||                     |	                    t          |d         ||                     |	                    t          |d         ||                     |	                                |S )	a  
    Finds the pole of inaccessibility (the point that is farthest from the edges of the polygon)
    using an iterative grid-based approach.

    Parameters
    ----------
    rings
        A list of lists, where each list is a sequence of points representing the rings of the polygon.
        Typically, multiple rings indicate holes in the polygon.
    precision
        The precision of the result (default is 0.01).

    Returns
    -------
    Cell
        A Cell containing the pole of inaccessibility to a given precision.
    c                N    g | ]"}t          j        |          d d d df         #S rr   r   r   s     r   r   zpolylabel.<locals>.<listcomp>   s3    $O$O$ORZ%5%5aaa!e%<$O$O$Or   r   r    g       @r"   r\   r#      )r   r   rB   r'   rQ   r   meshgridarangevstackravelTputrm   r2   rG   emptygetrt   ro   rn   )r   r   r4   rp   minsmaxsdimssro   queuexvyvcornerbestbbox
directionscelloffsetss                     r   	polylabelr      s|   & %P$O$O$O$OHhG 6'-a(((D6'-a(((D$;D
tA	CA "/E[47DGQ7747DQRGUV9W9WXXFB)RXXZZ4557 0 0		$vz1g..////  !W--Dq!1g..Dv B8aWr1g1v>??Jkkmm 4yy{{$;;D6DF?Y&&AfzA~-GIId71:q'22333IId71:q'22333IId71:q'22333IId71:q'22333 kkmm 4 Kr   )r   )r   r   r   r?   r   rm   )
__future__r   r   r   typingr   numpyr   collections.abcr   manim.typingr   r   r	   r
   r   r   rm   r   r   r   r   <module>r      s$   " " " " " "                       	((((((             {" {" {" {" {" {" {" {"|! ! ! ! ! ! ! !B7 7 7 7 7 7 7r   