What you need is the De Casteljau algorithm. This will allow you to split your curve into whatever segments you'd like.
However, since you're dealing with just cubic curves, I'd like to suggest a slightly easier to use formulation that'll give you a segment from t0
to t1
where 0 <= t0 <= t1 <= 1
. Here's some pseudocode:
u0 = 1.0 - t0
u1 = 1.0 - t1
qxa = x1*u0*u0 + bx1*2*t0*u0 + bx2*t0*t0
qxb = x1*u1*u1 + bx1*2*t1*u1 + bx2*t1*t1
qxc = bx1*u0*u0 + bx2*2*t0*u0 + x2*t0*t0
qxd = bx1*u1*u1 + bx2*2*t1*u1 + x2*t1*t1
qya = y1*u0*u0 + by1*2*t0*u0 + by2*t0*t0
qyb = y1*u1*u1 + by1*2*t1*u1 + by2*t1*t1
qyc = by1*u0*u0 + by2*2*t0*u0 + y2*t0*t0
qyd = by1*u1*u1 + by2*2*t1*u1 + y2*t1*t1
xa = qxa*u0 + qxc*t0
xb = qxa*u1 + qxc*t1
xc = qxb*u0 + qxd*t0
xd = qxb*u1 + qxd*t1
ya = qya*u0 + qyc*t0
yb = qya*u1 + qyc*t1
yc = qyb*u0 + qyd*t0
yd = qyb*u1 + qyd*t1
Then just draw the Bézier curve formed by (xa,ya)
, (xb,yb)
, (xc,yc)
and (xd,yd)
.
Note that t0
and t1
are not exactly percentages of the curve distance but rather the curves parameter space. If you absolutely must have distance then things are much more difficult. Try this out and see if it does what you need.
Edit: It's worth noting that these equations simplify quite a bit if either t0
or t1
is 0 or 1 (i.e. you only want to trim from one side).
Also, the relationship 0 <= t0 <= t1 <= 1
isn't a strict requirement. For example t0 = 1
and t1 = 0
can be used to "flip" the curve backwards, or t0 = 0
and t1 = 1.5
could be used to extend the curve past the original end. However, the curve might look different than you expect if you try to extend it past the [0,1]
range.
Edit2: More than 3 years after my original answer, MvG pointed out an error in my equations. I forgot the last step (an extra linear interpolation to get the final control points). The equations above have been corrected.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…