Any royal road to Bezier curves...?

Tom Anderson twic at urchin.earth.li
Tue Nov 22 18:33:59 EST 2005


On Tue, 22 Nov 2005, Warren Francis wrote:

> For my purposes, I think you're right about the natural cubic splines. 
> Guaranteeing that an object passes through an exact point in space will 
> be more immediately useful than trying to create rules governing where 
> control points ought to be placed so that the object passes close enough 
> to where I intended it to go.

Right so. I wrote that code the first time when i was in a similar spot 
myself - trying to draw maps with nice smooth roads etc based on a fairly 
sparse set of points - so i felt your pain.

> Thanks for the insight, I never would have found that on my own.  At 
> least not until Google labs comes out with a search engine that gives 
> names for what you're thinking of. ;-)

You're in for a wait - i think that feature's scheduled for summer 2006.

> I know this is a fairly pitiful request, since it just involves parsing 
> your code, but I'm new enough to this that I'd benefit greatly from an 
> couple of lines of example code, implementing your classes... how do I 
> go from a set of coordinates to a Natural Cubic Spline, using your 
> python code?

Pitiful but legit - i haven't documented that code at all well. If you go 
right to the foot of my code, you'll find a simple test routine, which 
shows you the skeleton of how to drive the code. It looks a bit like this 
(this is slightly simplified):

def test_spline():
 	knots = [(0, 0), (0, 1), (1, 0), (0, -2), (-3, 0)] # a spiral
 	trace = []
 	c = NaturalCubicSpline(tuples2points(knots))
 	u = 0.0
 	du = 0.1
 	lim = len(c) + du
 	while (u < lim):
 		p = c(u)
 		trace.append(tuple(p))
 		u = u + du
 	return trace

tuples2points is a helper function which turns your coordinates from a 
list of tuples (really, an iterable of length-2 iterables) to a list of 
Points. The alternative way of doing it is something like:

curve = NaturalCubicSpline()
for x, y in knot_coords:
 	curve.knots.append(Point(x, y))
do_something_with(curve)

tom

-- 
I DO IT WRONG!!!



More information about the Python-list mailing list