o
    OZhH                     @   s$  d Z ddlmZ ddlmZmZ dd Zdd Zedg d	Zed
ddgZ	dd Z
d@ddZdd Zdd Zdd Zdd ZdZdZdZdZdd ZdAd!d"Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd@d1d2ZdBd3d4Z		 	 dCd5d6ZdBd7d8Z d@d9d:Z!d;d< Z"d=d> Z#d?S )DzPygame Drawing algorithms written in Python. (Work in Progress)

Implement Pygame's Drawing Algorithms in a Python version for testing
and debugging.
    )
namedtuple)floorceilc                 C   s   | t |  S )zreturn fractional part of xr   value r   =/var/www/auris/lib/python3.10/site-packages/pygame/draw_py.pyfrac   s   r
   c                 C   s   d| t |   S )z#return inverse fractional part of x   r   r   r   r   r	   inv_frac   s   r   BoundingBox)lefttoprightbottomPointxyc                 C   s   |  ||f| dS )z%Set the color of a pixel in a surfaceN)set_at)surfin_xin_ycolorr   r   r	   r   "      r   Tc                    sX   z|r|  |nd}W n
 ty   Y dS w t fddt||D }| || dS )z-draw one blended pixel with given brightness.)r   r   r   r   Nc                 3   s(    | ]\}} | d   |  V  qdS )r   Nr   ).0colZpixbrightr   r	   	<genexpr>-   s    
zdraw_pixel.<locals>.<genexpr>)Zget_at
IndexErrortuplezipr   )r   posr   r   blendZ	other_col	new_colorr   r   r	   
draw_pixel'   s   r&   c                 C   s`   ||kr|  ||f| d S ||kr||fn||f\}}t||d D ]
}|  ||f| q#d S Nr   r   range)r   r   x_fromr   x_tostartendline_xr   r   r	   _drawhorzline4      r/   c                 C   s`   ||kr|  ||f| d S ||kr||fn||f\}}t||d D ]
}|  ||f| q#d S r'   r(   )r   r   r   y_fromy_tor,   r-   line_yr   r   r	   _drawvertline>   r0   r4   c                 C   sz   |   }||jk s||j|j krdS t||j}t||j|j d }||jk s1||j|j kr3dS t| |||| dS )zdraw clipped horizontal line.Nr   )get_clipr   hmaxr   minwr/   )r   r   r*   r   r+   clipr   r   r	   _clip_and_draw_horizlineK   s   r;   c                 C   sz   |   }||jk s||j|j krdS t||j}t||j|j d }||jk s1||j|j kr3dS t| |||| dS )zdraw clipped vertical line.Nr   )r5   r   r9   r7   r   r8   r6   r4   )r   r   r   r1   r2   r:   r   r   r	   _clip_and_draw_vertline\   s   r<   r            c                 C   sH   | d |j k t | d |jkt  | d |jk t  | d |jkt  S )zCreturns a code that defines position with respect to a bounding boxr   r   )r   	LEFT_EDGEr   
RIGHT_EDGEr   TOP_EDGEr   BOTTOM_EDGE)r#   b_boxr   r   r	   encodeu   s   rE   Fc                 C   sz  dd }dd }dd }t | tsJ | \}}}}	|rtnt}
	 t||f|}t||	f|}|||r?||||	f| dd< dS |||rFd	S ||rY||}}|	|}}	||}}||krg|	| t||  nd
}|t@ r|||
|j| | 7 }|j}n@|t@ r||
|j| | 7 }|j}n-|t	@ r||kr||
|j
| | 7 }|j
}n|t@ r||kr||
|j| | 7 }|j}q )a  Algorithm to calculate the clipped line.

    We calculate the coordinates of the part of the line segment within the
    bounding box (defined by left, top, right, bottom). The we write
    the coordinates of the line segment into "line", much like the C-algorithm.
    With `use_float` True, clip_line is usable for float-clipping.

    Returns: true if the line segment cuts the bounding box (false otherwise)
    c                 S   s   |  S Nr   )coder   r   r	   inside   s   zclip_line.<locals>.insidec                 S   s
   | p| S rF   r   Zcode_aZcode_br   r   r	   accept   s   
