bezier

Tools to work with Bezier curves.

getBezierPoint(t, pt1, pt2, pt3, pt4, reverse=True)

Use the Bernstein Basis Function to get a point in a Bezier curve.

Parameters
  • t (float) – A float representing the ratio of the desired point.

  • pt1 (tuple) – The x,y coordinate of the first point (on-curve).

  • pt2 (tuple) – The x,y coordinate of the second point (off-curve).

  • pt3 (tuple) – The x,y coordinate of the third point (off-curve).

  • pt4 (tuple) – The x,y coordinate of the last point (on-curve).

  • reverse (bool) – Get point for the reverse value of t.

>>> pt1 = 320, 162
>>> pt2 = 138, 528
>>> pt3 = 416, 856
>>> pt4 = 854, 794
>>> getBezierPoint(0.5, pt1, pt2, pt3, pt4, reverse=True)
(354.5, 638.5)
lerp(p1, p2, t)

Linear interpolation between two points.

Parameters
  • p1 (Point) – A Point object.

  • p2 (Point) – Another Point object.

  • t (float) – The interpolation factor.

Returns

A new Point object interpolated from p1 and p2.

glyphToBezierSegments(glyph)

Convert an RGlyph into lists of BezierSegment objects.

class Point(pos)

A basic Bezier Point object with support for math operations and normalization.

__init__(pos)

Create a new Point object from x,y coordinates.

Parameters

pos (tuple) – A pair of x,y coordinates.

Returns

A new Point object.

>>> p = Point((100, 100))
>>> p.x, p.y
(100, 100)
mag()
normal()
normalise()
class BezierSegment(p0, p1, p2, p3)

A basic Bezier Segment object with support for point and curvature calculations.

__init__(p0, p1, p2, p3)

Initiliaze a BezierSegment from four Points (two on-curve, two off-curve).

Parameters
  • p0 (Point) – The first on-curve point.

  • p1 (Point) – The first off-curve point.

  • p2 (Point) – The second off-curve point.

  • p3 (Point) – The second on-curve point.

position(t)

Get a point on the curve based on a given factor.

Parameters

t (float) – A float representing the ratio of the desired point.

Returns

A Point object.

>>> p0 = Point((0, 0))
>>> p1 = Point((0, 60))
>>> p2 = Point((40, 100))
>>> p3 = Point((100, 100))
>>> B = BezierSegment(p0, p1, p2, p3)
>>> pt = B.position(0.5)
>>> pt.x, pt.y
(27.5, 72.5)
d(t)

First derivative of the curve. Describes the tangent along the curve. Used to calculate the normal (line perpedincular to the curve).

dd(t)

Second derivative of the curve. Describes how quickly the tangent is changing.

curvature(t)

Calculate the segments’s curvature at a given point.

Parameters

t (float) – A float representing the ratio of the desired point.

Returns

The curvature value.

>>> p0 = Point((0, 0))
>>> p1 = Point((0, 60))
>>> p2 = Point((40, 100))
>>> p3 = Point((100, 100))
>>> B = BezierSegment(p0, p1, p2, p3)
>>> B.curvature(0.5)
-0.011544600509168123