
XTc           @   s<   d  Z  d d l m Z d d l Z d e f d     YZ d S(   sM   A vector is a class that behaves like a 2-tuple, but with convenient
methods.i(   t   divisionNt   Vec2Dc           B   s4  e  Z d  Z d d g Z d   Z d   Z d   Z d   Z d   Z d   Z	 d	   Z
 e
 Z d
   Z e Z d   Z d   Z e Z e Z d   Z e Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d d  Z d   Z d   Z  d   Z! e" d    Z# d   Z$ RS(    sB  
    Vec2D is a class that behaves like a 2-tuple, but with a number
    of convenient methods for vector calculation and manipulation.
    It can be created with two arguments for x,y, or by passing a
    2-tuple.

    In addition to the methods documented below, Vec2D supports
    the following:

    >>> from spyral import Vec2D
    >>> v1 = Vec2D(1,0)
    >>> v2 = Vec2D((0,1))    # Note 2-tuple argument!

    Tuple access, or x,y attribute access

    >>> v1.x
    1
    >>> v1.y
    0
    >>> v1[0]
    1
    >>> v1[1]
    0

    Addition, subtraction, and multiplication

    >>> v1 + v2
    Vec2D(1, 1)
    >>> v1 - v2
    Vec2D(1, -1)
    >>> 3 * v1
    Vec2D(3, 0)
    >>> (3, 4) * (v1+v2)
    Vec2D(3, 4)

    Compatibility with standard tuples

    >>> v1 + (1,1)
    Vec2D(2, 1)
    >>> (1,1) + v1
    Vec2D(2, 1)

    t   xt   yc         G   sk   t  |  d k r+ | d \ |  _ |  _ n< t  |  d k r[ | d | d |  _ |  _ n t d   d  S(   Ni   i    i   s   Invalid Vec2D arguments(   t   lenR   R   t
   ValueError(   t   selft   args(    (    s1   /usr/lib/python2.7/site-packages/spyral/vector.pyt   __init__5   s
    c         C   s   d S(   Ni   (    (   R   (    (    s1   /usr/lib/python2.7/site-packages/spyral/vector.pyt   __len__=   s    c         C   s@   | d k r |  j  S| d k r& |  j St d t |    d  S(   Ni    i   s   Invalid subscript %s(   R   R   t
   IndexErrort   str(   R   t   key(    (    s1   /usr/lib/python2.7/site-packages/spyral/vector.pyt   __getitem__@   s
    c         C   s    d t  |  j  t  |  j  f S(   Ns   Vec2D(%s, %s)(   R   R   R   (   R   (    (    s1   /usr/lib/python2.7/site-packages/spyral/vector.pyt   __repr__G   s    c         C   sG   y( |  j  | d k o& |  j | d k SWn t t f k
 rB t SXd  S(   Ni    i   (   R   R   R
   t	   TypeErrort   False(   R   t   o(    (    s1   /usr/lib/python2.7/site-packages/spyral/vector.pyt   __eq__J   s    (c         C   s   |  j  |  S(   N(   R   (   R   R   (    (    s1   /usr/lib/python2.7/site-packages/spyral/vector.pyt   __ne__P   s    c         C   sF   y' t  |  j | d |  j | d  SWn t t f k
 rA t SXd  S(   Ni    i   (   R   R   R   R
   R   t   NotImplemented(   R   R   (    (    s1   /usr/lib/python2.7/site-packages/spyral/vector.pyt   __add__S   s    'c         C   sO   y' t  |  j | d |  j | d  SWn! t t f k
 rJ |  G| GHt SXd  S(   Ni    i   (   R   R   R   R
   R   R   (   R   R   (    (    s1   /usr/lib/python2.7/site-packages/spyral/vector.pyt   __sub__[   s
    '	c         C   sF   y' t  | d |  j | d |  j  SWn t t f k
 rA t SXd  S(   Ni    i   (   R   R   R   R
   R   R   (   R   R   (    (    s1   /usr/lib/python2.7/site-packages/spyral/vector.pyt   __rsub__d   s    'c         C   sx   y' t  |  j | d |  j | d  SWn t t f k
 r@ n Xt | t t t f  rt t  |  j | |  j |  St	 S(   Ni    i   (
   R   R   R   R
   R   t
   isinstancet   intt   longt   floatR   (   R   R   (    (    s1   /usr/lib/python2.7/site-packages/spyral/vector.pyt   __mul__j   s    'c         C   sx   y' t  |  j | d |  j | d  SWn t t f k
 r@ n Xt | t t t f  rt t  |  j | |  j |  Sd  S(   Ni    i   (	   R   R   R   R
   R   R   R   R   R   (   R   R   (    (    s1   /usr/lib/python2.7/site-packages/spyral/vector.pyt   __div__x   s    'c         C   s   |  j  |  j f S(   N(   R   R   (   R   (    (    s1   /usr/lib/python2.7/site-packages/spyral/vector.pyt   __neg__   s    c         C   s   |  S(   N(    (   R   (    (    s1   /usr/lib/python2.7/site-packages/spyral/vector.pyt   __pos__   s    c         C   s%   t  j |  j |  j |  j |  j  S(   sJ   
        Return the length of this vector.

        :rtype: float
        (   t   matht   sqrtR   R   (   R   (    (    s1   /usr/lib/python2.7/site-packages/spyral/vector.pyt
   get_length   s    c         C   s   |  j  |  j  |  j |  j S(   sP   
        Return the squared length of this vector.

        :rtype: int
        (   R   R   (   R   (    (    s1   /usr/lib/python2.7/site-packages/spyral/vector.pyt   get_length_squared   s    c         C   s   t  j |  j |  j  S(   se   
        Return the angle this vector makes with the positive x axis.

        :rtype: float
        (   R    t   atan2R   R   (   R   (    (    s1   /usr/lib/python2.7/site-packages/spyral/vector.pyt	   get_angle   s    c         C   s   t  |  j |  j  S(   s   
        Returns a new :class:`Vec2D <spyral.Vec2D>` perpendicular to this one.

        :rtype: :class:`Vec2D <spyral.Vec2D>`
        (   R   R   R   (   R   (    (    s1   /usr/lib/python2.7/site-packages/spyral/vector.pyt   perpendicular   s    c         C   s   |  j  | d |  j | d S(   s   
        Returns the `dot product <http://en.wikipedia.org/wiki/Dot_product>`_
        of this point with another.

        :param other: the other point
        :type other: 2-tuple or :class:`Vec2D <spyral.Vec2D>`
        :rtype: int
        i    i   (   R   R   (   R   t   other(    (    s1   /usr/lib/python2.7/site-packages/spyral/vector.pyt   dot   s    	c         C   s   | |  j    S(   s   
        Returns the distance from this :class:`Vec2D <spyral.Vec2D>` to the
        other point.

        :param other: the other point
        :type other: 2-tuple or :class:`Vec2D <spyral.Vec2D>`
        :rtype: float
        (   R"   (   R   R'   (    (    s1   /usr/lib/python2.7/site-packages/spyral/vector.pyt   distance   s    	c         C   sP   |  j  | d |  j | d } |  j  | d |  j | d } t j | |  S(   s   
        Returns the angle between this point and another point.

        :param other: the other point
        :type other: 2-tuple or :class:`Vec2D <spyral.Vec2D>`
        :rtype: float
        i   i    (   R   R   R    R$   (   R   R'   R   t   d(    (    s1   /usr/lib/python2.7/site-packages/spyral/vector.pyt   angle   s      c         C   sZ   t  |  } t | j | j | j | j  } |  j | j |  j | j } | | | S(   s!  
        Returns the
        `projection <http://en.wikipedia.org/wiki/Vector_projection>`_
        of this :class:`Vec2D <spyral.Vec2D>` onto another point.

        :param other: the other point
        :type other: 2-tuple or :class:`Vec2D <spyral.Vec2D>`
        :rtype: float
        (   R   R   R   R   (   R   R'   t   l2R*   (    (    s1   /usr/lib/python2.7/site-packages/spyral/vector.pyt
   projection   s    
$i    c         C   si   |  | } t  j |  } t  j |  } | j | | j | } | j | | j | } t | |  | S(   sT  
        Returns a new vector from the old point rotated by `angle` radians about
        the optional `center`.

        :param angle: angle in radians.
        :type angle: float
        :param center: an optional center
        :type center: 2-tuple or :class:`Vec2D <spyral.Vec2D>`
        :rtype: :class:`Vec2D <spyral.Vec2D>`
        (   R    t   cost   sinR   R   R   (   R   R+   t   centert   pt   ct   sR   R   (    (    s1   /usr/lib/python2.7/site-packages/spyral/vector.pyt   rotated   s    
c         C   s=   |  j    } |  j    d k r" d St |  j | |  j |  S(   s   
        Returns a new vector based on this one, normalized to length 1. That is,
        it keeps the same angle, but its length is now 1.

        :rtype: :class:`Vec2D <spyral.Vec2D>`
        i    N(   R"   t   NoneR   R   R   (   R   t   l(    (    s1   /usr/lib/python2.7/site-packages/spyral/vector.pyt
   normalized   s    c         C   s(   t  |  j  |  _ t  |  j  |  _ |  S(   s   
        Converts the components of this vector into ints, discarding anything
        past the decimal place.

        :returns: this :class:`Vec2D <spyral.Vec2D>`
        (   R   R   R   (   R   (    (    s1   /usr/lib/python2.7/site-packages/spyral/vector.pyt   floor   s    c         C   s   t  |  j   |  j    S(   s   
        Returns `Vec2D(radius, theta)` for this vector, where `radius` is the
        length and `theta` is the angle.

        :rtype: :class:`Vec2D <spyral.Vec2D>`
        (   R   R"   R%   (   R   (    (    s1   /usr/lib/python2.7/site-packages/spyral/vector.pyt   to_polar  s    c          G   s?   t  |    } t  | j t j | j  | j t j | j   S(   s   
        Takes in radius, theta or (radius, theta) and returns rectangular
        :class:`Vec2D <spyral.Vec2D>`.

        :rtype: :class:`Vec2D <spyral.Vec2D>`
        (   R   R   R    R.   R   R/   (   R   t   v(    (    s1   /usr/lib/python2.7/site-packages/spyral/vector.pyt
   from_polar  s    c         C   s   |  j  |  j S(   N(   R   R   (   R   (    (    s1   /usr/lib/python2.7/site-packages/spyral/vector.pyt   __hash__  s    (   i    i    (%   t   __name__t
   __module__t   __doc__t	   __slots__R   R	   R   R   R   R   R   t   __radd__R   t   __isub__R   R   t   __rmul__t   __imul__R   t   __truediv__R   R   R"   R#   R%   R&   R(   R)   R+   R-   R4   R7   R8   R9   t   staticmethodR;   R<   (    (    (    s1   /usr/lib/python2.7/site-packages/spyral/vector.pyR      sD   +																										(   R?   t
   __future__R    R    t   objectR   (    (    (    s1   /usr/lib/python2.7/site-packages/spyral/vector.pyt   <module>   s   