Skip to content

Instantly share code, notes, and snippets.

@jksuom
Created July 7, 2015 06:34
Show Gist options
  • Save jksuom/6fe3254b668a667d30b8 to your computer and use it in GitHub Desktop.
Save jksuom/6fe3254b668a667d30b8 to your computer and use it in GitHub Desktop.
Drawing SVG

Interpretation of

M 210.11173,460.41281 C 167.68532,274.54475 107.07617,197.77315 290.92393,304.84932 474.77169,411.92549 610.13214,541.22502 408.10163,444.25038 206.07112,347.27573 597.03099,636.66328 367.69552,609.91539 21.255008,569.50929 228.75342,542.08117 210.11173,460.41281 Z

The M command sets the starting point.

The C command defines a series of consecutive cubic Bézier curves.

The Z command closes the path.

Each cubic curve is determined by four points, a start point, two control points and an end point.

Writing M x0,y0 C x1,y1 x2,y2 ... for brevity, the first cubic curve starts at P0 = (x0, y0), then P1 = (x1, y1) and P2 are the control points and finally P3 is the endpoint.

The next cubic curve then starts at P3, has P4 and P5 as its control points, and ends at P6, etc...

A cubic curve is drawn by means of two cubic polynomials x(t) and y(t) for t in an interval (a, b) on the real line. These polynomials are obtained by combining four basic B-splines by means of the components of the four defining points.

The four basic B-splines can be obtained from SymPy's bsplines module as follows

p0 = bspline_basis(3, [a, a, a, a, b], 0, t)
p1 = bspline_basis(3, [a, a, a, b, b], 0, t)
p2 = bspline_basis(3, [a, a, b, b, b], 0, t)
p3 = bspline_basis(3, [a, b, b, b, b], 0, t).

The polynomial coordinates x(t) and y(t) of the curve are then obtained by linear combination of the basic B-splines using binomial coefficients and the components of the defining points. For example,

x(t) = x0 p0(t) + 3 x1 p1(t) + 3 x2 p2(t) + x3 p3(t),

and similarly for y(t). (Note: This line is not SymPy code.)

In practise, there is no need to use bsplines. If the interval (a, b) is the unit interval (0, 1), then the basic B-splines together with correct binomial coefficients can be read off the binomial expansion of 1 = ((1 - t) + t)^3:

1 = (1 - t)^3 + 3t(1 - t)^2 + 3t^2(1 - t) + t^3
  = p0(t) + 3p1(t) + 3p2(t) + p3(t)
  = B0(t) + B1(t) + B2(t) + B3(t).

The polynomials Bi obtained from the basic B-splines by multiplication with the binomial coefficients are called Bernstein polynomials.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment