vecrec — 2D vector and rectangle classes

This package provides 2D vector and rectangle classes. These classes were written to be used in games, so they have some methods that conveniently tie into pyglet and pygame, but for the most part they are quite general and could be used for almost anything.

https://img.shields.io/pypi/v/vecrec.svg https://img.shields.io/pypi/pyversions/vecrec.svg https://img.shields.io/travis/kxgames/vecrec.svg https://img.shields.io/coveralls/kxgames/vecrec.svg https://readthedocs.org/projects/vecrec/badge/?version=latest

Installation

The vecrec module is pure-python, dependency-free, and available from PyPI:

$ pip install vecrec

Basic Usage

Here are a few examples showing how to construct and use the Vector and Rect classes provided by this package:

>>> from vecrec import Vector, Rect
>>> a = Vector(1, 2)
>>> b = Vector(3, 4)
>>> a + b
Vector(4, 6)

Rectangles are more commonly constructed using factory methods:

>>> Rect.from_size(8, 11)
Rect(0, 0, 8, 11)
>>> Rect.from_center(a, 1, 1)
Rect(0, 1, 1, 1)

vecrec.Shape()

Provide an interface for custom shape classes to interact with the rectangle class.

vecrec.Vector(x, y)

Represent a mutable two-dimensional vector.

vecrec.Rect(left, bottom, width, height)

Represent a mutable two-dimensional rectangle.

vecrec.cast_anything_to_vector(input)

Convert the given input to a vector if possible, otherwise raise a VectorCastError.

vecrec.cast_anything_to_rectangle(input)

Convert the given input to a rectangle if possible, otherwise raise a RectCastError.

vecrec.cast_shape_to_rectangle(input)

Convert the given shape to a rectangle if possible, otherwise raise a RectCastError.

vecrec.accept_anything_as_vector(function)

Method decorator that converts an argument to a vector.

vecrec.accept_anything_as_rectangle(function)

Method decorator that converts an argument to a rectangle.

vecrec.accept_shape_as_rectangle(function)

Method decorator that converts an argument to a rectangle.

vecrec.golden_ratio

float(x) -> floating point number

vecrec.get_distance(a, b)

Return the distance between the two given vectors.

vecrec.interpolate(a, b[, num_points])

Return a list of vectors that linearly interpolate between the given vectors.

vecrec.NullVectorError

Thrown when an operation chokes on a null vector.

vecrec.VectorCastError(object)

Thrown when an inappropriate object is used as a vector.

vecrec.RectCastError(object)

Thrown when an inappropriate object is used as a rectangle.