Fork me on GitHub

Math functions

Py2D has classes for Vectors, Polygons and Affine Transformations. The following sections will outline some of the common use cases of these classes.

Table of Contents

Vector

Vectors in Py2D are always two-dimensional and can have int or float components. You can initialize them like this:

>>> v = Vector(3.0, 2.0)

Vector components

You can access a Vector’s components using a variety of ways:

>>> v.x, v.y
(3.0, 2.0)

>>> x,y = v[0], v[1]
(3.0, 2.0)

>>> v.as_tuple()
(3.0, 2.0)

>>>v.x = 10
>>>v[0] = 10

Vector arithmetic

The Vector class has all the important arithmetic operators overridden:

>>> u = Vector(3.0, 4.0)
>>> v = Vector(1.0, 7.0)

>>> u + v
Vector(4.000, 11.000)

>>> u - v
Vector(2.000, -3.000)

>>> u * 2
Vector(6.000, 8.000)

>>> u / 2
Vector(1.500, 2.000)

>>> u * v	# this is the dot product.
31

Vector equality

Vectors are defined as equal if the differences of their x and y values are both smaller than Math.EPSILON (i.e. 0.0001):

>>> u = Vector(3.0, 4.0)
>>> v = Vector(1.0, 2.0)
>>> w = Vector(2.0, 2.0)

>>> u == v, u == w, v == w
(False, False, False)

>>> u == v + w
True

Vector length and normalization

The length and length_squared properties give the vector’s length. To normalize to a length of 1, use normalize. To normalize to a length of 1 or smaller, use clamp.

>>> u = Vector(3.0, 4.0)
>>> v = Vector(0.5, 0)
>>> u.length, u.length_squared
(5.0, 25.0)

>>> u.normalize()
Vector(0.600, 0.800)

>>> v.normalize()
Vector(1.000, 0.000)

>>> u.clamp()
Vector(0.600, 0.800)

>>> v.clamp()
Vector(0.500, 0.000)

Derived vectors

You can clone vectors and get normal vectors easily:

>>> u = Vector(3.0, 4.0)
>>> u.clone()
Vector(3.000, 4.000)

>>> u.normal()
Vector(-4.000, 3.000)

Polygon

In Py2D, Polygons are essentially list of Vectors. We normally assume that the polygon is closed, i.e. that the last Vector will be connected to the first Vector.

There are multiple ways of constructing polygons:

>>> p = Polygon()
>>> q = Polygon.regular(center, radius, points)
>>> r = Polygon.from_pointlist([ Vector(1,2), Vector(2,1), Vector(0,0) ])
>>> s = Polygon.from_tuples([ (1,2), (2,1), (0,0) ])

Points on the polygon

The list of points is accessible by the points property. Alternatively, you may use add_point to add one point to the end of the polygon, or add_points to add multiple points.

You can check whether the polygon contains a certain point by using the contains_point function:

>>> s = Polygon.from_tuples([ (0,0), (3,2), (1,3) ])
>>> s.contains_point(Vector(2,2))
True

Polygon orientation, convexity

A polygon may be oriented either clock-wise or counter-clockwise. You can test using the is_clockwise function or get a new clockwise or counter-clockwise copy using the clone_cw and clone_ccw functions:

>>> p = Polygon.from_tuples([ (1,2), (2,1), (0,0) ])
>>> p.is_clockwise()
False

>>> q = Polygon.from_tuples([ (0,0), (2,1), (1,2) ])
>>> q.is_clockwise()
True

>>> p.clone_cw()
Polygon([ (1,2), (2,1), (0,0) ])

>>> q.clone_ccw()
Polygon([ (1,2), (2,1), (0,0) ])

Additionally, a polygon may be convex or concave. You can test for convexity using the is_convex function:

>>> s = Polygon.from_tuples([ (0,0), (10,0), (10,10), (5,5), (0,10)])
>>> s.is_convex()
False

>>> del s.points[-2]
>>> s.is_convex()
True

Transform