Coverage for /home/kale/kxgames/libraries/vecrec/vecrec/shapes : 0%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
from __future__ import division
import math import random import operator
# Utility Functions
def cast_anything_to_vector(input): if isinstance(input, Vector): return input
# If the input object is a container with two elements, use those elements # to construct a vector.
try: return Vector(*input) except: pass
# If the input object has x and y attributes, use those attributes to # construct a vector.
try: return Vector(input.x, input.y) except: pass
# If the function has returned by now, the input object could not be cast # to a vector. Throw an exception.
raise VectorCastError(input)
def cast_anything_to_rectangle(input):
# If the input object implements the shape interface, use that information # to directly return a rectangle object.
try: return cast_shape_to_rectangle(input) except: pass
# If the input object can be cast to a vector, try to represent that vector # as a rectangle. Such a rectangle is located where the vector points and # has no area (i.e. width = height = 0).
try: return Rectangle.from_vector(input) except: pass
# If the function has returned by now, the input object could not be cast # to a rectangle. Throw an exception.
raise RectangleCastError(input)
def cast_shape_to_rectangle(input):
if isinstance(input, Rectangle): return input
# If the input object implements the shape interface (i.e. has bottom, left, # width, and height attributes), use that information to construct a bona # fide rectangle object.
try: return Rectangle.from_shape(input) except: pass
raise RectangleCastError(input)
def accept_anything_as_vector(function): def decorator(self, *input): if len(input) == 1: input = input[0] vector = cast_anything_to_vector(input) return function(self, vector) return decorator
def accept_anything_as_rectangle(function): def decorator(self, input): rect = cast_anything_to_rectangle(input) return function(self, rect) return decorator
def accept_shape_as_rectangle(function): def decorator(self, input): rect = cast_shape_to_rectangle(input) return function(self, rect) return decorator
def _overload_left_side(f, scalar_ok=False): def operator(self, other): try: x, y = cast_anything_to_vector(other) except: pass else: return Vector(f(self.x, x), f(self.y, y))
# Zero is treated as a special case, because the built-in sum() # function expects to be able to add zero to things.
if (other is 0) or (scalar_ok): return Vector(f(self.x, other), f(self.y, other)) else: raise VectorCastError(other)
return operator
def _overload_right_side(f, scalar_ok=False): def operator(self, other): try: x, y = cast_anything_to_vector(other) except: pass else: return Vector(f(x, self.x), f(y, self.y))
if (other is 0) or (scalar_ok): return Vector(f(other, self.x), f(other, self.y)) else: raise VectorCastError(other)
return operator
def _overload_in_place(f, scalar_ok=False): def operator(self, other): try: x, y = cast_anything_to_vector(other) except: pass else: self.x, self.y = f(self.x, x), f(self.y, y); return self
if (other is 0) or (scalar_ok): self.x, self.y = f(self.x, other), f(self.y, other) return self else: raise VectorCastError(other)
return operator
# Geometry Functions
golden_ratio = 1/2 + math.sqrt(5) / 2
class Shape (object): """ Provide an interface for custom shape classes to interact with the rectangle class. For example, rectangles can be instantiated from shapes and can test for collisions against shapes. The interface is very simple, requiring only four methods to be redefined. """
def get_bottom(self): raise NotImplementedError
def get_left(self): raise NotImplementedError
def get_width(self): raise NotImplementedError
def get_height(self): raise NotImplementedError
# Properties (fold) bottom = property(get_bottom) left = property(get_left) width = property(get_width) height = property(get_height)
class Vector (object): """ Represents a two-dimensional vector. In particular, this class features a number of factory methods to create vectors from various inputs and a number of overloaded operators to facilitate vector arithmetic. """
@staticmethod def null(): """ Return a null vector. """ return Vector(0, 0)
@staticmethod def random(magnitude=1): """ Create a unit vector pointing in a random direction. """ theta = random.uniform(0, 2 * math.pi) return magnitude * Vector(math.cos(theta), math.sin(theta))
@staticmethod def from_radians(angle): """ Create a vector that makes the given angle with the x-axis. """ return Vector(math.cos(angle), math.sin(angle))
@staticmethod def from_degrees(angle): """ Create a vector that makes the given angle with the x-axis. """ return Vector.from_radians(angle * math.pi / 180)
@staticmethod def from_tuple(coordinates): """ Create a vector from a two element tuple. """ return Vector(*coordinates)
@staticmethod def from_scalar(scalar): """ Create a vector from a single scalar value. """ return Vector(scalar, scalar)
@staticmethod def from_rectangle(box): """ Create a vector randomly within the given rectangle. """ x = box.left + box.width * random.uniform(0, 1) y = box.bottom + box.height * random.uniform(0, 1) return Vector(x, y)
def copy(self): """ Return a copy of this vector. """ from copy import deepcopy return deepcopy(self)
@accept_anything_as_vector def assign(self, other): """ Copy the given vector into this one. """ self.x, self.y = other.tuple
def normalize(self): """ Set the magnitude of this vector to unity, in place. """ try: self /= self.magnitude except ZeroDivisionError: raise NullVectorError
def scale(self, magnitude): """ Set the magnitude of this vector in place. """ self.normalize() self *= magnitude
def interpolate(self, target, extent): """ Move this vector towards the given towards the target by the given extent. The extent should be between 0 and 1. """ target = cast_anything_to_vector(target) self += extent * (target - self)
@accept_anything_as_vector def project(self, axis): """ Project this vector onto the given axis. """ projection = self.get_projection(axis) self.assign(projection)
@accept_anything_as_vector def dot_product(self, other): """ Return the dot product of the given vectors. """ return self.x * other.x + self.y * other.y
@accept_anything_as_vector def perp_product(self, other): """ Return the perp product of the given vectors. The perp product is just a cross product where the third dimension is taken to be zero and the result is returned as a scalar. """
return self.x * other.y - self.y * other.x
def rotate(self, angle): """ Rotate the given vector by an angle. Angle measured in radians counter-clockwise. """ x, y = self.tuple self.x = x * math.cos(angle) - y * math.sin(angle) self.y = x * math.sin(angle) + y * math.cos(angle)
def round(self, digits): """ Round the elements of the given vector to the given number of digits. """ # Meant as a way to clean up Vector.rotate() # For example: # V = Vector(1,0) # V.rotate(2*pi) # # V is now <1.0, -2.4492935982947064e-16>, when it should be # <1,0>. V.round(15) will correct the error in this example.
x, y = self.tuple self.x = round(x, digits) self.y = round(y, digits)
def __init__(self, x, y): """ Construct a vector using the given coordinates. """ self.x = x self.y = y
def __repr__(self): """ Return a string representation of this vector. """ return "Vector(%f, %f)" % self.get_tuple()
def __str__(self): """ Return a string representation of this vector. """ return "<%.2f, %.2f>" % self.get_tuple()
def __iter__(self): """ Iterate over this vectors coordinates. """ yield self.x; yield self.y
def __bool__(self): """ Return true is the vector is not degenerate. """ return self != (0, 0)
__nonzero__ = __bool__
def __getitem__(self, i): """ Return the specified coordinate. """ return self.tuple[i]
@accept_anything_as_vector def __eq__(self, other): return self.x == other.x and self.y == other.y
@accept_anything_as_vector def __ne__(self, other): return self.x != other.x or self.y != other.y
def __neg__(self): """ Return a copy of this vector with the signs flipped. """ return Vector(-self.x, -self.y)
def __abs__(self): """ Return the absolute value of this vector. """ return Vector(abs(self.x), abs(self.y))
# Binary Operators (fold) __add__ = _overload_left_side(operator.add) __radd__ = _overload_right_side(operator.add) __iadd__ = _overload_in_place(operator.add)
__sub__ = _overload_left_side(operator.sub) __rsub__ = _overload_right_side(operator.sub) __isub__ = _overload_in_place(operator.sub)
__mul__ = _overload_left_side(operator.mul, scalar_ok=True) __rmul__ = _overload_right_side(operator.mul, scalar_ok=True) __imul__ = _overload_in_place(operator.mul, scalar_ok=True)
__floordiv__ = _overload_left_side(operator.floordiv, scalar_ok=True) __rfloordiv__ = _overload_right_side(operator.floordiv, scalar_ok=True) __ifloordiv__ = _overload_in_place(operator.floordiv, scalar_ok=True)
__truediv__ = _overload_left_side(operator.truediv, scalar_ok=True) __rtruediv__ = _overload_right_side(operator.truediv, scalar_ok=True) __itruediv__ = _overload_in_place(operator.truediv, scalar_ok=True)
__div__ = __truediv__ __rdiv__ = __rtruediv__ __idiv__ = __itruediv__
__mod__ = _overload_left_side(operator.mod, scalar_ok=True) __rmod__ = _overload_right_side(operator.mod, scalar_ok=True) __imod__ = _overload_in_place(operator.mod, scalar_ok=True)
__pow__ = _overload_left_side(operator.pow, scalar_ok=True) __rpow__ = _overload_right_side(operator.pow, scalar_ok=True) __ipow__ = _overload_in_place(operator.pow, scalar_ok=True)
def get_x(self): """ Get the x coordinate of this vector. """ return self.x
def get_y(self): """ Get the y coordinate of this vector. """ return self.y
def get_tuple(self): """ Return the vector as a tuple. """ return self.x, self.y
def get_magnitude(self): """ Calculate the length of this vector. """ return math.sqrt(self.magnitude_squared)
def get_magnitude_squared(self): """ Calculate the square of the length of this vector. This is slightly more efficient that finding the real length. """ return self.x**2 + self.y**2
@accept_anything_as_vector def get_distance(self, other): """ Return the Euclidean distance between the two input vectors. """ return (other - self).magnitude
@accept_anything_as_vector def get_manhattan(self, other): """ Return the Manhattan distance between the two input vectors. """ return sum(abs(other - self))
def get_unit(self): """ Return a unit vector parallel to this one. """ result = self.copy() result.normalize() return result
def get_orthogonal(self): """ Return a vector that is orthogonal to this one. The resulting vector is not normalized. """ return Vector(-self.y, self.x)
def get_orthonormal(self): """ Return a vector that is orthogonal to this one and that has been normalized. """ return self.orthogonal.unit
def get_scaled(self, magnitude): """ Return a unit vector parallel to this one. """ result = self.copy() result.scale(magnitude) return result
def get_interpolated(self, target, extent): """ Return a new vector that has been moved towards the given target by the given extent. The extent should be between 0 and 1. """ result = self.copy() result.interpolate(target, extent) return result
@accept_anything_as_vector def get_projection(self, axis): """ Return the projection of this vector onto the given axis. The axis does not need to be normalized. """ scale = axis.dot(self) / axis.dot(axis) return axis * scale
@accept_anything_as_vector def get_components(self, other): """ Break this vector into one vector that is perpendicular to the given vector and another that is parallel to it. """ tangent = self.get_projection(other) normal = self - tangent return normal, tangent
def get_radians(self): """ Return the angle between this vector and the positive x-axis measured in radians. Result will be between -pi and pi. """ if not self: raise NullVectorError() return math.atan2(self.y, self.x)
def get_positive_radians(self): """ Return the positive angle between this vector and the positive x-axis measured in radians. """ return (2 * math.pi + self.get_radians()) % (2 * math.pi)
def get_degrees(self): """ Return the angle between this vector and the positive x-axis measured in degrees. """ return self.radians * 180 / math.pi
@accept_anything_as_vector def get_radians_to(self, other): """ Return the angle between the two given vectors in radians. If either of the inputs are null vectors, an exception is thrown. """ return other.radians - self.radians
@accept_anything_as_vector def get_degrees_to(self, other): """ Return the angle between the two given vectors in degrees. If either of the inputs are null vectors, an exception is thrown. """ return other.degrees - self.degrees
def get_rotated(self, angle): """ Return a vector rotated by angle from the given vector. Angle measured in radians counter-clockwise. """ result = self.copy() result.rotate(angle) return result
def get_rounded(self, digits): """ Return a vector with the elements rounded to the given number of digits. """ result = self.copy() result.round(digits) return result
def set_x(self, x): """ Set the x coordinate of this vector. """ self.x = x
def set_y(self, y): """ Set the y coordinate of this vector. """ self.y = y
def set_radians(self, angle): """ Set the angle that this vector makes with the x-axis. """ self.x, self.y = math.cos(angle), math.sin(angle)
def set_degrees(self, angle): """ Set the angle that this vector makes with the x-axis. """ self.set_radians(angle * math.pi / 180)
def set_tuple(self, coordinates): """ Set the x and y coordinates of this vector. """ self.x, self.y = coordinates
# Aliases (fold) dot = dot_product perp = perp_product
# Properties (fold) tuple = property(get_tuple, set_tuple)
magnitude = property(get_magnitude, scale) magnitude_squared = property(get_magnitude_squared)
unit = property(get_unit) orthogonal = property(get_orthogonal) orthonormal = property(get_orthonormal)
radians = property(get_radians, set_radians) degrees = property(get_degrees, set_degrees)
class Rectangle (Shape):
def __init__(self, left, bottom, width, height): self.__left = left self.__bottom = bottom self.__width = width self.__height = height
def __repr__(self): return "Rectangle(%d, %d, %d, %d)" % self.tuple
def __str__(self): return '<Rect bottom={} left={} width={} height={}>'.format( self.bottom, self.left, self.width, self.height)
def __eq__(self, other): return ( self.__bottom == other.__bottom and self.__left == other.__left and self.__width == other.__width and self.__height == other.__height )
@accept_anything_as_vector def __add__(self, vector): result = self.copy() result.displace(vector) return result
@accept_anything_as_vector def __iadd__(self, vector): self.displace(vector) return self
@accept_anything_as_vector def __sub__(self, vector): result = self.copy() result.displace(-vector) return result
@accept_anything_as_vector def __isub__(self, vector): self.displace(-vector) return self
def __contains__(self, other): return self.contains(other)
@staticmethod def from_size(width, height): return Rectangle(0, 0, width, height)
@staticmethod def from_width(width, ratio=1/golden_ratio): return Rectangle.from_size(width, ratio * width)
@staticmethod def from_height(height, ratio=golden_ratio): return Rectangle.from_size(ratio * height, height)
@staticmethod def from_square(size): return Rectangle.from_size(size, size)
@staticmethod def from_dimensions(left, bottom, width, height): return Rectangle(left, bottom, width, height)
@staticmethod def from_sides(left, top, right, bottom): width = right - left; height = top - bottom return Rectangle.from_dimensions(left, bottom, width, height)
@staticmethod def from_corners(first, second): first = cast_anything_to_vector(first) second = cast_anything_to_vector(second)
left = min(first.x, second.x); top = max(first.y, second.y) right = max(first.x, second.x); bottom = min(first.y, second.y)
return Rectangle.from_sides(left, top, right, bottom)
@staticmethod def from_bottom_left(position, width, height): position = cast_anything_to_vector(position) return Rectangle(position.x, position.y, width, height)
@staticmethod def from_center(position, width, height): position = cast_anything_to_vector(position) - (width/2, height/2) return Rectangle(position.x, position.y, width, height)
@staticmethod def from_vector(position): position = cast_anything_to_vector(position) return Rectangle(position.x, position.y, 0, 0)
@staticmethod def from_points(*points): left = min(cast_anything_to_vector(p).x for p in points) top = max(cast_anything_to_vector(p).y for p in points) right = max(cast_anything_to_vector(p).x for p in points) bottom = min(cast_anything_to_vector(p).y for p in points) return Rectangle.from_sides(left, top, right, bottom)
@staticmethod def from_shape(shape): bottom, left = shape.bottom, shape.left width, height = shape.width, shape.height return Rectangle(left, bottom, width, height)
@staticmethod def from_surface(surface): width, height = surface.get_size() return Rectangle.from_size(width, height)
@staticmethod def from_pyglet_window(window): return Rectangle.from_size(window.width, window.height)
@staticmethod def from_pyglet_image(image): return Rectangle.from_size(image.width, image.height)
@staticmethod def from_union(*inputs): rectangles = [cast_shape_to_rectangle(x) for x in inputs] left = min(x.left for x in rectangles) top = max(x.top for x in rectangles) right = max(x.right for x in rectangles) bottom = min(x.bottom for x in rectangles) return Rectangle.from_sides(left, top, right, bottom)
@staticmethod def from_intersection(*inputs): rectangles = [cast_shape_to_rectangle(x) for x in inputs] left = max(x.left for x in rectangles) top = min(x.top for x in rectangles) right = min(x.right for x in rectangles) bottom = max(x.bottom for x in rectangles) return Rectangle.from_sides(left, top, right, bottom)
def grow(self, padding): """ Grow this rectangle by the given padding on all sides. """ self.__bottom -= padding self.__left -= padding self.__width += 2 * padding self.__height += 2 * padding return self
def shrink(self, padding): """ Shrink this rectangle by the given padding on all sides. """ self.grow(-padding) return self
@accept_anything_as_vector def displace(self, vector): """ Displace this rectangle by the given vector. """ self.__bottom += vector.y self.__left += vector.x return self
def set(self, shape): """ Fill this rectangle with the dimensions of the given shape. """ self.bottom, self.left = shape.bottom, shape.left self.width, self.height = shape.width, shape.height return self
def copy(self): """ Return a copy of this rectangle. """ from copy import deepcopy return deepcopy(self)
@accept_shape_as_rectangle def inside(self, other): """ Return true if this rectangle is inside the given shape. """ return ( self.left >= other.left and self.right <= other.right and self.top <= other.top and self.bottom >= other.bottom)
@accept_anything_as_rectangle def outside(self, other): """ Return true if this rectangle is outside the given shape. """ return not self.touching(other)
@accept_anything_as_rectangle def touching(self, other): """ Return true if this rectangle is touching the given shape. """ if self.top < other.bottom: return False if self.bottom > other.top: return False
if self.left > other.right: return False if self.right < other.left: return False
return True
@accept_anything_as_rectangle def contains(self, other): """ Return true if the given shape is inside this rectangle. """ return (self.left <= other.left and self.right >= other.right and self.top >= other.top and self.bottom <= other.bottom)
@accept_anything_as_rectangle def align_left(self, target): self.left = target.left
@accept_anything_as_rectangle def align_center_x(self, target): self.center_x = target.center_x
@accept_anything_as_rectangle def align_right(self, target): self.right = target.right
@accept_anything_as_rectangle def align_top(self, target): self.top = target.top
@accept_anything_as_rectangle def align_center_y(self, target): self.center_y = target.center_y
@accept_anything_as_rectangle def align_bottom(self, target): self.bottom = target.bottom
def get_left(self): return self.__left
def get_center_x(self): return self.__left + self.__width / 2
def get_right(self): return self.__left + self.__width
def get_top(self): return self.__bottom + self.__height
def get_center_y(self): return self.__bottom + self.__height / 2
def get_bottom(self): return self.__bottom
def get_width(self): return self.__width
def get_height(self): return self.__height
def get_half_width(self): return self.__width / 2
def get_half_height(self): return self.__height / 2
def get_size(self): return self.__width, self.__height
def get_size_as_int(self): from math import ceil return int(ceil(self.__width)), int(ceil(self.__height))
def get_top_left(self): return Vector(self.left, self.top)
def get_top_center(self): return Vector(self.center_x, self.top)
def get_top_right(self): return Vector(self.right, self.top)
def get_center_left(self): return Vector(self.left, self.center_y)
def get_center(self): return Vector(self.center_x, self.center_y)
def get_center_right(self): return Vector(self.right, self.center_y)
def get_bottom_left(self): return Vector(self.left, self.bottom)
def get_bottom_center(self): return Vector(self.center_x, self.bottom)
def get_bottom_right(self): return Vector(self.right, self.bottom)
def get_dimensions(self): return (self.__left, self.__bottom), (self.__width, self.__height)
def get_tuple(self): return self.__left, self.__bottom, self.__width, self.__height
def get_union(self, *rectangles): return Rectangle.from_union(self, *rectangles)
def get_intersection(self, *rectangles): return Rectangle.from_intersection(self, *rectangles)
def get_grown(self, padding): result = self.copy() result.grow(padding) return result
def get_shrunk(self, padding): result = self.copy() result.shrink(padding) return result
def set_left(self, x): self.__left = x
def set_center_x(self, x): self.__left = x - self.__width / 2
def set_right(self, x): self.__left = x - self.__width
def set_top(self, y): self.__bottom = y - self.__height
def set_center_y(self, y): self.__bottom = y - self.__height / 2
def set_bottom(self, y): self.__bottom = y
def set_width(self, width): self.__width = width
def set_height(self, height): self.__height = height
def set_size(self, width, height): self.__width = width self.__height = height
@accept_anything_as_vector def set_top_left(self, point): self.top = point[1] self.left = point[0]
@accept_anything_as_vector def set_top_center(self, point): self.top = point[1] self.center_x = point[0]
@accept_anything_as_vector def set_top_right(self, point): self.top = point[1] self.right = point[0]
@accept_anything_as_vector def set_center_left(self, point): self.center_y = point[1] self.left = point[0]
@accept_anything_as_vector def set_center(self, point): self.center_y = point[1] self.center_x = point[0]
@accept_anything_as_vector def set_center_right(self, point): self.center_y = point[1] self.right = point[0]
@accept_anything_as_vector def set_bottom_left(self, point): self.bottom = point[1] self.left = point[0]
@accept_anything_as_vector def set_bottom_center(self, point): self.bottom = point[1] self.center_x = point[0]
@accept_anything_as_vector def set_bottom_right(self, point): self.bottom = point[1] self.right = point[0]
# Properties (fold) left = property(get_left, set_left) center_x = property(get_center_x, set_center_x) right = property(get_right, set_right) top = property(get_top, set_top) center_y = property(get_center_y, set_center_y) bottom = property(get_bottom, set_bottom) width = property(get_width, set_width) height = property(get_height, set_height) half_width = property(get_half_width) half_height = property(get_half_height) size = property(get_size, set_size) size_as_int = property(get_size_as_int)
top_left = property(get_top_left, set_top_left) top_center = property(get_top_center, set_top_center) top_right = property(get_top_right, set_top_right) center_left = property(get_center_left, set_center_left) center = property(get_center, set_center) center_right = property(get_center_right, set_center_right) bottom_left = property(get_bottom_left, set_bottom_left) bottom_center = property(get_bottom_center, set_bottom_center) bottom_right = property(get_bottom_right, set_bottom_right)
dimensions = property(get_dimensions) tuple = property(get_tuple)
Rect = Rectangle
def get_distance(a, b): a = cast_anything_to_vector(a) b = cast_anything_to_vector(b) return a.get_distance(b)
def interpolate(a, b, num_points=3): a = cast_anything_to_vector(a) b = cast_anything_to_vector(b) interpolation = [a] steps = num_points - 1 assert steps >= 0
for step in range(1, steps): v = a.get_interpolated(b, step/steps) interpolation.append(v)
interpolation.append(b) return interpolation
# Exceptions
class NullVectorError (Exception): """ Thrown when an operation chokes on a null vector. """ pass
class VectorCastError (Exception): """ Thrown when an inappropriate object is used as a vector. """
def __init__(self, object): Exception.__init__(self, "Could not cast %s to vector." % type(object))
class RectangleCastError (Exception): """ Thrown when an inappropriate object is used as a rectangle. """
def __init__(self, object): Exception.__init__(self, "Could not cast %s to rectangle." % type(object))
|