def convert_vector(value): return Vector.from_string(value) def convert_color(value): return list(map(int, value.split(',')))
class AABox(object): ''' Axis-Aligned Box. Records the lower left corner and width/height ''' def __init__(self, x=0, y=0, w=1, h=1): self.position = Vector(x,y) self.size = Vector(w,h) def copy(self): ''' Returns a new AABox with the same contents as this one ''' return AABox(self.x, self.y, self.width, self.height) def contains(self, x, y): ''' Checks if the point is contained in the rectangle ''' return ( self.x <= x <= self.x + self.width and self.y <= y <= self.y + self.height ) def contains_point(self, vector): ''' Functions as the contains function, but unrolls the vector ''' return self.contains(vector.x, vector.y) @property def expansion(self): ''' Expands the coordinates of the rectangle to a format that works for a vertex list in absolute coordinates ''' return ( self.x, self.y, self.x, self.y + self.height, self.x + self.width, self.y + self.height, self.x + self.width, self.y ) @property def relative_expansion(self): return (0,0,0,self.height,self.width,self.height,self.width,0) # VECTOR PROPERTIES @property def position(self): return self._position @position.setter def position(self, other): if not isinstance(other, Vector): raise TypeError('Position must be a Vector') self._position = other @property def size(self): return self._size @size.setter def size(self, other): if not isinstance(other, Vector): raise TypeError('Size must be a Vector') if other.x < 0 or other.y < 0: raise TypeError('Size must have positive coordinates') self._size = other # COMPONENT PROPERTIES @property def x(self): return self.position.x @x.setter def x(self, other): self._position.x = other @property def y(self): return self.position.y @y.setter def y(self, other): self._position.y = other @property def width(self): return self.size.x @width.setter def width(self, other): tmp_size = self.size.copy() tmp_size.x = other self.size = tmp_size @property def height(self): return self.size.y @height.setter def height(self, other): tmp_size = self.size.copy() tmp_size.y = other self.size = tmp_size # INFO METHOD def __str__(self): return '{} to {}'.format(str(self.position), str(self.size)) def __repr__(self): return 'AABox({})'.format(str(self))
def test_vector_copy(self): for x, y in TestVector.values: vector = Vector(x, y) vector2 = vector.copy() self.assertEqual(vector, vector2)
def __init__(self, x=0, y=0, w=1, h=1): self.position = Vector(x,y) self.size = Vector(w,h)