3 points ? Catmull-Rom is defined for 4 points, say p_1 p0 p1 p2;
a cubic curve goes from p0 to p1, and outer points p_1 and p2 determine the slopes at p0 and p1.
To draw a curve through some points in an array P, do something like this:
for j in range( 1, len(P)-2 ): # skip the ends
for t in range( 10 ): # t: 0 .1 .2 .. .9
p = spline_4p( t/10, P[j-1], P[j], P[j+1], P[j+2] )
# draw p
def spline_4p( t, p_1, p0, p1, p2 ):
""" Catmull-Rom
(Ps can be numpy vectors or arrays too: colors, curves ...)
"""
# wikipedia Catmull-Rom -> Cubic_Hermite_spline
# 0 -> p0, 1 -> p1, 1/2 -> (- p_1 + 9 p0 + 9 p1 - p2) / 16
# assert 0 <= t <= 1
return (
t*((2-t)*t - 1) * p_1
+ (t*t*(3*t - 5) + 2) * p0
+ t*((4 - 3*t)*t + 1) * p1
+ (t-1)*t*t * p2 ) / 2
One can use piecewise quadratic curves through 3 points --
see Dodgson, Quadratic Interpolation for Image Resampling.
What do you really want to do ?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…