zclip_line.<locals>.acceptc                 S   s   | o|S rF   r   rI   r   r   r	   reject   s   zclip_line.<locals>.rejectTNFg      ?)
isinstancelistfloatintrE   r@   r   rA   r   rC   r   rB   r   )linerD   	use_floatrH   rJ   rK   x_1y_1x_2y_2Zdtypecode1code2sloper   r   r	   	clip_line   sD   




 rY   c           
      C   sh  |j |j krtt|j|j |j |j   }d}|dk rf|j |j k r4|j |j |_ |_ |j|j|_|_|j}|j|jk r?dnd}t|j |j d D ]}t| ||| ||7 }|dkrc||7 }|d8 }qJdS |j|jkr~|j |j |_ |_ |j|j|_|_|j }d| }|j |j k rdnd}	t|j|jd D ]}t| ||| ||7 }|dkr||	7 }|d8 }qdS )z3draw a non-horizontal line (without anti-aliasing).g        r   g      ?N)r   
ValueErrorabsr   r)   r   )
r   r   r,   r-   rX   errorr3   Zdy_signr.   Zdx_signr   r   r	   
_draw_line   s@   
r^   c           	         s   |j |j  }|j|j }|dkr#|dkr#tt|j t|j dS |j |j ks/|j|jkrG|j |j |_ |_ |j|j|_|_| }| }t|t|kre|| } fdd}t||||| dS || } fdd}t||||| dS )a  draw an anti-aliased line.

    The algorithm yields identical results with _draw_line for horizontal,
    vertical or diagonal lines, and results changes smoothly when changing
    any of the endpoint coordinates.

    Note that this yields strange results for very short lines, eg
    a line from (0, 0) to (0, 1) will draw 2 pixels, and a line from
    (0, 0) to (0, 1.1) will blend 10 % on the pixel (0, 2).
    r   Nc                    sH   t |}t| |f|t|   t| |d f|t|   d S r'   r   r&   r   r
   )r   Zfloat_yfactorZflr_yr$   r   r   r   r	   draw_two_pixel     $z$_draw_aaline.<locals>.draw_two_pixelc                    sH   t | }t||f|t|    t|d |f|t|    d S r'   r_   )Zfloat_xr   r`   Zfl_xra   r   r	   rb   %  rc   )r   r   r   rO   r\   _draw_aaline_dx_draw_aaline_dy)	r   r   r,   r-   r$   d_xd_yrX   rb   r   ra   r	   _draw_aaline   s"   rh   c                 C   s   t |j}|j||j |  }|j|k r"||| t|jt|j t|j}t |j}|dkrB|j|| d |   }	||	|| n|d7 }t||D ]}
|||
|   }|||
d qKd S Nr   r   )r   r   r   r   r   r
   r)   )rg   rX   r-   r,   rb   g_yg_xrests_ys_xr3   r.   r   r   r	   re   -  s   



re   c                 C   s   t |j}|j||j |  }|j|k r"|t|j|| t|j t|j}t |j}|dkrB|j|| d |   }	|||	| n|d7 }t||D ]}
|||
|   }||
|d qKd S ri   )r   r   r   r   r   r
   r)   )rf   rX   r-   r,   rb   rk   rj   rl   rn   rm   r.   r3   r   r   r	   rd   A  s   



rd   c                 C   s   t |t|j|j|j|j d |j|j d sdS |d |d kr2t| ||d |d |d  dS |d |d krJt| ||d |d |d  dS t| |t	|d |d t	|d |d  dS )znclip the line into the rectangle and draw if needed.

    Returns true if anything has been drawn, else false.r   r      r=   )
rY   r   r   r   r9   r6   r/   r4   r^   r   )r   rectr   Zptsr   r   r	   _clip_and_draw_line_  s   **rq   c                 C   s  d }}t |d |d  t |d |d  krd}nd}|d d  }t| |||r3d}|d d  }	nd}g d}	td|d d D ]}
|d ||
  |d< |d ||
  |d< |d ||
  |d< |d ||
  |d< t| |||rd}t|d |	d |	d< t|d |	d |	d< t|d |	d |	d< t|d |	d |	d< |
