Core Concepts

A Point

Beziers.py provides a rich abstraction over the concept of a two-dimensional point, containing the kind of methods that someone manipulating Bezier curves would find handy.

class beziers.point.Point(x, y)

Bases: object

A representation of a point within the Beziers world.

Here are some things you can do with points. You can interpret them as vectors, and add them together:

>>> a = Point(5,5)
>>> b = Point(10,10)
>>> a + b
<15.0,15.0>

You can multiply them by a scalar to scale them:

>>> a * 2
<10.0,10.0>

You can adjust them:

>>> a += b
>>> a
<15.0,15.0>

If you’re using Python 3, you can abuse operator overloading and compute the dot product of two vectors:

>>> a = Point(5,5)
>>> b = Point(10,10)
>>> a @ b
100.0
classmethod fromRepr(text)
dot(other)
clone()

Clone a point, returning a new object with the same co-ordinates.

rounded()

Return a point with the co-ordinates truncated to integers

lerp(other, t)

Interpolate between two points, at time t.

property squareMagnitude

Interpreting this point as a vector, returns the squared magnitude (Euclidean length) of the vector.

property magnitude

Interpreting this point as a vector, returns the magnitude (Euclidean length) of the vector.

toUnitVector()

Divides this point by its magnitude, returning a vector of length 1.

property angle

Interpreting this point as a vector, returns the angle in radians of the vector.

property slope

Returns slope y/x

classmethod fromAngle(angle)

Given an angle in radians, return a unit vector representing that angle.

rotated(around, by)

Return a new point found by rotating this point around another point, by an angle given in radians.

rotate(around, by)

Mutate this point by rotating it around another point, by an angle given in radians.

squareDistanceFrom(other)

Returns the squared Euclidean distance between this point and another.

distanceFrom(other)

Returns the Euclidean distance between this point and another.

transformed(transformation)
transform(transformation)

A Segment

class beziers.segment.Segment

Bases: beziers.utils.intersectionsmixin.IntersectionsMixin, beziers.utils.samplemixin.SampleMixin, object

A segment is part of a path. Although this package is called beziers.py, it’s really for font people, and paths in the font world are made up of cubic Bezier curves, lines and (if you’re dealing with TrueType) quadratic Bezier curves. Each of these things is represented as an object derived from the Segment base class. So, when you inspect the path in the segment representation, you will get a list of CubicBezier, Line and QuadraticBezier objects, all of which derive from Segment.

Because of this, a Segment can have two, three or four elements: lines have two end points; quadratic Beziers have a start, a control point and an end point; cubic have a start, two control points and an end point.

You can pretend that a Segment object is an array and index it like one:

q = CubicBezier(
  Point(122,102), Point(35,200), Point(228,145), Point(190,46)
)

start, cp1, cp2, end = q[0],q[1],q[2],q[3]

You can also access the start and end points like so:

start = q.start
end = q.end
clone()

Returns a new Segment which is a copy of this segment.

round()

Rounds the points of segment to integer coordinates.

property order
property start

Returns a Point object representing the start of this segment.

property end

Returns a Point object representing the end of this segment.

property startAngle
property endAngle
tangentAtTime(t)

Returns a Point representing the unit vector of tangent at time t.

normalAtTime(t)

Returns a Point representing the normal (rotated tangent) at time t.

translated(vector)

Returns a new Segment object representing the translation of this segment by the given vector. i.e.:

>>> l = Line(Point(0,0), Point(10,10))
>>> l.translated(Point(5,5))
L<<5.0,5.0>--<15.0,15.0>>
>>> l
L<<0.0,0.0>--<10.0,10.0>>
rotated(around, by)

Returns a new Segment object representing the rotation of this segment around the given point and by the given angle. i.e.:

>>> l = Line(Point(0,0), Point(10,10))
>>> l.rotated(Point(5,5), math.pi/2)
L<<10.0,-8.881784197e-16>--<-8.881784197e-16,10.0>>
scaled(bx)

Returns a new Segment object representing the scaling of this segment by the given magnification. i.e.:

>>> l = Line(Point(0,0), Point(10,10))
>>> l.scaled(2)
L<<0,0>--<20,20>>
transformed(transformation)

Returns a new Segment object transformed by the given AffineTransformation matrix.

alignmentTransformation()
aligned()

Returns a new Segment object aligned to the origin. i.e. with the first point translated to the origin (0,0) and the last point with y=0. Obviously, for a Line this is a bit pointless, but it’s quite handy for higher-order curves.

lengthAtTime(t)

