'fast' bezier curve length python library

1.2k Views Asked by At

To calculate the length of a bezier curve, I have been using pyx.

from pyx import path, unit
bez = path.curve(p[0], p[1], p[0], p[1] + d, q[0], q[1]-d, q[0],q[1])
edgelen = unit.tocm(bez.arclen())

where p and q are the endpoints of the curve and d is a vertical offset for the relative position of the two control points. (In my application, the curves are always "upward").

However, I found that this library call for getting the curve length is painfully slow. Are there other python libraries that might have better performance?

1

There are 1 best solutions below

1
On

I'd probably roll my own bezier callable using pypy - I don't recall it being particularly difficult when I took a graphics class a while back. If you code it in pure python, pypy should make it quite fast. If pypy isn't an option, using cython with cpython is the next best thing.

I'll often take a pure python module, and m4 preprocess it to get common code for the pure python and cython versions of the same thing. It seems to work pretty well. Indeed, sometimes I'll even use m4 as a macro language over python, just to avoid method calls without duplicating code in the maintenance version of a module.