d |k r|d ||
  |d< |d ||
  |d< |d ||
  |d< |d ||
  |d< t| |||rd}t|d |	d |	d< t|d |	d |	d< t|d |	d |	d< t|d |	d |	d< qB|S )Nr   r=   r   ro   )'  rr   rs   )r\   rq   r)   r8   r7   )r   rp   r   rP   widthZyincZxincZnewptsZanydrawnframeloopr   r   r	   _clip_and_draw_line_widtht  sD   (rw   c                 C   sh   t |t|jd |jd |j|j |j|j ddsdS t| |t|d |d t|d |d | dS )-draw anti-aliased line between two endpoints.r   T)rQ   Nr   r=   ro   )rY   r   r   r   r9   r6   rh   r   )r   rp   r   rP   r$   r   r   r	   _clip_and_draw_aaline  s   (,ry   c                 C   0   |d |d |d |d g}t | |  |||S rx   r   r   )ry   r5   )r   r   
from_pointto_pointr$   rP   r   r   r	   draw_aaline     r~   c                 C   rz   r{   )rw   r5   )r   r   r|   r}   rt   rP   r   r   r	   	draw_line  r   r   c                 C   s  t |dkrtdgd }dd |D }dd |D }	|d |d< |	d |d< t|d |d |	d |	d d}
|dd	 D ] \}}t|
j||
_t|
j||
_t|
j||
_t|
j||
_q=| 	 }t
dt |D ]1}||d  |d< |	|d  |d< || |d< |	| |d
< |rt| |||| qit| |||| qi|r|t |d  |d< |	t |d  |d< |d |d< |	d |d
< |rt| |||| d	S t| |||| d	S d	S )z/draw several lines, either anti-aliased or not.r=   r   r>   c                 S      g | ]}|d  qS )r   r   r   ptr   r   r	   
<listcomp>      z _multi_lines.<locals>.<listcomp>c                 S   r   r   r   r   r   r   r	   r     r   r   )r   r   r   r   Nro   )len	TypeErrorr   r8   r   r7   r   r   r   r5   r)   ry   rw   )r   r   closedpointsrt   r$   aalinerP   ZxlistZylistrD   r.   r3   rp   rv   r   r   r	   _multi_lines  s>   
 r   c                 C      t | ||||ddS )z0draw several lines connected through the points.F)r   r   )r   r   r   r   rt   r   r   r	   
draw_lines  r   r   c                 C   r   )z=draw several anti-aliased lines connected through the points.T)r$   r   r   )r   r   r   r   r$   r   r   r	   draw_aalines  r   r   c              
   C   sT  |rt | |d|| dS t|}dd |D }dd |D }t|}t|}||kr<t|}	t|}
t| ||	||
 dS t||d D ]0}g }t|D ]
}t||||| qK|  tdt|dD ]}t| ||| |||d   qbqCt|D ]/}|r|d n|d }|||   k r||   kr|k rn qxt| ||| || ||  qxdS )zDraw a polygonr   Nc                 S   s   g | ]\}}|qS r   r   r   r   r   r   r   r	   r     r   z draw_polygon.<locals>.<listcomp>c                 S   s   g | ]\}}|qS r   r   r   r   r   r	   r     r   r   r=   )r   r   r8   r7   r;   r)   _draw_polygon_inner_loopsort)Zsurfacer   r   rt   Z
num_pointspoint_xpoint_yZminyZmaxyZminxZmaxxy_coordx_intersectii_prevr   r   r	   draw_polygon  s>   *r   c           
      C   s   | r| d nt |d }|| }||  }||k r!|| }||  }	n||kr6|| }||  }|| }	||  }nd S ||  krB|ksMn |t|kr`||krb||| |	|  ||  |  d S d S d S r'   )r   r7   append)
indexr   r   r   r   r   rS   rU   rR   rT   r   r   r	   r      s   

*&r   N)T)Fr   )r   FF)$__doc__collectionsr   mathr   r   r
   r   r   r   r   r&   r/   r4   r;   r<   r@   rA   rC   rB   rE   rY   r^   rh   re   rd   rq   rw   ry   r~   r   r   r   r   r   r   r   r   r   r	   <module>   sH    


>4:)



3
(