<s> from math import sqrt <EOL> from . util import format_number <EOL> class Vector2 ( object ) : <EOL> __slots__ = ( '<STR_LIT>' , ) <EOL> _gameobjects_vector = <NUM_LIT:2> <EOL> def __init__ ( self , x = <NUM_LIT:0.> , y = <NUM_LIT:0.> ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( x , "<STR_LIT>" ) : <EOL> x , y = x <EOL> self . _v = [ float ( x ) , float ( y ) ] <EOL> else : <EOL> self . _v = [ float ( x ) , float ( y ) ] <EOL> def _get_length ( self ) : <EOL> x , y = self . _v <EOL> return sqrt ( x * x + y * y ) <EOL> def _set_length ( self , length ) : <EOL> v = self . _v <EOL> try : <EOL> x , y = v <EOL> l = length / sqrt ( x * x + y * y ) <EOL> except ZeroDivisionError : <EOL> v [ <NUM_LIT:0> ] = <NUM_LIT:0.0> <EOL> v [ <NUM_LIT:1> ] = <NUM_LIT:0.0> <EOL> return self <EOL> v [ <NUM_LIT:0> ] *= l <EOL> v [ <NUM_LIT:1> ] *= l <EOL> length = property ( _get_length , _set_length , None , "<STR_LIT>" ) <EOL> @ classmethod <EOL> def from_floats ( cls , x , y ) : <EOL> vec = cls . __new__ ( cls , object ) <EOL> vec . _v = [ x , y ] <EOL> return vec <EOL> @ classmethod <EOL> def from_iter ( cls , iterable ) : <EOL> """<STR_LIT>""" <EOL> next = iter ( iterable ) . __next__ <EOL> vec = cls . __new__ ( cls , object ) <EOL> vec . _v = [ float ( next ( ) ) , float ( next ( ) ) ] <EOL> return vec <EOL> @ classmethod <EOL> def from_points ( cls , p1 , p2 ) : <EOL> """<STR_LIT>""" <EOL> v = cls . __new__ ( cls , object ) <EOL> x , y = p1 <EOL> xx , yy = p2 <EOL> v . _v = [ float ( xx - x ) , float ( yy - y ) ] <EOL> return v <EOL> @ classmethod <EOL> def _from_float_sequence ( cls , sequence ) : <EOL> v = cls . __new__ ( cls , object ) <EOL> v . _v = list ( sequence [ : <NUM_LIT:2> ] ) <EOL> return v <EOL> def copy ( self ) : <EOL> """<STR_LIT>""" <EOL> vec = self . __new__ ( self . __class__ , object ) <EOL> vec . _v = self . _v [ : ] <EOL> return vec <EOL> def get_x ( self ) : <EOL> return self . _v [ <NUM_LIT:0> ] <EOL> def set_x ( self , x ) : <EOL> try : <EOL> self . _v [ <NUM_LIT:0> ] = <NUM_LIT:1.0> * x <EOL> except : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> x = property ( get_x , set_x , None , "<STR_LIT>" ) <EOL> def get_y ( self ) : <EOL> return self . _v [ <NUM_LIT:1> ] <EOL> def set_y ( self , y ) : <EOL> try : <EOL> self . _v [ <NUM_LIT:1> ] = <NUM_LIT:1.0> * y <EOL> except : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> y = property ( get_y , set_y , None , "<STR_LIT>" ) <EOL> def __str__ ( self ) : <EOL> x , y = self . _v <EOL> return "<STR_LIT>" % ( format_number ( x ) , format_number ( y ) ) <EOL> def __repr__ ( self ) : <EOL> x , y = self . _v <EOL> return "<STR_LIT>" % ( x , y ) <EOL> def __iter__ ( self ) : <EOL> return iter ( self . _v [ : ] ) <EOL> def __len__ ( self ) : <EOL> return <NUM_LIT:2> <EOL> def __getitem__ ( self , index ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . _v [ index ] <EOL> except IndexError : <EOL> raise IndexError ( "<STR_LIT>" ) <EOL> def __setitem__ ( self , index , value ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . _v [ index ] = <NUM_LIT:1.0> * value <EOL> except IndexError : <EOL> raise IndexError ( "<STR_LIT>" ) <EOL> except TypeError : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> def __eq__ ( self , rhs ) : <EOL> x , y = self . _v <EOL> xx , yy = rhs <EOL> return x == xx and y == yy <EOL> def __ne__ ( self , rhs ) : <EOL> x , y = self . _v <EOL> xx , yy , = rhs <EOL> return x != xx or y != yy <EOL> def __hash__ ( self ) : <EOL> return hash ( self . _v ) <EOL> def __add__ ( self , rhs ) : <EOL> x , y = self . _v <EOL> xx , yy = rhs <EOL> return Vector2 . from_floats ( x + xx , y + yy ) <EOL> def __iadd__ ( self , rhs ) : <EOL> xx , yy = rhs <EOL> v = self . _v <EOL> v [ <NUM_LIT:0> ] += xx <EOL> v [ <NUM_LIT:1> ] += yy <EOL> return self <EOL> def __radd__ ( self , lhs ) : <EOL> x , y = self . _v <EOL> xx , yy = lhs <EOL> return self . from_floats ( x + xx , y + yy ) <EOL> def __sub__ ( self , rhs ) : <EOL> x , y = self . _v <EOL> xx , yy = rhs <EOL> return Vector2 . from_floats ( x - xx , y - yy ) <EOL> def __rsub__ ( self , lhs ) : <EOL> x , y = self . _v <EOL> xx , yy = lhs <EOL> return self . from_floats ( xx - x , yy - y ) <EOL> def _isub__ ( self , rhs ) : <EOL> xx , yy = rhs <EOL> v = self . _v <EOL> v [ <NUM_LIT:0> ] -= xx <EOL> v [ <NUM_LIT:1> ] -= yy <EOL> return self <EOL> def __mul__ ( self , rhs ) : <EOL> """<STR_LIT>""" <EOL> x , y = self . _v <EOL> if hasattr ( rhs , "<STR_LIT>" ) : <EOL> xx , yy = rhs <EOL> return Vector2 . from_floats ( x * xx , y * yy ) <EOL> else : <EOL> return Vector2 . from_floats ( x * rhs , y * rhs ) <EOL> def __imul__ ( self , rhs ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( rhs , "<STR_LIT>" ) : <EOL> xx , yy = rhs <EOL> v = self . _v <EOL> v [ <NUM_LIT:0> ] *= xx <EOL> v [ <NUM_LIT:1> ] *= yy <EOL> else : <EOL> v = self . _v <EOL> v [ <NUM_LIT:0> ] *= rhs <EOL> v [ <NUM_LIT:1> ] *= rhs <EOL> return self <EOL> def __rmul__ ( self , lhs ) : <EOL> x , y = self . _v <EOL> if hasattr ( lhs , "<STR_LIT>" ) : <EOL> xx , yy = lhs <EOL> else : <EOL> xx = lhs <EOL> yy = lhs <EOL> return self . from_floats ( x * xx , y * yy ) <EOL> def __div__ ( self , rhs ) : <EOL> """<STR_LIT>""" <EOL> x , y = self . _v <EOL> if hasattr ( rhs , "<STR_LIT>" ) : <EOL> xx , yy , = rhs <EOL> return Vector2 . from_floats ( x / xx , y / yy ) <EOL> else : <EOL> return Vector2 . from_floats ( x / rhs , y / rhs ) <EOL> def __idiv__ ( self , rhs ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( rhs , "<STR_LIT>" ) : <EOL> xx , yy = rhs <EOL> v = self . _v <EOL> v [ <NUM_LIT:0> ] /= xx <EOL> v [ <NUM_LIT:1> ] /= yy <EOL> else : <EOL> v = self . _v <EOL> v [ <NUM_LIT:0> ] /= rhs <EOL> v [ <NUM_LIT:1> ] /= rhs <EOL> return self <EOL> def __rdiv__ ( self , lhs ) : <EOL> x , y = self . _v <EOL> if hasattr ( lhs , "<STR_LIT>" ) : <EOL> xx , yy = lhs <EOL> else : <EOL> xx = lhs <EOL> yy = lhs <EOL> return self . from_floats ( xx / x , yy / x ) <EOL> def __neg__ ( self ) : <EOL> """<STR_LIT>""" <EOL> x , y = self . _v <EOL> return Vector2 . from_floats ( - x , - y ) <EOL> def __pos__ ( self ) : <EOL> return self . copy ( ) <EOL> def __bool__ ( self ) : <EOL> x , y = self . _v <EOL> return bool ( x or y ) <EOL> def __call__ ( self , keys ) : <EOL> """<STR_LIT>""" <EOL> ord_x = ord ( '<STR_LIT:x>' ) <EOL> v = self . _v <EOL> return tuple ( v [ ord ( c ) - ord_x ] for c in keys ) <EOL> def as_tuple ( self ) : <EOL> """<STR_LIT>""" <EOL> return tuple ( self . _v ) <EOL> def get_length ( self ) : <EOL> """<STR_LIT>""" <EOL> x , y = self . _v <EOL> return sqrt ( x * x + y * y ) <EOL> get_magnitude = get_length <EOL> def normalise ( self ) : <EOL> """<STR_LIT>""" <EOL> v = self . _v <EOL> x , y = v <EOL> l = sqrt ( x * x + y * y ) <EOL> try : <EOL> v [ <NUM_LIT:0> ] /= l <EOL> v [ <NUM_LIT:1> ] /= l <EOL> except ZeroDivisionError : <EOL> v [ <NUM_LIT:0> ] = <NUM_LIT:0.> <EOL> v [ <NUM_LIT:1> ] = <NUM_LIT:0.> <EOL> return self <EOL> normalize = normalise <EOL> def get_normalised ( self ) : <EOL> x , y = self . _v <EOL> l = sqrt ( x * x + y * y ) <EOL> return Vector2 . from_floats ( x / l , y / l ) <EOL> get_normalized = get_normalised <EOL> def get_distance_to ( self , p ) : <EOL> """<STR_LIT>""" <EOL> x , y = self . _v <EOL> xx , yy = p <EOL> dx = xx - x <EOL> dy = yy - y <EOL> return sqrt ( dx * dx + dy * dy ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> v1 = Vector2 ( <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> print ( v1 ( '<STR_LIT>' ) ) <EOL> print ( Vector2 . from_points ( ( <NUM_LIT:5> , <NUM_LIT:5> ) , ( <NUM_LIT:10> , <NUM_LIT:10> ) ) ) </s>