Returns the length of the subset of the path from the start up to the point t (0->1), where 1 is the end of the whole curve.

reversed()

Returns a new segment with the points reversed.

bounds()

Returns a BoundingBox object for this segment.

property hasLoop

Returns True if the segment has a loop. (Only possible for cubics.)

A Line

class beziers.line.Line(start, end)

Bases: beziers.segment.Segment

Represents a line segment within a Bezier path.

classmethod fromRepr(text)
pointAtTime(t)

Returns the point at time t (0->1) along the line.

tangentAtTime(t)

Returns the tangent at time t (0->1) along the line.

normalAtTime(t)

Returns the normal at time t (0->1) along the line.

curvatureAtTime(t)
splitAtTime(t)

Returns two segments, dividing the given segment at a point t (0->1) along the line.

tOfPoint(point)

Returns the t (0->1) value of the given point, assuming it lies on the line, or -1 if it does not.

flatten(degree=8)
property slope
property intercept
property length
findExtremes()
property area

A QuadraticBezier curve

class beziers.quadraticbezier.QuadraticBezier(start, c1, end)

Bases: beziers.utils.arclengthmixin.ArcLengthMixin, beziers.segment.Segment

classmethod fromRepr(text)
pointAtTime(t)

Returns the point at time t (0->1) along the curve.

tOfPoint(p)

Returns the time t (0->1) of a point on the curve.

splitAtTime(t)

Returns two segments, dividing the given segment at a point t (0->1) along the curve.

derivative()

Returns a Line representing the derivative of this curve.

flatten(degree=8)
findExtremes()

Returns a list of time t values for extremes of the curve.

property area

Returns the signed area between the curve and the y-axis

toCubicBezier()

Converts the quadratic bezier to a CubicBezier

A CubicBezier curve

class beziers.cubicbezier.CubicBezier(start, c1, c2, end)

Bases: beziers.utils.arclengthmixin.ArcLengthMixin, beziers.segment.Segment

classmethod fromRepr(text)
pointAtTime(t)

Returns the point at time t (0->1) along the curve.

tOfPoint(p)
splitAtTime(t)

Returns two segments, dividing the given segment at a point t (0->1) along the curve.

join(other)

Not currently implemented: join two CubicBezier together.

toQuadratic()

Not currently implemented: reduce this to a QuadraticBezier.

derivative()

Returns a QuadraticBezier representing the derivative of this curve.

flatten(degree=8)
findExtremes(inflections=False)

Returns a list of time t values for extremes of the curve.

curvatureAtTime(t)

Returns the C curvature at time t..

property tunniPoint

Returns the Tunni point of this Bezier (the intersection of the handles).

balance()

Perform Tunni balancing on this Bezier.

property hasLoop

Returns True if the segment has a loop. (Only possible for cubics.)

property area

Returns the signed rea between the curve and the y-axis

A BoundingBox

class beziers.boundingbox.BoundingBox

Bases: object

A representation of a rectangle within the Beziers world, used to store bounding boxes.

property area

Returns the area of the bounding box.

property left

Returns the X coordinate of the left edge of the box.

property right

Returns the X coordinate of the right edge of the box.

property top

Returns the Y coordinate of the top edge of the box.

property bottom

Returns the Y coordinate of the bottom edge of the box.

property width

Returns the width of the box.

property height

Returns the height of the box.

property centroid

Returns a Point representing the centroid of the box.

extend(other)

Add an object to the bounding box. Object can be a Point, another BoundingBox, or something which has a bounds() method.

translated(point)

Returns a new BoundingBox translated by the vector

includes(point)

Returns True if the point is included in this bounding box.

overlaps(other)

Returns True if the given bounding box overlaps with this bounding box.

addMargin(size)

Adds a few units of margin around the edges of the bounding box.

Helpful utility classes

Geometric shapes

beziers.path.geometricshapes.Circle(x_radius, origin=None, superness=0.5522847498307935)

Returns a path representing a circle of given radius. You can specify the origin as a Point and the superness of the circle.

beziers.path.geometricshapes.Ellipse(x_radius, y_radius, origin=None, superness=0.5522847498307935)

Returns a path representing an ellipse of given x and y radii. You can specify the origin as a Point and the superness of the ellipse.

beziers.path.geometricshapes.Rectangle(width, height, origin=None)

Returns a path representing an rectangle of given width and height. You can specify the origin as a Point.

beziers.path.geometricshapes.Square(width, origin=None)

Returns a path representing a square of given width. You can specify the origin as a Point.